183 lines
6.3 KiB
C#
183 lines
6.3 KiB
C#
using FlaxEngine;
|
|
using Cabrito;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Game
|
|
{
|
|
public class PlayerMovement : Script
|
|
{
|
|
[Limit(0, 9000), Tooltip("Base Movement speed")]
|
|
public float MoveSpeed { get; set; } = 320;
|
|
|
|
private float viewPitch;
|
|
private float viewYaw;
|
|
private float viewRoll;
|
|
|
|
private InputEvent onExit = new InputEvent("Exit");
|
|
|
|
Actor rootActor;
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
|
|
onExit.Triggered += () =>
|
|
{
|
|
if (Console.IsSafeToQuit)
|
|
Engine.RequestExit();
|
|
};
|
|
|
|
rootActor = Actor.GetChild(0);
|
|
}
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
base.OnDestroy();
|
|
|
|
onExit.Dispose();
|
|
}
|
|
|
|
public override void OnStart()
|
|
{
|
|
var initialEulerAngles = Actor.Orientation.EulerAngles;
|
|
viewPitch = initialEulerAngles.X;
|
|
viewYaw = initialEulerAngles.Y;
|
|
viewRoll = initialEulerAngles.Z;
|
|
}
|
|
|
|
//Vector3 wishVelocity = Vector3.Zero;
|
|
Vector3 gravityVelocity = Vector3.Zero;
|
|
public override void OnFixedUpdate()
|
|
{
|
|
var camera = rootActor.GetChild<Camera>();
|
|
var camTrans = camera.Transform;
|
|
var rootTrans = rootActor.Transform;
|
|
|
|
float inputH = InputManager.GetAxis("Horizontal");
|
|
float inputV = InputManager.GetAxis("Vertical");
|
|
var move = new Vector3(inputH, 0.0f, inputV);
|
|
|
|
var rigidBody = Actor.As<RigidBody>();
|
|
|
|
const float groundAccel = 30 * 100f;
|
|
//const float groundDamping = 30f * 100;
|
|
var moveDirection = rootTrans.TransformDirection(move);
|
|
|
|
Vector3 wishVelocity = Vector3.Zero;
|
|
if (!Console.IsOpen && !move.IsZero)
|
|
{
|
|
move = moveDirection.Normalized * MoveSpeed * Time.DeltaTime / (1.0f/Time.PhysicsFPS);
|
|
wishVelocity = move;
|
|
|
|
/*var groundVelocity = rigidBody.LinearVelocity;
|
|
groundVelocity.Y = 0f;
|
|
if (groundVelocity.Length > 320)
|
|
wishVelocity = wishVelocity.Normalized * 320;*/
|
|
//wishVelocity = move;
|
|
}
|
|
/*else if (!wishVelocity.IsZero)
|
|
{
|
|
moveDirection = wishVelocity.Normalized;
|
|
wishVelocity -= moveDirection * groundDamping * Time.DeltaTime;
|
|
if (Vector3.Dot(wishVelocity, moveDirection) < 0)
|
|
wishVelocity = Vector3.Zero;
|
|
}*/
|
|
//else if (!wishVelocity.IsZero)
|
|
// wishVelocity = new Vector3(0);
|
|
|
|
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
|
|
|
|
Vector3 velocity = wishVelocity;
|
|
|
|
bool collided = Physics.BoxCastAll(rigidBody.Position, new Vector3(capsuleCollider.Radius, (capsuleCollider.Height + capsuleCollider.Radius) / 2, capsuleCollider.Radius), Physics.Gravity.Normalized, out RayCastHit[] hitInfos, Quaternion.Identity, Physics.Gravity.Length * Time.DeltaTime, uint.MaxValue, false);
|
|
|
|
const float minDistanceFromFloor = 0.1f;
|
|
|
|
Vector3 floorNormal = Vector3.Zero;
|
|
Vector3 floorPosition = Vector3.Zero;
|
|
float floorDistance = 0.0f;
|
|
if (collided)
|
|
{
|
|
collided = false;
|
|
foreach (var hitInfo in hitInfos)
|
|
{
|
|
if (hitInfo.Collider == capsuleCollider)
|
|
continue;
|
|
|
|
//if (hitInfo.Distance < minDistanceFromFloor)
|
|
// continue;
|
|
|
|
collided = true;
|
|
floorNormal = hitInfo.Normal;
|
|
floorPosition = hitInfo.Point;
|
|
floorDistance = hitInfo.Distance;
|
|
//Console.Print("collided " + hitInfo.Distance.ToString());
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!collided)
|
|
gravityVelocity += Physics.Gravity * Time.DeltaTime;
|
|
else
|
|
{
|
|
gravityVelocity = Vector3.Zero;
|
|
|
|
// if (floorDistance < minDistanceFromFloor)
|
|
{
|
|
var pos = rigidBody.Position;
|
|
pos.Y = floorPosition.Y + (capsuleCollider.Height * 0.5f + capsuleCollider.Radius) + (minDistanceFromFloor * 0.5f);
|
|
rigidBody.Position = pos;
|
|
}
|
|
|
|
/*if (floorDistance < minDistanceFromFloor)
|
|
{
|
|
Console.Print("grav ");
|
|
rigidBody.Position = new Vector3(rigidBody.Position.X, (floorPosition + (floorNormal * minDistanceFromFloor*0.9f)).Y, rigidBody.Position.Z);
|
|
}*/
|
|
//rigidBody.Position = floorPosition + (floorNormal * minDistanceFromFloor);
|
|
}
|
|
|
|
Vector3 newVelocity = velocity + gravityVelocity;
|
|
|
|
if (newVelocity != rigidBody.LinearVelocity)
|
|
rigidBody.LinearVelocity = newVelocity;
|
|
|
|
if (rigidBody.IsKinematic && !rigidBody.LinearVelocity.IsZero)
|
|
rigidBody.Position += rigidBody.LinearVelocity * Time.DeltaTime;
|
|
/*
|
|
- check gravity
|
|
- get normal for slope
|
|
|
|
moveVelocity = wishVelocity
|
|
|
|
|
|
LinearVelocity = moveVelocity + _jumpVelocity + _gravityVelocity
|
|
*/
|
|
|
|
}
|
|
|
|
public override void OnUpdate()
|
|
{
|
|
float xAxis = InputManager.GetAxis("Mouse X");
|
|
float yAxis = InputManager.GetAxis("Mouse Y");
|
|
if (xAxis != 0.0f || yAxis != 0.0f)
|
|
{
|
|
var camera = rootActor.GetChild<Camera>();
|
|
var camTrans = camera.Transform;
|
|
var rootTrans = rootActor.Transform;
|
|
|
|
viewPitch += yAxis;
|
|
viewYaw += xAxis;
|
|
|
|
viewPitch = Mathf.Clamp(viewPitch, -90.0f, 90.0f);
|
|
|
|
camTrans.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll);
|
|
rootTrans.Orientation = Quaternion.Euler(0, viewYaw, 0);
|
|
|
|
camera.Transform = camTrans;
|
|
rootActor.Transform = rootTrans;
|
|
}
|
|
}
|
|
}
|
|
}
|