2025-05-13 19:22:01 +08:00

178 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Bullets/PencilEraser.cs")]
public partial class PencilEraser : Node2D
{
[Export(PropertyHint.None, "")]
private NodePath pencilPath;
[Export(PropertyHint.None, "")]
private NodePath eraserPath;
[Export(PropertyHint.None, "")]
private NodePath trailPath;
[Export(PropertyHint.None, "")]
private NodePath colliderPath;
[Export(PropertyHint.None, "")]
private NodePath trailColliderPath;
[Export(PropertyHint.None, "")]
private NodePath boxPath;
[Export(PropertyHint.None, "")]
private NodePath eraserSprite;
private Node2D pencil;
private Node2D eraser;
private Node2D eraserVisual;
private Line2D trail;
private CollisionShape2D trailCollider;
private Node2D collider;
private Control box;
private IBullet pencilB;
private IBullet eraserB;
[Export(PropertyHint.None, "")]
private float startDelay = 20f;
[Export(PropertyHint.None, "")]
private float pencilSpeed = 0.5f;
[Export(PropertyHint.None, "")]
private float eraserSpeed = 0.35f;
private const float snap = (float)Math.PI / 4f;
private float startwait;
private float cd;
private float grazeReset;
private Vector2 dir = Vector2.Left;
private List<CollisionShape2D> colliders = new List<CollisionShape2D>();
private List<SegmentShape2D> shapes = new List<SegmentShape2D>();
private static readonly Vector2 snapVec = Vector2.One.Rotated((float)Math.PI / 4f);
public override void _EnterTree()
{
pencil = GetNode<Node2D>(pencilPath);
eraser = GetNode<Node2D>(eraserPath);
trail = GetNode<Line2D>(trailPath);
collider = GetNode<Node2D>(colliderPath);
trailCollider = GetNode<CollisionShape2D>(trailColliderPath);
box = GetNode<Control>(boxPath);
eraserVisual = GetNode<Node2D>(eraserSprite);
box.Size = BattleDR.current.activeBoard.boardSize;
box.Position = -box.Size / 2f;
trail.Position = box.Position * -1f;
eraserB = eraser as IBullet;
pencilB = pencil as IBullet;
base.Modulate = Main.colorClear;
Main.SetActive(trailCollider, state: false);
startwait = startDelay;
Add();
}
private void Add()
{
colliders.Insert(0, (CollisionShape2D)trailCollider.Duplicate());
colliders[0].Shape = new SegmentShape2D
{
A = pencil.Position,
B = ((colliders.Count == 0) ? eraser.Position : pencil.Position)
};
shapes.Insert(0, (SegmentShape2D)colliders[0].Shape);
collider.AddChild(colliders[0], forceReadableName: false, InternalMode.Disabled);
Main.SetActive(colliders[0], state: true);
cd = 15f;
}
public override void _Process(double delta)
{
base.Modulate = base.Modulate.Lerp(Main.colorWhite, Main.deltaTime * 0.1f);
if (grazeReset < 15f)
{
grazeReset += Main.deltaTime;
}
else
{
grazeReset = 0f;
pencilB.grazed = false;
eraserB.grazed = false;
}
if (startDelay > 0f)
{
startDelay -= Main.deltaTime;
return;
}
Vector2 to = ToLocal(DWMenu.instance.soulBody.GlobalPosition);
Vector2 vector = pencil.Position.DirectionTo(to).Snapped(snapVec).Normalized();
if (dir != vector && cd <= 0f)
{
Add();
dir = vector;
}
else if (cd > 0f)
{
cd -= Main.deltaTime;
}
if (pencil.Position.DistanceTo(to) > 2f)
{
pencil.RotationDegrees = Common.SinOverTime(0.5f) * 2f;
pencil.Position += dir * pencilSpeed * Main.deltaTime;
shapes[0].A = pencil.Position;
}
Vector2 position = eraser.Position;
List<SegmentShape2D> list = shapes;
if (position.DistanceTo(list[list.Count - 1].A) > 1f)
{
Node2D node2D = eraser;
Vector2 position2 = node2D.Position;
position = eraser.Position;
List<SegmentShape2D> list2 = shapes;
node2D.Position = position2 + position.DirectionTo(list2[list2.Count - 1].A) * eraserSpeed * Main.deltaTime;
eraserVisual.RotationDegrees = Common.SinOverTime() * 2f;
List<SegmentShape2D> list3 = shapes;
list3[list3.Count - 1].B = eraser.Position;
}
else
{
if (shapes.Count > 1)
{
List<CollisionShape2D> list4 = colliders;
list4[list4.Count - 1].QueueFree();
colliders.RemoveAt(colliders.Count - 1);
shapes.RemoveAt(shapes.Count - 1);
}
eraserVisual.RotationDegrees = 0f;
}
List<Vector2> list5 = new List<Vector2>();
for (int i = 0; i < shapes.Count; i++)
{
list5.Add(shapes[i].A);
list5.Add(shapes[i].B);
}
trail.Points = list5.ToArray();
}
}