Files
2026-02-01 14:06:04 +02:00

90 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
public enum TurretType
{
Kitsune,
Oni,
Tengu
}
public partial class TurretController : Node2D
{
public static TurretController Instance;
[Export] private Godot.Collections.Dictionary<TurretType, PackedScene> _typeToPrefab;
[Export] private Godot.Collections.Dictionary<TurretType, PackedScene> _typeToPlaceholder;
[Export] private Node2D _turretParent;
[Export] public int BasePrice;
public int TurretAmount => _allTurrets.Count;
public int TurretPrice => BasePrice + (int)Math.Round(30f * Math.Pow(TurretAmount, 1.5f));
[Signal]
public delegate void PlaceTurretEventHandler(TurretType t);
private bool _placingTurret = false;
private TurretPlaceholder _turretPlaceholder;
private TurretType _currentlyPlacingType;
private HashSet<Node2D> _allTurrets = new();
public override void _Ready()
{
Instance = this;
PlaceTurret += StartPlaceingTurret;
}
public override void _Input(InputEvent @event)
{
if (_turretPlaceholder == null) return;
if (!_placingTurret) return;
var mpos = GetGlobalMousePosition();
_turretPlaceholder.GlobalPosition = mpos;
if (@event is InputEventMouseButton mb)
{
switch (mb.ButtonIndex)
{
case MouseButton.Left:
if (!_turretPlaceholder.CanPlace) return;
if (!GameController.Instance.TryRemoveCurrency(TurretPrice)) return;
var newT = _typeToPrefab[_currentlyPlacingType].Instantiate() as Node2D;
_allTurrets.Add(newT);
_turretParent.AddChild(newT);
newT.GlobalPosition = mpos;
break;
case MouseButton.Right:
break;
default:
return;
}
StopPlacingTurret();
}
}
private void StartPlaceingTurret(TurretType t)
{
if (_turretPlaceholder != null)
{
_turretPlaceholder.GetParent()?.RemoveChild(_turretPlaceholder);
_turretPlaceholder.QueueFree();
}
_turretPlaceholder = _typeToPlaceholder[t].Instantiate() as TurretPlaceholder;
_turretParent.AddChild(_turretPlaceholder);
_currentlyPlacingType = t;
_placingTurret = true;
}
private void StopPlacingTurret()
{
_turretPlaceholder.GetParent()?.RemoveChild(_turretPlaceholder);
_turretPlaceholder.QueueFree();
_turretPlaceholder = null;
_placingTurret = false;
}
}