Files
Emberwild/scripts/player/player.gd
Paul Bailey 4640f5e577 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.
2026-11-04 00:34:46 +00:00

94 lines
3.5 KiB
GDScript

extends CharacterBody3D
## First-person player controller.
##
## Move (WASD), look (mouse), jump (space), sprint (shift). Gravity + ground
## collision via CharacterBody3D. In deep water the player floats with their
## head above the surface and moves at swimming speed. Deliberately minimal: no
## stamina/health yet (M1), no network ownership yet (M2). Press ESC to free the
## mouse, click to recapture.
@export var speed: float = 5.0
@export var sprint_speed: float = 8.0
@export var swim_speed: float = 2.6
@export var jump_velocity: float = 5.5
@export var mouse_sensitivity: float = 0.0025
## Set by world.gd from the terrain. Feet this far below the surface = swimming.
@export var water_level: float = -INF
const FLOAT_DEPTH := 1.3
@onready var camera: Camera3D = $Camera3D
signal selected_changed
signal inventory_requested(container: Node) ## the HUD opens the inventory screen
const HOTBAR := 8
var inventory := Inventory.new(40) ## slots 0-7 are the hotbar
var selected := 0
var ui_open := false ## set by the inventory screen; freezes look/move
var hands: Node3D
var _gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity", 9.8)
func _ready() -> void:
if not get_parent().get("menu_mode"): # title-screen backdrop: leave the cursor alone
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
hands = preload("res://scripts/player/hands.gd").new()
hands.name = "Hands"
hands.player = self
camera.add_child(hands)
func held_item_id() -> String:
var s = inventory.get_slot(selected)
return s.id if s != null else ""
func select(i: int) -> void:
selected = wrapi(i, 0, HOTBAR)
selected_changed.emit()
## Called by workbenches and chests (container = null for plain crafting).
func open_inventory(container: Node) -> void:
inventory_requested.emit(container)
func _unhandled_input(event: InputEvent) -> void:
if ui_open:
return
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
rotate_y(-event.relative.x * mouse_sensitivity)
camera.rotate_x(-event.relative.y * mouse_sensitivity)
camera.rotation.x = clamp(camera.rotation.x, -1.4, 1.4)
elif event is InputEventMouseButton and event.pressed:
# Recapture after the window lost focus (ESC now opens the pause menu).
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta: float) -> void:
var float_y := water_level - FLOAT_DEPTH
var swimming := global_position.y < float_y + 0.05 and not is_on_floor()
if global_position.y < float_y:
# Buoyancy: ease back up to floating height.
velocity.y = move_toward(velocity.y, clampf((float_y - global_position.y) * 4.0, -3.0, 3.0), 20.0 * delta)
elif not is_on_floor():
velocity.y -= _gravity * delta
var jump := Input.is_action_just_pressed("jump") and not ui_open
if jump and is_on_floor():
velocity.y = jump_velocity
elif jump and global_position.y > float_y - 0.4 and global_position.y < float_y + 0.3:
# Swim-jump: kick up out of the water to climb onto a bank.
velocity.y = jump_velocity
var input_dir := Vector2.ZERO if ui_open else Input.get_vector("move_left", "move_right", "move_forward", "move_back")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var current_speed := sprint_speed if Input.is_key_pressed(KEY_SHIFT) else speed
if swimming:
current_speed = swim_speed
if direction:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
velocity.x = move_toward(velocity.x, 0, current_speed)
velocity.z = move_toward(velocity.z, 0, current_speed)
move_and_slide()