Cheat Gun

This commit is contained in:
Marco 2025-02-15 17:51:06 +01:00
commit cb27f33a6d
21 changed files with 620 additions and 436 deletions

View file

@ -9,204 +9,223 @@ using Cirno.Scripts.Resources;
public partial class Bullet : Area2D
{
[Export]
public float Speed = 1900f;
public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
public float Damage => _bulletInfo?.Damage ?? 1;
[Export] public float Speed = 1900f;
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
protected Vector2 _direction = Vector2.Right;
private double _elapsedTime = 0f;
private BulletInfo _bulletInfo;
public float Damage => _bulletInfo?.Damage ?? 1;
public BulletInfo BulletInfo => _bulletInfo;
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
private List<ModifierWrapper> _modifiers = new();
private GameManager _gameManager;
protected Vector2 _direction = Vector2.Right;
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
{
_bulletInfo = bulletInfo;
private double _elapsedTime = 0f;
private BulletInfo _bulletInfo;
_gameManager = gameManager;
public BulletInfo BulletInfo => _bulletInfo;
// Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
private List<ModifierWrapper> _modifiers = new();
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList();
private GameManager _gameManager;
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
{
_bulletInfo = bulletInfo;
_gameManager = gameManager;
// Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
}
private void ApplyTimeModifiers(double delta)
{
foreach (var modifier in _modifiers)
{
if (_elapsedTime >= modifier.TimeModifier.TimeInSeconds)
{
if (!modifier.Applied)
{
modifier.Applied = true;
modifier.TimeModifier.Start(this);
}
modifier.TimeModifier.Update(this, delta);
// switch (modifier.ModifierType)
// {
// case TimeModifierType.SpeedChange:
// //_bulletInfo.Speed += modifier.Value;
// Speed = modifier.Value;
// break;
// case TimeModifierType.RotationChange:
// RotateBullet(modifier.Value);
// //Rotation += Mathf.DegToRad(modifier.Value);
// break;
// case TimeModifierType.FacePlayer:
// FacePlayer();
// break;
// }
// if (!modifier.Continuous)
// {
// modifier.Applied = true;
// }
}
}
}
public virtual void RotateBullet(float degrees)
{
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
SetRotation(Rotation + radians);
//Rotation = radians;
}
public void FacePlayer()
{
if (_gameManager.Player != null)
{
_direction = (_gameManager.Player.GlobalPosition - this.GlobalPosition).Normalized();
RotateBullet(0); // quick hack to rotate lasers
//LookAt(player.GlobalPosition);
}
}
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList();
//private void OnBodyEntered(Node body)
//{
// When a body is entered, invoke the event and pass the collided body
// BulletHit?.Invoke(body);
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
}
// Then remove the bullet
// QueueFree();
//}
private void ApplyTimeModifiers(double delta)
{
foreach (var modifier in _modifiers)
{
if (_elapsedTime >= modifier.TimeModifier.TimeInSeconds)
{
if (!modifier.Applied)
{
modifier.Applied = true;
modifier.TimeModifier.Start(this);
}
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
modifier.TimeModifier.Update(this, delta);
_direction = normalized;
SetRotation(Mathf.Atan2(normalized.Y,normalized.X) + Mathf.Pi / 2);
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}
// switch (modifier.ModifierType)
// {
// case TimeModifierType.SpeedChange:
// //_bulletInfo.Speed += modifier.Value;
// Speed = modifier.Value;
// break;
// case TimeModifierType.RotationChange:
// RotateBullet(modifier.Value);
// //Rotation += Mathf.DegToRad(modifier.Value);
// break;
// case TimeModifierType.FacePlayer:
// FacePlayer();
// break;
// }
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
_elapsedTime += delta;
}
// if (!modifier.Continuous)
// {
// modifier.Applied = true;
// }
}
}
}
public override void _PhysicsProcess(double delta)
{
if (_bulletInfo != null)
{
ApplyTimeModifiers(delta);
}
this.Position += ((float)(Speed * delta) * _direction);
}
public virtual void RotateBullet(float degrees)
{
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
SetRotation(Rotation + radians);
//Rotation = radians;
}
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
//Debug.WriteLine("Destroy bullet out of screen");
Destroy();
}
public virtual void RotateSpriteDegrees(float degrees)
{
SetRotationDegrees(RotationDegrees + degrees);
}
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
//Debug.WriteLine("Collision");
Destroy();
}
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
// {
// Debug.WriteLine("Collision with destroyable object body");
// QueueFree();
// }
}
public virtual void RotateSprite(float radians)
{
SetRotation(Rotation + radians);
}
private void _on_area_entered(Area2D area)
{
if (area.IsInGroup("Solid"))
{
Destroy();
return;
}
public void FacePlayer()
{
if (_gameManager.Player != null)
{
_direction = (_gameManager.Player.GlobalPosition - this.GlobalPosition).Normalized();
RotateBullet(0); // quick hack to rotate lasers
//LookAt(player.GlobalPosition);
}
}
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
{
//Debug.WriteLine("Collision with destroyable object area");
destructible.Hit(Damage, DamageType);
Destroy();
}
}
//private void OnBodyEntered(Node body)
//{
// When a body is entered, invoke the event and pass the collided body
// BulletHit?.Invoke(body);
public void Destroy()
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
var particle = this.CreateSibling<AutodeleteParticle>(_bulletInfo.DestructionParticlesScene);
// Then remove the bullet
// QueueFree();
//}
particle.Init();
}
QueueFree();
}
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
SetRotation(Mathf.Atan2(normalized.Y, normalized.X) + Mathf.Pi / 2);
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
_elapsedTime += delta;
}
public override void _PhysicsProcess(double delta)
{
if (_bulletInfo != null)
{
ApplyTimeModifiers(delta);
}
this.Position += ((float)(Speed * delta) * _direction);
}
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
//Debug.WriteLine("Destroy bullet out of screen");
Destroy();
}
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
//Debug.WriteLine("Collision");
Destroy();
}
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
// {
// Debug.WriteLine("Collision with destroyable object body");
// QueueFree();
// }
}
private void _on_area_entered(Area2D area)
{
if (area.IsInGroup("Solid"))
{
Destroy();
return;
}
if (area.IsInGroup("Destroyable") && area is IDestructible destructible &&
CanHit(BulletOwner, destructible.BulletGroup))
{
// hit
destructible.Hit(Damage, DamageType);
Destroy();
}
}
public bool CanHit(BulletOwner bulletOwner, BulletOwner targetGroup)
{
// If either is None, it always hits
if (bulletOwner == BulletOwner.None || targetGroup == BulletOwner.None)
{
return true;
}
// Otherwise, it hits only if they are different groups
return bulletOwner != targetGroup;
}
public void Destroy()
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
var particle = this.CreateSibling<AutodeleteParticle>(_bulletInfo.DestructionParticlesScene);
particle.Init();
}
QueueFree();
}
}
public enum BulletOwner
{
None,
Player,
Enemy
None,
Player,
Enemy
}
public enum DamageType
{
Neutral,
Ballistic,
Fire,
Ice,
Explosive,
Acid
Neutral,
Ballistic,
Fire,
Ice,
Explosive,
Acid
}

