SHADE_Y3/Assets/Scripts/SC_PlayerController.cs

441 lines
11 KiB
C#
Raw Normal View History

2022-10-30 23:59:35 +08:00
using SHADE;
using System;
using System.Collections.Generic;
using static Item;
2022-10-30 23:59:35 +08:00
public class PlayerController : Script
{
2022-10-31 16:45:47 +08:00
public enum RaccoonStates
{
IDLE,
2022-10-31 16:45:47 +08:00
WALKING,
RUNNING,
2022-11-01 23:28:31 +08:00
JUMP,
2022-10-31 16:45:47 +08:00
FALLING,
LANDED,
2022-10-31 16:45:47 +08:00
CAUGHT,
TOTAL
}
public enum WalkingState
{
CARRY,
AIMING,
THROW,
WALK,
TOTAL
}
2022-11-05 17:44:34 +08:00
public RigidBody rb { get; set; }
2022-11-01 01:31:13 +08:00
private Transform tranform;
private Camera cam;
private PickAndThrow pat;
2022-11-13 21:56:28 +08:00
private StateMachine stateMachine;
public bool holdItem { get; set; }
2022-11-14 19:05:48 +08:00
2022-11-01 23:28:31 +08:00
[Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDLE;
2022-10-31 16:45:47 +08:00
2022-11-01 23:28:31 +08:00
//Movement variables============================================================
[Tooltip("Max vel for walking")]
2022-10-31 16:45:47 +08:00
public float maxMoveVel = 2.0f;
2022-11-01 23:28:31 +08:00
[Tooltip("how much force is apply for walking")]
public float moveForce = 2.0f;
2022-11-01 23:28:31 +08:00
[Tooltip("increase the moveForce and maxMoveVel by its amt")]
2022-11-01 17:49:01 +08:00
public float sprintMultiplier = 2.0f;
2022-11-01 23:28:31 +08:00
2022-11-01 13:24:14 +08:00
private float oldForce;
private float maxOldVel;
private bool sprintIncreaseOnce = false;
public float xAxisMove { get; set; }
public float zAxisMove { get; set; }
public Vector2 axisMove { get; set; }
public bool isMoveKeyPress { get; set; }
2022-10-31 16:45:47 +08:00
2022-11-01 23:28:31 +08:00
[Tooltip("curr not working")]
public float rotationFactorPerFrame = 1.0f;
2022-11-01 17:49:01 +08:00
2022-11-01 23:28:31 +08:00
//Jumping vars==================================================================
[SerializeField]
[Tooltip("max height of the jump")]
public float maxJumpHeight = 4.0f;
[SerializeField]
[Tooltip("max amt of time it will take for the jump")]
public float maxJumpTime = 0.75f;
[SerializeField]
[Tooltip("increase gravity when falling")]
public float fallMultipler = 2.0f;
private float initialJumpVel;
private bool isGrounded = true;
2022-11-01 17:49:01 +08:00
private float gravity = -9.8f;
private float groundGravity = -0.5f;
2022-10-30 23:59:35 +08:00
//ItemMultipler==================================================================
public float lightMultiper = 0.75f;
public float mediumMultiper = 0.5f;
public float heavyMultiper = 0.25f;
2022-10-30 23:59:35 +08:00
protected override void awake()
{
Audio.PlayBGMOnce2D("event:/Music/player_undetected");
//default setup
isMoveKeyPress = false;
holdItem = false;
//Jump setup
float timeToApex = maxJumpTime / 2;
gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2);
initialJumpVel = (2 * maxJumpHeight) / timeToApex;
2022-11-01 17:49:01 +08:00
//rigidbody check
2022-10-30 23:59:35 +08:00
rb = GetComponent<RigidBody>();
if (rb == null)
Debug.LogError("RigidBody is NULL!");
2022-11-01 13:24:14 +08:00
else
{
rb.IsGravityEnabled = false;
rb.Interpolating = false;
}
2022-11-01 01:31:13 +08:00
2022-11-01 17:49:01 +08:00
//Transform check
2022-11-01 01:31:13 +08:00
tranform = GetComponent<Transform>();
if(tranform == null)
Debug.LogError("tranform is NULL!");
2022-11-13 21:56:28 +08:00
//PickAndThrow check
pat = GetScript<PickAndThrow>();
if (pat == null)
Debug.LogError("PickAndThrow is NULL!");
2022-11-01 17:49:01 +08:00
2022-11-14 19:05:48 +08:00
stateMachine = AddScript<StateMachine>();
2022-11-13 21:56:28 +08:00
Dictionary<Type, BaseState> dictionary = new Dictionary<Type, BaseState>();
dictionary.Add(typeof(IdleState), new IdleState(stateMachine));
dictionary.Add(typeof(WalkState), new WalkState(stateMachine));
dictionary.Add(typeof(RunState), new RunState(stateMachine));
2022-11-14 19:05:48 +08:00
stateMachine.InitStateMachine(dictionary);
2022-11-13 21:56:28 +08:00
2022-10-30 23:59:35 +08:00
}
protected override void update()
{
if (cam == null)
cam = GetComponentInChildren<Camera>();
GotCaught();
2022-11-14 19:05:48 +08:00
//Debug.Log($"{currentState}");
//Debug.Log($" axisX: {axisMove.x} axisY:{axisMove.y}");
2022-11-16 22:28:08 +08:00
//Debug.Log($"X: {rb.LinearVelocity.x}" + $" Z: {rb.LinearVelocity.z}");
2022-11-01 23:28:31 +08:00
//Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
2022-11-01 13:24:14 +08:00
}
protected override void fixedUpdate()
{
2022-11-01 17:49:01 +08:00
//Rotation();
2022-11-14 19:05:48 +08:00
2022-11-13 21:56:28 +08:00
MoveKey();
2022-11-01 17:49:01 +08:00
Move();
Sprint();
Jump();
Gravity();
//Debug.Log($"X: {rb.LinearVelocity.x}" + $" Z: {rb.LinearVelocity.z}");
2022-10-30 23:59:35 +08:00
}
2022-11-01 13:24:14 +08:00
2022-11-01 01:31:13 +08:00
private void MoveKey()
2022-10-30 23:59:35 +08:00
{
axisMove = Vector2.Zero;
2022-10-30 23:59:35 +08:00
if (Input.GetKey(Input.KeyCode.W))
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
axisMove += new Vector2(camerAixs.x, camerAixs.z);
}
2022-11-14 19:05:48 +08:00
if (Input.GetKey(Input.KeyCode.S))
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
axisMove -= new Vector2(camerAixs.x, camerAixs.z);
}
2022-11-14 19:05:48 +08:00
if (Input.GetKey(Input.KeyCode.A))
{
Vector3 camerAixs = cam.GetRight();
camerAixs.y = 0;
camerAixs.Normalise();
axisMove -= new Vector2(camerAixs.x, camerAixs.z);
}
2022-11-14 19:05:48 +08:00
if (Input.GetKey(Input.KeyCode.D))
{
Vector3 camerAixs = cam.GetRight();
camerAixs.y = 0;
camerAixs.Normalise();
axisMove += new Vector2(camerAixs.x, camerAixs.z);
}
2022-11-14 19:05:48 +08:00
axisMove.Normalise();
isMoveKeyPress = axisMove.x != 0 || axisMove.y != 0;
2022-11-01 01:31:13 +08:00
2022-11-13 21:56:28 +08:00
if (isMoveKeyPress && isGrounded && !Input.GetKey(Input.KeyCode.LeftShift))
{
2022-10-31 16:45:47 +08:00
currentState = RaccoonStates.WALKING;
2022-11-13 21:56:28 +08:00
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState));
}
2022-10-31 16:45:47 +08:00
2022-11-01 23:28:31 +08:00
if (!isMoveKeyPress && isGrounded)
{
currentState = RaccoonStates.IDLE;
2022-11-13 21:56:28 +08:00
if(stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState));
}
2022-11-01 01:31:13 +08:00
}
private void Move()
{
2022-11-01 17:49:01 +08:00
if (rb != null)
2022-10-31 16:45:47 +08:00
{
2022-11-16 22:28:08 +08:00
rb.LinearVelocity += new Vector3(axisMove.x * moveForce, 0.0f, axisMove.y * moveForce) * Time.DeltaTimeF;
2022-11-01 17:49:01 +08:00
if (isMoveKeyPress)
2022-10-31 16:45:47 +08:00
{
if (rb)
2022-11-01 17:49:01 +08:00
{
Vector3 velNor = rb.LinearVelocity;
velNor.y = 0.0f;
if (velNor.GetMagnitude() > maxMoveVel)
{
velNor.Normalise();
velNor *= maxMoveVel;
rb.LinearVelocity = new Vector3(velNor.x, rb.LinearVelocity.y, velNor.z);
}
2022-11-01 17:49:01 +08:00
}
2022-10-31 16:45:47 +08:00
}
}
}
private void Sprint()
{
2022-11-01 23:28:31 +08:00
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
2022-10-31 16:45:47 +08:00
{
2022-11-01 01:31:13 +08:00
currentState = RaccoonStates.RUNNING;
2022-11-13 21:56:28 +08:00
if (stateMachine && !stateMachine.IsState(typeof(RunState)))
stateMachine.SetState(typeof(RunState));
2022-11-14 19:05:48 +08:00
holdItem = false;
if (!sprintIncreaseOnce)
2022-11-01 13:24:14 +08:00
{
sprintIncreaseOnce = true;
oldForce = moveForce;
2022-11-01 17:49:01 +08:00
moveForce *= sprintMultiplier;
2022-11-01 13:24:14 +08:00
maxOldVel = maxMoveVel;
2022-11-01 17:49:01 +08:00
maxMoveVel *= sprintMultiplier;
2022-11-01 13:24:14 +08:00
}
2022-10-31 16:45:47 +08:00
}
2022-11-01 13:24:14 +08:00
2022-11-01 23:28:31 +08:00
if (Input.GetKeyUp(Input.KeyCode.LeftShift))
2022-10-31 16:45:47 +08:00
{
if (isMoveKeyPress)
{
2022-11-01 13:24:14 +08:00
currentState = RaccoonStates.WALKING;
2022-11-13 21:56:28 +08:00
if(stateMachine && !stateMachine.IsState(typeof(WalkState)))
stateMachine.SetState(typeof(WalkState));
}
2022-11-01 13:24:14 +08:00
sprintIncreaseOnce = false;
moveForce = oldForce;
maxMoveVel = maxOldVel;
2022-10-31 16:45:47 +08:00
}
}
//press and hold jump
private void Jump()
{
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDLE)
2022-10-31 16:45:47 +08:00
{
2022-11-01 23:28:31 +08:00
if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null)
2022-10-31 16:45:47 +08:00
{
2022-11-01 23:28:31 +08:00
currentState = RaccoonStates.JUMP;
2022-11-01 17:49:01 +08:00
Vector3 v = rb.LinearVelocity;
2022-11-01 23:28:31 +08:00
v.y = initialJumpVel * 0.5f;
2022-11-13 21:56:28 +08:00
if (holdItem && pat != null && pat.item.GetScript<Item>() != null)
{
Item item = pat.item.GetScript<Item>();
2022-11-13 21:56:28 +08:00
if (item != null && item.currCategory == ItemCategory.LIGHT)
v.y *= lightMultiper;
2022-11-13 21:56:28 +08:00
if (item != null && item.currCategory == ItemCategory.MEDIUM)
v.y *= mediumMultiper;
2022-11-13 21:56:28 +08:00
if (item != null && item.currCategory == ItemCategory.HEAVY)
v.y *= heavyMultiper;
}
2022-11-01 17:49:01 +08:00
rb.LinearVelocity = v;
2022-10-31 16:45:47 +08:00
}
}
2022-11-14 19:05:48 +08:00
if(!isGrounded && rb != null && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space)))
2022-11-01 23:28:31 +08:00
currentState = RaccoonStates.FALLING;
2022-11-14 19:05:48 +08:00
2022-10-30 23:59:35 +08:00
}
2022-10-31 16:45:47 +08:00
2022-11-01 01:31:13 +08:00
private void Rotation()
{
Vector3 poitionToLookAt;
poitionToLookAt.x = axisMove.x;
2022-11-01 01:31:13 +08:00
poitionToLookAt.y = 0.0f;
poitionToLookAt.z = axisMove.y;
2022-11-01 01:31:13 +08:00
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);
}
}
}
2022-11-01 17:49:01 +08:00
private void Gravity()
{
if (rb != null)
{
//check player vel.y if its close to zero its on the ground
2022-11-01 23:28:31 +08:00
if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f))
2022-11-14 19:05:48 +08:00
{
2022-11-01 17:49:01 +08:00
isGrounded = true;
2022-11-14 19:05:48 +08:00
if (currentState == RaccoonStates.FALLING)
currentState = RaccoonStates.LANDED;
}
2022-11-01 17:49:01 +08:00
else
isGrounded = false;
Vector3 v = rb.LinearVelocity;
if (isGrounded)
v.y = groundGravity;
2022-11-01 23:28:31 +08:00
else if (currentState == RaccoonStates.FALLING)
{
float prevYVel = v.y;
float newYVel = v.y + (gravity * fallMultipler * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
else
{
float prevYVel = v.y;
float newYVel = v.y + (gravity * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
2022-11-01 17:49:01 +08:00
rb.LinearVelocity = v;
}
}
private void GotCaught()
{
if (currentState == RaccoonStates.CAUGHT && tranform != null)
{
currentState = RaccoonStates.IDLE;
2022-11-13 21:56:28 +08:00
if (stateMachine && !stateMachine.IsState(typeof(IdleState)))
stateMachine.SetState(typeof(IdleState));
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
}
}
2022-11-01 13:24:14 +08:00
protected override void onCollisionEnter(CollisionInfo info)
{
}
}
2022-11-01 13:24:14 +08:00
public class WalkState : BaseState
{
public WalkState(StateMachine stateMachine) : base(stateMachine)
{
2022-11-13 21:56:28 +08:00
stateName = "Walk State";
}
public override void OnEnter()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("WALK ENTER");
}
2022-11-13 21:56:28 +08:00
public override void update()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("WALKING");
}
2022-11-13 21:56:28 +08:00
public override void fixedUpdate()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("FIXED WALKING");
2022-11-13 21:56:28 +08:00
}
public override void OnExit()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("WALK EXIT");
}
2022-11-13 21:56:28 +08:00
public override void onTriggerEnter(CollisionInfo info)
{
2022-11-14 19:05:48 +08:00
//Debug.Log("TRIGGER");
2022-11-13 21:56:28 +08:00
}
}
public class RunState : BaseState
{
public RunState(StateMachine stateMachine) : base(stateMachine)
{
stateName = "Run State";
}
public override void OnEnter()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("Run ENTER");
}
2022-11-13 21:56:28 +08:00
public override void update()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("RUNING");
2022-11-13 21:56:28 +08:00
}
public override void fixedUpdate()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("FIXED RUNNING");
}
public override void OnExit()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("Run EXIT");
}
}
public class IdleState : BaseState
{
public IdleState(StateMachine stateMachine) : base(stateMachine)
{
2022-11-13 21:56:28 +08:00
stateName = "Idle State";
}
public override void OnEnter()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("IDLE ENTER");
}
2022-11-13 21:56:28 +08:00
public override void update()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("IDLING");
}
2022-11-13 21:56:28 +08:00
public override void fixedUpdate()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("FIXED IDLING");
2022-11-13 21:56:28 +08:00
}
public override void OnExit()
{
2022-11-14 19:05:48 +08:00
//Debug.Log("IDLE EXIT");
}
2022-10-30 23:59:35 +08:00
}