cirnogodot/Scripts/Bullet.cs

212 lines
4.5 KiB
C#
Raw 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;
using Cirno.Scripts.Resources;
2024-02-27 17:16:55 +01:00
2024-02-27 22:54:42 +01:00
public partial class Bullet : Area2D
2024-02-27 17:16:55 +01:00
{
2024-02-27 22:54:42 +01:00
[Export]
public float Speed = 1900f;
2025-01-31 16:06:15 +01:00
2025-02-11 19:00:01 +01:00
public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
public float Damage => _bulletInfo?.Damage ?? 1;
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
2024-06-09 00:34:36 +02:00
2025-02-14 10:59:32 +01:00
protected Vector2 _direction = Vector2.Right;
2025-02-09 11:48:30 +01:00
private double _elapsedTime = 0f;
private BulletInfo _bulletInfo;
2025-02-11 19:00:01 +01:00
public BulletInfo BulletInfo => _bulletInfo;
2025-02-13 18:25:55 +01:00
2025-02-14 16:18:33 +01:00
private List<ModifierWrapper> _modifiers = new();
2025-02-09 11:48:30 +01:00
2025-02-13 21:55:14 +01:00
private GameManager _gameManager;
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
2025-02-09 11:48:30 +01:00
{
_bulletInfo = bulletInfo;
2025-02-13 18:25:55 +01:00
2025-02-13 21:55:14 +01:00
_gameManager = gameManager;
2025-02-14 16:18:33 +01:00
// Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
2025-02-14 14:24:26 +01:00
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList();
2025-02-14 16:18:33 +01:00
2025-02-14 14:24:26 +01:00
// Ugly hack to make instances unique
2025-02-14 16:18:33 +01:00
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
2025-02-14 14:24:26 +01:00
2025-02-09 11:48:30 +01:00
}
2025-02-14 16:18:33 +01:00
private void ApplyTimeModifiers(double delta)
2025-02-09 11:48:30 +01:00
{
2025-02-13 18:25:55 +01:00
foreach (var modifier in _modifiers)
2025-02-09 11:48:30 +01:00
{
2025-02-14 16:18:33 +01:00
if (_elapsedTime >= modifier.TimeModifier.TimeInSeconds)
2025-02-09 11:48:30 +01:00
{
2025-02-14 16:18:33 +01:00
if (!modifier.Applied)
2025-02-14 13:27:30 +01:00
{
modifier.Applied = true;
2025-02-14 16:18:33 +01:00
modifier.TimeModifier.Start(this);
2025-02-14 13:27:30 +01:00
}
2025-02-14 16:18:33 +01:00
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;
// }
2025-02-09 11:48:30 +01:00
}
}
}
2025-02-14 16:18:33 +01:00
public virtual void RotateBullet(float degrees)
2025-02-09 11:48:30 +01:00
{
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
2025-02-13 18:25:55 +01:00
SetRotation(Rotation + radians);
//Rotation = radians;
2025-02-09 11:48:30 +01:00
}
2024-02-27 22:54:42 +01:00
2025-02-14 16:18:33 +01:00
public void FacePlayer()
2024-02-27 17:16:55 +01:00
{
2025-02-13 21:55:14 +01:00
if (_gameManager.Player != null)
2025-02-09 11:48:30 +01:00
{
2025-02-13 21:55:14 +01:00
_direction = (_gameManager.Player.GlobalPosition - this.GlobalPosition).Normalized();
2025-02-14 10:59:32 +01:00
RotateBullet(0); // quick hack to rotate lasers
2025-02-09 11:48:30 +01:00
//LookAt(player.GlobalPosition);
}
2024-02-27 17:16:55 +01:00
}
2025-02-09 11:48:30 +01:00
2024-05-15 15:48:48 +02:00
//private void OnBodyEntered(Node body)
//{
2024-05-26 11:40:35 +02:00
// When a body is entered, invoke the event and pass the collided body
2024-05-15 15:48:48 +02:00
// BulletHit?.Invoke(body);
2024-05-26 11:40:35 +02:00
// Then remove the bullet
2024-05-15 15:48:48 +02:00
// QueueFree();
//}
2024-05-01 11:48:04 +02:00
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
2025-02-13 18:25:55 +01:00
SetRotation(Mathf.Atan2(normalized.Y,normalized.X) + Mathf.Pi / 2);
2024-05-02 12:50:08 +02:00
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
2024-05-01 11:48:04 +02:00
}
2024-02-27 17:16:55 +01:00
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
2025-02-09 11:48:30 +01:00
_elapsedTime += delta;
2025-02-14 16:18:33 +01:00
2025-02-14 14:24:26 +01:00
}
public override void _PhysicsProcess(double delta)
{
2025-02-14 16:18:33 +01:00
if (_bulletInfo != null)
{
ApplyTimeModifiers(delta);
}
2024-05-26 11:40:35 +02:00
this.Position += ((float)(Speed * delta) * _direction);
2024-02-27 22:54:42 +01:00
}
2024-02-27 17:16:55 +01:00
2024-02-27 22:54:42 +01:00
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
2024-05-02 12:50:08 +02:00
//Debug.WriteLine("Destroy bullet out of screen");
2025-02-12 16:20:55 +01:00
Destroy();
2024-02-27 17:16:55 +01:00
}
2024-02-27 22:54:42 +01:00
2024-05-26 11:40:35 +02:00
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
2024-06-09 00:34:36 +02:00
//Debug.WriteLine("Collision");
2025-02-12 16:20:55 +01:00
Destroy();
2024-06-09 00:34:36 +02:00
}
2024-08-18 17:38:32 +02:00
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
// {
// Debug.WriteLine("Collision with destroyable object body");
// QueueFree();
// }
2024-05-26 11:40:35 +02:00
}
2024-06-09 00:34:36 +02:00
private void _on_area_entered(Area2D area)
{
2024-08-18 11:33:17 +02:00
if (area.IsInGroup("Solid"))
{
2025-02-12 16:20:55 +01:00
Destroy();
2024-08-18 11:33:17 +02:00
return;
}
2024-06-09 00:34:36 +02:00
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
{
//Debug.WriteLine("Collision with destroyable object area");
2025-02-11 19:00:01 +01:00
destructible.Hit(Damage, DamageType);
2024-05-26 11:40:35 +02:00
2025-02-12 16:20:55 +01:00
Destroy();
2024-06-09 00:34:36 +02:00
}
}
2025-02-12 16:20:55 +01:00
public void Destroy()
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
var particle = this.CreateSibling<AutodeleteParticle>(_bulletInfo.DestructionParticlesScene);
particle.Init();
}
QueueFree();
}
2024-06-09 00:34:36 +02:00
}
2025-01-31 16:06:15 +01:00
public enum BulletOwner
{
None,
Player,
Enemy
}
2025-02-11 19:00:01 +01:00
public enum DamageType
{
Neutral,
Ballistic,
Fire,
Ice,
Explosive,
Acid
}