SHADE_Y3/TempScriptsFolder/PlayerController.cs

201 lines
4.9 KiB
C#

using SHADE;
using System;
//in air controls?
public class PlayerController : Script
{
public enum RaccoonStates
{
IDILE,
WALKING,
RUNNING,
INAIR,
FALLING,
HOLDING,
CAUGHT,
TOTAL
}
private RigidBody rb;
private Transform tranform;
private float xAxisMove;
private float zAxisMove;
public float drag = 4.0f;
private bool isMoveKeyPress = false;
public RaccoonStates currentState = RaccoonStates.IDILE;
public float maxMoveVel = 2.0f;
public float moveForce = 200.0f;
public float sprintMultiplier = 1.5f;
private float oldForce;
private float maxOldVel;
private bool sprintIncreaseOnce = false;
public float rotationFactorPerFrame = 1.0f;
public float jumpForce = 500.0f;
/* public float initialJumpForce = 100.0f;
public float maxJumpForce = 500.0f;
public float maxJumpTime = 500.0f;*/
private bool isJumping = false;
private bool isGrounded = false;
public PlayerController(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{
rb = GetComponent<RigidBody>();
if (rb == null)
Debug.LogError("RigidBody is NULL!");
else
rb.Drag = drag;
tranform = GetComponent<Transform>();
if(tranform == null)
Debug.LogError("tranform is NULL!");
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
tranform.LocalRotation = Quaternion.Euler(0.0f,0.0f,0.0f);
}
protected override void update()
{
if (Input.GetKey(Input.KeyCode.G))
{
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
}
MoveKey();
//Rotation();
Move();
Sprint();
Jump();
//Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
}
protected override void fixedUpdate()
{
}
private void MoveKey()
{
if (Input.GetKey(Input.KeyCode.A))
xAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.D))
xAxisMove = 1;
else
xAxisMove = 0;
if (Input.GetKey(Input.KeyCode.W))
zAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.S))
zAxisMove = 1;
else
zAxisMove = 0;
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING)
currentState = RaccoonStates.WALKING;
if (!isMoveKeyPress)
currentState = RaccoonStates.IDILE;
}
private void Move()
{
if (isMoveKeyPress && rb != null)
{
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove));
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel)
{
Vector3 v = rb.LinearVelocity;
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel);
rb.LinearVelocity = v;
}
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel)
{
Vector3 v = rb.LinearVelocity;
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
rb.LinearVelocity = v;
}
}
}
private void Sprint()
{
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress)
{
currentState = RaccoonStates.RUNNING;
if(!sprintIncreaseOnce)
{
sprintIncreaseOnce = true;
oldForce = moveForce;
moveForce = moveForce * sprintMultiplier;
maxOldVel = maxMoveVel;
maxMoveVel = maxMoveVel * sprintMultiplier;
}
}
if (Input.GetKeyUp(Input.KeyCode.LeftShift) && (currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE))
{
if(isMoveKeyPress)
currentState = RaccoonStates.WALKING;
sprintIncreaseOnce = false;
moveForce = oldForce;
maxMoveVel = maxOldVel;
}
}
//press and hold jump
private void Jump()
{
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)
{
if (Input.GetKeyDown(Input.KeyCode.Space) && rb != null)
{
currentState = RaccoonStates.INAIR;
isJumping = true;
rb.AddForce(new Vector3(0.0f, jumpForce, 0.0f));
}
}
//collision check when grounded
}
private void Rotation()
{
Vector3 poitionToLookAt;
poitionToLookAt.x = xAxisMove;
poitionToLookAt.y = 0.0f;
poitionToLookAt.z = zAxisMove;
if (tranform != null)
{
Quaternion currentRotation = tranform.LocalRotation;
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING)
{
Quaternion targetRotation = Quaternion.LookRotation(poitionToLookAt, new Vector3(0.0f, 1.0f, 0.0f));
tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.DeltaTime);
}
}
}
protected override void onCollisionEnter(CollisionInfo info)
{
Debug.Log($"Collision Enter: {info.GameObject.Name}");
}
}