M1a: survival core, pause menu, title screen and save/load
Design: docs/SURVIVAL_DESIGN.md (core loop, systems, architecture, phases). Survival core (M1a): - Items/recipes as data (data/*.json) in the addon SDK's define_item / define_recipe shape; procedural item meshes and offscreen-rendered icons - Inventory class (40 slots, hotbar = row 1) shared by player and chests; click / right-click split / shift-click quick-move - Actions autoload: the intent layer (craft, pickup, harvest_hit, place, transfer) that validates every change; becomes the RPC surface in M2 - Gathering: ground pickups (sticks, stones, flint); trees chopped with an axe fall and leave stumps; boulders mined with a pickaxe shrink per hit - Crafting screen with station checks (workbench); placement mode with green/red ghost, rotation and overlap/slope validation - Pieces: campfire (light, flames, embers), workbench, chest (16 slots) - HUD: hotbar, held tool view model with swing, prompts, toasts Menus and saving: - ESC pause menu: resume, controls, fullscreen, mouse sensitivity, quit - Title screen over a flyover of the island: Continue / New World (name + seed) / Load World (with delete) / Quit - Saves autoload: one folder per world under user://worlds, atomic writes with a .bak; stores only player changes (player, pieces, chest contents, harvested resources, taken pickups) and regenerates the rest from the seed - Autosave every 2 min, Save & Quit to title/desktop, save on window close Fixes: UI screens now fill the viewport (were pinned top-left); title backdrop no longer captures the mouse. Tests: test_survival_core (31 checks), test_save_load (round trip + slots), test_terrain_edges now checks 8 seeds for deep pits / unbanked water.
This commit is contained in:
135
scripts/core/item_db.gd
Normal file
135
scripts/core/item_db.gd
Normal file
@@ -0,0 +1,135 @@
|
||||
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
|
||||
var _item_mat: StandardMaterial3D
|
||||
|
||||
func _ready() -> void:
|
||||
_load("res://data/items.json", "items", define_item)
|
||||
_load("res://data/recipes.json", "recipes", define_recipe)
|
||||
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 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()
|
||||
Reference in New Issue
Block a user