blob: 6050cc7c8351e095b0d4468fad3f9f44dac42d7f (
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
|
using Godot;
public partial class Weapon : Node2D
{
[Export] public PackedScene BulletScene { get; set; }
// Future-proofing: Different weapons can have different damage multipliers!
[Export] public float DamageMultiplier { get; set; } = 1.0f;
public override void _Process(double delta)
{
LookAt(GetGlobalMousePosition());
GD.Print("Weapon rotation is running!"); // Add this line
}
public void Fire(Vector2 targetPosition)
{
if (BulletScene == null) return;
// 1. The weapon itself looks up to find the player's stats component
// (Assuming the weapon is a child of the Player, GetParent() gets the Player)
var statsComponent = GetParent().GetNodeOrNull<StatsComponent>("StatsComponent");
float finalDamage = 10.0f; // Fallback default
if (statsComponent?.RuntimeStats != null)
{
// 2. The weapon reads the player's base damage and applies its own modifier
finalDamage = statsComponent.RuntimeStats.Damage * DamageMultiplier;
}
// 3. Instantiate and configure the bullet
Bullet bullet = (Bullet)BulletScene.Instantiate();
bullet.GlobalPosition = GlobalPosition; // Spawns right where the weapon node is!
bullet.Direction = Vector2.Right.Rotated(GlobalRotation);
bullet.Damage = finalDamage;
// 4. Add it to the world
GetTree().Root.AddChild(bullet);
}
}
|