Bullet data overhaul

This commit is contained in:
Marco 2025-02-11 19:00:01 +01:00
commit 76221ca7a6
9 changed files with 57 additions and 26 deletions

View file

@ -104,7 +104,7 @@ public partial class Barrel : Area2D, IDestructible
} }
public void Hit(float damage) public void Hit(float damage, DamageType damageType = DamageType.Neutral)
{ {
if (_isDestroyed) return; if (_isDestroyed) return;

View file

@ -20,6 +20,8 @@ public partial class SpiralPattern : AttackPattern
[Export] private float burstInterval = 0.5f; [Export] private float burstInterval = 0.5f;
[Export] private float spread = 360f; [Export] private float spread = 360f;
[Export] private BulletOwner owner = BulletOwner.Enemy; [Export] private BulletOwner owner = BulletOwner.Enemy;
[Export] private DamageType _damageType = DamageType.Neutral;
[Export] private float _bulletDamage = 1f;
[Export] private Resource _modifier; [Export] private Resource _modifier;
[Export] private Array<Resource> _timeModifiers; [Export] private Array<Resource> _timeModifiers;
@ -49,6 +51,8 @@ public partial class SpiralPattern : AttackPattern
Direction = Vector2.Right, Direction = Vector2.Right,
Speed = bulletSpeed, Speed = bulletSpeed,
Owner = owner, Owner = owner,
DamageType = _damageType,
Damage = _bulletDamage,
BulletCount = bulletCount, BulletCount = bulletCount,
Spread = spread, Spread = spread,
BulletScene = BulletScene, BulletScene = BulletScene,

View file

@ -8,24 +8,25 @@ public partial class Bullet : Area2D
{ {
[Export] [Export]
public float Speed = 1900f; public float Speed = 1900f;
[Export]
public float Damage = 1f;
[Export] public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
public BulletOwner Owner = BulletOwner.None;
public float Damage => _bulletInfo?.Damage ?? 1;
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
private Vector2 _direction = Vector2.Right; private Vector2 _direction = Vector2.Right;
private double _elapsedTime = 0f; private double _elapsedTime = 0f;
private BulletInfo _bulletInfo; private BulletInfo _bulletInfo;
public BulletInfo BulletInfo => _bulletInfo;
public void Initialize(BulletInfo bulletInfo) public void Initialize(BulletInfo bulletInfo)
{ {
_bulletInfo = bulletInfo; _bulletInfo = bulletInfo;
Position = bulletInfo.Position; //Position = bulletInfo.Position;
Owner = bulletInfo.Owner;
} }
private void ApplyTimeModifiers() private void ApplyTimeModifiers()
@ -141,7 +142,7 @@ public partial class Bullet : Area2D
{ {
//Debug.WriteLine("Collision with destroyable object area"); //Debug.WriteLine("Collision with destroyable object area");
destructible.Hit(Damage); destructible.Hit(Damage, DamageType);
QueueFree(); QueueFree();
} }
@ -154,3 +155,13 @@ public enum BulletOwner
Player, Player,
Enemy Enemy
} }
public enum DamageType
{
Neutral,
Ballistic,
Fire,
Ice,
Explosive,
Acid
}

View file

@ -13,7 +13,7 @@ public partial class BulletSpawner : Node2D
public override void _Ready() public override void _Ready()
{ {
_gameManager = GetNode<GameManager>("/root/GameScene"); _gameManager = this.GetGameManager();
} }
public void SpawnBullet(BulletInfo bulletInfo) public void SpawnBullet(BulletInfo bulletInfo)
@ -52,7 +52,6 @@ public partial class BulletSpawner : Node2D
//var bullet = BulletScene.Instantiate<Bullet>(); //var bullet = BulletScene.Instantiate<Bullet>();
bullet.Position = position; bullet.Position = position;
bullet.Owner = owner;
//bullet.Speed = speed; //bullet.Speed = speed;
float modifiedSpeed = modifier?.ModifySpeed(speed, i, count) ?? speed; float modifiedSpeed = modifier?.ModifySpeed(speed, i, count) ?? speed;
@ -75,13 +74,6 @@ public partial class BulletSpawner : Node2D
Vector2 direction = (target - position).Normalized(); Vector2 direction = (target - position).Normalized();
SpawnBullet(position, direction, speed, owner, burstCount, spread: spread, bulletScene: bulletScene, modifier: modifier); SpawnBullet(position, direction, speed, owner, burstCount, spread: spread, bulletScene: bulletScene, modifier: modifier);
} }
[Obsolete]
public void SpawnSpiralPattern(Vector2 position, float speed, BulletOwner owner, int bulletCount, float rotationSpeed, double time, float spread, PackedScene bulletScene = null)
{
float angleOffset = (float)(rotationSpeed * time);
SpawnBullet(position, Vector2.Right, speed, owner, bulletCount, angleOffset, spread, bulletScene: bulletScene);
}
} }
public class BulletInfo public class BulletInfo
@ -91,6 +83,8 @@ public class BulletInfo
public float Speed { get; set; } public float Speed { get; set; }
public float LifeTime { get; set; } public float LifeTime { get; set; }
public BulletOwner Owner { get; set; } public BulletOwner Owner { get; set; }
public DamageType DamageType { get; set; }
public float Damage { get; set; }
public int BulletCount { get; set; } public int BulletCount { get; set; }
public float RotationSpeed { get; set; } public float RotationSpeed { get; set; }
public float RotationOffset { get; set; } public float RotationOffset { get; set; }

