summaryrefslogtreecommitdiff
path: root/Player.cs
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-24 21:15:53 +0200
committeralex <[email protected]>2026-07-24 21:15:53 +0200
commit6b666c7f25121e61bf1c58ee65cb2cd0f254b887 (patch)
treede012eb81e45de014bf451ab1e8e741ef5abf3c5 /Player.cs
parent7e0c071a0d86563f9d450bf9073e06006abfa86e (diff)
downloadgodot-testing-6b666c7f25121e61bf1c58ee65cb2cd0f254b887.tar.xz
godot-testing-6b666c7f25121e61bf1c58ee65cb2cd0f254b887.zip
replaced all the sprites added the tilemap and improved the stats system so it can be split into multiple scripts
Diffstat (limited to 'Player.cs')
-rw-r--r--Player.cs109
1 files changed, 55 insertions, 54 deletions
diff --git a/Player.cs b/Player.cs
index 6ad370b..5d4bb2a 100644
--- a/Player.cs
+++ b/Player.cs
@@ -2,58 +2,59 @@ using Godot;
public partial class Player : CharacterBody2D
{
- [Export] public EntityStats BaseStats { get; set; }
- [Export] public PackedScene BulletScene { get; set; }
-
- // Create a reference for your Sprite
- private Sprite2D _sprite;
-
- public override void _Ready()
- {
- // Grab the Sprite2D node when the game starts
- _sprite = GetNode<Sprite2D>("Sprite2D");
- }
-
- public override void _PhysicsProcess(double delta)
- {
- Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
- Velocity = direction * BaseStats.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;
-
- if (Input.IsActionJustPressed("shoot"))
- {
- Shoot();
- }
- }
+ [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;
+
+ if (Input.IsActionJustPressed("shoot"))
+ {
+ Shoot();
+ }
+ }
private void Shoot()
- {
- // Failsafe in case you forgot to assign the scene in the editor
- if (BulletScene == null) return;
-
- // Create an instance of the bullet
- Bullet bullet = (Bullet)BulletScene.Instantiate();
-
- // Spawn it at the player's current position
- bullet.GlobalPosition = GlobalPosition;
-
- // Calculate the direction from the player to the mouse
- Vector2 mousePos = GetGlobalMousePosition();
- bullet.Direction = GlobalPosition.DirectionTo(mousePos);
-
- // Rotate the bullet to face the direction it's traveling
- bullet.Rotation = bullet.Direction.Angle();
-
- // Add the bullet to the current scene tree so it actually exists in the game
- GetTree().Root.AddChild(bullet);
- }
-} \ No newline at end of file
+ {
+ // Failsafe in case you forgot to assign the scene in the editor
+ if (BulletScene == null) return;
+
+ // Create an instance of the bullet
+ Bullet bullet = (Bullet)BulletScene.Instantiate();
+
+ // Spawn it at the player's current position
+ bullet.GlobalPosition = GlobalPosition;
+
+ // Calculate the direction from the player to the mouse
+ Vector2 mousePos = GetGlobalMousePosition();
+ bullet.Direction = GlobalPosition.DirectionTo(mousePos);
+
+ // Rotate the bullet to face the direction it's traveling
+ bullet.Rotation = bullet.Direction.Angle();
+
+ // Add the bullet to the current scene tree so it actually exists in the game
+ GetTree().Root.AddChild(bullet);
+ }
+}