mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 09:05:53 +00:00
2D Character and weapons
This commit is contained in:
parent
072f6d0ce6
commit
cc9c4e5aa1
37 changed files with 1115 additions and 91 deletions
310
Scripts/Weapons/Bullet3D.cs
Normal file
310
Scripts/Weapons/Bullet3D.cs
Normal file
|
|
@ -0,0 +1,310 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Controllers;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Weapons;
|
||||
|
||||
public partial class Bullet3D : Area3D, IBullet
|
||||
{
|
||||
[Export] public float Speed { get; set; } = 1900f;
|
||||
|
||||
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;
|
||||
|
||||
public bool IsGrazed { get; set; } = false;
|
||||
|
||||
public bool IsFrozen { get; private set; } = false;
|
||||
public bool Enabled { get; private set; } = false;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnDestroyEventHandler();
|
||||
|
||||
private AudioStreamPlayer2D _grazeSound;
|
||||
private GpuParticles2D _grazeParticles;
|
||||
|
||||
private CollisionShape2D _collisionShape2D;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_grazeSound = GetNodeOrNull<AudioStreamPlayer2D>("AudioStreamPlayer2D");
|
||||
_grazeParticles = GetNodeOrNull<GpuParticles2D>("GrazeParticles");
|
||||
|
||||
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
|
||||
}
|
||||
|
||||
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
|
||||
{
|
||||
_bulletInfo = bulletInfo;
|
||||
|
||||
_gameManager = gameManager;
|
||||
|
||||
_elapsedTime = 0f;
|
||||
|
||||
this.Speed = bulletInfo.Speed;
|
||||
|
||||
// Need to clone them here
|
||||
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
|
||||
|
||||
|
||||
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
|
||||
// _modifiers = clonedModifiers.ToList();
|
||||
|
||||
// Ugly hack to make instances unique
|
||||
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
|
||||
}
|
||||
|
||||
/// <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");
|
||||
}
|
||||
|
||||
_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;
|
||||
if (hideSprite && !BulletInfo.Attributes.HasFlag(BulletFlags.PersistSprite))
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
if (this._collisionShape2D is null)
|
||||
{
|
||||
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
|
||||
}
|
||||
|
||||
_collisionShape2D.SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
|
||||
}
|
||||
|
||||
public void Graze()
|
||||
{
|
||||
if (!Enabled) return;
|
||||
_grazeSound?.Play();
|
||||
if (_grazeParticles is not null)
|
||||
{
|
||||
_grazeParticles.Emitting = true;
|
||||
}
|
||||
|
||||
IsGrazed = true;
|
||||
}
|
||||
|
||||
private void ApplyTimeModifiers(double delta)
|
||||
{
|
||||
return;
|
||||
// foreach (var modifier in _modifiers)
|
||||
// {
|
||||
// if (_elapsedTime >= modifier.TimeModifier.TimeInSeconds)
|
||||
// {
|
||||
// if (!modifier.Applied)
|
||||
// {
|
||||
// modifier.Applied = true;
|
||||
// modifier.TimeModifier.Start(this);
|
||||
// modifier.Elapsed = 0;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// modifier.Elapsed += delta;
|
||||
// }
|
||||
//
|
||||
// modifier.TimeModifier.Update(this, delta, modifier.Elapsed);
|
||||
//
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public virtual void RotateBullet(float degrees)
|
||||
{
|
||||
float radians = Mathf.DegToRad(degrees);
|
||||
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
|
||||
|
||||
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
|
||||
//SetRotation(Rotation + radians);
|
||||
}
|
||||
|
||||
public virtual void RotateSpriteDegrees(float degrees)
|
||||
{
|
||||
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
|
||||
//SetRotationDegrees(RotationDegrees + degrees);
|
||||
}
|
||||
|
||||
public virtual void RotateSprite(float radians)
|
||||
{
|
||||
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
|
||||
//SetRotation(Rotation + radians);
|
||||
}
|
||||
|
||||
public void FacePlayer()
|
||||
{
|
||||
if (_gameManager.Player != null)
|
||||
{
|
||||
//_direction = (_gameManager.PlayerPosition.Value - this.GlobalPosition).Normalized();
|
||||
RotateBullet(0); // quick hack to rotate lasers
|
||||
//LookAt(player.GlobalPosition);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void SetDirection(Vector2 direction)
|
||||
{
|
||||
var normalized = direction.Normalized();
|
||||
|
||||
_direction = normalized;
|
||||
|
||||
if (!BulletInfo.Attributes.HasFlag(BulletFlags.Rotateable)) return;
|
||||
//SetRotation(Mathf.Atan2(normalized.Y, normalized.X) + Mathf.Pi / 2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
_elapsedTime += delta;
|
||||
|
||||
if (_elapsedTime >= _bulletInfo.LifeTime)
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (_bulletInfo != null)
|
||||
{
|
||||
ApplyTimeModifiers(delta);
|
||||
}
|
||||
|
||||
if (BulletInfo.Attributes.HasFlag(BulletFlags.Controllable))
|
||||
{
|
||||
ControlBullet(delta);
|
||||
}
|
||||
|
||||
//this.Position += ((float)(Speed * delta) * _direction);
|
||||
}
|
||||
|
||||
private void ControlBullet(double delta)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
var axis = Input.GetAxis("left", "right");
|
||||
|
||||
if (axis != 0)
|
||||
{
|
||||
float rotationSpeed = 180f; // Degrees per second
|
||||
RotateBullet(axis * rotationSpeed * (float)delta);
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_visible_on_screen_notifier_2d_screen_exited()
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (!BulletInfo.Attributes.HasFlag(BulletFlags.DieOutOfScreen)) return;
|
||||
//Debug.WriteLine("Destroy bullet out of screen");
|
||||
Destroy();
|
||||
}
|
||||
|
||||
private void _on_body_entered(Node2D body)
|
||||
{
|
||||
if (body.IsInGroup("Solid"))
|
||||
{
|
||||
//Debug.WriteLine("Collision");
|
||||
RequestCollisionDestruction();
|
||||
}
|
||||
//// 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"))
|
||||
{
|
||||
RequestCollisionDestruction();
|
||||
return;
|
||||
}
|
||||
|
||||
if (area.IsInGroup("Destroyable") && area is IDestructible destructible &&
|
||||
CanHit(BulletOwner, destructible.BulletGroup))
|
||||
{
|
||||
// hit
|
||||
destructible.Hit(Damage, DamageType);
|
||||
|
||||
RequestCollisionDestruction();
|
||||
}
|
||||
}
|
||||
|
||||
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 RequestCollisionDestruction()
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (_bulletInfo.DestroyOnCollision)
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private void Destroy()
|
||||
{
|
||||
if (_bulletInfo?.DestructionParticlesScene != null)
|
||||
{
|
||||
//this.CreateSibling<Node2D>(_bulletInfo.DestructionParticlesScene);
|
||||
|
||||
//particle.Init();
|
||||
}
|
||||
|
||||
EmitSignal(Bullet.SignalName.OnDestroy);
|
||||
//QueueFree();
|
||||
PoolingManager.Instance.DisableBullet(this);
|
||||
}
|
||||
|
||||
public void Freeze()
|
||||
{
|
||||
IsFrozen = true;
|
||||
EmitSignal(Bullet.SignalName.OnDestroy);
|
||||
//QueueFree();
|
||||
PoolingManager.Instance.DisableBullet(this);
|
||||
}
|
||||
}
|
||||
1
Scripts/Weapons/Bullet3D.cs.uid
Normal file
1
Scripts/Weapons/Bullet3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cg6y36s7buapp
|
||||
34
Scripts/Weapons/IBullet.cs
Normal file
34
Scripts/Weapons/IBullet.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using Cirno.Scripts.Components;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Weapons;
|
||||
|
||||
public interface IBullet
|
||||
{
|
||||
public float Speed { get; set; }
|
||||
public BulletOwner BulletOwner { get; }
|
||||
public float Damage { get; }
|
||||
public DamageType DamageType { get; }
|
||||
public BulletInfo BulletInfo { get; }
|
||||
|
||||
public bool IsGrazed { get; }
|
||||
|
||||
public bool IsFrozen { get; }
|
||||
public bool Enabled { get; }
|
||||
|
||||
public delegate void OnDestroyEventHandler();
|
||||
|
||||
public void Initialize(BulletInfo bulletInfo, GameManager gameManager);
|
||||
|
||||
public void Enable();
|
||||
public void Disable(bool hideSprite = true);
|
||||
public void Graze();
|
||||
public void RotateBullet(float degrees);
|
||||
public void RotateSpriteDegrees(float degrees);
|
||||
public void RotateSprite(float radians);
|
||||
public void FacePlayer();
|
||||
public void SetDirection(Vector2 direction);
|
||||
public bool CanHit(BulletOwner bulletOwner, BulletOwner targetGroup);
|
||||
public void RequestCollisionDestruction();
|
||||
public void Freeze();
|
||||
}
|
||||
1
Scripts/Weapons/IBullet.cs.uid
Normal file
1
Scripts/Weapons/IBullet.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://58w1nidcpf2j
|
||||
228
Scripts/Weapons/Weapon3D.cs
Normal file
228
Scripts/Weapons/Weapon3D.cs
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
using Cirno.Scripts.Controllers;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Weapons;
|
||||
|
||||
public partial class Weapon3D : Node
|
||||
{
|
||||
[Export]
|
||||
public WeaponResource WeaponData { get; set; }
|
||||
|
||||
[Export]
|
||||
public PackedScene BulletScene { get; set; }
|
||||
|
||||
[Export]
|
||||
public Marker3D Muzzle { get; set; }
|
||||
|
||||
[Export]
|
||||
public Marker3D Pivot { get; set; }
|
||||
|
||||
[Export]
|
||||
public Sprite3D Sprite { get; private set; }
|
||||
|
||||
[Export] public StringName PowerKey { get; set; } = "POWER";
|
||||
|
||||
[Signal]
|
||||
public delegate void ShootingEventHandler();
|
||||
|
||||
[Signal]
|
||||
public delegate void ReloadingEventHandler();
|
||||
|
||||
[Signal] public delegate void EmptyEventHandler();
|
||||
|
||||
public int Ammo { get; set; } = 0;
|
||||
|
||||
private int _loadedAmmo;
|
||||
public int LoadedAmmo
|
||||
{
|
||||
get => _loadedAmmo;
|
||||
private set
|
||||
{
|
||||
_loadedAmmo = value;
|
||||
_inventoryManager?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
|
||||
|
||||
private Timer _cooldownTimer;
|
||||
|
||||
private GameManager _gameManager;
|
||||
|
||||
private InventoryManager _inventoryManager;
|
||||
|
||||
private readonly StringName _shieldAmmoType = "SHIELD";
|
||||
private bool UsesBattery => WeaponData.AmmoKey == _shieldAmmoType;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
_cooldownTimer = GetNode<Timer>("./ShootTimer");
|
||||
|
||||
// Start full
|
||||
if (WeaponData != null)
|
||||
{
|
||||
LoadedAmmo = WeaponData.BulletCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
EmitSignalReloading();
|
||||
|
||||
_cooldownTimer.Start(WeaponData.ReloadTime);
|
||||
|
||||
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
|
||||
{
|
||||
LoadedAmmo = WeaponData.BulletCapacity;
|
||||
}
|
||||
else
|
||||
{
|
||||
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity - LoadedAmmo);
|
||||
|
||||
if (ammoToLoad > 0)
|
||||
{
|
||||
LoadedAmmo = ammoToLoad;
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalEmpty();
|
||||
//GD.Print("Out of ammo");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Shoot(BulletOwner? ownerOverride = null)
|
||||
{
|
||||
// Waiting on reload or Rate of Fire cooldown?
|
||||
if (!_cooldownTimer.IsStopped())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for battery if it's used
|
||||
if (UsesBattery)
|
||||
{
|
||||
if (GameManager.Instance.Player.Shield.CurrentResource >= WeaponData.AmmoPerShot)
|
||||
{
|
||||
GameManager.Instance.Player.Shield.CurrentResource -= WeaponData.AmmoPerShot;
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalEmpty();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Out of ammo?
|
||||
if (LoadedAmmo < WeaponData.AmmoPerShot)
|
||||
{
|
||||
if (WeaponData.AutoReload)
|
||||
{
|
||||
Reload();
|
||||
}
|
||||
EmitSignalEmpty();
|
||||
return;
|
||||
}
|
||||
|
||||
EmitSignalShooting();
|
||||
|
||||
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
|
||||
|
||||
float halfSpread = WeaponData.SpreadAngle / 2f;
|
||||
float spreadStep = WeaponData.BulletsPerShot > 1 ? WeaponData.SpreadAngle / (WeaponData.BulletsPerShot - 1) : 0;
|
||||
|
||||
for (int i = 0; i < WeaponData.BulletsPerShot; i++)
|
||||
{
|
||||
// Calculate angle offset for this bullet
|
||||
float spreadOffset = -halfSpread + (spreadStep * i);
|
||||
|
||||
// Add random spread
|
||||
if (WeaponData.RandomSpread > 0)
|
||||
{
|
||||
// Gaussian with mean = 0, stddev = WeaponData.RandomSpread
|
||||
spreadOffset += RandomStuff.GaussianClamped(
|
||||
mean: 0f,
|
||||
stdDev: WeaponData.RandomSpread, // tuning knob
|
||||
min: -halfSpread,
|
||||
max: halfSpread
|
||||
);
|
||||
}
|
||||
|
||||
// Rotate the ShootDirection by the spread angle
|
||||
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
|
||||
|
||||
// Restore pooling
|
||||
var bullet = PoolingManager.Instance.SpawnBullet<Bullet3D>(WeaponData.BulletData);
|
||||
|
||||
//var bullet = WeaponData.BulletData.BulletScene.Instantiate<Bullet3D>()
|
||||
|
||||
bullet.GlobalPosition = Muzzle.GlobalPosition;
|
||||
|
||||
//var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletData.BulletScene, _muzzle.GlobalPosition);
|
||||
|
||||
if (bullet == null)
|
||||
{
|
||||
GD.PrintErr("Bullet is null, not shooting");
|
||||
return;
|
||||
};
|
||||
|
||||
var bulletData = WeaponData.MakeBullet(Muzzle.GlobalPosition.ToVector2()); // TODO: Fix for 3D
|
||||
if (ownerOverride.HasValue)
|
||||
{
|
||||
bulletData.Owner = ownerOverride.Value;
|
||||
}
|
||||
|
||||
if (bulletData.Owner is BulletOwner.Player || ownerOverride is BulletOwner.Player)
|
||||
{
|
||||
// Apply the P multiplier
|
||||
bulletData.Damage *=
|
||||
GetBulletStrengthMultiplier(bulletData.Damage, bulletData.OriginalBulletResource.MaxDamage, 20);
|
||||
}
|
||||
|
||||
bullet.Initialize(bulletData, _gameManager);
|
||||
|
||||
//bullet.SetDirection(ShootDirection);
|
||||
bullet.SetDirection(spreadDirection);
|
||||
bullet.Speed = WeaponData.BulletData.BulletSpeed;
|
||||
}
|
||||
|
||||
if (!UsesBattery)
|
||||
{
|
||||
LoadedAmmo -= WeaponData.AmmoPerShot;
|
||||
}
|
||||
|
||||
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
|
||||
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
|
||||
// {
|
||||
// // Notify hud to decrease weapon
|
||||
//
|
||||
// }
|
||||
|
||||
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
|
||||
}
|
||||
|
||||
private float GetBulletStrengthMultiplier(float baseDamage, float maxDamage, float maxPower)
|
||||
{
|
||||
var p = InventoryManager.Instance.GetItemCount(PowerKey);
|
||||
|
||||
float minMultiplier = 1.0f;
|
||||
float maxMultiplier = maxDamage / baseDamage;
|
||||
|
||||
float normalizedPower = Mathf.Clamp((float)p / maxPower, 0f, 1f);
|
||||
return Mathf.Lerp(minMultiplier, maxMultiplier, normalizedPower);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
1
Scripts/Weapons/Weapon3D.cs.uid
Normal file
1
Scripts/Weapons/Weapon3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dutroqc0grqyv
|
||||
Loading…
Add table
Add a link
Reference in a new issue