reorganizing stuff
This commit is contained in:
61
components/movement/CFlyingMovement.cs
Normal file
61
components/movement/CFlyingMovement.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Godot;
|
||||
using Movementtests.interfaces;
|
||||
|
||||
namespace Movementtests.scenes.movement;
|
||||
|
||||
public partial class CFlyingMovement : Node3D, IMoveable
|
||||
{
|
||||
[Export] public RMovement RMovement { get; set; }
|
||||
|
||||
[Export(PropertyHint.Layers3DPhysics)]
|
||||
public uint TerrainCollision { get; set; } = 1;
|
||||
|
||||
private bool _movingToDesiredHeight = true;
|
||||
private Vector3 _randomDirection;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
|
||||
}
|
||||
|
||||
public Vector3 ComputeVelocity(MovementInputs inputs)
|
||||
{
|
||||
var velocity = inputs.Velocity;
|
||||
var spaceState = GetWorld3D().DirectSpaceState;
|
||||
var target = inputs.TargetLocation;
|
||||
var direction = (target - GlobalPosition).Normalized();
|
||||
// Check if we have a direct line of sight to the player
|
||||
if (!_movingToDesiredHeight)
|
||||
{
|
||||
velocity = velocity.Lerp(direction * RMovement.Speed, (float) inputs.delta * RMovement.Acceleration);
|
||||
|
||||
var query = PhysicsRayQueryParameters3D.Create(GlobalPosition, target, TerrainCollision);
|
||||
var result = spaceState.IntersectRay(query);
|
||||
if (result.Count > 0)
|
||||
{
|
||||
_movingToDesiredHeight = true;
|
||||
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity = velocity.Lerp(_randomDirection * RMovement.Speed, (float) inputs.delta * RMovement.Acceleration);
|
||||
|
||||
var groundQuery = PhysicsRayQueryParameters3D.Create(GlobalPosition, GlobalPosition+Vector3.Down*RMovement.TargetHeight, TerrainCollision);
|
||||
var groundResult = spaceState.IntersectRay(groundQuery);
|
||||
if (groundResult.Count == 0)
|
||||
{
|
||||
velocity.Y = 0;
|
||||
|
||||
var query = PhysicsRayQueryParameters3D.Create(GlobalPosition, target, TerrainCollision);
|
||||
var result = spaceState.IntersectRay(query);
|
||||
if (result.Count == 0)
|
||||
{
|
||||
_movingToDesiredHeight = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return velocity;
|
||||
}
|
||||
}
|
||||
1
components/movement/CFlyingMovement.cs.uid
Normal file
1
components/movement/CFlyingMovement.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cps1rbkxs3nvq
|
||||
7
components/movement/CFlyingMovement.tscn
Normal file
7
components/movement/CFlyingMovement.tscn
Normal file
@@ -0,0 +1,7 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dmw5ibwrb483f"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cps1rbkxs3nvq" path="res://components/movement/CFlyingMovement.cs" id="1_i26q2"]
|
||||
|
||||
[node name="CFlyingMovement" type="Node3D"]
|
||||
script = ExtResource("1_i26q2")
|
||||
CollisionMask = 256
|
||||
34
components/movement/CGroundedMovement.cs
Normal file
34
components/movement/CGroundedMovement.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Godot;
|
||||
using Movementtests.interfaces;
|
||||
|
||||
namespace Movementtests.scenes.movement;
|
||||
|
||||
public partial class CGroundedMovement : Node3D, IMoveable
|
||||
{
|
||||
[Export] public RMovement RMovement { get; set; }
|
||||
|
||||
[Export]
|
||||
public RayCast3D WallInFrontRayCast { get; set; }
|
||||
|
||||
|
||||
public Vector3 ComputeVelocity(MovementInputs inputs)
|
||||
{
|
||||
var velocity = inputs.Velocity;
|
||||
var target = inputs.TargetLocation;
|
||||
var direction = (target - GlobalPosition).Normalized();
|
||||
|
||||
var targetPlane = new Vector3(target.X, GlobalPosition.Y, target.Z);
|
||||
LookAt(targetPlane);
|
||||
float xAcc = (float) Mathf.Lerp(velocity.X, direction.X * RMovement.Speed, inputs.delta * RMovement.Acceleration);
|
||||
float zAcc = (float) Mathf.Lerp(velocity.Z, direction.Z * RMovement.Speed, inputs.delta * RMovement.Acceleration);
|
||||
velocity.X = xAcc;
|
||||
velocity.Z = zAcc;
|
||||
|
||||
if (WallInFrontRayCast.IsColliding())
|
||||
velocity.Y = RMovement.Speed;
|
||||
else if (!inputs.isOnFloor)
|
||||
velocity += inputs.gravity * (float)inputs.delta * RMovement.GravityModifier;
|
||||
|
||||
return velocity;
|
||||
}
|
||||
}
|
||||
1
components/movement/CGroundedMovement.cs.uid
Normal file
1
components/movement/CGroundedMovement.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bdag2eeixw2lt
|
||||
6
components/movement/CGroundedMovement.tscn
Normal file
6
components/movement/CGroundedMovement.tscn
Normal file
@@ -0,0 +1,6 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dbr7ioio158ew"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bdag2eeixw2lt" path="res://components/movement/CGroundedMovement.cs" id="1_e0agf"]
|
||||
|
||||
[node name="CGroundedMovement" type="Node3D"]
|
||||
script = ExtResource("1_e0agf")
|
||||
31
components/movement/RMovement.cs
Normal file
31
components/movement/RMovement.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class RMovement : Resource
|
||||
{
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float Speed { get; set;}
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float Acceleration { get; set;}
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float GravityModifier { get; set;}
|
||||
|
||||
[Export(PropertyHint.Range, "0,20,1,or_greater")]
|
||||
public float TargetHeight { get; set;}
|
||||
|
||||
public RMovement()
|
||||
{
|
||||
Speed = 3.0f;
|
||||
Acceleration = 1.0f;
|
||||
GravityModifier = 1.0f;
|
||||
TargetHeight = 10.0f;
|
||||
}
|
||||
public RMovement(float speed, float acceleration, float gravityModifier, float targetHeight)
|
||||
{
|
||||
Speed = speed;
|
||||
Acceleration = acceleration;
|
||||
GravityModifier = gravityModifier;
|
||||
TargetHeight = targetHeight;
|
||||
}
|
||||
}
|
||||
1
components/movement/RMovement.cs.uid
Normal file
1
components/movement/RMovement.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dtpxijlnb2c5
|
||||
Reference in New Issue
Block a user