GlobalGameJam-2021/Assets/Scripts/PlayerManager.cs

439 lines
14 KiB
C#
Raw Normal View History

2021-01-29 19:55:53 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-01-30 19:04:43 +00:00
using UnityEngine.SceneManagement;
2021-01-29 19:55:53 +00:00
public class PlayerManager : MonoBehaviour
{
2021-01-30 12:51:35 +00:00
public float speed = 8;
public float runSpeed = 20;
2021-01-29 19:55:53 +00:00
public float mouseSensitive = 100;
2021-01-30 12:51:35 +00:00
public float jump = 5;
public float health = 100;
public float actualHealth;
private float[] actualPowerTimes = new float[] { 0f, 0f, 0f };
public bool onGround = true;
2021-01-29 19:55:53 +00:00
private bool run = false;
2021-01-30 12:51:35 +00:00
private Animator playerAnimator;
2021-01-29 19:55:53 +00:00
private Rigidbody rigidBody;
2021-01-30 12:51:35 +00:00
private PowerCubeManager powerCubeManager;
2021-01-30 19:56:04 +00:00
private bool interact = false;
2021-01-30 12:51:35 +00:00
2021-01-30 13:55:08 +00:00
public List<int> activeAbility = new List<int>(); //without ability=0 or null, dubleJump = 1, push/pull = 2, dash = 3
public List<GameObject> PowerPrefabs = new List<GameObject>(); //dubleJump = 0, push/pull = 1, dash = 2
2021-01-30 13:55:08 +00:00
private bool dubleJump = true;
2021-01-30 15:16:42 +00:00
private GameObject pushPullObject;
2021-01-30 15:40:42 +00:00
public float dashPower = 40f;
public float dashTime = 0.2f;
private float actualDashTime;
private int dashButton;
2021-01-30 15:49:08 +00:00
private bool dash = false;
2021-01-30 13:55:08 +00:00
2021-01-30 12:51:35 +00:00
private bool startEating = false;
private float saveSpeed;
private float saveRunSpeed;
private float saveJump;
private Vector3 saveSize;
2021-01-30 23:44:40 +00:00
private Vector3 saveNewSize;
2021-01-30 12:51:35 +00:00
private Vector3 savePosition;
private Vector3 saveCameraPosition;
2021-01-30 23:44:40 +00:00
private float savePower;
2021-01-29 19:55:53 +00:00
// Start is called before the first frame update
void Start()
{
2021-01-30 12:51:35 +00:00
actualHealth = health;
playerAnimator = GetComponentInChildren<Animator>();
2021-01-29 19:55:53 +00:00
rigidBody = GetComponent<Rigidbody>();
2021-01-30 12:51:35 +00:00
saveSpeed = speed;
saveRunSpeed = runSpeed;
saveJump = jump;
2021-01-30 22:32:59 +00:00
saveSize = transform.localScale;
2021-01-29 19:55:53 +00:00
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene("MainMenu");
2021-01-29 19:55:53 +00:00
}
2021-01-30 22:32:59 +00:00
if (actualHealth <= 0 || gameObject.transform.position.y < -400.0f)
2021-01-30 12:51:35 +00:00
{
playerAnimator.Play("Die");
}
2021-01-30 19:56:04 +00:00
if (interact)
{
if (powerCubeManager != null && (
powerCubeManager.powerType == PowerCubeManager.PowerType.Artefact ||
powerCubeManager.powerType == PowerCubeManager.PowerType.DubleJump ||
powerCubeManager.powerType == PowerCubeManager.PowerType.PushPull ||
powerCubeManager.powerType == PowerCubeManager.PowerType.Dash
)) {
2021-01-30 19:56:04 +00:00
if (Input.GetKeyUp(KeyCode.E))
{
playerAnimator.SetTrigger("Eat");
interact = false;
}
}
}
2021-01-30 15:16:42 +00:00
AbilityAction();
2021-01-30 12:51:35 +00:00
DeactivePowerCube();
2021-01-29 19:55:53 +00:00
Move();
RunSwitch();
2021-01-30 09:51:42 +00:00
Animation();
2021-01-30 12:51:35 +00:00
if (startEating)
{
float stepSmaller = 0.8f * Time.deltaTime;
2021-01-30 22:32:59 +00:00
if (powerCubeManager != null) {
powerCubeManager.transform.localScale = Vector3.Slerp(powerCubeManager.transform.localScale, new Vector3(0.01f, 0.01f, 0.01f), stepSmaller);
}
2021-01-30 12:51:35 +00:00
}
2021-01-30 23:44:40 +00:00
if (actualPowerTimes[0] != 0f && actualPowerTimes[0] > Time.time)
{
transform.localScale = Vector3.Slerp(transform.localScale, saveNewSize, 1 * Time.deltaTime);
}
2021-01-30 09:51:42 +00:00
}
private void FixedUpdate()
{
Jump();
2021-01-29 19:55:53 +00:00
}
2021-01-30 15:16:42 +00:00
private void AbilityAction()
{
if (activeAbility.Count > 0 && activeAbility[0] == 2)
{
if (pushPullObject != null)
{
pushPullObject.GetComponent<Rigidbody>().MovePosition(
pushPullObject.transform.position +
(pushPullObject.transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
);
}
}
2021-01-30 15:40:42 +00:00
else if (activeAbility.Count > 0 && activeAbility[0] == 3)
{
2021-01-30 15:49:08 +00:00
if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A) && !dash) {
2021-01-30 15:40:42 +00:00
if (actualDashTime < Time.time)
{
if (Input.GetKeyUp(KeyCode.D))
{
dashButton = 1;
}
else if (Input.GetKeyUp(KeyCode.A))
{
dashButton = 2;
}
actualDashTime = Time.time + dashTime;
}
else
{
if (dashButton == 1 && Input.GetKeyUp(KeyCode.D)) {
rigidBody.AddForce(
(transform.right * dashPower * 10 * 5 * 1 * Time.deltaTime) +
(transform.up * 1 * 10 * Time.deltaTime),
ForceMode.VelocityChange
);
2021-01-30 15:49:08 +00:00
dash = true;
2021-01-30 15:40:42 +00:00
dashButton = 0;
actualDashTime = Time.time - 1f;
}
else if (dashButton == 2 && Input.GetKeyUp(KeyCode.A))
{
rigidBody.AddForce(
(transform.right * dashPower * 10 * 5 * -1 * Time.deltaTime) +
(transform.up * 1 * 10 * Time.deltaTime),
ForceMode.VelocityChange
);
2021-01-30 15:49:08 +00:00
dash = true;
2021-01-30 15:40:42 +00:00
dashButton = 0;
actualDashTime = Time.time - 1f;
}
}
}
}
2021-01-30 15:16:42 +00:00
}
public GameObject GetPushPullObject()
{
return pushPullObject;
}
public void SetPushPullObject(GameObject objectPP)
{
if (activeAbility.Count > 0 && activeAbility[0] == 2) {
pushPullObject = objectPP;
}
}
public void RemovePushPullObject()
{
pushPullObject = null;
}
2021-01-29 19:55:53 +00:00
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
}
else
{
run = false;
}
}
2021-01-30 12:51:35 +00:00
public void Die()
{
2021-01-30 19:04:43 +00:00
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
2021-01-30 12:51:35 +00:00
}
2021-01-30 09:51:42 +00:00
void Animation()
{
float localSpeed = 5f;
2021-01-30 13:55:08 +00:00
if (Input.GetAxis("Horizontal") > 0)
2021-01-30 09:51:42 +00:00
{
2021-01-30 12:51:35 +00:00
playerAnimator.SetBool("Walk", true);
2021-01-30 15:16:42 +00:00
if (pushPullObject == null) {
playerAnimator.transform.rotation = Quaternion.Lerp(
playerAnimator.transform.rotation,
Quaternion.Euler(playerAnimator.transform.eulerAngles.x, -90f, playerAnimator.transform.eulerAngles.z),
localSpeed * Time.deltaTime
);
}
2021-01-30 09:51:42 +00:00
}
2021-01-30 13:55:08 +00:00
else if (Input.GetAxis("Horizontal") < 0)
2021-01-30 09:51:42 +00:00
{
2021-01-30 12:51:35 +00:00
playerAnimator.SetBool("Walk", true);
2021-01-30 15:16:42 +00:00
if (pushPullObject == null)
{
playerAnimator.transform.rotation = Quaternion.Lerp(
playerAnimator.transform.rotation,
Quaternion.Euler(playerAnimator.transform.eulerAngles.x, 90f, playerAnimator.transform.eulerAngles.z),
localSpeed * Time.deltaTime
);
}
2021-01-30 09:51:42 +00:00
}
2021-01-30 12:51:35 +00:00
else
{
playerAnimator.SetBool("Walk", false);
}
2021-01-30 09:51:42 +00:00
}
2021-01-29 19:55:53 +00:00
void Move()
{
2021-01-30 13:29:14 +00:00
if (onGround) {
rigidBody.MovePosition(
transform.position +
(transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
);
}
2021-01-29 19:55:53 +00:00
}
2021-01-29 22:00:16 +00:00
void Jump()
{
2021-01-30 12:51:35 +00:00
if (Input.GetAxisRaw("Jump") > 0)
{
2021-01-30 13:55:08 +00:00
if (rigidBody.velocity.y <= 1 && (onGround || (dubleJump && activeAbility.Count > 0 && activeAbility[0] == 1)))
2021-01-29 22:00:16 +00:00
{
if (!onGround)
{
dubleJump = false;
}
2021-01-30 15:16:42 +00:00
pushPullObject = null;
2021-01-30 13:29:14 +00:00
rigidBody.AddForce(
(transform.right * (run ? runSpeed : speed) * 5 * Input.GetAxis("Horizontal") * Time.deltaTime) +
2021-01-30 13:29:14 +00:00
(transform.up * jump * 10 * Time.deltaTime),
ForceMode.VelocityChange
);
2021-01-29 22:00:16 +00:00
}
}
}
private void DropPower()
{
if (activeAbility.Count > 0 && activeAbility[0] != 0)
{
Instantiate(PowerPrefabs[activeAbility[0] - 1], new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z + 0.5f), PowerPrefabs[activeAbility[0] - 1].transform.rotation);
}
}
2021-01-30 19:56:04 +00:00
public void ActivePowerCube(float power, float powerTime, PowerCubeManager.PowerType powerType, string nextSceneName = "")
2021-01-30 12:51:35 +00:00
{
2021-01-30 19:56:04 +00:00
if ((powerType.GetHashCode() - 1) == 3)
{
SceneManager.LoadScene(nextSceneName);
}
else if ((powerType.GetHashCode() - 1) == 4)
{
DropPower();
//doubleJump
activeAbility[0] = 1;
}
else if ((powerType.GetHashCode() - 1) == 5)
{
DropPower();
//pushpull
activeAbility[0] = 2;
}
else if ((powerType.GetHashCode() - 1) == 6)
{
DropPower();
//dash
activeAbility[0] = 3;
}
2021-01-30 19:56:04 +00:00
else if (actualPowerTimes.Length <= (powerType.GetHashCode()) || actualPowerTimes[powerType.GetHashCode() - 1] < Time.time)
{
2021-01-30 12:51:35 +00:00
actualPowerTimes[powerType.GetHashCode() - 1] = Time.time + powerTime;
if (powerType == PowerCubeManager.PowerType.Bigger)
{
2021-01-30 22:32:59 +00:00
saveSize = transform.localScale;
2021-01-30 23:44:40 +00:00
savePower = power;
saveNewSize = new Vector3(
transform.localScale.x + savePower,
transform.localScale.y + savePower,
transform.localScale.z + savePower
);
/*transform.localScale = new Vector3(
2021-01-30 22:32:59 +00:00
transform.localScale.x + power,
transform.localScale.y + power,
transform.localScale.z + power
2021-01-30 23:44:40 +00:00
);*/
/*transform.localPosition = new Vector3(
2021-01-30 22:32:59 +00:00
transform.localPosition.x,
transform.localPosition.y + power * 2,
transform.localPosition.z
2021-01-30 23:44:40 +00:00
);*/
2021-01-30 12:51:35 +00:00
}
else if (powerType == PowerCubeManager.PowerType.Faster)
{
saveSpeed = speed;
saveRunSpeed = runSpeed;
speed += power;
runSpeed += power;
}
else if (powerType == PowerCubeManager.PowerType.Jumper)
{
saveJump = jump;
jump += power;
}
}
}
private void DeactivePowerCube()
{
if (actualPowerTimes[0] != 0f && actualPowerTimes[0] < Time.time)
{
2021-01-30 23:44:40 +00:00
transform.localScale = Vector3.Slerp(transform.localScale, saveSize, 1 * Time.deltaTime);
if (transform.localScale == saveSize) {
actualPowerTimes[0] = 0f;
}
2021-01-30 12:51:35 +00:00
}
if (actualPowerTimes[1] != 0f && actualPowerTimes[1] < Time.time)
{
speed = saveSpeed;
runSpeed = saveRunSpeed;
actualPowerTimes[1] = 0f;
}
if (actualPowerTimes[2] != 0f && actualPowerTimes[2] < Time.time)
{
jump = saveJump;
actualPowerTimes[2] = 0f;
}
}
2021-01-29 22:00:16 +00:00
public void OnTriggerEnter(Collider other)
{
2021-01-29 22:56:23 +00:00
if (other.tag == "Ground" || other.tag == "Objects")
2021-01-29 22:00:16 +00:00
{
2021-01-30 09:51:42 +00:00
rigidBody.MovePosition(transform.position);
2021-01-29 22:00:16 +00:00
onGround = true;
2021-01-30 13:55:08 +00:00
dubleJump = true;
2021-01-30 15:49:08 +00:00
dash = false;
2021-01-29 22:00:16 +00:00
}
2021-01-30 22:17:05 +00:00
if (other.gameObject.GetComponent<PowerCubeManager>() != null)
2021-01-30 19:56:04 +00:00
{
2021-01-30 22:17:05 +00:00
powerCubeManager = other.gameObject.GetComponent<PowerCubeManager>();
2021-01-30 19:56:04 +00:00
interact = true;
}
2021-01-29 22:00:16 +00:00
}
public void OnTriggerExit(Collider other)
{
2021-01-29 22:56:23 +00:00
if (other.tag == "Ground" || other.tag == "Objects")
2021-01-29 22:00:16 +00:00
{
2021-01-30 19:04:43 +00:00
rigidBody.AddForce(
(transform.right * (run ? runSpeed : speed) * 5 * Input.GetAxis("Horizontal") * Time.deltaTime) +
2021-01-30 19:04:43 +00:00
(transform.up * 10 * Time.deltaTime),
ForceMode.VelocityChange
);
2021-01-29 22:00:16 +00:00
onGround = false;
2021-01-30 15:49:08 +00:00
dash = false;
2021-01-29 22:00:16 +00:00
}
2021-01-30 19:56:04 +00:00
if (other.gameObject.GetComponent<PowerCubeManager>() != null)
{
interact = false;
2021-01-30 19:56:04 +00:00
}
2021-01-29 22:00:16 +00:00
}
2021-01-30 09:51:42 +00:00
public void OnTriggerStay(Collider other)
{
if (other.tag == "Ground" || other.tag == "Objects")
{
2021-01-30 12:51:35 +00:00
if (rigidBody.velocity.y <= 1 && !onGround) {
2021-01-30 09:51:42 +00:00
rigidBody.MovePosition(transform.position);
onGround = true;
2021-01-30 13:55:08 +00:00
dubleJump = true;
2021-01-30 09:51:42 +00:00
}
2021-01-30 15:49:08 +00:00
if (onGround) {
dash = false;
}
2021-01-30 09:51:42 +00:00
}
}
2021-01-30 12:51:35 +00:00
public void OnCollisionEnter(Collision collision)
{
2021-01-30 15:16:42 +00:00
if (collision.gameObject.GetComponent<PowerCubeManager>() != null)
2021-01-30 12:51:35 +00:00
{
Vector3 hit = collision.contacts[0].normal;
2021-01-30 15:16:42 +00:00
if (hit.x != 0 && hit.y == 0)
{
2021-01-30 12:51:35 +00:00
powerCubeManager = collision.gameObject.GetComponent<PowerCubeManager>();
playerAnimator.SetTrigger("Eat");
}
}
}
public void damage(float damage, bool instaKill)
{
if (instaKill)
{
actualHealth = 0;
}
else
{
actualHealth -= damage;
}
}
public void StartEatPowerCube()
{
startEating = true;
}
public void EndEatPowerCube()
{
startEating = false;
2021-01-30 19:56:04 +00:00
ActivePowerCube(powerCubeManager.powerUnit, powerCubeManager.powerTime, powerCubeManager.powerType, powerCubeManager.nextSceneName);
2021-01-30 12:51:35 +00:00
Destroy(powerCubeManager.gameObject);
}
2021-01-29 19:55:53 +00:00
}