View file

@ -8,217 +8,243 @@ using Godot.Collections;
public partial class GameManager : Node2D
{
private Hud _hud;
private Hud _hud;
private PlayerMovement _player;
public GameState GameState { get; private set; }
public PlayerMovement Player => _player;
private PlayerMovement _player;
private Node2D _cameraTarget;
public GameState GameState { get; private set; }
public Vector2? PlayerPosition => _player?.GlobalPosition ?? null;
public PlayerMovement Player => _player;
[Export]
public PackedScene PlayerTemplate { get; set; }
[Export]
public Marker2D PlayerSpawnMarker { get; set; }
[Export] public PackedScene WeaponTemplate { get; private set; }
private Node2D _cameraTarget;
[Export] public Array<LootItem> StartingEquipment { get; private set; }
private InventoryManager _inventoryManager { get; set; }
//private AlarmManager _alarmManager { get; set; }
public Vector2? PlayerPosition => _player?.GlobalPosition ?? null;
//public InventoryManager Inventory => _inventoryManager;
//public AlarmManager AlarmManager => _alarmManager;
[Export] public PackedScene PlayerTemplate { get; set; }
private Node2D _bulletsContainer;
public Node2D BulletsContainer => _bulletsContainer;
[Export] public Marker2D PlayerSpawnMarker { get; set; }
[Signal]
public delegate void GameStateChangeEventHandler(GameState state);
[Export] public PackedScene WeaponTemplate { get; private set; }
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_hud = GetNodeOrNull<Hud>("HUD");
if (_hud == null) GD.Print("No HUD in scene.");
_cameraTarget = GetNodeOrNull<Node2D>("CameraTarget");
if (_cameraTarget == null) GD.Print("No camera target in scene.");
_inventoryManager = GetNodeOrNull<InventoryManager>("InventoryManager");
if (_inventoryManager == null) GD.Print("No inventory manager in scene.");
//_alarmManager = GetNode<AlarmManager>("AlarmManager");
SpawnBulletsContainer();
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
}
GameState = GameState.Playing;
[Export] public Array<LootItem> StartingEquipment { get; private set; }
_ = DelayPlayerSpawn();
}
private InventoryManager _inventoryManager { get; set; }
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("pause"))
{
TogglePause();
}
}
//private AlarmManager _alarmManager { get; set; }
private async Task DelayPlayerSpawn()
{
await Task.Delay(500);
if (PlayerSpawnMarker != null)
{
SpawnPlayer();
SpawnWeapons();
}
if (_player != null && _hud != null)
{
_player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
_player.ShieldChanged += (newShield, maxShield) => _hud.UpdateShield(newShield, maxShield);
//public InventoryManager Inventory => _inventoryManager;
_player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
}
}
//public AlarmManager AlarmManager => _alarmManager;
public void SpawnPlayer()
{
if (_player != null) return;
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
_player = PlayerTemplate.Instantiate<PlayerMovement>();
this.CallDeferred("add_child", _player);
_player.Transform = this.GlobalTransform;
_player.GlobalPosition = PlayerSpawnMarker.Position;
private Node2D _bulletsContainer;
public Node2D BulletsContainer => _bulletsContainer;
CameraTargetPlayer();
//
// if (_cameraTarget != null)
// {
// _cameraTarget.Reparent(_player, true);
// _cameraTarget.GlobalPosition = _player.Position;
// }
}
[Signal]
public delegate void GameStateChangeEventHandler(GameState state);
public void CameraTargetPlayer()
{
if (_player is null) return;
CameraTargetObject(_player);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_hud = GetNodeOrNull<Hud>("HUD");
if (_hud == null) GD.Print("No HUD in scene.");
public void CameraTargetObject(Node2D target)
{
if (_cameraTarget is null) return;
_cameraTarget.Reparent(target, true);
_cameraTarget.GlobalPosition = target.GlobalPosition;
}
private void SpawnWeapons()
{
if (!StartingEquipment.Any())
{
GD.Print("No items to spawn on Player");
return;
}
foreach (var startingItem in StartingEquipment)
{
switch (startingItem.Item)
{
case ItemTypes.Weapon:
if (WeaponTemplate == null)
{
GD.Print("Could not spawn weapon because template is null");
break;
}
var weapon = _player.CreateChild<Weapon>(WeaponTemplate);
weapon.WeaponData = startingItem.WeaponData;
_cameraTarget = GetNodeOrNull<Node2D>("CameraTarget");
if (_cameraTarget == null) GD.Print("No camera target in scene.");
_player.AddWeapon(weapon);
if (_player.EquippedWeapon == null)
{
_player.EquipWeapon(weapon);
}
//_player.EquippedWeapon ??= weapon;
break;
}
_inventoryManager = GetNodeOrNull<InventoryManager>("InventoryManager");
if (_inventoryManager == null) GD.Print("No inventory manager in scene.");
_inventoryManager.AddItem(startingItem);
}
}
//_alarmManager = GetNode<AlarmManager>("AlarmManager");
private void SpawnBulletsContainer()
{
_bulletsContainer = new Node2D();
_bulletsContainer.Name = "BulletsContainer";
AddChild(_bulletsContainer);
}
SpawnBulletsContainer();
public void TogglePause()
{
if (GameState == GameState.Paused)
{
Unpause();
}
else if (GameState == GameState.Playing)
{
Pause();
}
}
public void Pause()
{
if (GameState == GameState.Playing)
{
ChangeState(GameState.Paused);
}
}
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
}
public void Unpause()
{
if (GameState == GameState.Paused)
{
ChangeState(GameState.Playing);
}
}
public void ChangeState(GameState state)
{
if (state == GameState) return;
GameState = state;
EmitSignal(nameof(GameStateChange), (int)GameState);
GD.Print($"Game state changed to {state}");
}
GameState = GameState.Playing;
_ = DelayPlayerSpawn();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("pause"))
{
TogglePause();
}
}
private async Task DelayPlayerSpawn()
{
await Task.Delay(500);
if (PlayerSpawnMarker != null)
{
SpawnPlayer();
}
else
{
GD.Print("No player spawn marker in scene.");
return;
}
if (_player != null && _hud != null)
{
_player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
_player.ShieldChanged += (newShield, maxShield) => _hud.UpdateShield(newShield, maxShield);
_player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
}
else
{
GD.Print("No player and hud in scene");
return;
}
if (_inventoryManager is not null)
{
_inventoryManager.ItemAdded += (LootItem item, int amount) =>
{
if (item.Item == ItemTypes.Weapon)
{
SpawnPlayerWeapon(item);
}
};
}
SpawnWeapons();
}
public void SpawnPlayer()
{
if (_player != null) return;
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
_player = PlayerTemplate.Instantiate<PlayerMovement>();
this.CallDeferred("add_child", _player);
_player.Transform = this.GlobalTransform;
_player.GlobalPosition = PlayerSpawnMarker.Position;
CameraTargetPlayer();
//
// if (_cameraTarget != null)
// {
// _cameraTarget.Reparent(_player, true);
// _cameraTarget.GlobalPosition = _player.Position;
// }
}
public void CameraTargetPlayer()
{
if (_player is null) return;
CameraTargetObject(_player);
}
public void CameraTargetObject(Node2D target)
{
if (_cameraTarget is null) return;
_cameraTarget.Reparent(target, true);
_cameraTarget.GlobalPosition = target.GlobalPosition;
}
private void SpawnWeapons()
{
if (!StartingEquipment.Any())
{
GD.Print("No items to spawn on Player");
return;
}
foreach (var startingItem in StartingEquipment)
{
// Now automatically taken care of by the event
// switch (startingItem.Item)
// {
// case ItemTypes.Weapon:
// SpawnPlayerWeapon(startingItem);
//
// //_player.EquippedWeapon ??= weapon;
// break;
// }
_inventoryManager.AddItem(startingItem);
}
}
private void SpawnPlayerWeapon(LootItem startingItem)
{
if (WeaponTemplate == null)
{
GD.Print("Could not spawn weapon because template is null");
return;
}
var weapon = _player.CreateChild<Weapon>(WeaponTemplate);
weapon.WeaponData = startingItem.WeaponData;
_player.AddWeapon(weapon);
if (_player.EquippedWeapon == null)
{
_player.EquipWeapon(weapon);
}
}
private void SpawnBulletsContainer()
{
_bulletsContainer = new Node2D();
_bulletsContainer.Name = "BulletsContainer";
AddChild(_bulletsContainer);
}
public void TogglePause()
{
if (GameState == GameState.Paused)
{
Unpause();
}
else if (GameState == GameState.Playing)
{
Pause();
}
}
public void Pause()
{
if (GameState == GameState.Playing)
{
ChangeState(GameState.Paused);
}
}
public void Unpause()
{
if (GameState == GameState.Paused)
{
ChangeState(GameState.Playing);
}
}
public void ChangeState(GameState state)
{
if (state == GameState) return;
GameState = state;
EmitSignal(nameof(GameStateChange), (int)GameState);
GD.Print($"Game state changed to {state}");
}
}
public enum GameState
{
Menu,
Paused,
Playing,
Dialogue
}
Menu,
Paused,
Playing,
Dialogue
}

