cirnogodot/Scripts/Bullet.cs

340 lines
9.3 KiB
C#
Raw Permalink Normal View History

2024-02-27 17:16:55 +01:00
using Godot;
using System;
2025-02-13 18:25:55 +01:00
using System.Collections.Generic;
2024-02-27 17:16:55 +01:00
using System.Diagnostics;
2025-02-13 18:25:55 +01:00
using System.Linq;
2025-02-12 16:20:55 +01:00
using Cirno.Scripts;
2025-02-09 11:48:30 +01:00
using Cirno.Scripts.Components;
2025-06-08 16:33:38 +02:00
using Cirno.Scripts.Controllers;
2025-02-09 11:48:30 +01:00
using Cirno.Scripts.Resources;
2025-06-17 11:57:59 +02:00
using Cirno.Scripts.Weapons;
2024-02-27 17:16:55 +01:00
2025-06-17 11:57:59 +02:00
public partial class Bullet : Area2D, IBullet
2024-02-27 17:16:55 +01:00
{
2025-06-17 11:57:59 +02:00
[Export] public float Speed { get; set; } = 1900f;
2025-02-15 17:51:06 +01:00
public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
public float Damage => _bulletInfo?.Damage ?? 1;
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
protected Vector2 _direction = Vector2.Right;
private double _elapsedTime = 0f;
private BulletInfo _bulletInfo;
public BulletInfo BulletInfo => _bulletInfo;
private List<ModifierWrapper> _modifiers = new();
private GameManager _gameManager;
2025-04-01 15:46:25 +02:00
public bool IsGrazed { get; set; } = false;
2025-04-26 11:24:20 +02:00
public bool IsFrozen { get; private set; } = false;
2025-06-08 16:33:38 +02:00
public bool Enabled { get; private set; } = false;
2025-06-08 16:50:38 +02:00
[Signal]
public delegate void OnDestroyEventHandler();
2025-02-28 11:17:28 +01:00
2025-04-01 16:13:54 +02:00
private AudioStreamPlayer2D _grazeSound;
private GpuParticles2D _grazeParticles;
2025-06-08 16:50:38 +02:00
2025-06-08 16:33:38 +02:00
private CollisionShape2D _collisionShape2D;
2025-06-08 16:50:38 +02:00
2025-04-01 16:13:54 +02:00
public override void _Ready()
{
_grazeSound = GetNodeOrNull<AudioStreamPlayer2D>("AudioStreamPlayer2D");
_grazeParticles = GetNodeOrNull<GpuParticles2D>("GrazeParticles");
2025-06-08 16:33:38 +02:00
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
2025-04-01 16:13:54 +02:00
}
2025-06-08 16:50:38 +02:00
2025-06-19 17:55:23 +02:00
public void Initialize(BulletInfo bulletInfo)
2025-02-15 17:51:06 +01:00
{
_bulletInfo = bulletInfo;
2025-06-19 17:55:23 +02:00
_gameManager = GameManager.Instance;
2025-02-15 17:51:06 +01:00
2025-02-20 12:17:21 +01:00
_elapsedTime = 0f;
2025-02-27 18:48:13 +01:00
this.Speed = bulletInfo.Speed;
2025-06-08 16:50:38 +02:00
2025-02-15 17:51:06 +01:00
// Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList();
2025-02-27 18:48:13 +01:00
2025-02-15 17:51:06 +01:00
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
}
2025-06-08 16:33:38 +02:00
/// <summary>
/// Enables the bullet, shows the sprite and activates collisions
/// </summary>
public void Enable()
{
Enabled = true;
Show();
if (this._collisionShape2D is null)
{
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
2025-06-08 16:50:38 +02:00
2025-06-08 16:33:38 +02:00
_collisionShape2D.SetDeferred(CollisionShape2D.PropertyName.Disabled, false);
}
/// <summary>
/// Disables the bullet, hides the sprite and disables collisions
/// </summary>
public void Disable(bool hideSprite = true)
{
Enabled = false;
2025-06-08 16:50:38 +02:00
if (hideSprite && !BulletInfo.Attributes.HasFlag(BulletFlags.PersistSprite))
2025-06-08 16:33:38 +02:00
{
Hide();
}
2025-06-08 16:50:38 +02:00
2025-06-08 16:33:38 +02:00
if (this._collisionShape2D is null)
{
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
2025-06-08 16:50:38 +02:00
2025-06-08 16:33:38 +02:00
_collisionShape2D.SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
}
2025-06-08 16:50:38 +02:00
2025-04-01 16:13:54 +02:00
public void Graze()
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-04-01 16:13:54 +02:00
_grazeSound?.Play();
if (_grazeParticles is not null)
{
_grazeParticles.Emitting = true;
}
2025-06-08 16:50:38 +02:00
2025-04-01 16:13:54 +02:00
IsGrazed = true;
}
2025-02-15 17:51:06 +01:00
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);
2025-03-04 14:07:51 +01:00
modifier.Elapsed = 0;
}
else
{
modifier.Elapsed += delta;
2025-02-15 17:51:06 +01:00
}
2025-03-04 14:07:51 +01:00
modifier.TimeModifier.Update(this, delta, modifier.Elapsed);
2025-02-15 17:51:06 +01:00
// 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)
{
2025-03-14 00:04:11 +01:00
//SetRotationDegrees(RotationDegrees + degrees);
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
2025-06-08 16:50:38 +02:00
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
2025-03-14 00:04:11 +01:00
SetRotation(Rotation + radians);
2025-02-15 17:51:06 +01:00
}
public virtual void RotateSpriteDegrees(float degrees)
{
2025-06-08 16:50:38 +02:00
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
2025-02-15 17:51:06 +01:00
SetRotationDegrees(RotationDegrees + degrees);
}
public virtual void RotateSprite(float radians)
{
2025-06-08 16:50:38 +02:00
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
2025-02-15 17:51:06 +01:00
SetRotation(Rotation + radians);
}
public void FacePlayer()
{
if (_gameManager.Player != null)
{
2025-06-10 16:33:43 +02:00
_direction = (_gameManager.PlayerPosition.Value - this.GlobalPosition).Normalized();
2025-02-15 17:51:06 +01:00
RotateBullet(0); // quick hack to rotate lasers
//LookAt(player.GlobalPosition);
}
}
//private void OnBodyEntered(Node body)
//{
// When a body is entered, invoke the event and pass the collided body
// BulletHit?.Invoke(body);
// Then remove the bullet
// QueueFree();
//}
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
2025-06-08 16:50:38 +02:00
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
2025-02-15 17:51:06 +01:00
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)
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-02-15 17:51:06 +01:00
_elapsedTime += delta;
2025-02-20 12:17:21 +01:00
if (_elapsedTime >= _bulletInfo.LifeTime)
{
Destroy();
}
2025-02-15 17:51:06 +01:00
}
public override void _PhysicsProcess(double delta)
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-02-15 17:51:06 +01:00
if (_bulletInfo != null)
{
ApplyTimeModifiers(delta);
}
2025-06-08 16:50:38 +02:00
if (BulletInfo.Attributes.HasFlag(BulletFlags.Controllable))
2025-02-27 18:48:13 +01:00
{
ControlBullet(delta);
}
2025-02-15 17:51:06 +01:00
this.Position += ((float)(Speed * delta) * _direction);
}
2025-02-27 18:48:13 +01:00
private void ControlBullet(double delta)
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-02-27 18:48:13 +01:00
var axis = Input.GetAxis("left", "right");
if (axis != 0)
{
float rotationSpeed = 180f; // Degrees per second
RotateBullet(axis * rotationSpeed * (float)delta);
}
}
2025-02-15 17:51:06 +01:00
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-06-08 16:50:38 +02:00
if (!BulletInfo.Attributes.HasFlag(BulletFlags.DieOutOfScreen)) return;
2025-02-15 17:51:06 +01:00
//Debug.WriteLine("Destroy bullet out of screen");
Destroy();
}
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
//Debug.WriteLine("Collision");
2025-02-20 12:17:21 +01:00
RequestCollisionDestruction();
2025-02-15 17:51:06 +01:00
}
//// 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"))
{
2025-02-20 12:17:21 +01:00
RequestCollisionDestruction();
2025-02-15 17:51:06 +01:00
return;
}
if (area.IsInGroup("Destroyable") && area is IDestructible destructible &&
CanHit(BulletOwner, destructible.BulletGroup))
{
// hit
destructible.Hit(Damage, DamageType);
2025-02-20 12:17:21 +01:00
RequestCollisionDestruction();
2025-02-15 17:51:06 +01:00
}
}
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;
}
2025-02-20 12:17:21 +01:00
public void RequestCollisionDestruction()
{
2025-06-08 16:33:38 +02:00
if (!Enabled) return;
2025-02-20 12:17:21 +01:00
if (_bulletInfo.DestroyOnCollision)
{
Destroy();
}
}
2025-02-15 17:51:06 +01:00
2025-02-20 12:17:21 +01:00
private void Destroy()
2025-02-15 17:51:06 +01:00
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
2025-02-28 11:17:28 +01:00
this.CreateSibling<Node2D>(_bulletInfo.DestructionParticlesScene);
2025-02-15 17:51:06 +01:00
2025-02-28 11:17:28 +01:00
//particle.Init();
2025-02-15 17:51:06 +01:00
}
2025-06-08 16:50:38 +02:00
2025-02-28 11:17:28 +01:00
EmitSignal(SignalName.OnDestroy);
2025-06-08 16:33:38 +02:00
//QueueFree();
PoolingManager.Instance.DisableBullet(this);
2025-02-15 17:51:06 +01:00
}
2025-04-26 11:24:20 +02:00
public void Freeze()
{
IsFrozen = true;
EmitSignal(SignalName.OnDestroy);
2025-06-08 16:33:38 +02:00
//QueueFree();
PoolingManager.Instance.DisableBullet(this);
2025-04-26 11:24:20 +02:00
}
2025-02-11 19:00:01 +01:00
}