Moved weapons to their own scene

This commit is contained in:
Marco 2024-11-15 16:32:26 +01:00
commit 34e0603170
6 changed files with 159 additions and 62 deletions

View file

@ -8,34 +8,19 @@ public partial class Enemy : Area2D, IDestructible
private InteractionController _cachedPlayer;
private EnemyState _currentState = EnemyState.Idle;
[Export]
public PackedScene BulletScene { get; set; }
[Export] public float Health = 4f;
[Export] public double RateOfFire = 0.4f;
[Export] public double ReloadTime = 1.0f;
[Export] public int BulletCount = 4;
[Export] public float BulletSpeed = 100f;
[Export] public Weapon EquippedWeapon;
private float _currentHealth = 0f;
private bool _isDestroyed = false;
private Timer _cooldownTimer;
[DebugGUIPrint]
private int _ammo = 0;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_ammo = BulletCount;
_cooldownTimer = GetNode<Timer>("./ShootTimer");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
@ -72,7 +57,7 @@ public partial class Enemy : Area2D, IDestructible
return;
}
if (_cooldownTimer.IsStopped() && IsPlayerInSight())
if (IsPlayerInSight())
{
Shoot();
}
@ -80,26 +65,32 @@ public partial class Enemy : Area2D, IDestructible
private void Shoot()
{
// SHOOT
var bullet = this.CreateChild<Bullet>(BulletScene);
// var bullet = BulletScene.Instantiate<Bullet>();
// Owner.AddChild(bullet);
// bullet.Transform = this.GlobalTransform;
// bullet.Position = this.Position;
bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
bullet.Speed = BulletSpeed;
if (EquippedWeapon == null) return;
_ammo -= 1;
if (_ammo <= 0)
{
_ammo = BulletCount;
_cooldownTimer.Start(ReloadTime);
}
else
{
_cooldownTimer.Start(RateOfFire);
}
EquippedWeapon.ShootDirection = (_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized();
EquippedWeapon.Shoot();
// // SHOOT
// var bullet = this.CreateChild<Bullet>(BulletScene);
// // var bullet = BulletScene.Instantiate<Bullet>();
// // Owner.AddChild(bullet);
// // bullet.Transform = this.GlobalTransform;
// // bullet.Position = this.Position;
// bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
// bullet.Speed = BulletSpeed;
//
// _ammo -= 1;
//
// if (_ammo <= 0)
// {
// _ammo = BulletCount;
// _cooldownTimer.Start(ReloadTime);
// }
// else
// {
// _cooldownTimer.Start(RateOfFire);
// }
}
private bool IsPlayerInSight()

View file

@ -11,9 +11,6 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
[Export]
public float CrosshairDistance { get; set; } = 10f;
[Export]
public PackedScene BulletScene { get; set; }
[Export]
public PackedScene SelectorScene { get; set; }
@ -35,17 +32,13 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private Sprite2D _crosshair;
private Timer _cooldownTimer;
[Export] public float Health = 4f;
[Export] public Weapon EquippedWeapon;
[DebugGUIPrint]
private float _currentHealth = 0f;
[Export] public double RateOfFire = 0.4f;
[Export] public float BulletSpeed = 300f;
private bool _isDestroyed = false;
public override void _Ready()
@ -54,7 +47,6 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
_movementDirection = Vector2.Zero;
_facingDirection = Vector2.Zero;
@ -100,13 +92,18 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private void HandleShoot()
{
if (!Input.IsActionJustPressed("shoot")) return;
//Debug.WriteLine("Shoot");
var bullet = BulletScene.Instantiate<Bullet>();
Owner.AddChild(bullet);
bullet.Transform = Muzzle.GlobalTransform;
bullet.Position = this.Position;
bullet.SetDirection(this._facingDirection);
if (EquippedWeapon == null) return;
if (!Input.IsActionPressed("shoot")) return;
EquippedWeapon.ShootDirection = this._facingDirection;
EquippedWeapon.Shoot();
// //Debug.WriteLine("Shoot");
// var bullet = BulletScene.Instantiate<Bullet>();
// Owner.AddChild(bullet);
// bullet.Transform = Muzzle.GlobalTransform;
// bullet.Position = this.Position;
// bullet.SetDirection(this._facingDirection);
}
private void SetAnimation()

84
Scripts/Weapon.cs Normal file
View file

@ -0,0 +1,84 @@
using Godot;
using System;
using Cirno.Scripts;
public partial class Weapon : Node2D
{
[Export]
public PackedScene BulletScene { get; set; }
[Export]
public Marker2D Muzzle { get; set; }
[Export] public double RateOfFire = 0.4f;
[Export] public int BulletCapacity = 20;
[Export] public double ReloadTime = 1.0f;
[Export] public float BulletSpeed = 100f;
[Export] public bool AutoReload = true;
[Export] public bool InfiniteAmmo = true;
public int Ammo { get; set; } = 0;
public int LoadedAmmo { get; private set; }
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
private Timer _cooldownTimer;
private Marker2D _muzzle;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_muzzle = GetNode<Marker2D>("./Muzzle");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
}
public void Reload()
{
_cooldownTimer.Start(ReloadTime);
if (InfiniteAmmo)
{
LoadedAmmo = BulletCapacity;
}
else
{
// TODO: Calculate subtraction, etc
LoadedAmmo = BulletCapacity;
}
}
public void Shoot()
{
// Waiting on reload or Rate of Fire cooldown?
if (!_cooldownTimer.IsStopped())
{
return;
}
// Out of ammo?
if (LoadedAmmo <= 0)
{
if (AutoReload)
{
Reload();
}
return;
}
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
var bullet = this.CreateChild<Bullet>(BulletScene);
bullet.SetDirection(ShootDirection);
bullet.Speed = BulletSpeed;
LoadedAmmo -= 1;
_cooldownTimer.Start(RateOfFire);
}
}