119 lines
3.2 KiB
C#
119 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
public class PlayerController : MonoBehaviour{
|
|
|
|
public GameObject humo;
|
|
public float speed;
|
|
public Transform child;
|
|
public LightBar lightBar;
|
|
public GameObject aura;
|
|
public GameController gameController;
|
|
public GameObject trigger;
|
|
public GameObject circle;
|
|
public Animator anim;
|
|
public GameObject luz;
|
|
[HideInInspector] public bool runOutOfLight;
|
|
bool lost;
|
|
bool lighted;
|
|
Vector2 mov;
|
|
Rigidbody2D rb2d;
|
|
float defaultSpeed;
|
|
|
|
float turnSmoothVelocity;
|
|
|
|
bool brujula;
|
|
|
|
bool lowLight;
|
|
|
|
// Start is called before the first frame update
|
|
void Start(){
|
|
rb2d = GetComponent<Rigidbody2D>();
|
|
defaultSpeed = speed;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
if (!lost){
|
|
if (Input.GetButtonDown("Fire")){
|
|
if (brujula){
|
|
Invoke(nameof(VelaOff), 2);
|
|
humo.SetActive(false);
|
|
brujula = false;
|
|
anim.SetBool("Vela", false);
|
|
}else{
|
|
VelaOn();
|
|
brujula = true;
|
|
}
|
|
}
|
|
|
|
float h = Input.GetAxisRaw("Horizontal");
|
|
float v = Input.GetAxisRaw("Vertical");
|
|
mov = new Vector2(h, v);
|
|
|
|
if (mov.magnitude > .1f){
|
|
anim.SetBool("Walking", true);
|
|
|
|
float targetAngle = Mathf.Atan2(mov.x, mov.y) * Mathf.Rad2Deg;
|
|
float angle = Mathf.SmoothDampAngle(child.localEulerAngles.y, targetAngle, ref turnSmoothVelocity, .1f);
|
|
child.localRotation = Quaternion.Euler(0f, angle, 0f);
|
|
}else{
|
|
anim.SetBool("Walking", false);
|
|
}
|
|
}else{
|
|
mov = Vector2.zero;
|
|
}
|
|
if(lightBar.time <= lightBar.maxTime / 2 && lowLight){
|
|
AudioManager.instance.FadeMutedIn("Theme2", 2f);
|
|
lowLight = true;
|
|
}
|
|
|
|
if(lightBar.time <= 0 && !lighted){
|
|
luz.SetActive(false);
|
|
Invoke(nameof(FinalLight), 1);
|
|
trigger.SetActive(true);
|
|
gameController.readyToSpawn = false;
|
|
AudioManager.instance.Stop("Vela");
|
|
trigger.SetActive(true);
|
|
lighted = true;
|
|
}
|
|
}
|
|
|
|
void FixedUpdate(){
|
|
rb2d.MovePosition(rb2d.position + mov.normalized * speed * Time.fixedDeltaTime);
|
|
}
|
|
|
|
void FinalLight(){
|
|
aura.SetActive(false);
|
|
trigger.SetActive(false);
|
|
CancelInvoke();
|
|
runOutOfLight = true;
|
|
}
|
|
|
|
void VelaOn(){
|
|
humo.SetActive(true);
|
|
speed = 0;
|
|
anim.SetBool("Vela", true);
|
|
}
|
|
|
|
void VelaOff(){
|
|
speed = defaultSpeed;
|
|
}
|
|
|
|
void OnCollisionEnter2D(Collision2D col){
|
|
if(col.gameObject.CompareTag("Enemy") && !aura.activeSelf && !lost){
|
|
Invoke(nameof(GameOver), 5f);
|
|
anim.SetBool("Dead", true);
|
|
AudioManager.instance.Stop("Theme");
|
|
AudioManager.instance.Stop("Theme2");
|
|
AudioManager.instance.Play("Death");
|
|
lost = true;
|
|
}
|
|
}
|
|
|
|
void GameOver(){
|
|
gameController.gameOver = true;
|
|
}
|
|
}
|