feat: add S1 walking skeleton
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
class_name ActorState
|
||||
extends RefCounted
|
||||
|
||||
var id: StringName
|
||||
var cell: Vector2i
|
||||
var hit_points: int
|
||||
var max_hit_points: int
|
||||
var attack_damage: int
|
||||
|
||||
func _init(new_id: StringName, new_cell: Vector2i, new_hit_points: int, new_attack_damage: int) -> void:
|
||||
id = new_id
|
||||
cell = new_cell
|
||||
hit_points = new_hit_points
|
||||
max_hit_points = new_hit_points
|
||||
attack_damage = new_attack_damage
|
||||
|
||||
func is_alive() -> bool:
|
||||
return hit_points > 0
|
||||
@@ -0,0 +1 @@
|
||||
uid://bcv8k6trgcwpv
|
||||
@@ -0,0 +1,35 @@
|
||||
class_name GameState
|
||||
extends RefCounted
|
||||
|
||||
const TurnResolverType = preload("res://scripts/domain/turn_resolver.gd")
|
||||
|
||||
var player: ActorState
|
||||
var enemies: Dictionary[StringName, ActorState]
|
||||
var map: GridMapState
|
||||
var visible_cells: Dictionary[Vector2i, bool] = {}
|
||||
var explored_cells: Dictionary[Vector2i, bool] = {}
|
||||
|
||||
func _init(new_map: GridMapState, new_player: ActorState, new_enemies: Dictionary[StringName, ActorState]) -> void:
|
||||
map = new_map
|
||||
player = new_player
|
||||
enemies = new_enemies
|
||||
update_exploration()
|
||||
|
||||
func try_player_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
var results := TurnResolverType.resolve_player_action(self, direction)
|
||||
update_exploration()
|
||||
return results
|
||||
|
||||
func compute_visible_cells(origin: Vector2i, radius: int) -> Dictionary[Vector2i, bool]:
|
||||
var visible: Dictionary[Vector2i, bool] = {}
|
||||
for y in range(origin.y - radius, origin.y + radius + 1):
|
||||
for x in range(origin.x - radius, origin.x + radius + 1):
|
||||
var cell := Vector2i(x, y)
|
||||
if map.is_inside(cell) and origin.distance_to(cell) <= radius:
|
||||
visible[cell] = true
|
||||
return visible
|
||||
|
||||
func update_exploration() -> void:
|
||||
visible_cells = compute_visible_cells(player.cell, 6)
|
||||
for cell in visible_cells:
|
||||
explored_cells[cell] = true
|
||||
@@ -0,0 +1 @@
|
||||
uid://l7gu65bsx57o
|
||||
@@ -0,0 +1,32 @@
|
||||
class_name GridMapState
|
||||
extends RefCounted
|
||||
|
||||
const TILE_SIZE := Vector2i(32, 32)
|
||||
enum CellKind { FLOOR, WALL, CLOSED_DOOR, CHEST, STAIRS }
|
||||
|
||||
var size: Vector2i
|
||||
var _cells: Dictionary[Vector2i, CellKind] = {}
|
||||
|
||||
func _init(new_size: Vector2i, fill: CellKind = CellKind.FLOOR) -> void:
|
||||
size = new_size
|
||||
for y in range(size.y):
|
||||
for x in range(size.x):
|
||||
_cells[Vector2i(x, y)] = fill
|
||||
|
||||
func set_cell(cell: Vector2i, kind: CellKind) -> void:
|
||||
if is_inside(cell):
|
||||
_cells[cell] = kind
|
||||
|
||||
func get_cell(cell: Vector2i) -> CellKind:
|
||||
return _cells.get(cell, CellKind.WALL)
|
||||
|
||||
func is_inside(cell: Vector2i) -> bool:
|
||||
return cell.x >= 0 and cell.y >= 0 and cell.x < size.x and cell.y < size.y
|
||||
|
||||
func is_walkable(cell: Vector2i) -> bool:
|
||||
var kind := get_cell(cell)
|
||||
return kind == CellKind.FLOOR or kind == CellKind.CHEST or kind == CellKind.STAIRS
|
||||
|
||||
func is_opaque(cell: Vector2i) -> bool:
|
||||
var kind := get_cell(cell)
|
||||
return kind == CellKind.WALL or kind == CellKind.CLOSED_DOOR
|
||||
@@ -0,0 +1 @@
|
||||
uid://dfmm2nqwdoel1
|
||||
@@ -0,0 +1,53 @@
|
||||
class_name TurnResolver
|
||||
extends RefCounted
|
||||
|
||||
const TurnResultType = preload("res://scripts/domain/turn_result.gd")
|
||||
|
||||
static func resolve_player_action(state: GameState, direction: Vector2i) -> Array[TurnResult]:
|
||||
var results: Array[TurnResult] = []
|
||||
if direction == Vector2i.ZERO:
|
||||
return results
|
||||
var player: ActorState = state.player
|
||||
var destination: Vector2i = player.cell + direction
|
||||
var enemy: ActorState = _enemy_at(state, destination)
|
||||
if enemy != null:
|
||||
enemy.hit_points -= player.attack_damage
|
||||
results.append(TurnResultType.new(player.id, &"melee", player.cell, destination, enemy.id, player.attack_damage, "You strike the goblin for %d." % player.attack_damage))
|
||||
if not enemy.is_alive():
|
||||
state.enemies.erase(enemy.id)
|
||||
results.append(TurnResultType.new(enemy.id, &"enemy_defeated", enemy.cell, enemy.cell, player.id, 0, "The goblin is defeated."))
|
||||
else:
|
||||
_append_enemy_response(state, results)
|
||||
return results
|
||||
if not state.map.is_walkable(destination):
|
||||
results.append(TurnResultType.new(player.id, &"blocked", player.cell, destination, &"", 0, "Your way is blocked."))
|
||||
return results
|
||||
var from: Vector2i = player.cell
|
||||
player.cell = destination
|
||||
results.append(TurnResultType.new(player.id, &"moved", from, destination, &"", 0, "You move to (%d, %d)." % [destination.x, destination.y]))
|
||||
_append_enemy_response(state, results)
|
||||
return results
|
||||
|
||||
static func _enemy_at(state: GameState, cell: Vector2i) -> ActorState:
|
||||
for enemy: ActorState in state.enemies.values():
|
||||
if enemy.cell == cell:
|
||||
return enemy
|
||||
return null
|
||||
|
||||
static func _append_enemy_response(state: GameState, results: Array[TurnResult]) -> void:
|
||||
for enemy: ActorState in state.enemies.values():
|
||||
var delta: Vector2i = state.player.cell - enemy.cell
|
||||
if abs(delta.x) + abs(delta.y) == 1:
|
||||
state.player.hit_points = max(0, state.player.hit_points - enemy.attack_damage)
|
||||
results.append(TurnResultType.new(enemy.id, &"enemy_melee", enemy.cell, state.player.cell, state.player.id, enemy.attack_damage, "The goblin hits you for %d." % enemy.attack_damage))
|
||||
return
|
||||
var step: Vector2i = Vector2i(int(sign(delta.x)), int(sign(delta.y)))
|
||||
if abs(delta.x) >= abs(delta.y):
|
||||
step.y = 0
|
||||
else:
|
||||
step.x = 0
|
||||
var target: Vector2i = enemy.cell + step
|
||||
if state.map.is_walkable(target) and target != state.player.cell and _enemy_at(state, target) == null:
|
||||
var from: Vector2i = enemy.cell
|
||||
enemy.cell = target
|
||||
results.append(TurnResultType.new(enemy.id, &"enemy_moved", from, target, &"", 0, "The goblin moves."))
|
||||
@@ -0,0 +1 @@
|
||||
uid://dbd7lcsp1kfkl
|
||||
@@ -0,0 +1,19 @@
|
||||
class_name TurnResult
|
||||
extends RefCounted
|
||||
|
||||
var actor_id: StringName
|
||||
var kind: StringName
|
||||
var from_cell: Vector2i
|
||||
var to_cell: Vector2i
|
||||
var target_id: StringName
|
||||
var damage: int
|
||||
var message: String
|
||||
|
||||
func _init(new_actor_id: StringName, new_kind: StringName, from: Vector2i, to: Vector2i, target: StringName = &"", amount: int = 0, text: String = "") -> void:
|
||||
actor_id = new_actor_id
|
||||
kind = new_kind
|
||||
from_cell = from
|
||||
to_cell = to
|
||||
target_id = target
|
||||
damage = amount
|
||||
message = text
|
||||
@@ -0,0 +1 @@
|
||||
uid://g2rbece1qsu4
|
||||
@@ -0,0 +1,83 @@
|
||||
class_name Game
|
||||
extends Node2D
|
||||
|
||||
const TILE_SIZE := Vector2i(32, 32)
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
|
||||
const GameStateType = preload("res://scripts/domain/game_state.gd")
|
||||
const ActorViewType = preload("res://scripts/presentation/actor_view.gd")
|
||||
|
||||
@onready var map_renderer: MapRenderer = $World/MapRenderer
|
||||
@onready var player_view: ActorView = $World/Player
|
||||
@onready var enemies: Node2D = $World/Enemies
|
||||
@onready var hud: GameHud = $HUD
|
||||
var game_state: GameState
|
||||
var enemy_views: Dictionary[StringName, ActorView] = {}
|
||||
|
||||
func _ready() -> void:
|
||||
game_state = _create_initial_state()
|
||||
map_renderer.set_map(game_state.map)
|
||||
player_view.set_actor(game_state.player)
|
||||
_sync_enemy_views()
|
||||
hud.refresh(game_state)
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed(&"move_north"):
|
||||
_process_action(Vector2i.UP)
|
||||
elif event.is_action_pressed(&"move_south"):
|
||||
_process_action(Vector2i.DOWN)
|
||||
elif event.is_action_pressed(&"move_west"):
|
||||
_process_action(Vector2i.LEFT)
|
||||
elif event.is_action_pressed(&"move_east"):
|
||||
_process_action(Vector2i.RIGHT)
|
||||
elif event.is_action_pressed(&"move_northwest"):
|
||||
_process_action(Vector2i(-1, -1))
|
||||
elif event.is_action_pressed(&"move_northeast"):
|
||||
_process_action(Vector2i(1, -1))
|
||||
elif event.is_action_pressed(&"move_southwest"):
|
||||
_process_action(Vector2i(-1, 1))
|
||||
elif event.is_action_pressed(&"move_southeast"):
|
||||
_process_action(Vector2i(1, 1))
|
||||
|
||||
func submit_test_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
return _process_action(direction)
|
||||
|
||||
func get_game_state() -> GameState:
|
||||
return game_state
|
||||
|
||||
func _process_action(direction: Vector2i) -> Array[TurnResult]:
|
||||
var results := game_state.try_player_action(direction)
|
||||
player_view.refresh()
|
||||
_sync_enemy_views()
|
||||
hud.refresh(game_state, results)
|
||||
return results
|
||||
|
||||
func _create_initial_state() -> GameState:
|
||||
var map := GridMapStateType.new(Vector2i(12, 10), GridMapStateType.CellKind.FLOOR)
|
||||
for x in range(map.size.x):
|
||||
map.set_cell(Vector2i(x, 0), GridMapStateType.CellKind.WALL)
|
||||
map.set_cell(Vector2i(x, map.size.y - 1), GridMapStateType.CellKind.WALL)
|
||||
for y in range(map.size.y):
|
||||
map.set_cell(Vector2i(0, y), GridMapStateType.CellKind.WALL)
|
||||
map.set_cell(Vector2i(map.size.x - 1, y), GridMapStateType.CellKind.WALL)
|
||||
map.set_cell(Vector2i(1, 2), GridMapStateType.CellKind.WALL)
|
||||
map.set_cell(Vector2i(2, 1), GridMapStateType.CellKind.CLOSED_DOOR)
|
||||
map.set_cell(Vector2i(8, 3), GridMapStateType.CellKind.CHEST)
|
||||
map.set_cell(Vector2i(9, 7), GridMapStateType.CellKind.STAIRS)
|
||||
var player := ActorStateType.new(&"player", Vector2i(2, 2), 12, 3)
|
||||
var goblin := ActorStateType.new(&"goblin", Vector2i(4, 2), 6, 2)
|
||||
return GameStateType.new(map, player, {&"goblin": goblin})
|
||||
|
||||
func _sync_enemy_views() -> void:
|
||||
for enemy_id in enemy_views.keys():
|
||||
if not game_state.enemies.has(enemy_id):
|
||||
enemy_views[enemy_id].queue_free()
|
||||
enemy_views.erase(enemy_id)
|
||||
for enemy_id in game_state.enemies:
|
||||
if not enemy_views.has(enemy_id):
|
||||
var view := ActorViewType.new()
|
||||
view.name = String(enemy_id).capitalize()
|
||||
view.texture = preload("res://assets/dcss/goblin.png")
|
||||
enemies.add_child(view)
|
||||
enemy_views[enemy_id] = view
|
||||
enemy_views[enemy_id].set_actor(game_state.enemies[enemy_id])
|
||||
@@ -0,0 +1 @@
|
||||
uid://beuuw22xidrrk
|
||||
@@ -0,0 +1,13 @@
|
||||
class_name ActorView
|
||||
extends Sprite2D
|
||||
|
||||
const TILE_SIZE := Vector2i(32, 32)
|
||||
var actor: ActorState
|
||||
|
||||
func set_actor(new_actor: ActorState) -> void:
|
||||
actor = new_actor
|
||||
refresh()
|
||||
|
||||
func refresh() -> void:
|
||||
if actor != null:
|
||||
position = Vector2(actor.cell * TILE_SIZE) + Vector2(TILE_SIZE) / 2.0
|
||||
@@ -0,0 +1 @@
|
||||
uid://dlf6v03yfptt3
|
||||
@@ -0,0 +1,16 @@
|
||||
class_name GameHud
|
||||
extends CanvasLayer
|
||||
|
||||
@onready var player_health_label: Label = $PlayerHealthLabel
|
||||
@onready var enemy_health_label: Label = $EnemyHealthLabel
|
||||
@onready var event_log: RichTextLabel = $EventLog
|
||||
|
||||
func refresh(state: GameState, results: Array[TurnResult] = []) -> void:
|
||||
player_health_label.text = "Player HP: %d/%d" % [state.player.hit_points, state.player.max_hit_points]
|
||||
if state.enemies.has(&"goblin"):
|
||||
var goblin: ActorState = state.enemies[&"goblin"]
|
||||
enemy_health_label.text = "Goblin HP: %d/%d" % [goblin.hit_points, goblin.max_hit_points]
|
||||
else:
|
||||
enemy_health_label.text = "Goblin defeated"
|
||||
for result in results:
|
||||
event_log.append_text(result.message + "\n")
|
||||
@@ -0,0 +1 @@
|
||||
uid://dtqn44vuiy5gd
|
||||
@@ -0,0 +1,26 @@
|
||||
class_name MapRenderer
|
||||
extends Node2D
|
||||
|
||||
const TILE_SIZE := Vector2i(32, 32)
|
||||
const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd")
|
||||
const TEXTURES := {
|
||||
GridMapStateType.CellKind.FLOOR: preload("res://assets/dcss/floor.png"),
|
||||
GridMapStateType.CellKind.WALL: preload("res://assets/dcss/wall.png"),
|
||||
GridMapStateType.CellKind.CLOSED_DOOR: preload("res://assets/dcss/closed_door.png"),
|
||||
GridMapStateType.CellKind.CHEST: preload("res://assets/dcss/chest.png"),
|
||||
GridMapStateType.CellKind.STAIRS: preload("res://assets/dcss/stairs.png"),
|
||||
}
|
||||
|
||||
var map: GridMapState
|
||||
|
||||
func set_map(new_map: GridMapState) -> void:
|
||||
map = new_map
|
||||
queue_redraw()
|
||||
|
||||
func _draw() -> void:
|
||||
if map == null:
|
||||
return
|
||||
for y in range(map.size.y):
|
||||
for x in range(map.size.x):
|
||||
var cell := Vector2i(x, y)
|
||||
draw_texture(TEXTURES[map.get_cell(cell)], Vector2(cell * TILE_SIZE))
|
||||
@@ -0,0 +1 @@
|
||||
uid://6yudfjeimwai
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
GODOT_BIN="${GODOT_BIN:-/snap/bin/godot4}"
|
||||
if [[ ! -x "$GODOT_BIN" ]]; then
|
||||
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
|
||||
fi
|
||||
: "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}"
|
||||
exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/e2e -gexit
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
GODOT_BIN="${GODOT_BIN:-/snap/bin/godot4}"
|
||||
if [[ ! -x "$GODOT_BIN" ]]; then
|
||||
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
|
||||
fi
|
||||
: "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}"
|
||||
exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/unit -gexit
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
"$(dirname "$0")/test-unit.sh"
|
||||
"$(dirname "$0")/test-e2e.sh"
|
||||
Reference in New Issue
Block a user