newlon/scripts/gui/ZombieLevelPreviewer.cs
2025-07-25 18:48:49 +05:00

57 lines
1.2 KiB
C#

using Godot;
using Newlon.Components.Zombies;
using System.Collections.Generic;
using Newlon.Resources;
namespace Newlon.Components.Level;
public partial class ZombieLevelPreviewer : Node2D
{
public void OnLevelStateChanged(RuntimeLevelData.LevelStates state)
{
switch (state)
{
case RuntimeLevelData.LevelStates.ChooseYourSeeds:
SetupZombies();
break;
case RuntimeLevelData.LevelStates.Pregame:
break;
default:
QueueFree();
break;
}
}
public void SetupZombies()
{
var to_spawn = FindUnique();
foreach (var zombie in to_spawn)
{
var rng_x = (float)GD.RandRange(0.0, 2.0);
var rng_y = (float)GD.RandRange(1.0, 5.0);
var spawned = zombie.Scene.Instantiate<RuntimeZombieData>();
spawned.DisableBrain();
AddChild(spawned);
spawned.Position += new Vector2(rng_x*FieldParams.TileWidth, rng_y*FieldParams.TileHeight);
}
}
public List<ZombieResource> FindUnique()
{
List<ZombieResource> zombies = new();
foreach (var wave in RuntimeLevelData.LevelResource.waves)
{
foreach (var spawn in wave.zombiesOrdered)
{
foreach (var zombie in spawn.zombies)
{
if (zombie == null || zombies.Contains(zombie)) continue;
zombies.Add(zombie);
}
}
}
return zombies;
}
}