blob: 11e3aa2b8da6b917629f24c68d9e1bf7062e8bb4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
using Godot;
using testing.interfaces;
public partial class Player : CharacterBody2D, IDamageable
{
[Export] public EntityStats BaseStats { get; set; }
[Export] public PackedScene BulletScene { get; set; }
// Create a reference for your Sprite
private Sprite2D _sprite;
private StatsComponent _stats;
public override void _Ready()
{
_stats = GetNode<StatsComponent>("StatsComponent");
_stats.Initialize(BaseStats);
_sprite = GetNode<Sprite2D>("Sprite2D");
}
public override void _PhysicsProcess(double delta)
{
Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
Velocity = direction * _stats.RuntimeStats.MoveSpeed;
MoveAndSlide();
}
public override void _Process(double delta)
{
// Get the mouse position
Vector2 mousePos = GetGlobalMousePosition();
// If the mouse is to the left of the player, flip the sprite.
// If it's to the right, keep it un-flipped.
_sprite.FlipH = mousePos.X < GlobalPosition.X;
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed("shoot"))
{
// Find the weapon node and tell it to fire at the mouse
var weapon = GetNodeOrNull<Weapon>("Weapon");
if (weapon != null)
{
weapon.Fire(GetGlobalMousePosition());
}
}
}
public void TakeDamage(float amount)
{
// For right now, just subtract it from the runtime stats we set up earlier!
if (_stats?.RuntimeStats != null)
{
_stats.RuntimeStats.MaxHealth -= amount;
GD.Print($"Ouch! Player took {amount} damage. Health remaining: {_stats.RuntimeStats.MaxHealth}");
if (_stats.RuntimeStats.MaxHealth <= 0)
{
GD.Print("Player Died!");
QueueFree(); // Deletes the player
}
}
}
}
|