cirnogodot/Scripts/Actors/EnemyMarker3D.cs
2025-09-22 16:33:15 +02:00

134 lines
No EOL
3.1 KiB
C#

using Cirno.Scripts.Components.FSM.Enemy._3D;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Actors;
[Tool]
public partial class EnemyMarker3D : PreviewMarker3D, IActivable
{
private readonly Vector3 _boxSize = new Vector3(0.5f, 0.8f, 0.5f);
private EnemyResource _enemy;
[Export]
public EnemyResource Enemy
{
get => _enemy;
set
{
_enemy = value;
if (Engine.IsEditorHint())
{
//QueueRedraw();
this.Texture = _enemy.IconSprite;
}
}
}
private bool _autoSpawn = false;
[Export]
public bool AutoSpawn
{
get => _autoSpawn;
set
{
_autoSpawn = value;
if (Engine.IsEditorHint())
{
if (_autoSpawn)
{
SetSpriteAlpha(1);
}
else
{
SetSpriteAlpha(0.5f);
}
}
}
}
private EnemyProxy3D _spawnedEnemy;
public void _func_godot_apply_properties(Dictionary<string, Variant> props)
{
//GroupName = (string)props["targetname"];
this.AddToGroup("EnemyMarkers");
AutoSpawn = props["autospawn"].AsBool();
var scriptPath = props["resource_path"].AsString();
if (!string.IsNullOrWhiteSpace(scriptPath))
{
Enemy = GD.Load<EnemyResource>(scriptPath);
}
else
{
GD.PushWarning($"Spawner {this.Name} has no enemy assigned");
}
Billboard = true;
//MarkerId = props["id"].AsInt32();
}
public override void _Ready()
{
base._Ready();
if (AutoSpawn)
{
Spawn(false);
}
}
public EnemyProxy3D Spawn(bool deleteMarker)
{
if (Engine.IsEditorHint()) return null;
if (Enemy is null) return null;
if (_spawnedEnemy is not null) return _spawnedEnemy;
if (deleteMarker)
{
this.QueueFree();
}
var enemyScene = GD.Load<PackedScene>(Enemy.PrefabPath);
_spawnedEnemy = this.CreateSibling<EnemyProxy3D>(enemyScene);
_spawnedEnemy.Init(Enemy);
//Spawned = true;
_spawnedEnemy.Death += SpawnedEnemyOnDeath;
return _spawnedEnemy;
}
private void SpawnedEnemyOnDeath(EnemyProxy3D enemy)
{
_spawnedEnemy.Death -= SpawnedEnemyOnDeath;
_spawnedEnemy = null;
//Spawned = false;
}
public bool Activate(ActivationType activationType = ActivationType.Toggle)
{
if (Engine.IsEditorHint()) return false;
Spawn(false);
return _spawnedEnemy is not null;
}
public void Toggle()
{
Activate();
}
public override void _Process(double delta)
{
if (!Engine.IsEditorHint()) return;
#if !DISABLE_DD3D
DebugDraw3D.DrawBox(this.GlobalPosition - _boxSize / 2, Quaternion.Identity, _boxSize, Colors.Red);
#endif
//DebugDraw3D.DrawSphere(this.GlobalPosition, 0.1f, Colors.Green);
}
}