GlobalGameJam-2021/Assets/Scripts/PlayerManager.cs

280 lines
8.3 KiB
C#
Raw Normal View History

2021-01-29 19:55:53 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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 13:55:08 +00:00
public List<int> activeAbility = new List<int>(); //without ability=0 or null, dubleJump = 1, push/pull = 2, dash = 3
private bool dubleJump = true;
2021-01-30 12:51:35 +00:00
private bool startEating = false;
private float saveSpeed;
private float saveRunSpeed;
private float saveJump;
private Vector3 saveSize;
private Vector3 savePosition;
private Vector3 saveCameraPosition;
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;
saveSize = playerAnimator.transform.localScale;
savePosition = playerAnimator.transform.localPosition;
saveCameraPosition = Camera.main.transform.localPosition;
2021-01-29 19:55:53 +00:00
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit(0);
}
2021-01-30 12:51:35 +00:00
if (actualHealth <= 0)
{
playerAnimator.Play("Die");
}
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;
powerCubeManager.transform.localScale = Vector3.Slerp(powerCubeManager.transform.localScale, new Vector3(0.01f, 0.01f, 0.01f), stepSmaller);
}
2021-01-30 09:51:42 +00:00
}
private void FixedUpdate()
{
Jump();
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()
{
Application.LoadLevel(Application.loadedLevel);
}
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);
playerAnimator.transform.rotation = Quaternion.Lerp(
playerAnimator.transform.rotation,
Quaternion.Euler(playerAnimator.transform.eulerAngles.x, -90f, playerAnimator.transform.eulerAngles.z),
2021-01-30 09:51:42 +00:00
localSpeed * Time.deltaTime
);
}
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);
playerAnimator.transform.rotation = Quaternion.Lerp(
playerAnimator.transform.rotation,
Quaternion.Euler(playerAnimator.transform.eulerAngles.x, 90f, playerAnimator.transform.eulerAngles.z),
2021-01-30 09:51:42 +00:00
localSpeed * Time.deltaTime
);
}
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
{
2021-01-30 13:29:14 +00:00
rigidBody.AddForce(
(transform.right * (run ? runSpeed : speed) * 5 * Input.GetAxis("Horizontal") * Time.deltaTime) +
(transform.up * jump * 10 * Time.deltaTime),
ForceMode.VelocityChange
);
2021-01-30 13:55:08 +00:00
if (!onGround)
{
dubleJump = false;
}
2021-01-29 22:00:16 +00:00
}
}
}
2021-01-30 12:51:35 +00:00
public void ActivePowerCube(float power, float powerTime, PowerCubeManager.PowerType powerType)
{
if (actualPowerTimes.Length <= (powerType.GetHashCode()) || actualPowerTimes[powerType.GetHashCode() - 1] < Time.time) {
actualPowerTimes[powerType.GetHashCode() - 1] = Time.time + powerTime;
if (powerType == PowerCubeManager.PowerType.Bigger)
{
saveSize = playerAnimator.transform.localScale;
savePosition = playerAnimator.transform.localPosition;
saveCameraPosition = Camera.main.transform.localPosition;
playerAnimator.transform.localScale = new Vector3(
playerAnimator.transform.localScale.x + power,
playerAnimator.transform.localScale.y + power,
playerAnimator.transform.localScale.z + power
);
playerAnimator.transform.localPosition = new Vector3(
playerAnimator.transform.localPosition.x,
playerAnimator.transform.localPosition.y + power * 2,
playerAnimator.transform.localPosition.z
);
Camera.main.transform.localPosition = new Vector3(
Camera.main.transform.localPosition.x,
Camera.main.transform.localPosition.y,
Camera.main.transform.localPosition.z - power * 2
);
}
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)
{
playerAnimator.transform.localScale = saveSize;
playerAnimator.transform.localPosition = savePosition;
Camera.main.transform.localPosition = saveCameraPosition;
actualPowerTimes[0] = 0f;
}
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-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 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 12:51:35 +00:00
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Objects")
{
Vector3 hit = collision.contacts[0].normal;
if (hit.x != 0) {
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;
ActivePowerCube(powerCubeManager.powerUnit, powerCubeManager.powerTime, powerCubeManager.powerType);
Destroy(powerCubeManager.gameObject);
}
2021-01-29 19:55:53 +00:00
}