130 lines
6.3 KiB
GDScript
130 lines
6.3 KiB
GDScript
extends RefCounted
|
|
## Phase state machine: the untimed morning, phase order and lengths, five-night completion,
|
|
## the early-night option, night loss records, and retrying a failed night from its morning.
|
|
|
|
const O := CampaignController.Outcome
|
|
const P := CampaignController.Phase
|
|
|
|
|
|
func run(r: GameTestRunner) -> void:
|
|
_order_and_timers(r)
|
|
_five_nights(r)
|
|
_early_night(r)
|
|
_night_log(r)
|
|
_retry(r)
|
|
|
|
|
|
func _order_and_timers(r: GameTestRunner) -> void:
|
|
var c := r.make_campaign()
|
|
var seen: Array[CampaignController.Phase] = []
|
|
c.phase_changed.connect(func(p: CampaignController.Phase, _prev: CampaignController.Phase) -> void: seen.append(p))
|
|
r.check(c.state.phase == P.MORNING and c.state.night == 1, "a new vacation starts on the morning of night 1")
|
|
c.tick(1000.0)
|
|
r.check(c.state.phase == P.MORNING, "the morning review is untimed (waits for the player)")
|
|
r.check(c.begin_day(), "begin_day ends the morning review")
|
|
r.check(not c.begin_day(), "begin_day only works in the morning")
|
|
|
|
var cfg := c.config
|
|
var expected := {P.DAY: cfg.day_length, P.DUSK: cfg.dusk_length, P.NIGHT: cfg.night_length, P.DAWN: cfg.dawn_grace}
|
|
for phase: CampaignController.Phase in [P.DAY, P.DUSK, P.NIGHT, P.DAWN]:
|
|
r.check(c.state.phase == phase and is_equal_approx(c.state.phase_time_left, expected[phase]),
|
|
"%s starts with %.0f s on the clock" % [P.keys()[phase], expected[phase]])
|
|
var took := GameTestRunner.run_phase(c)
|
|
r.check(absf(took - expected[phase]) <= GameTestRunner.TICK + 1e-4,
|
|
"%s lasts %.0f s of game time (took %.3f s)" % [P.keys()[phase], expected[phase], took])
|
|
r.check(seen == [P.DAY, P.DUSK, P.NIGHT, P.DAWN, P.MORNING], "phases run MORNING -> DAY -> DUSK -> NIGHT -> DAWN -> MORNING")
|
|
r.check(c.state.night == 2 and c.state.nights_survived == 1, "after the first dawn it is the morning of night 2")
|
|
|
|
# advance_phase (debug) goes through the same transitions.
|
|
c.advance_phase()
|
|
r.check(c.state.phase == P.DAY, "advance_phase ends the morning")
|
|
c.advance_phase()
|
|
c.advance_phase()
|
|
c.advance_phase()
|
|
c.advance_phase()
|
|
r.check(c.state.phase == P.MORNING and c.state.night == 3, "advance_phase walks a whole cycle")
|
|
|
|
|
|
func _five_nights(r: GameTestRunner) -> void:
|
|
var c := r.make_campaign()
|
|
var ended: Array[CampaignController.Outcome] = []
|
|
var mornings: Array[int] = []
|
|
c.campaign_ended.connect(func(o: CampaignController.Outcome) -> void: ended.append(o))
|
|
c.phase_changed.connect(func(p: CampaignController.Phase, _prev: CampaignController.Phase) -> void:
|
|
if p == P.MORNING:
|
|
mornings.append(c.state.night))
|
|
GameTestRunner.run_until(c, P.FARMERS_RETURN)
|
|
r.check(c.state.phase == P.FARMERS_RETURN and c.outcome == O.VACATION_COMPLETE, "surviving every night brings the farmers back")
|
|
r.check(c.state.nights_survived == 5 and c.state.night == 5, "the vacation is exactly five nights (night %d, survived %d)" % [c.state.night, c.state.nights_survived])
|
|
r.check(mornings == [2, 3, 4, 5], "a morning follows each night except the last (got %s)" % [mornings])
|
|
r.check(ended == [O.VACATION_COMPLETE], "campaign_ended fires once")
|
|
r.check(c.state.night_log.size() == 5, "every night is recorded")
|
|
c.tick(1000.0)
|
|
c.advance_phase()
|
|
r.check(c.state.phase == P.FARMERS_RETURN and ended.size() == 1, "the farmers' return is terminal")
|
|
|
|
c.new_campaign()
|
|
r.check(c.state.phase == P.MORNING and c.state.night == 1 and c.outcome == O.CONTINUE and c.state.night_log.is_empty(),
|
|
"new_campaign starts a fresh vacation")
|
|
|
|
|
|
func _early_night(r: GameTestRunner) -> void:
|
|
var c := r.make_campaign()
|
|
r.check(not c.config.allow_early_night, "starting the night early is off by default")
|
|
c.begin_day()
|
|
r.check(not c.can_start_night_early() and not c.start_night_early() and c.state.phase == P.DAY,
|
|
"with the option off, the day can't be cut short")
|
|
GameTestRunner.run_until(c, P.DUSK)
|
|
r.check(not c.start_night_early() and c.state.phase == P.DUSK, "with the option off, dusk can't be cut short")
|
|
|
|
var cfg := c.config.duplicate() as GameConfig
|
|
cfg.allow_early_night = true
|
|
c = r.make_campaign(cfg)
|
|
r.check(not c.start_night_early(), "the night can't start early from the morning review")
|
|
c.begin_day()
|
|
c.tick(10.0)
|
|
r.check(c.start_night_early() and c.state.phase == P.NIGHT and is_equal_approx(c.state.phase_time_left, cfg.night_length),
|
|
"with the option on, the day can skip straight to a full night")
|
|
r.check(not c.start_night_early(), "the night can't be started early again")
|
|
c = r.make_campaign(cfg)
|
|
GameTestRunner.run_until(c, P.DUSK)
|
|
r.check(c.start_night_early() and c.state.phase == P.NIGHT, "with the option on, dusk can skip to the night")
|
|
|
|
|
|
func _night_log(r: GameTestRunner) -> void:
|
|
var c := r.make_campaign()
|
|
GameTestRunner.run_until(c, P.NIGHT)
|
|
GameTestRunner.lose_crops(c.state, 3)
|
|
GameTestRunner.run_until(c, P.DAWN)
|
|
GameTestRunner.lose_crops(c.state, 1) # loot escaping during the dawn grace still counts for that night
|
|
GameTestRunner.run_until(c, P.MORNING)
|
|
var e: Dictionary = c.state.night_log[0]
|
|
r.check(e["night"] == 1 and e["crops_lost"] == 4, "night 1 records 4 crops lost, dawn included (got %s)" % [e])
|
|
r.check(c.state.crops == c.config.start_crops - 4, "losses persist into the next day")
|
|
GameTestRunner.lose_crops(c.state, 2) # lost during the day don't count against the next night
|
|
c.begin_day()
|
|
GameTestRunner.run_until(c, P.MORNING)
|
|
r.check(c.state.night_log[1]["crops_lost"] == 0, "night 2 only counts losses after it started")
|
|
|
|
|
|
func _retry(r: GameTestRunner) -> void:
|
|
var c := r.make_campaign()
|
|
GameTestRunner.run_until(c, P.NIGHT)
|
|
GameTestRunner.lose_crops(c.state, 3)
|
|
c.state.scarecrow_hp -= 4
|
|
GameTestRunner.run_until(c, P.MORNING)
|
|
var morning := c.state.to_dict()
|
|
|
|
c.begin_day()
|
|
GameTestRunner.run_until(c, P.NIGHT)
|
|
GameTestRunner.lose_crops(c.state, 5)
|
|
c.state.scarecrow_hp = 0
|
|
c.tick(GameTestRunner.TICK)
|
|
r.check(c.state.phase == P.GAME_OVER, "the scarecrow falls on night 2")
|
|
r.check(c.retry_night(), "retry_night is available after Game Over")
|
|
r.check(c.state.to_dict() == morning, "retry restores the morning of the failed night exactly")
|
|
r.check(c.state.night == 2 and c.state.scarecrow_hp == c.config.scarecrow_max_hp - 4 and c.outcome == O.CONTINUE,
|
|
"night 1's losses and damage are kept; night 2 is replayed")
|
|
c.begin_day()
|
|
GameTestRunner.run_until(c, P.MORNING)
|
|
r.check(c.state.night == 3 and c.state.night_log.size() == 2, "the retried night plays through normally")
|