cirnogodot/Scripts/Actors/RogueliteEnemySpawner.cs

157 lines
3.8 KiB
C#
Raw Normal View History

2025-06-03 11:32:26 +02:00
using System;
using System.Linq;
2025-04-28 12:22:00 +02:00
using Cirno.Scripts.Components.FSM.Enemy;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.Loot;
using Cirno.Scripts.Utils;
using Godot;
namespace Cirno.Scripts.Actors;
[Tool]
2025-06-03 11:32:26 +02:00
public partial class RogueliteEnemySpawner : Marker2D, IActivable
{
private EnemyResource _enemy;
[Export]
public EnemyResource Enemy
{
get => _enemy;
set
{
_enemy = value;
if (Engine.IsEditorHint())
{
QueueRedraw();
}
}
}
2025-06-03 11:32:26 +02:00
private bool _autoSpawn = false;
[Export]
public bool AutoSpawn
{
get => _autoSpawn;
set
{
_autoSpawn = value;
if (Engine.IsEditorHint())
{
QueueRedraw();
}
}
}
2025-06-03 10:11:09 +02:00
2025-04-28 12:22:00 +02:00
[Export] public int Wave { get; private set; } = 0;
public bool Spawned { get; private set; } = false;
2025-06-03 11:41:50 +02:00
[Export] public Path2D Path { get; private set; }
2025-04-28 12:22:00 +02:00
[ExportToolButton("Update Icon")] public Callable RedrawButton => Callable.From(Redraw);
2025-04-28 12:22:00 +02:00
private EnemyFSMProxy _spawnedEnemy;
public override void _Draw()
{
if (!Engine.IsEditorHint()) return;
if (Enemy is null) return;
if (Enemy.IconSprite is null) return;
2025-06-03 11:32:26 +02:00
var modulate = AutoSpawn ? new Color(1.0f, 1.0f, 1.0f, 1.0f) : new Color(1.0f, 1.0f, 1.0f, 0.5f);
DrawTexture(Enemy.IconSprite, - new Vector2(_enemy.IconSprite.GetWidth() / 2f, _enemy.IconSprite.GetHeight() / 2f), modulate);
}
private void Redraw()
{
QueueRedraw();
}
2025-04-28 12:22:00 +02:00
2025-06-03 10:11:09 +02:00
public override void _Ready()
{
if (Engine.IsEditorHint()) return;
if (!AutoSpawn) return;
Spawn();
}
public EnemyFSMProxy Spawn()
2025-04-28 12:22:00 +02:00
{
if (Engine.IsEditorHint()) return null;
if (Spawned) return _spawnedEnemy;
var enemyScene = GD.Load<PackedScene>(Enemy.PrefabPath);
2025-06-03 10:11:09 +02:00
2025-04-28 12:22:00 +02:00
_spawnedEnemy = this.CreateSibling<EnemyFSMProxy>(enemyScene);
2025-06-03 10:11:09 +02:00
_spawnedEnemy.Init(Enemy);
Spawned = true;
_spawnedEnemy.Death += SpawnedEnemyOnDeath;
return _spawnedEnemy;
}
public EnemyFSMProxy Spawn(RogueliteMapTheme mapTheme)
{
if (Engine.IsEditorHint()) return null;
if (Spawned) return _spawnedEnemy;
2025-04-28 12:22:00 +02:00
2025-06-03 10:11:09 +02:00
var spawnedEnemy = Spawn();
spawnedEnemy.OverrideLoot = true;
spawnedEnemy.ExtraLoot.Add(new LootDrop()
2025-04-28 12:22:00 +02:00
{
Item = mapTheme.EnemiesLootTable.Items.ToList().Shuffle().First(),
Chance = mapTheme.EnemyDropChance
});
2025-04-29 16:10:17 +02:00
2025-06-03 10:11:09 +02:00
spawnedEnemy.ExtraLoot.Add(new LootDrop()
2025-04-28 12:22:00 +02:00
{
Item = mapTheme.PointItemResource,
Chance = 100
});
2025-06-03 10:11:09 +02:00
return spawnedEnemy;
2025-04-28 12:22:00 +02:00
}
private void SpawnedEnemyOnDeath(EnemyFSMProxy enemy)
{
_spawnedEnemy.Death -= SpawnedEnemyOnDeath;
_spawnedEnemy = null;
Spawned = false;
}
2025-06-03 11:32:26 +02:00
public bool Activate(ActivationType activationType = ActivationType.Toggle)
{
switch (activationType)
{
case ActivationType.Toggle:
Spawn();
break;
case ActivationType.Enable:
break;
case ActivationType.Disable:
break;
case ActivationType.Use:
break;
case ActivationType.Destroy:
break;
case ActivationType.Open:
break;
case ActivationType.Close:
break;
default:
throw new ArgumentOutOfRangeException(nameof(activationType), activationType, null);
}
return true;
}
2025-06-18 11:33:27 +02:00
public void Toggle()
{
this.Activate();
}
}