- data/fish.json: fish, weights, dawn/dusk odds, bite timing and minimum depth per water type; ItemDB.define_water() registers them - world.water_at(x, z) answers lake / sea / dry land; the float uses it, so a worldlet with a sea only needs a "sea" entry - Clear messages for shallow water and water with nothing to catch - Lake fish: more of them, bigger, nearer the shore, jumping with splash rings - docs/WORLDLETS_DESIGN.md: proposal for data-defined world types
164 lines
5.8 KiB
GDScript
164 lines
5.8 KiB
GDScript
extends Node
|
|
## Autoload "ItemDB": item and recipe definitions loaded from data/*.json, plus
|
|
## procedurally rendered inventory icons.
|
|
##
|
|
## Definitions use the same shape as the addon SDK's define_item /
|
|
## define_recipe (docs/ADDON_SDK.md), so addon content will later be
|
|
## registered through `define_item()` / `define_recipe()` like the base game.
|
|
|
|
signal icons_ready
|
|
|
|
const ItemMeshes = preload("res://scripts/items/item_meshes.gd")
|
|
const ICON_SIZE := 96
|
|
|
|
var items: Dictionary = {} # id -> definition Dictionary
|
|
var recipes: Dictionary = {} # id -> recipe Dictionary (insertion order kept)
|
|
var icons: Dictionary = {} # id -> Texture2D
|
|
## Fishing by water type (data/fish.json): {"modifiers": {...}, "waters": {type: {...}}}.
|
|
var fishing: Dictionary = {"modifiers": {}, "waters": {}}
|
|
var _item_mat: StandardMaterial3D
|
|
|
|
func _ready() -> void:
|
|
_load("res://data/items.json", "items", define_item)
|
|
_load("res://data/recipes.json", "recipes", define_recipe)
|
|
_load_fishing("res://data/fish.json")
|
|
if DisplayServer.get_name() != "headless":
|
|
_render_icons.call_deferred()
|
|
|
|
func _load(path: String, key: String, define: Callable) -> void:
|
|
var text := FileAccess.get_file_as_string(path)
|
|
var data = JSON.parse_string(text)
|
|
if typeof(data) != TYPE_DICTIONARY or not data.has(key):
|
|
push_error("ItemDB: could not parse %s" % path)
|
|
return
|
|
for d in data[key]:
|
|
define.call(d)
|
|
|
|
func define_item(d: Dictionary) -> void:
|
|
for f in ["id", "name", "category", "stack"]:
|
|
if not d.has(f):
|
|
push_error("ItemDB: item missing '%s': %s" % [f, d])
|
|
return
|
|
d.stack = int(d.stack)
|
|
items[d.id] = d
|
|
|
|
func define_recipe(r: Dictionary) -> void:
|
|
if not items.has(r.get("output", "")):
|
|
push_error("ItemDB: recipe '%s' outputs unknown item" % r.get("id", "?"))
|
|
return
|
|
for id in r.inputs:
|
|
if not items.has(id):
|
|
push_error("ItemDB: recipe '%s' uses unknown item '%s'" % [r.id, id])
|
|
return
|
|
r.count = int(r.get("count", 1))
|
|
r.station = String(r.get("station", ""))
|
|
recipes[r.id] = r
|
|
|
|
func _load_fishing(path: String) -> void:
|
|
var data = JSON.parse_string(FileAccess.get_file_as_string(path))
|
|
if typeof(data) != TYPE_DICTIONARY or not data.has("waters"):
|
|
push_error("ItemDB: could not parse %s" % path)
|
|
return
|
|
fishing.modifiers = data.get("modifiers", {})
|
|
for water in data.waters:
|
|
define_water(water, data.waters[water])
|
|
|
|
## Registers (or replaces) the fish for one water type. Same shape an addon
|
|
## would use to add, say, a "swamp" water with its own catch.
|
|
func define_water(water: String, d: Dictionary) -> void:
|
|
var fish: Array = []
|
|
for f in d.get("fish", []):
|
|
if not items.has(f.get("id", "")):
|
|
push_error("ItemDB: fish '%s' in water '%s' is not an item" % [f.get("id", "?"), water])
|
|
continue
|
|
fish.append({"id": f.id, "weight": float(f.get("weight", 1)), "dawn_dusk": float(f.get("dawn_dusk", f.get("weight", 1)))})
|
|
var w: Array = d.get("bite_wait", [5.0, 14.0])
|
|
fishing.waters[water] = {"min_depth": float(d.get("min_depth", 0.5)), "bite_wait": [float(w[0]), float(w[1])], "fish": fish}
|
|
|
|
## The fishing table for a water type ({} = nothing to catch there).
|
|
func water_fishing(water: String) -> Dictionary:
|
|
return fishing.waters.get(water, {})
|
|
|
|
func get_item(id: String) -> Dictionary:
|
|
return items.get(id, {})
|
|
|
|
func item_name(id: String) -> String:
|
|
return items.get(id, {}).get("name", id)
|
|
|
|
func stack_size(id: String) -> int:
|
|
return int(items.get(id, {}).get("stack", 1))
|
|
|
|
func mesh_for(id: String) -> ArrayMesh:
|
|
return ItemMeshes.build(items.get(id, {}).get("mesh", id))
|
|
|
|
## Shared material for item meshes (vertex colours, authored as sRGB).
|
|
func item_material() -> StandardMaterial3D:
|
|
if _item_mat == null:
|
|
_item_mat = StandardMaterial3D.new()
|
|
_item_mat.vertex_color_use_as_albedo = true
|
|
_item_mat.vertex_color_is_srgb = true
|
|
_item_mat.roughness = 0.85
|
|
return _item_mat
|
|
|
|
func icon(id: String) -> Texture2D:
|
|
return icons.get(id, null)
|
|
|
|
# --- icon renderer ----------------------------------------------------------
|
|
|
|
## Renders every item mesh once in an offscreen viewport and keeps the images.
|
|
func _render_icons() -> void:
|
|
var vp := SubViewport.new()
|
|
vp.size = Vector2i(ICON_SIZE, ICON_SIZE)
|
|
vp.transparent_bg = true
|
|
vp.own_world_3d = true
|
|
vp.msaa_3d = Viewport.MSAA_4X
|
|
vp.render_target_update_mode = SubViewport.UPDATE_ALWAYS
|
|
add_child(vp)
|
|
|
|
var env := Environment.new()
|
|
env.background_mode = Environment.BG_CLEAR_COLOR
|
|
env.ambient_light_source = Environment.AMBIENT_SOURCE_COLOR
|
|
env.ambient_light_color = Color(0.75, 0.78, 0.85)
|
|
env.ambient_light_energy = 0.7
|
|
env.tonemap_mode = Environment.TONE_MAPPER_FILMIC
|
|
var we := WorldEnvironment.new()
|
|
we.environment = env
|
|
vp.add_child(we)
|
|
|
|
var key := DirectionalLight3D.new()
|
|
key.light_energy = 1.6
|
|
key.rotation = Vector3(deg_to_rad(-50), deg_to_rad(-35), 0)
|
|
vp.add_child(key)
|
|
var fill := DirectionalLight3D.new()
|
|
fill.light_energy = 0.5
|
|
fill.light_color = Color(0.7, 0.8, 1.0)
|
|
fill.rotation = Vector3(deg_to_rad(-20), deg_to_rad(140), 0)
|
|
vp.add_child(fill)
|
|
|
|
var cam := Camera3D.new()
|
|
cam.projection = Camera3D.PROJECTION_ORTHOGONAL
|
|
vp.add_child(cam)
|
|
var mi := MeshInstance3D.new()
|
|
mi.material_override = item_material()
|
|
vp.add_child(mi)
|
|
|
|
for id in items:
|
|
var mesh := mesh_for(id)
|
|
mi.mesh = mesh
|
|
# Tools are long and thin: tilt them diagonally across the icon.
|
|
var cat: String = items[id].category
|
|
mi.rotation = Vector3(0, 0, deg_to_rad(-45)) if cat in ["tool", "weapon"] else Vector3(0, deg_to_rad(30), 0)
|
|
var aabb := mi.transform * mesh.get_aabb()
|
|
var c := aabb.get_center()
|
|
var extent := maxf(aabb.size.length(), 0.05)
|
|
cam.size = extent * 0.95
|
|
cam.position = c + Vector3(0, 0.55, 1).normalized() * extent * 2.0
|
|
cam.look_at(c, Vector3.UP)
|
|
cam.near = 0.01
|
|
cam.far = extent * 5.0
|
|
await RenderingServer.frame_post_draw
|
|
await RenderingServer.frame_post_draw
|
|
icons[id] = ImageTexture.create_from_image(vp.get_texture().get_image())
|
|
vp.queue_free()
|
|
icons_ready.emit()
|