cirnogodot/Scripts/Actors/EnemyMarker3D.cs

174 lines
4.1 KiB
C#
Raw Permalink Normal View History

2025-06-21 16:44:44 +02:00
using Cirno.Scripts.Components.FSM.Enemy._3D;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
using Godot;
2025-09-22 16:33:10 +02:00
using Godot.Collections;
2025-06-21 16:44:44 +02:00
namespace Cirno.Scripts.Actors;
[Tool]
2025-07-01 10:00:02 +02:00
public partial class EnemyMarker3D : PreviewMarker3D, IActivable
2025-06-21 16:44:44 +02:00
{
2025-07-02 10:34:14 +02:00
private readonly Vector3 _boxSize = new Vector3(0.5f, 0.8f, 0.5f);
2025-08-12 14:37:48 +02:00
2025-06-21 16:44:44 +02:00
private EnemyResource _enemy;
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
[Export]
public EnemyResource Enemy
{
get => _enemy;
set
{
_enemy = value;
if (Engine.IsEditorHint())
{
//QueueRedraw();
2025-12-28 22:53:31 +01:00
this.Texture = _enemy?.IconSprite;
2025-06-21 16:44:44 +02:00
}
}
}
2025-07-01 10:00:02 +02:00
private bool _autoSpawn = false;
[Export]
public bool AutoSpawn
{
get => _autoSpawn;
set
{
_autoSpawn = value;
if (Engine.IsEditorHint())
{
if (_autoSpawn)
{
2025-09-26 10:53:20 +02:00
Alpha = 1.0f;
// SetSpriteAlpha(1);
2025-07-01 10:00:02 +02:00
}
else
{
2025-09-26 10:53:20 +02:00
Alpha = 0.5f;
//SetSpriteAlpha(0.5f);
2025-07-01 10:00:02 +02:00
}
2025-09-26 10:53:20 +02:00
QueueRedraw();
2025-07-01 10:00:02 +02:00
}
}
}
2025-06-21 16:44:44 +02:00
private EnemyProxy3D _spawnedEnemy;
2025-07-01 10:00:02 +02:00
2025-09-22 18:19:40 +02:00
[Export] public StringName TargetName { get; set; }
2025-09-22 16:33:10 +02:00
public void _func_godot_apply_properties(Dictionary<string, Variant> props)
{
//GroupName = (string)props["targetname"];
this.AddToGroup("EnemyMarkers");
2025-09-26 10:53:20 +02:00
_billboard = true;
_autoSpawn = props["autospawn"].AsBool();
if (_autoSpawn)
{
_alpha = 1.0f;
// SetSpriteAlpha(1);
}
else
{
_alpha = 0.5f;
//SetSpriteAlpha(0.5f);
}
2025-09-22 16:33:10 +02:00
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");
}
2025-09-22 18:19:40 +02:00
TargetName = props["targetname"].AsStringName();
2025-09-26 10:53:20 +02:00
//QueueRedraw(); // Redraw should be automatic when enemy was changed and texture updated
2025-09-22 16:33:10 +02:00
//MarkerId = props["id"].AsInt32();
}
2025-09-26 10:53:20 +02:00
protected override void QueueRedraw()
{
if (!Engine.IsEditorHint()) return;
if (_autoSpawn)
{
_alpha = 1.0f;
// SetSpriteAlpha(1);
}
else
{
_alpha = 0.5f;
//SetSpriteAlpha(0.5f);
}
base.QueueRedraw();
}
2025-06-21 16:44:44 +02:00
public override void _Ready()
{
base._Ready();
2025-09-22 18:19:40 +02:00
if (!string.IsNullOrWhiteSpace(TargetName))
{
this.AddToGroup(TargetName);
}
2025-06-21 16:44:44 +02:00
if (AutoSpawn)
{
Spawn(false);
}
}
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
public EnemyProxy3D Spawn(bool deleteMarker)
{
2025-07-01 10:00:02 +02:00
if (Engine.IsEditorHint()) return null;
2025-06-21 16:44:44 +02:00
if (Enemy is null) return null;
if (_spawnedEnemy is not null) return _spawnedEnemy;
if (deleteMarker)
{
this.QueueFree();
}
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
var enemyScene = GD.Load<PackedScene>(Enemy.PrefabPath);
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
_spawnedEnemy = this.CreateSibling<EnemyProxy3D>(enemyScene);
_spawnedEnemy.Init(Enemy);
//Spawned = true;
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
_spawnedEnemy.Death += SpawnedEnemyOnDeath;
return _spawnedEnemy;
}
2025-07-01 10:00:02 +02:00
2025-06-21 16:44:44 +02:00
private void SpawnedEnemyOnDeath(EnemyProxy3D enemy)
{
_spawnedEnemy.Death -= SpawnedEnemyOnDeath;
_spawnedEnemy = null;
//Spawned = false;
}
2025-07-01 10:00:02 +02:00
public bool Activate(ActivationType activationType = ActivationType.Toggle)
{
if (Engine.IsEditorHint()) return false;
Spawn(false);
return _spawnedEnemy is not null;
}
public void Toggle()
{
Activate();
}
2025-08-12 14:37:48 +02:00
2025-07-02 10:34:14 +02:00
public override void _Process(double delta)
{
if (!Engine.IsEditorHint()) return;
2025-08-12 14:37:48 +02:00
#if !DISABLE_DD3D
2025-07-02 10:34:14 +02:00
DebugDraw3D.DrawBox(this.GlobalPosition - _boxSize / 2, Quaternion.Identity, _boxSize, Colors.Red);
2025-08-12 14:37:48 +02:00
#endif
2025-07-02 10:34:14 +02:00
//DebugDraw3D.DrawSphere(this.GlobalPosition, 0.1f, Colors.Green);
}
2025-06-21 16:44:44 +02:00
}