View file

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

View file

@ -59,8 +59,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private InventoryManager _inventoryManager;
[Export] public Sprite2D HitboxSprite { get; set; }
[Export] public Sprite2D HitboxSprite { get; set; }
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.Player;
private bool _isStrafing { get; set; }
private bool _canMove = true;
@ -179,6 +179,16 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
}
}*/
// public void ItemAdded(LootItem item, int amount)
// {
// switch (item.Item)
// {
// case ItemTypes.Weapon:
// AddWeapon(item.WeaponData);
// break;
// }
// }
public void AddWeapon(Weapon weapon)
{
EquippedWeapons.Add(weapon);
@ -443,7 +453,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private void _on_damage_hit_box_area_entered(Area2D area)
{
if (!_canMove) return;
if (area is Bullet bullet && bullet.BulletOwner != BulletOwner.Player)
if (area is Bullet bullet && bullet.BulletOwner != BulletGroup)
{
this.Hit(bullet.Damage, bullet.DamageType);
bullet.QueueFree();

View file

@ -0,0 +1,12 @@
using Godot;
namespace Cirno.Scripts.Resources.Modifiers;
[GlobalClass]
public partial class DelayedContinuousRotationModifier : TimeModifier
{
public override void Update(Bullet bullet, double delta)
{
bullet.RotateSpriteDegrees((float)(Value * delta));
}
}