class_name CampaignController extends Node ## Owns the five-night vacation: the phase state machine, phase timers, the outcome rules, and ## the morning snapshot used to retry a failed night (rules: game/RULES.md). ## ## 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) @export var config: GameConfig var state: FarmState var outcome: Outcome = Outcome.CONTINUE 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 _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 _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) ## D9: restarts the current night from its morning (the autosave point once saving exists). func retry_night() -> bool: var restored := FarmState.from_dict(_morning_snapshot) if restored == null: return false state = restored outcome = Outcome.CONTINUE _enter(Phase.MORNING) return true 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(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: 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: state.nights_survived = state.night state.log_night(_night_start) var result := check_outcome(state, config.campaign_nights) if result != Outcome.CONTINUE: outcome = result _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 := config.phase_length(phase) state.phase_time_left = length if is_finite(length) else 0.0 match phase: Phase.MORNING: _morning_snapshot = state.to_dict() 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