2024-02-27 17:16:55 +01:00
|
|
|
using Godot;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
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;
|
|
|
|
|
|
2024-06-09 00:34:36 +02:00
|
|
|
[Export]
|
|
|
|
|
public float Damage = 1f;
|
2025-01-31 16:06:15 +01:00
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
public BulletOwner Owner = BulletOwner.None;
|
2024-06-09 00:34:36 +02:00
|
|
|
|
2024-02-27 22:54:42 +01:00
|
|
|
private Vector2 _direction = Vector2.Right;
|
2025-02-09 11:48:30 +01:00
|
|
|
|
|
|
|
|
private double _elapsedTime = 0f;
|
|
|
|
|
private BulletInfo _bulletInfo;
|
|
|
|
|
|
|
|
|
|
public void Initialize(BulletInfo bulletInfo)
|
|
|
|
|
{
|
|
|
|
|
_bulletInfo = bulletInfo;
|
|
|
|
|
|
|
|
|
|
Position = bulletInfo.Position;
|
|
|
|
|
Owner = bulletInfo.Owner;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyTimeModifiers()
|
|
|
|
|
{
|
|
|
|
|
foreach (var modifier in _bulletInfo.TimeModifiers)
|
|
|
|
|
{
|
|
|
|
|
if (modifier.Applied)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (_elapsedTime >= modifier.TimeInSeconds)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modifier.Applied = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RotateBullet(float degrees)
|
|
|
|
|
{
|
|
|
|
|
float radians = Mathf.DegToRad(degrees);
|
|
|
|
|
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
|
|
|
|
|
}
|
2024-02-27 22:54:42 +01:00
|
|
|
|
2024-05-15 15:48:48 +02:00
|
|
|
|
2025-02-09 11:48:30 +01:00
|
|
|
private void FacePlayer()
|
2024-02-27 17:16:55 +01:00
|
|
|
{
|
2025-02-09 11:48:30 +01:00
|
|
|
// TODO: cache player
|
|
|
|
|
var player = GetTree().GetFirstNodeInGroup("Player") as Node2D;
|
|
|
|
|
if (player != null)
|
|
|
|
|
{
|
|
|
|
|
_direction = (player.GlobalPosition - this.GlobalPosition).Normalized();
|
|
|
|
|
//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;
|
|
|
|
|
|
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;
|
|
|
|
|
if (_bulletInfo != null)
|
|
|
|
|
{
|
|
|
|
|
ApplyTimeModifiers();
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
2024-02-27 22:54:42 +01:00
|
|
|
QueueFree();
|
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");
|
|
|
|
|
QueueFree();
|
|
|
|
|
}
|
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();
|
|
|
|
|
// }
|
2025-02-07 10:01:07 +01:00
|
|
|
|
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"))
|
|
|
|
|
{
|
|
|
|
|
QueueFree();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 00:34:36 +02:00
|
|
|
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
|
|
|
|
|
{
|
|
|
|
|
//Debug.WriteLine("Collision with destroyable object area");
|
|
|
|
|
|
|
|
|
|
destructible.Hit(Damage);
|
2024-05-26 11:40:35 +02:00
|
|
|
|
2024-06-09 00:34:36 +02:00
|
|
|
QueueFree();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 16:06:15 +01:00
|
|
|
|
|
|
|
|
public enum BulletOwner
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Player,
|
|
|
|
|
Enemy
|
|
|
|
|
}
|