mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 09:35:34 +00:00
Basic laser integration
This commit is contained in:
parent
d16f9b3b9d
commit
4011a7c6f6
15 changed files with 1028 additions and 284 deletions
|
|
@ -3,6 +3,7 @@
|
|||
namespace Cirno.Scripts.Actors._3D;
|
||||
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class LaserConfig : Resource
|
||||
{
|
||||
[Export] public float MaxLength = 50f;
|
||||
|
|
|
|||
|
|
@ -7,9 +7,15 @@ namespace Cirno.Scripts.Actors;
|
|||
|
||||
public partial class BulletSpawner3D : Node3D
|
||||
{
|
||||
|
||||
public void SpawnBullet(BulletInfo bulletInfo, Vector3 position)
|
||||
{
|
||||
// Check if this is a laser
|
||||
if (bulletInfo.IsLaser && bulletInfo.LaserConfig != null)
|
||||
{
|
||||
SpawnLaser(bulletInfo, position);
|
||||
return;
|
||||
}
|
||||
|
||||
int count = bulletInfo.BulletCount;
|
||||
|
||||
// Choose base direction (defaults to +X)
|
||||
|
|
@ -66,5 +72,28 @@ public partial class BulletSpawner3D : Node3D
|
|||
}
|
||||
}
|
||||
|
||||
private void SpawnLaser(BulletInfo bulletInfo, Vector3 position)
|
||||
{
|
||||
// Lasers don't use bullet count/spread the same way
|
||||
// Each laser is typically a single beam
|
||||
LaserBullet3D laser = PoolingManager.Instance.SpawnBullet<LaserBullet3D>(bulletInfo.OriginalBulletResource);
|
||||
laser.GlobalPosition = position;
|
||||
|
||||
// Apply modifiers if present
|
||||
if (bulletInfo.Modifier is not null)
|
||||
{
|
||||
bulletInfo = bulletInfo.Modifier.ModifyBullet(bulletInfo, 0, 1);
|
||||
}
|
||||
|
||||
laser.Initialize(bulletInfo);
|
||||
|
||||
// Set the laser direction
|
||||
Vector2 baseDirection = bulletInfo.Direction == Vector2.Zero
|
||||
? Vector2.Right
|
||||
: bulletInfo.Direction.Normalized();
|
||||
|
||||
laser.SetDirection(baseDirection);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,17 +1,20 @@
|
|||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Actors;
|
||||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.AttackPatterns;
|
||||
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class LaserPattern : SpiralPattern
|
||||
public partial class LaserPattern : ShootingPattern3D
|
||||
{
|
||||
[ExportGroup("Laser")][Export] public float SpawnDelay { get; set; } = 0.3f; // Delay before beam appears
|
||||
[ExportGroup("Laser")][Export] public float PreFireTime { get; set; } = 0.5f; // Time before laser becomes lethal
|
||||
[ExportGroup("Laser")][Export] public float LethalTime { get; set; } = 1.5f; // Time laser remains lethal
|
||||
[ExportGroup("Laser")][Export] public Color PreFireColor { get; set; } = new Color(1, 0, 0, 0.5f); // Thin red beam
|
||||
[ExportGroup("Laser")][Export] public Color LethalColor { get; set; } = new Color(1, 0, 0, 1.0f); // Thicker beam
|
||||
[ExportGroup("Laser")][Export] public Color LethalColor { get; set; } = Colors.Red; // Thicker beam
|
||||
|
||||
protected override BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
|
||||
{
|
||||
|
|
@ -26,4 +29,194 @@ public partial class LaserPattern : SpiralPattern
|
|||
|
||||
return bf;
|
||||
}
|
||||
|
||||
public override IPatternMachine MakeMachine(Node parent)
|
||||
{
|
||||
return new LaserPatternMachine(this, parent);
|
||||
}
|
||||
|
||||
public class LaserPatternMachine(LaserPattern pattern, Node parent) : IPatternMachine
|
||||
{
|
||||
public Node Parent => parent;
|
||||
public IScriptHost3D ScriptHost { get; private set; }
|
||||
private double _timer;
|
||||
private double _burstTimer;
|
||||
private BulletSpawner3D _spawner;
|
||||
private ShootStatus _state = ShootStatus.Idle;
|
||||
private int _burstBullets;
|
||||
private int _currentBurstOffset;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
ScriptHost = Parent as IScriptHost3D;
|
||||
|
||||
_timer = 0;
|
||||
_burstBullets = pattern.ShotsPerBurst;
|
||||
_burstTimer = pattern.burstInterval;
|
||||
_spawner = parent.GetNode<BulletSpawner3D>("BulletSpawner3D");
|
||||
|
||||
_state = pattern.Delay == 0 ? ShootStatus.Shooting : ShootStatus.Idle;
|
||||
}
|
||||
|
||||
public void UpdatePattern(double delta)
|
||||
{
|
||||
switch (_state)
|
||||
{
|
||||
case ShootStatus.Idle:
|
||||
IdleUpdate(delta);
|
||||
break;
|
||||
case ShootStatus.Done:
|
||||
return;
|
||||
case ShootStatus.Shooting:
|
||||
ShootingUpdate(delta);
|
||||
break;
|
||||
case ShootStatus.WaitingBurst:
|
||||
WaitingBurstUpdate(delta);
|
||||
break;
|
||||
case ShootStatus.WaitingReload:
|
||||
WaitingReloadUpdate(delta);
|
||||
break;
|
||||
}
|
||||
|
||||
if (pattern.duration > -1 && _timer >= pattern.duration)
|
||||
{
|
||||
_state = ShootStatus.Done;
|
||||
}
|
||||
}
|
||||
|
||||
private void IdleUpdate(double delta)
|
||||
{
|
||||
_timer += delta;
|
||||
if (_timer >= pattern.Delay)
|
||||
{
|
||||
_state = ShootStatus.Shooting;
|
||||
}
|
||||
}
|
||||
|
||||
private void WaitingBurstUpdate(double delta)
|
||||
{
|
||||
_timer += delta;
|
||||
_burstTimer += delta;
|
||||
|
||||
if (_burstTimer >= pattern.burstInterval)
|
||||
{
|
||||
_state = ShootStatus.Shooting;
|
||||
}
|
||||
}
|
||||
|
||||
private void WaitingReloadUpdate(double delta)
|
||||
{
|
||||
_timer += delta;
|
||||
_burstTimer += delta;
|
||||
|
||||
if (_burstTimer >= pattern.BurstRate)
|
||||
{
|
||||
_burstBullets = pattern.ShotsPerBurst;
|
||||
_state = ShootStatus.Shooting;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShootingUpdate(double delta)
|
||||
{
|
||||
_timer += delta;
|
||||
_burstTimer = 0;
|
||||
Shoot();
|
||||
_burstBullets--;
|
||||
|
||||
if (_burstBullets <= 0)
|
||||
{
|
||||
if (pattern.LoopType == LoopType.PlayOnce)
|
||||
{
|
||||
_state = ShootStatus.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = ShootStatus.WaitingReload;
|
||||
_currentBurstOffset++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = ShootStatus.WaitingBurst;
|
||||
}
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
if (pattern.BulletResource == null)
|
||||
{
|
||||
GD.PushError("LaserPattern: BulletResource is null! Cannot spawn laser.");
|
||||
_state = ShootStatus.Done;
|
||||
return;
|
||||
}
|
||||
|
||||
float angleOffset = pattern._rotationOffset + (float)(pattern.rotationSpeed * _timer) +
|
||||
(float)pattern.BurstRotationSpeed * _currentBurstOffset;
|
||||
|
||||
Vector2 direction = pattern.BulletResource.Direction;
|
||||
|
||||
// Rotate with parent rotation
|
||||
if (pattern.UseParentRotationOffset)
|
||||
{
|
||||
direction = direction.Rotated(-_spawner.GlobalRotation.Y + Mathf.DegToRad(90));
|
||||
}
|
||||
|
||||
// Handle player targeting for 3D
|
||||
if (pattern._targetPlayer && GameController.Instance.PlayerPosition.HasValue)
|
||||
{
|
||||
if (pattern._predictPlayer && GameController.Instance.PlayerVelocity.HasValue)
|
||||
{
|
||||
var predictedDirection = MathFunctions.PredictInterceptPosition(
|
||||
_spawner.GlobalPosition.ToVector2(),
|
||||
GameController.Instance.PlayerPosition.Value.ToVector2(),
|
||||
GameController.Instance.PlayerVelocity.Value.ToVector2(),
|
||||
pattern.BulletResource.BulletSpeed);
|
||||
|
||||
if (predictedDirection.HasValue)
|
||||
{
|
||||
direction = (predictedDirection.Value - _spawner.GlobalPosition.ToVector2()).Normalized();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
direction = (GameController.Instance.PlayerPosition.Value.ToVector2() -
|
||||
_spawner.GlobalPosition.ToVector2()).Normalized();
|
||||
}
|
||||
}
|
||||
|
||||
var spawnPosition = _spawner.GlobalPosition + pattern.EmitterOffset;
|
||||
|
||||
// Create laser bullet with laser-specific properties
|
||||
var bullet = pattern.MakeBullet(
|
||||
spawnPosition.ToVector2(),
|
||||
pattern.bulletCount,
|
||||
pattern.spread,
|
||||
angleOffset);
|
||||
|
||||
bullet.Direction = direction;
|
||||
|
||||
_spawner.SpawnBullet(bullet, spawnPosition);
|
||||
}
|
||||
|
||||
public bool IsComplete()
|
||||
{
|
||||
if (!pattern.WaitForCompletion) return _state is ShootStatus.Done;
|
||||
if (_state is not ShootStatus.Done) return false;
|
||||
|
||||
if (pattern.duration > -1)
|
||||
{
|
||||
return (_timer >= pattern.duration);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private enum ShootStatus
|
||||
{
|
||||
Idle,
|
||||
Shooting,
|
||||
WaitingBurst,
|
||||
WaitingReload,
|
||||
Done
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using Cirno.Scripts.Actors._3D;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
|
@ -39,10 +40,13 @@ public class BulletInfo(BulletResource originalBulletResource)
|
|||
|
||||
#region Laser
|
||||
public bool IsLaser { get; set; }
|
||||
public float SpawnDelay { get; set; } = 0.3f; // Delay before beam appears
|
||||
public float PreFireTime { get; set; } = 0.5f; // Time before laser becomes lethal
|
||||
public float LethalTime { get; set; } = 1.5f; // Time laser remains lethal
|
||||
public Color PreFireColor { get; set; } = new Color(1, 0, 0, 0.5f); // Thin red beam
|
||||
public Color LethalColor { get; set; } = new Color(1, 0, 0, 1.0f); // Thicker beam
|
||||
public LaserConfig LaserConfig { get; set; } // For 3D lasers
|
||||
|
||||
// 2D Laser properties (legacy support)
|
||||
public float SpawnDelay { get; set; } = 0.3f;
|
||||
public float PreFireTime { get; set; } = 0.5f;
|
||||
public float LethalTime { get; set; } = 1.5f;
|
||||
public Color PreFireColor { get; set; } = new Color(1, 0, 0, 0.5f);
|
||||
public Color LethalColor { get; set; } = new Color(1, 0, 0, 1.0f);
|
||||
#endregion
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Actors._3D;
|
||||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
|
@ -32,6 +33,11 @@ public partial class BulletResource : Resource
|
|||
[Export]
|
||||
public BulletCreationModifier Modifier;
|
||||
[Export] public Array<TimeModifier> TimeModifiers;
|
||||
|
||||
// Laser-specific properties
|
||||
[ExportGroup("Laser Settings")]
|
||||
[Export] public bool IsLaser { get; set; }
|
||||
[Export] public LaserConfig LaserConfig { get; set; }
|
||||
|
||||
public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
|
||||
{
|
||||
|
|
@ -59,6 +65,8 @@ public partial class BulletResource : Resource
|
|||
//Grazeable = Attributes.HasFlag(BulletFlags.Grazeable),
|
||||
GrazeValue = GrazeValue,
|
||||
Attributes = Attributes,
|
||||
IsLaser = IsLaser,
|
||||
LaserConfig = LaserConfig
|
||||
// TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().Select(m => new ModifierWrapper()
|
||||
// {
|
||||
// TimeModifier = m,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,404 @@
|
|||
namespace Cirno.Scripts.Weapons;
|
||||
using Cirno.Scripts.Actors._3D;
|
||||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Controllers;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
|
||||
public partial class LaserBullet3D : Bullet3D
|
||||
namespace Cirno.Scripts.Weapons;
|
||||
|
||||
/// <summary>
|
||||
/// Laser implementation that conforms to the IBullet interface.
|
||||
/// Wraps the Laser class functionality to integrate with the bullet system.
|
||||
/// </summary>
|
||||
public partial class LaserBullet3D : Area3D, IBullet
|
||||
{
|
||||
//private line3d
|
||||
}
|
||||
private BulletInfo _bulletInfo;
|
||||
|
||||
public float Speed { get; set; }
|
||||
public BulletOwner BulletOwner => _bulletInfo?.Owner ?? BulletOwner.None;
|
||||
public float Damage => _bulletInfo?.Damage ?? 1;
|
||||
public DamageType DamageType => _bulletInfo?.DamageType ?? DamageType.Neutral;
|
||||
public BulletInfo BulletInfo => _bulletInfo;
|
||||
public bool IsGrazed { get; set; }
|
||||
public bool IsFrozen { get; private set; }
|
||||
public bool Enabled { get; private set; }
|
||||
|
||||
private Vector2 _direction = Vector2.Right;
|
||||
private double _elapsedTime;
|
||||
|
||||
private MeshInstance3D _mesh;
|
||||
private CollisionShape3D _collision;
|
||||
private RayCast3D _ray;
|
||||
private ShaderMaterial _beamMaterial;
|
||||
private float _currentRadius;
|
||||
private Laser.LaserState _state = Laser.LaserState.Inactive;
|
||||
private float _stateTimer;
|
||||
private Vector3 _origin;
|
||||
private Vector3 _laserDirection;
|
||||
private float _currentLength;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnDestroyEventHandler();
|
||||
|
||||
[Signal]
|
||||
public delegate void InitializedEventHandler();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_mesh = GetNode<MeshInstance3D>("MeshInstance3D");
|
||||
_collision = GetNode<CollisionShape3D>("CollisionShape3D");
|
||||
_ray = GetNode<RayCast3D>("RayCast3D");
|
||||
_beamMaterial = _mesh.MaterialOverride as ShaderMaterial;
|
||||
}
|
||||
|
||||
public void Initialize(BulletInfo bulletInfo)
|
||||
{
|
||||
_bulletInfo = bulletInfo;
|
||||
_elapsedTime = 0f;
|
||||
_direction = bulletInfo.Direction.Normalized();
|
||||
|
||||
IsGrazed = false;
|
||||
IsFrozen = false;
|
||||
|
||||
// Setup laser from direction
|
||||
_origin = GlobalPosition;
|
||||
_laserDirection = new Vector3(_direction.X, 0, _direction.Y).Normalized();
|
||||
|
||||
if (_bulletInfo.LaserConfig != null)
|
||||
{
|
||||
_ray.CollisionMask = _bulletInfo.LaserConfig.GeometryLayer;
|
||||
}
|
||||
|
||||
ChangeCollisionStateDeferred(true);
|
||||
|
||||
StartLaser();
|
||||
EmitSignal(SignalName.Initialized);
|
||||
}
|
||||
|
||||
private void StartLaser()
|
||||
{
|
||||
_mesh.Scale = Vector3.One;
|
||||
_collision.Scale = Vector3.One;
|
||||
|
||||
_stateTimer = 0f;
|
||||
_state = _bulletInfo.LaserConfig.WarningDuration > 0
|
||||
? Laser.LaserState.Warning
|
||||
: Laser.LaserState.Expanding;
|
||||
|
||||
UpdateVisualOrientation();
|
||||
SetRadius(_bulletInfo.LaserConfig.WarningRadius);
|
||||
}
|
||||
|
||||
public void Enable()
|
||||
{
|
||||
Enabled = true;
|
||||
Show();
|
||||
if (_collision != null)
|
||||
{
|
||||
_collision.SetDeferred(CollisionShape3D.PropertyName.Disabled, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void Disable(bool hideSprite = true)
|
||||
{
|
||||
Enabled = false;
|
||||
if (hideSprite)
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
if (_collision != null)
|
||||
{
|
||||
_collision.SetDeferred(CollisionShape3D.PropertyName.Disabled, true);
|
||||
}
|
||||
}
|
||||
|
||||
public void Graze()
|
||||
{
|
||||
if (!Enabled) return;
|
||||
IsGrazed = true;
|
||||
}
|
||||
|
||||
public void RotateBullet(float degrees)
|
||||
{
|
||||
float radians = Mathf.DegToRad(degrees);
|
||||
_direction = _direction.Rotated(radians).Normalized();
|
||||
_laserDirection = new Vector3(_direction.X, 0, _direction.Y).Normalized();
|
||||
UpdateVisualOrientation();
|
||||
}
|
||||
|
||||
public void RotateSpriteDegrees(float degrees)
|
||||
{
|
||||
// Lasers don't rotate sprite independently
|
||||
RotateBullet(degrees);
|
||||
}
|
||||
|
||||
public void RotateSprite(float radians)
|
||||
{
|
||||
// Lasers don't rotate sprite independently
|
||||
RotateBullet(Mathf.RadToDeg(radians));
|
||||
}
|
||||
|
||||
public void FacePlayer()
|
||||
{
|
||||
if (GameController.Instance.PlayerPosition.HasValue)
|
||||
{
|
||||
_direction = (GameController.Instance.PlayerPosition.Value.ToVector2() - GlobalPosition.ToVector2()).Normalized();
|
||||
_laserDirection = new Vector3(_direction.X, 0, _direction.Y).Normalized();
|
||||
UpdateVisualOrientation();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDirection(Vector2 direction)
|
||||
{
|
||||
_direction = direction.Normalized();
|
||||
_laserDirection = new Vector3(_direction.X, 0, _direction.Y).Normalized();
|
||||
UpdateVisualOrientation();
|
||||
}
|
||||
|
||||
public bool CanHit(BulletOwner bulletOwner, BulletOwner targetGroup)
|
||||
{
|
||||
if (bulletOwner == BulletOwner.None || targetGroup == BulletOwner.None)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return bulletOwner != targetGroup;
|
||||
}
|
||||
|
||||
public void RequestCollisionDestruction()
|
||||
{
|
||||
// Lasers typically don't get destroyed on collision
|
||||
if (_bulletInfo.DestroyOnCollision)
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void Freeze()
|
||||
{
|
||||
IsFrozen = true;
|
||||
EmitSignal(SignalName.OnDestroy);
|
||||
PoolingManager.Instance.DisableBullet(this);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
|
||||
_elapsedTime += delta;
|
||||
|
||||
if (_elapsedTime >= _bulletInfo.LifeTime)
|
||||
{
|
||||
Destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_state == Laser.LaserState.Finished)
|
||||
return;
|
||||
|
||||
_stateTimer += (float)delta;
|
||||
|
||||
UpdateRaycast();
|
||||
UpdateBeam();
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case Laser.LaserState.Warning:
|
||||
if (_stateTimer >= _bulletInfo.LaserConfig.WarningDuration)
|
||||
TransitionTo(Laser.LaserState.Expanding);
|
||||
break;
|
||||
|
||||
case Laser.LaserState.Expanding:
|
||||
HandleExpansion();
|
||||
break;
|
||||
|
||||
case Laser.LaserState.Active:
|
||||
if (_bulletInfo.LaserConfig.ActiveDuration >= 0 &&
|
||||
_stateTimer >= _bulletInfo.LaserConfig.ActiveDuration)
|
||||
TransitionTo(Laser.LaserState.Finished);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void TransitionTo(Laser.LaserState next)
|
||||
{
|
||||
_state = next;
|
||||
_stateTimer = 0f;
|
||||
|
||||
switch (next)
|
||||
{
|
||||
case Laser.LaserState.Expanding:
|
||||
if (_bulletInfo.LaserConfig.ExpansionDuration <= 0f)
|
||||
{
|
||||
SetRadius(_bulletInfo.LaserConfig.DamageRadius);
|
||||
EnableCollision();
|
||||
TransitionTo(Laser.LaserState.Active);
|
||||
}
|
||||
break;
|
||||
|
||||
case Laser.LaserState.Active:
|
||||
EnableCollision();
|
||||
SetRadius(_bulletInfo.LaserConfig.DamageRadius);
|
||||
break;
|
||||
|
||||
case Laser.LaserState.Finished:
|
||||
Destroy();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleExpansion()
|
||||
{
|
||||
if (_stateTimer < _bulletInfo.LaserConfig.ExpansionDelay)
|
||||
return;
|
||||
|
||||
var t = Mathf.Clamp(
|
||||
(_stateTimer - _bulletInfo.LaserConfig.ExpansionDelay) /
|
||||
Mathf.Max(_bulletInfo.LaserConfig.ExpansionDuration, 0.001f),
|
||||
0f, 1f
|
||||
);
|
||||
|
||||
var radius = Mathf.Lerp(_bulletInfo.LaserConfig.WarningRadius,
|
||||
_bulletInfo.LaserConfig.DamageRadius, t);
|
||||
SetRadius(radius);
|
||||
|
||||
if (t >= 1f)
|
||||
TransitionTo(Laser.LaserState.Active);
|
||||
}
|
||||
|
||||
private void UpdateRaycast()
|
||||
{
|
||||
_ray.TargetPosition = _laserDirection * _bulletInfo.LaserConfig.MaxLength;
|
||||
_ray.ForceRaycastUpdate();
|
||||
|
||||
_currentLength = _ray.IsColliding()
|
||||
? GlobalPosition.DistanceTo(_ray.GetCollisionPoint())
|
||||
: _bulletInfo.LaserConfig.MaxLength;
|
||||
}
|
||||
|
||||
private void UpdateVisualOrientation()
|
||||
{
|
||||
Basis look = Basis.LookingAt(_laserDirection, Vector3.Up);
|
||||
Basis correction = new Basis(Vector3.Right, Mathf.Pi / 2f);
|
||||
Basis finalBasis = look * correction;
|
||||
|
||||
_mesh.Basis = finalBasis;
|
||||
_collision.Basis = finalBasis;
|
||||
}
|
||||
|
||||
private void UpdateBeam()
|
||||
{
|
||||
if (_mesh.Mesh is CylinderMesh cyl)
|
||||
cyl.Height = _currentLength;
|
||||
|
||||
if (_collision.Shape is CapsuleShape3D capsule)
|
||||
capsule.Height = _currentLength;
|
||||
|
||||
Vector3 center = _origin + _laserDirection * (_currentLength * 0.5f);
|
||||
|
||||
Transform3D meshXform = _mesh.GlobalTransform;
|
||||
meshXform.Origin = center;
|
||||
_mesh.GlobalTransform = meshXform;
|
||||
|
||||
Transform3D colXform = _collision.GlobalTransform;
|
||||
colXform.Origin = center;
|
||||
_collision.GlobalTransform = colXform;
|
||||
}
|
||||
|
||||
private void SetRadius(float radius)
|
||||
{
|
||||
_currentRadius = radius;
|
||||
|
||||
if (_mesh.Mesh is CylinderMesh cyl)
|
||||
{
|
||||
cyl.TopRadius = radius;
|
||||
cyl.BottomRadius = radius;
|
||||
}
|
||||
|
||||
if (_collision.Shape is CapsuleShape3D capsule)
|
||||
capsule.Radius = radius;
|
||||
|
||||
UpdateMaterial();
|
||||
}
|
||||
|
||||
private void EnableCollision()
|
||||
{
|
||||
ChangeCollisionStateDeferred(false);
|
||||
}
|
||||
|
||||
private void ChangeCollisionStateDeferred(bool value)
|
||||
{
|
||||
_collision.SetDeferred(CollisionShape3D.PropertyName.Disabled, value);
|
||||
}
|
||||
|
||||
private void UpdateMaterial()
|
||||
{
|
||||
if (_beamMaterial == null)
|
||||
return;
|
||||
|
||||
_beamMaterial.SetShaderParameter("beam_length", _currentLength);
|
||||
_beamMaterial.SetShaderParameter("beam_radius", _currentRadius);
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case Laser.LaserState.Warning:
|
||||
_beamMaterial.SetShaderParameter("beam_color", new Color(1f, 1f, 0.2f));
|
||||
_beamMaterial.SetShaderParameter("intensity", 0.5f);
|
||||
break;
|
||||
|
||||
case Laser.LaserState.Active:
|
||||
_beamMaterial.SetShaderParameter("beam_color", new Color(1f, 0.2f, 0.2f));
|
||||
_beamMaterial.SetShaderParameter("intensity", 1.0f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsPointInsideBeam(Vector3 worldPoint)
|
||||
{
|
||||
if (_state != Laser.LaserState.Active)
|
||||
return false;
|
||||
|
||||
Vector3 local = worldPoint - _origin;
|
||||
float projection = local.Dot(_laserDirection);
|
||||
|
||||
if (projection < 0 || projection > _currentLength)
|
||||
return false;
|
||||
|
||||
Vector3 closestPoint = _origin + _laserDirection * projection;
|
||||
float distanceSq = worldPoint.DistanceSquaredTo(closestPoint);
|
||||
|
||||
float radius = _bulletInfo.LaserConfig.DamageRadius;
|
||||
return distanceSq <= radius * radius;
|
||||
}
|
||||
|
||||
private void _on_area_entered(Area3D area)
|
||||
{
|
||||
if (!Enabled || _state != Laser.LaserState.Active) return;
|
||||
|
||||
if (area.IsInGroup("Destroyable") && area is IDestructible destructible &&
|
||||
CanHit(BulletOwner, destructible.BulletGroup))
|
||||
{
|
||||
destructible.Hit(Damage, DamageType);
|
||||
RequestCollisionDestruction();
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_body_entered(Node3D body)
|
||||
{
|
||||
if (!Enabled || _state != Laser.LaserState.Active) return;
|
||||
|
||||
if (body.IsInGroup("Destroyable") && body is IDestructible destructible &&
|
||||
CanHit(BulletOwner, destructible.BulletGroup))
|
||||
{
|
||||
destructible.Hit(Damage, DamageType);
|
||||
RequestCollisionDestruction();
|
||||
}
|
||||
}
|
||||
|
||||
private void Destroy()
|
||||
{
|
||||
EmitSignal(SignalName.OnDestroy);
|
||||
PoolingManager.Instance.DisableBullet(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue