GlobalGameJam-2021/Assets/Scripts/PlayerManager.cs

483 lines
16 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;
using UnityEngine.UI;
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-31 01:35:47 +00:00
public bool onLadder = false;
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-31 01:35:47 +00:00
public List<int> activeAbility = new List<int>(); //without ability=0 or null, dubleJump = 1, push/pull = 2, dash = 3, ladder = 4
public List<GameObject> PowerPrefabs = new List<GameObject>(); //dubleJump = 0, push/pull = 1, dash = 2, ladder = 3
public List<Sprite> powerIcons = new List<Sprite>(); //dubleJump = 0, push/pull = 1, dash = 2, ladder = 3
public Image powerImage;
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-31 05:05:43 +00:00
private float pushPullObjectDistance;
2021-01-30 15:40:42 +00:00
public float dashPower = 40f;
2021-01-31 13:34:49 +00:00
public float dashTime = 0.4f;
2021-01-30 15:40:42 +00:00
private float actualDashTime;
private int dashButton;
2021-01-30 15:49:08 +00:00
private bool dash = false;
2021-01-31 05:05:43 +00:00
private bool cannotMove = 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-31 10:58:07 +00:00
FindObjectOfType<AudioManager>().Play("MainTheme");
2021-01-29 19:55:53 +00:00
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
2021-01-31 03:20:04 +00:00
DataManager.Level(SceneManager.GetActiveScene().buildIndex);
SceneManager.LoadScene(6);
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
{
2021-01-31 05:05:43 +00:00
cannotMove = true;
2021-01-30 12:51:35 +00:00
playerAnimator.Play("Die");
2021-01-31 13:22:06 +00:00
FindObjectOfType<AudioManager>().Play("lose");
2021-01-30 12:51:35 +00:00
}
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 ||
2021-01-31 01:35:47 +00:00
powerCubeManager.powerType == PowerCubeManager.PowerType.Dash ||
powerCubeManager.powerType == PowerCubeManager.PowerType.Ladder
)) {
2021-01-30 19:56:04 +00:00
if (Input.GetKeyUp(KeyCode.E))
{
2021-01-31 13:22:06 +00:00
FindObjectOfType<AudioManager>().Play("Pickup");
2021-01-30 19:56:04 +00:00
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-31 05:05:43 +00:00
if (!cannotMove) {
Move();
RunSwitch();
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()
{
2021-01-31 13:34:49 +00:00
if (!cannotMove && (!onLadder || (onLadder && (activeAbility.Count < 0 || activeAbility[0] != 4))))
2021-01-31 05:05:43 +00:00
{
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)
{
2021-01-31 05:05:43 +00:00
float distance = Vector3.Distance(pushPullObject.transform.position, gameObject.transform.position);
if (pushPullObjectDistance - 0.1f <= distance && pushPullObjectDistance + 0.1f >= distance)
{
Rigidbody pcmRigidB = pushPullObject.GetComponent<Rigidbody>();
pcmRigidB.constraints = RigidbodyConstraints.FreezeRotation;
pushPullObject.GetComponent<Rigidbody>().MovePosition(
pushPullObject.transform.position +
(pushPullObject.transform.right * (run ? runSpeed : speed) * Input.GetAxis("Horizontal") * Time.deltaTime)
);
}
else
{
RemovePushPullObject();
}
2021-01-30 15:16:42 +00:00
}
powerImage.sprite = powerIcons[1];
2021-01-30 15:16:42 +00:00
}
2021-01-30 15:40:42 +00:00
else if (activeAbility.Count > 0 && activeAbility[0] == 3)
{
powerImage.sprite = powerIcons[2];
2021-01-31 01:35:47 +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
{
2021-01-31 01:35:47 +00:00
if (dashButton == 1 && Input.GetKeyUp(KeyCode.D))
{
2021-01-30 15:40:42 +00:00
rigidBody.AddForce(
(transform.right * dashPower * 10 * 5 * 1 * Time.deltaTime) +
2021-01-31 10:07:21 +00:00
(transform.up * 2 * 10 * Time.deltaTime),
2021-01-30 15:40:42 +00:00
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-31 01:35:47 +00:00
}
2021-01-30 15:40:42 +00:00
else if (dashButton == 2 && Input.GetKeyUp(KeyCode.A))
{
rigidBody.AddForce(
(transform.right * dashPower * 10 * 5 * -1 * Time.deltaTime) +
2021-01-31 10:07:21 +00:00
(transform.up * 2 * 10 * Time.deltaTime),
2021-01-30 15:40:42 +00:00
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 (activeAbility.Count > 0 && activeAbility[0] == 4)
{
powerImage.sprite = powerIcons[3];
}
else if (activeAbility.Count > 0 && activeAbility[0] == 1)
{
powerImage.sprite = powerIcons[0];
}
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) {
2021-01-31 05:05:43 +00:00
if (pushPullObject != null)
{
pushPullObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionX;
}
2021-01-30 15:16:42 +00:00
pushPullObject = objectPP;
2021-01-31 05:05:43 +00:00
pushPullObjectDistance = Vector3.Distance(pushPullObject.transform.position, gameObject.transform.position);
2021-01-30 15:16:42 +00:00
}
}
public void RemovePushPullObject()
{
2021-01-31 05:05:43 +00:00
if (pushPullObject != null) {
pushPullObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionX;
pushPullObject = null;
}
2021-01-30 15:16:42 +00:00
}
2021-01-29 19:55:53 +00:00
void RunSwitch()
{
if (Input.GetAxisRaw("Run") > 0)
{
run = true;
2021-01-31 13:22:06 +00:00
2021-01-29 19:55:53 +00:00
}
else
{
run = false;
}
}
2021-01-30 12:51:35 +00:00
public void Die()
{
2021-01-31 03:06:54 +00:00
DataManager.Level(SceneManager.GetActiveScene().buildIndex);
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-31 13:34:49 +00:00
rigidBody.MovePosition(
transform.position +
(onLadder && activeAbility.Count > 0 && activeAbility[0] == 4 ? (transform.up * speed * Input.GetAxis("Vertical") * Time.deltaTime) : Vector3.zero) +
(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-31 13:34:49 +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-31 14:20:36 +00:00
FindObjectOfType<AudioManager>().Play("Jump");
2021-01-30 15:16:42 +00:00
pushPullObject = null;
2021-01-30 13:29:14 +00:00
rigidBody.AddForce(
(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)
{
2021-01-31 14:12:34 +00:00
DataManager.Score(DataManager.Score() + 10);
2021-01-31 03:06:54 +00:00
DataManager.Level(SceneManager.GetSceneByName(nextSceneName).buildIndex);
2021-01-30 19:56:04 +00:00
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-31 01:35:47 +00:00
else if ((powerType.GetHashCode() - 1) == 7)
{
DropPower();
//ladder
activeAbility[0] = 4;
}
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
);
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
{
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()
{
2021-01-31 05:05:43 +00:00
cannotMove = true;
2021-01-30 12:51:35 +00:00
startEating = true;
}
public void EndEatPowerCube()
{
2021-01-31 05:05:43 +00:00
cannotMove = false;
2021-01-30 12:51:35 +00:00
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
}