82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using Godot;
|
|
|
|
namespace Movementtests.systems;
|
|
|
|
public partial class WeaponSystem : RigidBody3D
|
|
{
|
|
[Export(PropertyHint.Range, "0,100,1,or_greater")]
|
|
public float ThrowForce { get; set; } = 1f;
|
|
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
|
|
public float StraightThrowDuration { get; set; } = 0.1f;
|
|
|
|
private Node3D _head;
|
|
private ShapeCast3D _dashCast3D;
|
|
private Camera3D _camera;
|
|
private TweenQueueSystem _tweenQueueSystem;
|
|
|
|
private Transform3D _startTransform;
|
|
|
|
private Vector3 _throwDirection;
|
|
private Vector3 _plantLocation;
|
|
private Vector3 _plantNormal;
|
|
|
|
public void Init(Node3D head, Camera3D camera)
|
|
{
|
|
_head = head;
|
|
_camera = camera;
|
|
|
|
_tweenQueueSystem = GetNode<TweenQueueSystem>("TweenQueueSystem");
|
|
_tweenQueueSystem.Init(this);
|
|
|
|
_startTransform = Transform;
|
|
Freeze = true;
|
|
}
|
|
|
|
public void ThrowWeapon(Vector3 end, bool hasHit, Vector3 collisionLocation, Vector3 collisionNormal)
|
|
{
|
|
_throwDirection = (end - GlobalPosition).Normalized();
|
|
_plantLocation = collisionLocation;
|
|
_plantNormal = collisionNormal;
|
|
LookAt(end);
|
|
|
|
var tween = _tweenQueueSystem.TweenToLocation(new TweenQueueSystem.TweenInputs(end, StraightThrowDuration));
|
|
if (hasHit)
|
|
tween.Finished += PlantWeaponInWall;
|
|
else
|
|
tween.Finished += ThrowWeaponOnCurve;
|
|
}
|
|
|
|
public void ThrowWeaponOnCurve()
|
|
{
|
|
Freeze = false;
|
|
ApplyImpulse(_throwDirection * ThrowForce);
|
|
}
|
|
|
|
public void PlantWeaponInWall()
|
|
{
|
|
Freeze = true;
|
|
GlobalPosition = _plantLocation;
|
|
LookAt(GlobalTransform.Origin + _plantNormal, Vector3.Up, true);
|
|
}
|
|
|
|
public void ResetWeapon()
|
|
{
|
|
Transform = _startTransform;
|
|
Freeze = true;
|
|
}
|
|
|
|
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
|
|
{
|
|
base._IntegrateForces(state);
|
|
if (!Freeze && state.GetContactCount() > 0)
|
|
{
|
|
_plantLocation = state.GetContactLocalPosition(0);
|
|
_plantNormal = state.GetContactLocalNormal(0);
|
|
}
|
|
}
|
|
|
|
public void OnThrownWeaponReachesGround(Node other)
|
|
{
|
|
PlantWeaponInWall();
|
|
}
|
|
} |