40 lines
1.8 KiB
GDScript
40 lines
1.8 KiB
GDScript
class_name PartyState
|
|
extends RefCounted
|
|
|
|
# Recruitment order is the party's authoritative turn order. There is
|
|
# intentionally no capacity: every successfully recruited crawler is retained.
|
|
var members: Array[ActorState] = []
|
|
|
|
func recruit(actor: ActorState, player_cell: Vector2i, map: GridMapState, blocked_cells: Dictionary = {}) -> Dictionary:
|
|
if actor == null or not actor.is_alive() or has_member(actor.id):
|
|
return {"accepted": false, "reason": &"invalid_member"}
|
|
var occupied := blocked_cells.duplicate()
|
|
for member in members:
|
|
if member.is_alive():
|
|
occupied[member.cell] = true
|
|
occupied[player_cell] = true
|
|
var placement := _nearest_free_cell(actor.cell, player_cell, map, occupied, not blocked_cells.has(actor.cell))
|
|
if placement == Vector2i(-1, -1):
|
|
return {"accepted": false, "reason": &"no_free_placement"}
|
|
actor.cell = placement
|
|
members.append(actor)
|
|
return {"accepted": true, "member": actor, "cell": placement}
|
|
|
|
func has_member(actor_id: StringName) -> bool:
|
|
return members.any(func(member: ActorState) -> bool: return member.id == actor_id)
|
|
|
|
func living_members() -> Array[ActorState]:
|
|
return members.filter(func(member: ActorState) -> bool: return member != null and member.is_alive())
|
|
|
|
func _nearest_free_cell(preferred: Vector2i, player_cell: Vector2i, map: GridMapState, occupied: Dictionary, allow_preferred: bool) -> Vector2i:
|
|
if allow_preferred and absi(preferred.x - player_cell.x) + absi(preferred.y - player_cell.y) <= 1 and _is_free(preferred, map, occupied):
|
|
return preferred
|
|
for direction: Vector2i in [Vector2i.LEFT, Vector2i.DOWN, Vector2i.RIGHT, Vector2i.UP]:
|
|
var candidate := player_cell + direction
|
|
if _is_free(candidate, map, occupied):
|
|
return candidate
|
|
return Vector2i(-1, -1)
|
|
|
|
func _is_free(cell: Vector2i, map: GridMapState, occupied: Dictionary) -> bool:
|
|
return map.is_walkable(cell) and not occupied.has(cell)
|