221 lines
7.3 KiB
GDScript
221 lines
7.3 KiB
GDScript
class_name CampaignController
|
|
extends Node
|
|
## Owns the vacation (GameConfig.campaign_nights nights): the phase state machine, phase timers,
|
|
## the outcome rules, and the morning snapshot used to retry a failed night (rules:
|
|
## game/RULES.md). With save_path set, every morning is also saved to disk (SaveGame), and
|
|
## finishing the vacation deletes the save.
|
|
##
|
|
## MORNING -> DAY -> DUSK -> NIGHT -> DAWN -> MORNING of the next night; after the final dawn,
|
|
## FARMERS_RETURN. GAME_OVER can follow any phase. Both are terminal. The morning review is
|
|
## untimed and ends through begin_day(); the other phases run on game-time timers, so
|
|
## Engine.time_scale speeds the whole cycle up.
|
|
##
|
|
## Gameplay systems (later steps) run before tick() in each physics tick. tick() advances the
|
|
## phase timer, then evaluates the outcome before applying any transition. Failures are checked
|
|
## before completion, so a scarecrow destroyed on the tick the final dawn ends is a Game Over.
|
|
|
|
enum Phase { MORNING, DAY, DUSK, NIGHT, DAWN, FARMERS_RETURN, GAME_OVER }
|
|
enum Outcome { CONTINUE, GAME_OVER_SCARECROW, GAME_OVER_STRIPPED, VACATION_COMPLETE }
|
|
|
|
signal phase_changed(phase: Phase, previous: Phase)
|
|
signal campaign_ended(outcome: Outcome)
|
|
## The dawn grace is over: listeners settle the night (enemies leave, loot on the ground returns)
|
|
## before its losses are recorded and the outcome is checked.
|
|
signal night_ending
|
|
|
|
@export var config: GameConfig
|
|
|
|
var state: FarmState
|
|
var outcome: Outcome = Outcome.CONTINUE
|
|
## Set by FarmSim: night -> its plan's length, when the plan overrides GameConfig.night_length
|
|
## (returns 0 or less for "no override").
|
|
var night_length_for: Callable
|
|
## Co-op, on the partner's machine (a replica of the host's farm): what the player asks for
|
|
## (begin_day, retry_night, start_night_early) goes to the host instead ("begin_day" etc.).
|
|
var remote_request: Callable
|
|
## Where every morning is autosaved ("" = not saved; Main sets SaveGame.PATH).
|
|
var save_path := ""
|
|
## The last autosave's result.
|
|
var last_save_error: Error = OK
|
|
|
|
var _morning_snapshot: Dictionary = {}
|
|
var _night_start := PackedInt32Array()
|
|
|
|
|
|
func _ready() -> void:
|
|
if config == null:
|
|
config = load("res://game/resources/game_config.tres") as GameConfig
|
|
config = config.duplicate() as GameConfig # runtime overrides never touch the asset
|
|
new_campaign()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
tick(delta)
|
|
|
|
|
|
## Starts a fresh vacation: full farm, full scarecrow, the morning of night 1.
|
|
func new_campaign() -> void:
|
|
state = FarmState.create(config)
|
|
outcome = Outcome.CONTINUE
|
|
_enter(Phase.MORNING)
|
|
|
|
|
|
func tick(delta: float) -> void:
|
|
if is_over():
|
|
return
|
|
var finished := false
|
|
if is_timed(state.phase):
|
|
state.phase_time_left = maxf(state.phase_time_left - delta, 0.0)
|
|
finished = state.phase_time_left <= 0.0
|
|
_resolve(finished)
|
|
|
|
|
|
## Ends the morning review, the only way out of the untimed morning.
|
|
func begin_day() -> bool:
|
|
if state.phase != Phase.MORNING:
|
|
return false
|
|
if remote_request.is_valid():
|
|
remote_request.call("begin_day")
|
|
return true
|
|
_resolve(true)
|
|
return true
|
|
|
|
|
|
## D1: skips the rest of the day or dusk straight to the night, when the option is enabled.
|
|
func start_night_early() -> bool:
|
|
if not can_start_night_early():
|
|
return false
|
|
if remote_request.is_valid():
|
|
remote_request.call("start_night_early")
|
|
return true
|
|
_enter(Phase.NIGHT)
|
|
return true
|
|
|
|
|
|
func can_start_night_early() -> bool:
|
|
return config.allow_early_night and (state.phase == Phase.DAY or state.phase == Phase.DUSK)
|
|
|
|
|
|
## Debug: ends the current phase now, through the normal transition and outcome path.
|
|
func advance_phase() -> void:
|
|
if not is_over():
|
|
_resolve(true)
|
|
|
|
|
|
## Continues a saved vacation (SaveGame.read): its morning becomes the current one.
|
|
func load_state(saved: FarmState) -> void:
|
|
state = saved
|
|
outcome = Outcome.CONTINUE
|
|
_enter(Phase.MORNING)
|
|
|
|
|
|
## The latest morning's state (what retry_night() restores and what was saved).
|
|
func morning_snapshot() -> Dictionary:
|
|
return _morning_snapshot.duplicate(true)
|
|
|
|
|
|
## D9: restarts the current night from its morning (the autosave point).
|
|
func retry_night() -> bool:
|
|
if remote_request.is_valid():
|
|
remote_request.call("retry_night")
|
|
return true
|
|
var restored := FarmState.from_dict(_morning_snapshot)
|
|
if restored == null:
|
|
return false
|
|
state = restored
|
|
outcome = Outcome.CONTINUE
|
|
_enter(Phase.MORNING)
|
|
return true
|
|
|
|
|
|
## D8: gathering, building, and upgrades happen by day (day and dusk), not at night or during
|
|
## the morning review.
|
|
func is_daytime() -> bool:
|
|
return state.phase == Phase.DAY or state.phase == Phase.DUSK
|
|
|
|
|
|
func is_over() -> bool:
|
|
return state.phase == Phase.GAME_OVER or state.phase == Phase.FARMERS_RETURN
|
|
|
|
|
|
func is_timed(phase: Phase) -> bool:
|
|
return is_finite(phase_length(phase))
|
|
|
|
|
|
## Seconds a phase lasts tonight (the night can be longer or shorter than usual), or INF.
|
|
func phase_length(phase: Phase) -> float:
|
|
if phase == Phase.NIGHT and night_length_for.is_valid():
|
|
var override: float = night_length_for.call(state.night)
|
|
if override > 0.0:
|
|
return override
|
|
return config.phase_length(phase)
|
|
|
|
|
|
## The outcome rules as a pure function of the farm state. Scarecrow destruction is checked
|
|
## first, then a stripped farm, and only then completing the vacation.
|
|
static func check_outcome(s: FarmState, campaign_nights: int) -> Outcome:
|
|
if s.scarecrow_hp <= 0 and (not s.coop or s.partner_hp <= 0): # in co-op, both down
|
|
return Outcome.GAME_OVER_SCARECROW
|
|
if s.is_stripped():
|
|
return Outcome.GAME_OVER_STRIPPED
|
|
if s.nights_survived >= campaign_nights:
|
|
return Outcome.VACATION_COMPLETE
|
|
return Outcome.CONTINUE
|
|
|
|
|
|
func _resolve(phase_finished: bool) -> void:
|
|
var finished_phase := state.phase
|
|
if phase_finished and finished_phase == Phase.DAWN:
|
|
night_ending.emit()
|
|
state.nights_survived = state.night
|
|
state.log_night(_night_start)
|
|
state.settle_supplies()
|
|
var result := check_outcome(state, config.campaign_nights)
|
|
if result != Outcome.CONTINUE:
|
|
outcome = result
|
|
if result == Outcome.VACATION_COMPLETE and save_path != "":
|
|
SaveGame.delete(save_path)
|
|
_enter(Phase.FARMERS_RETURN if result == Outcome.VACATION_COMPLETE else Phase.GAME_OVER)
|
|
campaign_ended.emit(result)
|
|
return
|
|
if not phase_finished:
|
|
return
|
|
if finished_phase == Phase.DAWN:
|
|
state.night += 1
|
|
_enter(_next_phase(finished_phase))
|
|
|
|
|
|
func _enter(phase: Phase) -> void:
|
|
var previous := state.phase
|
|
state.phase = phase
|
|
var length := phase_length(phase)
|
|
state.phase_time_left = length if is_finite(length) else 0.0
|
|
match phase:
|
|
Phase.MORNING:
|
|
state.night_events.clear() # the day's and night's counts start now
|
|
if state.coop: # a scarecrow that fell in the night is back, at half health
|
|
if state.scarecrow_hp <= 0:
|
|
state.scarecrow_hp = ceili(state.scarecrow_max_hp / 2.0)
|
|
if state.partner_hp <= 0:
|
|
state.partner_hp = ceili(state.partner_max_hp / 2.0)
|
|
_morning_snapshot = state.to_dict()
|
|
if save_path != "":
|
|
last_save_error = SaveGame.write(_morning_snapshot, save_path)
|
|
if last_save_error != OK:
|
|
push_warning("Autosave to %s failed: %s" % [save_path, error_string(last_save_error)])
|
|
Phase.NIGHT:
|
|
_night_start = state.asset_counts()
|
|
phase_changed.emit(phase, previous)
|
|
|
|
|
|
static func _next_phase(phase: Phase) -> Phase:
|
|
match phase:
|
|
Phase.MORNING:
|
|
return Phase.DAY
|
|
Phase.DAY:
|
|
return Phase.DUSK
|
|
Phase.DUSK:
|
|
return Phase.NIGHT
|
|
Phase.NIGHT:
|
|
return Phase.DAWN
|
|
return Phase.MORNING
|