class_name WaveDirector extends Node3D ## Night arrivals. By default it runs the night's NightPlan: each group's enemies (thieves, crows, ## or fence breakers) arrive at their times after nightfall, each at its own random point along ## the farm's edge (FarmZones.edge): on foot GameConfig.spawn_margin outside the map, at a free ## cell with a route onto the farm (ground_spawns); flyers flyer_spawn_margin out. They go for the ## group's target (crops, animals, supplies, or any of their type's targets by ## GameConfig.target_weights); nothing arrives by day or after sunrise. From dusk until ## WARN_AFTER_NIGHTFALL into the night the phase HUD says what tonight brings (not where: they come ## from every side; nothing is marked on the farm itself). debug_plan (F2 in debug builds) ## replaces tonight's plan. Trickle mode (GameConfig.night_thieves > 0, for tests and soaks) ## ignores the plan and keeps that many enemies of GameConfig.trickle_type on the farm at night, ## the next arriving thief_spawn_interval after one leaves. enabled = false stops all arrivals. ## rng picks the points: seed it for a repeatable night. signal arrived(handle: int, at: Vector2) const WARN_AFTER_NIGHTFALL := 12.0 const FIRST_TRICKLE_DELAY := 3.0 ## spawn()'s default spot: a random point along the edge. const ANYWHERE := Vector2(INF, INF) ## NightPlan group target -> LootSystem.Category (or ANY). const TARGETS := { "crops": LootSystem.Category.CROP, "animals": LootSystem.Category.ANIMAL, "supplies": LootSystem.Category.SUPPLY, "any": LootSystem.ANY, } var enabled := true var plans: Array[NightPlan] = [] ## Replaces tonight's plan when set (debug scenarios). var debug_plan: NightPlan var config: GameConfig var campaign: CampaignController var enemies: EnemySystem var zones: FarmZones ## Set by FarmSim: where ground enemies can arrive (cell centers along the edge with a route in). var ground_spawns := PackedVector2Array() var rng := RandomNumberGenerator.new() ## Seconds since nightfall, and arrivals so far tonight. var night_time: float = 0.0 var spawned: int = 0 var _queue: Array = [] # [time, want, type], sorted var _next: int = 0 var _trickle_wait: float = FIRST_TRICKLE_DELAY func setup(p_campaign: CampaignController, p_enemies: EnemySystem, p_zones: FarmZones, p_plans: Array[NightPlan]) -> void: campaign = p_campaign config = p_campaign.config enemies = p_enemies zones = p_zones plans = p_plans rng.randomize() campaign.phase_changed.connect(_on_phase_changed) func plan_for(night: int) -> NightPlan: if plans.is_empty(): return null return plans[clampi(night - 1, 0, plans.size() - 1)] func tonight() -> NightPlan: return debug_plan if debug_plan != null else plan_for(campaign.state.night) ## A night's length from its plan (0 or less: GameConfig.night_length). The debug plan counts ## for tonight. func night_length(night: int) -> float: var plan := debug_plan if debug_plan != null and night == campaign.state.night else plan_for(night) return plan.night_length if plan != null else 0.0 func trickle() -> bool: return config.night_thieves > 0 static func want_of(group: Dictionary) -> int: return int(TARGETS.get(String(group.get("target", "any")), LootSystem.ANY)) ## A NightPlan group's enemy type ("thief", "crow", "breaker") as an EnemySystem type id. static func type_of(group: Dictionary) -> int: return maxi(EnemySystem.TYPE_NAMES.find(String(group.get("type", "thief"))), 0) ## What tonight brings: its groups' [type, want] pairs (in the plan's order, no repeats). func tonight_entries() -> Array: var out := [] var plan := tonight() if plan == null or trickle(): return out for g in plan.groups: var entry := [type_of(g), want_of(g)] if not out.has(entry): out.append(entry) return out ## What a [type, want] pair is called: "Crows", "Alien robots", "Aliens after the crops", ## "Aliens after the animals", "Aliens after the supplies", or "Aliens" (any target). static func entry_label(entry: Array) -> String: match int(entry[0]): EnemySystem.TYPE_CROW: return "Crows" EnemySystem.TYPE_BREAKER: return "Alien robots" match int(entry[1]): LootSystem.Category.CROP: return "Aliens after the crops" LootSystem.Category.ANIMAL: return "Aliens after the animals" LootSystem.Category.SUPPLY: return "Aliens after the supplies" return "Aliens" ## Tonight's warning as lines for the HUD: "Tonight, from every side: crows, aliens after the ## supplies and alien robots". func tonight_warning() -> PackedStringArray: var labels := PackedStringArray() for e in tonight_entries(): var label := entry_label(e).to_lower() if not labels.has(label): labels.append(label) if labels.is_empty(): return PackedStringArray() return PackedStringArray(["Tonight, from every side: %s" % PhaseHud.join_words(labels)]) func warning_active() -> bool: if not enabled or trickle(): return false var phase := campaign.state.phase return phase == CampaignController.Phase.DUSK or (phase == CampaignController.Phase.NIGHT and night_time < WARN_AFTER_NIGHTFALL) ## A random point along the farm's edge for an enemy of type: a flyer anywhere on its ring, a ## ground enemy at one of ground_spawns. func spawn_point(type: int) -> Vector2: if enemies.t_flying[type] == 1 or ground_spawns.is_empty(): var margin := config.flyer_spawn_margin if enemies.t_flying[type] == 1 else config.spawn_margin return zones.edge_point(rng.randf(), margin) return ground_spawns[rng.randi_range(0, ground_spawns.size() - 1)] ## Sends one enemy of type in, going for want: at a random point along the edge, or at the given ## point. Returns its handle (-1 if the pool is full). func spawn(type: int, want: int = LootSystem.ANY, at: Vector2 = ANYWHERE) -> int: var p := at if at.is_finite() else spawn_point(type) var h := enemies.spawn(type, p.x, p.y, zones.playable.get_center() - p, want) if h >= 0: spawned += 1 arrived.emit(h, p) return h ## Sends an enemy of the trickle type in. func spawn_next(want: int = LootSystem.ANY) -> int: return spawn(trickle_type(), want) func trickle_type() -> int: return maxi(EnemySystem.TYPE_NAMES.find(config.trickle_type), 0) ## Physics tick (FarmSim): arrivals while the night lasts. func step(dt: float) -> void: if campaign.state.phase != CampaignController.Phase.NIGHT: return night_time += dt if not enabled: return if trickle(): if enemies.alive_count() >= config.night_thieves: _trickle_wait = config.thief_spawn_interval return _trickle_wait -= dt if _trickle_wait <= 0.0: spawn_next(int(TARGETS.get(config.trickle_target, LootSystem.ANY))) _trickle_wait = config.thief_spawn_interval return while _next < _queue.size() and float(_queue[_next][0]) <= night_time: spawn(int(_queue[_next][2]), int(_queue[_next][1])) _next += 1 func _on_phase_changed(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void: if phase != CampaignController.Phase.NIGHT: return night_time = 0.0 spawned = 0 _trickle_wait = FIRST_TRICKLE_DELAY _queue.clear() _next = 0 var plan := tonight() if plan != null: for g in plan.groups: var every := NightPlan.scaled_every(g, plan.spacing_scale_in(config)) for k in NightPlan.scaled_count(g, plan.count_scale_in(config)): _queue.append([float(g.get("time", 0.0)) + k * every, want_of(g), type_of(g)]) _queue.sort_custom(func(a: Array, b: Array) -> bool: return float(a[0]) < float(b[0]))