123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Player_Move_Script : MonoBehaviour
- {
- public int maxSpeed = 8;
- public int jumpPower = 1400;
- public float speed;
- public bool grounded = true;
- public bool facingRight = true;
- public int playerLayer = 8;
- private float width;
- private float height;
- private Animator animator;
- private AudioManager_Script audioManager;
- void Start()
- {
- width = GetComponent<SpriteRenderer>().bounds.size.x;
- height = GetComponent<SpriteRenderer>().bounds.size.y;
- animator = GetComponent<Animator>();
- audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager_Script>();
- }
- void Update()
- {
- if (Static_GlobalStats.health <= 0) return;
- int layerMask = ~(1 << playerLayer);
- speed = Input.GetAxis("Horizontal");
- Rigidbody2D rigidbody = gameObject.GetComponent<Rigidbody2D>();
- if (Static_GlobalStats.paused) {
- rigidbody.velocity = Vector2.zero;
- animator.SetFloat("speed", 0);
- return;
- }
- // Ground Check
- // Jump On Enemy Check
- grounded = false;
- for (float x = gameObject.transform.position.x - width * 0.55f; x <= gameObject.transform.position.x + width * 0.55f; x += 0.1f)
- {
- RaycastHit2D groundHit = Physics2D.Raycast(new Vector2(x, gameObject.transform.position.y), Vector2.down, height * 0.8f, layerMask);
- if (groundHit.collider)
- {
- if (groundHit.collider.tag == "Enemy" || groundHit.collider.tag == "PseudoEnemy" || groundHit.collider.tag == "Boss") //Jump on Enemy
- {
- if (audioManager)
- {
- audioManager.playSfx("kill");
- if (Random.Range(0f, 1f) < 0.3f) audioManager.playComment("kill");
- }
- rigidbody.AddForce(Vector2.up * jumpPower);
- groundHit.collider.GetComponent<SpriteRenderer>().color = new Color(1, 0.5f, 0.3f, 1);
- groundHit.collider.enabled = false;
- if (groundHit.collider.tag == "Boss")
- {
- groundHit.collider.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
- groundHit.collider.GetComponent<Rigidbody2D>().AddForce(Vector2.down * 1000);
- StartCoroutine(WinTheFuckingGame());
- }
- break;
- }
- grounded = true;
- break;
- }
- }
- // Wall Check
- for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f)
- {
- RaycastHit2D wallHit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(speed, 0), width * 0.55f, layerMask);
- if (wallHit.collider && wallHit.collider.tag != "Collectable" && wallHit.collider.tag != "Projectile" && wallHit.collider.tag != "Finish")
- {
- speed = 0;
- break;
- }
- }
- // Controls
- if (Input.GetButtonDown("Jump") && grounded)
- {
- jump();
- }
- // Player Direction
- if ((speed < 0.0f && facingRight) || (speed > 0.0f && !facingRight))
- {
- flipPlayer();
- }
- // Physics
- rigidbody.velocity = new Vector2(speed * maxSpeed, rigidbody.velocity.y);
- // Animation
- animator.SetFloat("speed", Mathf.Abs(speed));
- animator.SetBool("grounded", grounded);
- }
- // Might as well...
- void jump()
- {
- gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpPower);
- if (audioManager) audioManager.playSfx("jump");
- }
- void flipPlayer()
- {
- facingRight = !facingRight;
- Vector2 localScale = gameObject.transform.localScale;
- localScale.x *= -1;
- transform.localScale = localScale;
- }
- IEnumerator WinTheFuckingGame()
- {
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- yield return new WaitForSeconds(0.2f);
- if (audioManager) audioManager.playSfx("kill");
- SceneManager.LoadScene("Menu_Finale");
- }
- }
|