View file

@ -80,9 +80,6 @@ public partial class Hud : CanvasLayer
public void UpdateInteractable(Interactable interactable) { public void UpdateInteractable(Interactable interactable) {
GD.Print($"Interactable ${interactable.Name} entered in HUD"); GD.Print($"Interactable ${interactable.Name} entered in HUD");
//_selector.Position = _selector.tolo //_selector.Position = _selector.tolo
} }

View file

@ -1,6 +1,6 @@
public interface IDestructible public interface IDestructible
{ {
public void Hit(float damage); public void Hit(float damage, DamageType type = DamageType.Neutral);
public bool IsDestroyed(); public bool IsDestroyed();
} }

View file

@ -426,7 +426,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
} }
} }
public void Hit(float damage) public void Hit(float damage, DamageType type = DamageType.Neutral)
{ {
if (!_canMove) return; if (!_canMove) return;
GD.Print($"Player damaged for {damage}"); GD.Print($"Player damaged for {damage}");
@ -446,10 +446,9 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private void _on_damage_hit_box_area_entered(Area2D area) private void _on_damage_hit_box_area_entered(Area2D area)
{ {
if (!_canMove) return; if (!_canMove) return;
if (area is Bullet bullet) if (area is Bullet bullet && bullet.BulletOwner != BulletOwner.Player)
{ {
GD.Print("Received damage manually"); this.Hit(bullet.Damage, bullet.DamageType);
this.Hit(bullet.Damage);
bullet.QueueFree(); bullet.QueueFree();
} }
} }

View file

@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components; using Cirno.Scripts.Components;
using Godot; using Godot;
using Godot.Collections; using Godot.Collections;
@ -33,11 +35,33 @@ public partial class WeaponResource : Resource
[Export] public string AmmoKey; [Export] public string AmmoKey;
[Export] public float BulletSpeed = 100f; [Export] public float BulletSpeed = 100f;
[Export] public float BulletDamage = 1; [Export] public float BulletDamage = 1;
[Export] public float LifeTime = 10f;
[Export] private float _rotationOffset = 0f; [Export] private float _rotationOffset = 0f;
[Export] private BulletOwner owner = BulletOwner.None; [Export] private BulletOwner owner = BulletOwner.None;
[Export] private DamageType _damageType = DamageType.Neutral;
[Export] private Resource _modifier; [Export] private Resource _modifier;
[Export] private Array<Resource> _timeModifiers; [Export] private Array<Resource> _timeModifiers;
#endregion #endregion
public BulletInfo MakeBullet(Vector2 position)
{
return new BulletInfo()
{
Position = position,
Direction = Vector2.Right,
Speed = BulletSpeed,
Owner = owner,
DamageType = _damageType,
Damage = BulletDamage,
BulletCount = BulletsPerShot,
Spread = SpreadAngle,
BulletScene = BulletScene,
RotationOffset = _rotationOffset,
Modifier = _modifier as IBulletModifier,
LifeTime = LifeTime,
TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
new List<TimeModifier>()
};
}
} }

View file

@ -106,6 +106,8 @@ public partial class Weapon : Node2D
GD.PrintErr("Bullet is null, not shooting"); GD.PrintErr("Bullet is null, not shooting");
return; return;
}; };
bullet.Initialize(WeaponData.MakeBullet(_muzzle.GlobalPosition));
//bullet.SetDirection(ShootDirection); //bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection); bullet.SetDirection(spreadDirection);