Files
peggle-roguelike/Tower.cs
T

82 lines
2.3 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
public partial class Tower : Sprite2D
{
public bool _hovered, _aiming;
public Vector2 _aimOffset;
public Marker2D _offset, _attackSpawn;
public Area2D _area;
public Commander _commander;
public override void _Ready()
{
base._Ready();
_area = GetNode<Area2D>("Area");
_offset = GetNode<Marker2D>("Offset");
_attackSpawn = GetNode<Marker2D>("AttackSpawn");
}
public override void _Process(double delta)
{
base._Process(delta);
if (_commander?._actions > 0)
{
if (_aiming)
{
if (Input.IsActionJustPressed("rightClick"))
{
_commander.UnloadAttack();
_aiming = false;
}
else if (Input.IsActionJustPressed("leftClick"))
{
_aimOffset = GetGlobalMousePosition() - GlobalPosition;
_commander.ShootCurrentAttack(_aimOffset);
_aiming = false;
}
}
if (Input.IsActionJustPressed("leftClick"))
{
if (_hovered)
{
_aiming = true;
_commander.LoadAttack(_attackSpawn.Position);
}
}
}
}
public List<Vector2> PredictPath()
{
Vector2 velocity = _aimOffset;
float timeBetweenSteps = 0.05f;
Vector2 startPosition = _attackSpawn.Position;
float gravity = -(float)ProjectSettings.GetSetting("physics/2d/default_gravity", 980.0f);
float drag = (float)ProjectSettings.GetSetting("physics/2d/default_linear_damp", 0.0f);
List<Vector2> points = [startPosition];
for (int i = 0; i < 151; i++)
{
velocity.Y += gravity * timeBetweenSteps;
// GD.Print(velocity);
velocity *= (float)Math.Clamp(1.0 - drag * timeBetweenSteps, 0, 1);
points.Add(points[^1] + velocity);
}
return points;
}
// public Dictionary<> Ra
public void OnMouseEntered(){
_hovered = true;
}
public void OnMouseExited(){
_hovered = true;
}
}