SHADE_Y3/Assets/Scripts/Gameplay/Player/SC_PlayerController.cs

425 lines
12 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
}
2022-11-05 17:44:34 +08:00
public RigidBody rb { get; set; }
public Transform tranform { get; set; }
public Camera cam { get; set; }
2022-11-21 00:12:09 +08:00
public CameraArm camArm { get; set; }
private PickAndThrow pat;
public StateMachine stateMachine;
public bool holdItem { get; set; }
2022-11-21 00:12:09 +08:00
public bool isAiming { get; set; }
2022-11-14 19:05:48 +08:00
2022-11-23 19:11:41 +08:00
[Tooltip("The game object for where the player will respawn to")]
public GameObject respawnPoint;
private float delayTimer = 0.0f;
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-11-23 20:48:40 +08:00
public float maxMoveVel = 3.0f;
2022-11-01 23:28:31 +08:00
[Tooltip("how much force is apply for walking")]
2022-11-23 20:48:40 +08:00
public float moveForce = 50.0f;
2022-11-01 23:28:31 +08:00
[Tooltip("increase the moveForce and maxMoveVel by its amt")]
2022-11-23 20:48:40 +08:00
public float sprintMultiplier = 1.5f;
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 Vector2 axisMove { get; set; }
public bool isMoveKeyPress { get; set; }
2022-10-31 16:45:47 +08:00
2022-11-21 00:12:09 +08:00
[Tooltip("How fast player will turn")]
2022-11-23 20:48:40 +08:00
public float rotationFactorPerFrame = 5.0f;
2022-11-01 17:49:01 +08:00
2022-11-01 23:28:31 +08:00
//Jumping vars==================================================================
[Tooltip("max height of the jump")]
2022-11-23 20:48:40 +08:00
public float maxJumpHeight = 1.0f;
2023-02-18 22:21:23 +08:00
[Tooltip("max amount of time it will take for the jump")]
2022-11-23 20:48:40 +08:00
public float maxJumpTime = 0.5f;
2022-11-01 23:28:31 +08:00
[Tooltip("increase gravity when falling")]
2022-11-23 20:48:40 +08:00
public float fallMultipler = 3.0f;
2022-11-01 23:28:31 +08:00
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;
2023-02-18 22:21:23 +08:00
public bool landedOnJumpPad { get; set; }
[Tooltip("multiply height on Jump Pad ")]
public float jumpPadMultiplayer = 2.0f;
2022-10-30 23:59:35 +08:00
//ItemMultipler==================================================================
2022-11-21 00:12:09 +08:00
[Tooltip("How light item will affect player jump")]
public float lightMultiper = 0.75f;
2022-11-21 00:12:09 +08:00
[Tooltip("How medium item will affect player jump")]
public float mediumMultiper = 0.5f;
2022-11-21 00:12:09 +08:00
[Tooltip("How heavy item will affect player jump")]
public float heavyMultiper = 0.25f;
//silhouette=====================================================================
public GameObject silhouettePlayer;
public Renderable silhouettePlayerRend;
public GameObject silhouetteBag;
public Renderable silhouetteBagRend;
2022-10-30 23:59:35 +08:00
protected override void awake()
{
//default setup
isMoveKeyPress = false;
holdItem = false;
2022-11-21 00:12:09 +08:00
isAiming = false;
2023-02-18 22:21:23 +08:00
landedOnJumpPad = 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)
2022-10-30 23:59:35 +08:00
Debug.LogError("RigidBody is NULL!");
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)
2022-11-01 01:31:13 +08:00
Debug.LogError("tranform is NULL!");
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(PlayerIdleState), new PlayerIdleState(stateMachine));
dictionary.Add(typeof(PlayerWalkState), new PlayerWalkState(stateMachine));
dictionary.Add(typeof(PlayerRunState), new PlayerRunState(stateMachine));
dictionary.Add(typeof(PlayerJumpState), new PlayerJumpState(stateMachine));
dictionary.Add(typeof(PlayerFallState), new PlayerFallState(stateMachine));
dictionary.Add(typeof(PlayerLandState), new PlayerLandState(stateMachine));
dictionary.Add(typeof(PlayerCaughtState), new PlayerCaughtState(stateMachine));
2022-11-14 19:05:48 +08:00
stateMachine.InitStateMachine(dictionary);
2022-11-13 21:56:28 +08:00
if (!silhouettePlayer)
Debug.LogError("silhouettePlayer EMPTY");
else
{
silhouettePlayerRend = silhouettePlayer.GetComponent<Renderable>();
silhouettePlayerRend.Material.SetProperty<float>("data.offset", 0.1f);
}
if (!silhouetteBag)
Debug.LogError("silhouetteBag EMPTY");
else
{
silhouetteBagRend = silhouetteBag.GetComponent<Renderable>();
silhouetteBagRend.Material.SetProperty<float>("data.offset", 0.1f);
}
2022-10-30 23:59:35 +08:00
}
protected override void lateUpdate()
{
}
2023-02-21 19:31:50 +08:00
2022-10-30 23:59:35 +08:00
protected override void update()
{
2023-03-02 17:33:02 +08:00
if (GameManager.Instance.GamePause || !GameManager.Instance.stealFoodPopUpDone)
2023-02-21 19:31:50 +08:00
{
return;
}
if (silhouettePlayerRend && silhouetteBagRend)
{
Vector3 dis = Camera.GetMainCamera().Position - GameObject.GetComponent<Transform>().LocalPosition;
float disSqr = dis.GetSqrMagnitude();
float ratio = System.Math.Clamp(1 - (disSqr / (1 + disSqr)), 0, 1.0f);
float temp = (1 - ratio) * 0.00075f;
if (temp <= 0.0006f)
temp = 0.1f;
silhouettePlayerRend.Material.SetProperty<float>("data.offset", temp);
silhouetteBagRend.Material.SetProperty<float>("data.offset", temp);
}
//PickAndThrow check
if (!pat)
{
pat = GetScript<PickAndThrow>();
if(!pat)
Debug.LogError("PickAndThrow is NULL!");
}
2022-11-21 00:12:09 +08:00
if (!cam)
2023-03-01 01:16:34 +08:00
{
cam = GetComponentInChildren<Camera>();
2023-03-01 01:16:34 +08:00
if(pat)
cam.FOV = pat.defaultFOV;
}
2022-11-21 00:12:09 +08:00
if(!camArm)
camArm = GetComponentInChildren<CameraArm>();
2023-02-21 19:31:50 +08:00
GotCaught();
2022-11-21 00:12:09 +08:00
Rotation();
MoveKey();
Sprint();
Jump();
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());
2023-02-21 19:31:50 +08:00
2022-11-01 13:24:14 +08:00
}
protected override void fixedUpdate()
{
2023-03-02 17:33:02 +08:00
if (GameManager.Instance.GamePause || !GameManager.Instance.stealFoodPopUpDone)
2023-02-21 19:31:50 +08:00
{
return;
}
2022-11-01 17:49:01 +08:00
Move();
Gravity();
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(PlayerWalkState)))
stateMachine.SetState(typeof(PlayerWalkState));
}
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(PlayerIdleState)))
stateMachine.SetState(typeof(PlayerIdleState));
}
2022-11-01 01:31:13 +08:00
}
private void Move()
{
2023-02-21 19:31:50 +08:00
if (rb != null && currentState != RaccoonStates.CAUGHT)
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-21 00:12:09 +08:00
if (isMoveKeyPress && rb)
2022-10-31 16:45:47 +08:00
{
2022-11-21 00:12:09 +08:00
Vector3 velNor = rb.LinearVelocity;
velNor.y = 0.0f;
if (velNor.GetMagnitude() > maxMoveVel)
2022-11-01 17:49:01 +08:00
{
2022-11-21 00:12:09 +08:00
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;
if (stateMachine && !stateMachine.IsState(typeof(PlayerRunState)))
stateMachine.SetState(typeof(PlayerRunState));
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;
if(stateMachine && !stateMachine.IsState(typeof(PlayerWalkState)))
stateMachine.SetState(typeof(PlayerWalkState));
}
2022-11-01 13:24:14 +08:00
sprintIncreaseOnce = false;
moveForce = oldForce;
maxMoveVel = maxOldVel;
2022-10-31 16:45:47 +08:00
}
}
private void Jump()
{
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDLE)
2022-10-31 16:45:47 +08:00
{
2023-02-18 22:21:23 +08:00
if ( (Input.GetKeyDown(Input.KeyCode.Space) || landedOnJumpPad ) && 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;
}
2023-02-18 22:21:23 +08:00
if (landedOnJumpPad)
{
v.y *= jumpPadMultiplayer;
landedOnJumpPad = false;
}
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()
{
2023-03-02 00:02:18 +08:00
tranform.LocalEulerAngles = new Vector3(0.0f, tranform.LocalEulerAngles.y, 0.0f);
2022-11-21 00:12:09 +08:00
if (isMoveKeyPress && tranform && !isAiming)
2022-11-01 01:31:13 +08:00
{
Quaternion currentRotation = tranform.LocalRotation;
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(axisMove.x, 0.0f, axisMove.y), Vector3.Up);
tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.FixedDeltaTime);
2022-11-21 00:12:09 +08:00
}
else if (camArm && tranform && isAiming)
{
Quaternion currentRotation = tranform.LocalRotation;
Quaternion targetRotation = Quaternion.Euler(0.0f, SHADE.Math.DegreesToRadians(camArm.Yaw + 180.0f), 0.0f);
tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.FixedDeltaTime);
2022-11-01 01:31:13 +08:00
}
2023-03-02 00:02:18 +08:00
2022-11-01 01:31:13 +08:00
}
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.FixedDeltaTime);
2022-11-01 23:28:31 +08:00
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
else
{
float prevYVel = v.y;
float newYVel = v.y + (gravity * (float)Time.FixedDeltaTime);
2022-11-01 23:28:31 +08:00
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 && respawnPoint)
{
currentState = RaccoonStates.IDLE;
if (stateMachine && !stateMachine.IsState(typeof(PlayerIdleState)))
stateMachine.SetState(typeof(PlayerIdleState));
tranform.LocalPosition = respawnPoint.GetComponent<Transform>().LocalPosition;
if (pat && pat.item)
{
holdItem = false;
isAiming = false;
pat.item.GetScript<Item>().returnBack = true;
}
}
}
2022-11-01 13:24:14 +08:00
protected override void onCollisionEnter(CollisionInfo info)
{
}
}
2022-11-01 13:24:14 +08:00