You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.2 KiB
C#
88 lines
2.2 KiB
C#
|
4 days ago
|
using Godot;
|
||
|
|
using RogueTank.Actors;
|
||
|
|
using RogueTank.Util;
|
||
|
|
|
||
|
|
namespace RogueTank.World;
|
||
|
|
|
||
|
|
public partial class Pickup : Area2D
|
||
|
|
{
|
||
|
|
public enum PickupType
|
||
|
|
{
|
||
|
|
Coin,
|
||
|
|
Xp,
|
||
|
|
Heal
|
||
|
|
}
|
||
|
|
|
||
|
|
[Export] public PickupType Type { get; set; } = PickupType.Xp;
|
||
|
|
[Export] public int Amount { get; set; } = 1;
|
||
|
|
|
||
|
|
private Sprite2D _sprite = null!;
|
||
|
|
private Node2D? _magnetTarget;
|
||
|
|
private float _magnetSpeed = 0f;
|
||
|
|
|
||
|
|
public override void _Ready()
|
||
|
|
{
|
||
|
|
BodyEntered += OnBodyEntered;
|
||
|
|
AreaEntered += OnAreaEntered;
|
||
|
|
|
||
|
|
_sprite = new Sprite2D
|
||
|
|
{
|
||
|
|
Centered = true,
|
||
|
|
TextureFilter = CanvasItem.TextureFilterEnum.Nearest
|
||
|
|
};
|
||
|
|
_sprite.Texture = Type switch
|
||
|
|
{
|
||
|
|
PickupType.Coin => PixelArt.MakePickupTexture(10, new Color(1f, 0.85f, 0.2f)),
|
||
|
|
PickupType.Xp => PixelArt.MakePickupTexture(10, new Color(0.2f, 0.85f, 1f)),
|
||
|
|
PickupType.Heal => PixelArt.MakePickupTexture(10, new Color(0.35f, 1f, 0.45f)),
|
||
|
|
_ => PixelArt.MakePickupTexture(10, Colors.White)
|
||
|
|
};
|
||
|
|
AddChild(_sprite);
|
||
|
|
|
||
|
|
var col = new CollisionShape2D { Shape = new CircleShape2D { Radius = 6f } };
|
||
|
|
AddChild(col);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void _PhysicsProcess(double delta)
|
||
|
|
{
|
||
|
|
if (_magnetTarget == null || !IsInstanceValid(_magnetTarget))
|
||
|
|
return;
|
||
|
|
|
||
|
|
_magnetSpeed = Mathf.Min(520f, _magnetSpeed + 1400f * (float)delta);
|
||
|
|
var dir = (_magnetTarget.GlobalPosition - GlobalPosition).Normalized();
|
||
|
|
GlobalPosition += dir * _magnetSpeed * (float)delta;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnAreaEntered(Area2D area)
|
||
|
|
{
|
||
|
|
// Magnet area: start homing when enter.
|
||
|
|
if (area.GetParent() is PlayerTank p && area == p.GetMagnetArea())
|
||
|
|
{
|
||
|
|
_magnetTarget = p;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnBodyEntered(Node body)
|
||
|
|
{
|
||
|
|
if (body is not PlayerTank p) return;
|
||
|
|
|
||
|
|
switch (Type)
|
||
|
|
{
|
||
|
|
case PickupType.Coin:
|
||
|
|
p.AddCoins(Amount);
|
||
|
|
break;
|
||
|
|
case PickupType.Xp:
|
||
|
|
p.AddXp(Amount);
|
||
|
|
break;
|
||
|
|
case PickupType.Heal:
|
||
|
|
p.Heal(Amount);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
QueueFree();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|