140 lines
2.3 KiB
C#
140 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using NodeWar.scripts;
|
|
|
|
namespace NodeWar;
|
|
|
|
public partial class Nodule : Area2D
|
|
{
|
|
private const float GrowthFactor = 0.01f;
|
|
|
|
[Export]
|
|
public float Radius { get; set; } = 120;
|
|
|
|
[Export]
|
|
public float MaxRadius { get; set; } = 360;
|
|
|
|
[Export]
|
|
public CollisionShape2D Collision { get; set; }
|
|
|
|
[Export]
|
|
public bool BuildMode { get; set; } = false;
|
|
|
|
[Export]
|
|
public float Age { get; set; } = 0;
|
|
|
|
[Export]
|
|
public PackedScene RootScene { get; set; }
|
|
|
|
public List<Nodule> ConnectedNodules { get; set; } = [];
|
|
|
|
[Signal]
|
|
public delegate void NoduleBuiltEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void BuildingCanceledEventHandler();
|
|
|
|
public override void _Ready()
|
|
{
|
|
Radius = Age * MaxRadius;
|
|
|
|
AreaEntered += OnAreaEntered;
|
|
AreaExited += OnAreaExited;
|
|
|
|
QueueRedraw();
|
|
}
|
|
|
|
private void OnAreaEntered(Area2D area)
|
|
{
|
|
if (area is Nodule nodule)
|
|
{
|
|
if (nodule.BuildMode)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ConnectedNodules.Add(nodule);
|
|
}
|
|
}
|
|
|
|
private void OnAreaExited(Area2D area)
|
|
{
|
|
if (area is Nodule nodule)
|
|
{
|
|
if (nodule.BuildMode)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ConnectedNodules.Remove(nodule);
|
|
}
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (BuildMode)
|
|
{
|
|
GlobalPosition = GetGlobalMousePosition();
|
|
|
|
if (Input.IsMouseButtonPressed(MouseButton.Left))
|
|
{
|
|
EmitSignalNoduleBuilt();
|
|
BuildMode = false;
|
|
|
|
MouseEntered += OnMouseEntered;
|
|
}
|
|
|
|
if (Input.IsMouseButtonPressed(MouseButton.Right))
|
|
{
|
|
EmitSignalBuildingCanceled();
|
|
BuildMode = false;
|
|
QueueFree();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Age += GrowthFactor * delta.ToFloat();
|
|
Age = MathF.Min(Age, 1);
|
|
|
|
Radius = Age * MaxRadius;
|
|
QueueRedraw();
|
|
}
|
|
|
|
// TODO: replace with line2d?
|
|
QueueRedraw();
|
|
}
|
|
|
|
private void OnMouseEntered()
|
|
{
|
|
GD.Print("Mouse entered");
|
|
}
|
|
|
|
public override void _Draw()
|
|
{
|
|
foreach (var nodule in ConnectedNodules)
|
|
{
|
|
if (BuildMode)
|
|
{
|
|
DrawDashedLine(
|
|
Vector2.Zero,
|
|
nodule.GlobalPosition - GlobalPosition,
|
|
Colors.White,
|
|
2f,
|
|
dash: 4f,
|
|
antialiased: true
|
|
);
|
|
}
|
|
}
|
|
|
|
if (BuildMode)
|
|
{
|
|
DrawCircle(Vector2.Zero, Radius, new Color(Colors.DarkOliveGreen, 0.2f));
|
|
}
|
|
else
|
|
{
|
|
DrawCircle(Vector2.Zero, Radius, new Color(Colors.WhiteSmoke, 0.4f));
|
|
}
|
|
}
|
|
}
|