Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- [RequireComponent(typeof(BoxCollider2D))]
- public class Player : MonoBehaviour
- {
- //public float moveSpeed = 5;
- public bool golem;
- public bool ghost;
- public bool seamonster;
- private const int wizardSpeed = 5;
- private const int golemSpeed = 4;
- private const int ghostSpeed = 6;
- private const int seaSpeed = 5;
- float moveSpeed = wizardSpeed;
- Vector3 distance;
- Vector2 input;
- const float skinWidth = .015f;
- public int horizontalRayCount = 3;
- public int verticalRayCount = 3;
- float horizontalRaySpacing;
- float verticalRaySpacing;
- Animator anim;
- BoxCollider2D col;
- RaycastOrigins raycastOrigins;
- public GameObject exitParticles;
- public GameObject bottleParticles;
- public GameObject golemParticles;
- public GameObject ghostParticles;
- public GameObject seaParticles;
- public Text potion1Display;
- public Text potion2Display;
- public Text potion3Display;
- public int potion1Count;
- public int potion2Count;
- public int potion3Count;
- AudioSource sfx;
- public AudioClip exit;
- public AudioClip effect;
- public AudioClip pickup;
- void Awake()
- {
- // set layer to ignore raycast
- gameObject.layer = 2;
- exitParticles.SetActive(false);
- bottleParticles.SetActive(false);
- golemParticles.SetActive(false);
- ghostParticles.SetActive(false);
- seaParticles.SetActive(false);
- anim = GetComponent<Animator>();
- sfx = GetComponent<AudioSource>();
- }
- void Start()
- {
- col = GetComponent<BoxCollider2D>();
- CalculateRaySpacing();
- updateUI();
- }
- void Update()
- {
- input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- if (Input.GetKey("1"))
- {
- DrinkPotion(1);
- }
- if (Input.GetKey("2"))
- {
- DrinkPotion(2);
- }
- if (Input.GetKey("3"))
- {
- DrinkPotion(3);
- }
- if (Input.GetKey("r"))
- {
- ResetLevel();
- }
- // Animation Parameters
- Vector2 dir = distance;
- anim.SetFloat("DirX", dir.x);
- anim.SetFloat("DirY", dir.y);
- anim.SetBool("Golem", golem);
- anim.SetBool("Ghost", ghost);
- anim.SetBool("Seamonster", seamonster);
- }
- void FixedUpdate()
- {
- distance.x = input.x * moveSpeed;
- distance.y = input.y * moveSpeed;
- Move(distance * Time.deltaTime);
- }
- public void Move(Vector3 velocity)
- {
- UpdateRaycastOrigins();
- if (velocity.x != 0)
- {
- HorizontalCollisions(ref velocity);
- }
- if (velocity.y != 0)
- {
- VerticalCollisions(ref velocity);
- }
- transform.Translate(velocity);
- }
- void HorizontalCollisions(ref Vector3 velocity)
- {
- float directionX = Mathf.Sign(velocity.x);
- float rayLength = Mathf.Abs(velocity.x) + skinWidth;
- for (int i = 0; i < horizontalRayCount; i++)
- {
- Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
- rayOrigin += Vector2.up * (horizontalRaySpacing * i);
- RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength);
- Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.green);
- if (hit)
- {
- if (hit.collider.name == "Exit" || hit.collider.name == "Potion1" || hit.collider.name == "Potion2" || hit.collider.name == "Potion3" || hit.collider.name == "Mist")
- {
- return;
- }
- if (hit.collider.name == "Lava" && golem)
- {
- return;
- }
- if (hit.collider.name == "Fence" && ghost)
- {
- return;
- }
- if (hit.collider.name == "Water" && seamonster)
- {
- return;
- }
- else
- {
- //Debug.Log(hit.collider.name);
- velocity.x = (hit.distance - skinWidth) * directionX;
- rayLength = hit.distance;
- }
- }
- }
- }
- void VerticalCollisions(ref Vector3 velocity)
- {
- float directionY = Mathf.Sign(velocity.y);
- float rayLength = Mathf.Abs(velocity.y) + skinWidth;
- for (int i = 0; i < verticalRayCount; i++)
- {
- Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
- rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
- RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength);
- ///Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.green);
- if (hit)
- {
- if (hit.collider.name == "Exit" || hit.collider.name == "Potion1" || hit.collider.name == "Potion2" || hit.collider.name == "Potion3" || hit.collider.name == "Mist")
- {
- return;
- }
- if (hit.collider.name == "Lava" && golem)
- {
- return;
- }
- if (hit.collider.name == "Fence" && ghost)
- {
- return;
- }
- if (hit.collider.name == "Water" && seamonster)
- {
- return;
- }
- else
- {
- velocity.y = (hit.distance - skinWidth) * directionY;
- rayLength = hit.distance;
- }
- }
- }
- }
- void UpdateRaycastOrigins()
- {
- Bounds bounds = col.bounds;
- bounds.Expand(skinWidth * -2);
- raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
- raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
- raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
- raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
- }
- void CalculateRaySpacing()
- {
- Bounds bounds = col.bounds;
- bounds.Expand(skinWidth * -2);
- horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue);
- verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue);
- horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
- verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
- }
- struct RaycastOrigins
- {
- public Vector2 topLeft, topRight;
- public Vector2 bottomLeft, bottomRight;
- }
- void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.name == "Exit")
- {
- //Debug.Log("Next Level");
- StartCoroutine("NextLevel");
- exitParticles.SetActive(true);
- }
- if (collision.name == "Potion1")
- {
- if (potion1Count==9)
- {
- return;
- }
- collision.gameObject.SetActive(false);
- potion1Count++;
- updateUI();
- sfx.PlayOneShot(pickup);
- }
- if (collision.name == "Potion2")
- {
- if (potion2Count == 9)
- {
- return;
- }
- collision.gameObject.SetActive(false);
- potion2Count++;
- updateUI();
- sfx.PlayOneShot(pickup);
- }
- if (collision.name == "Potion3")
- {
- if (potion3Count == 9)
- {
- return;
- }
- collision.gameObject.SetActive(false);
- potion3Count++;
- updateUI();
- sfx.PlayOneShot(pickup);
- }
- if (collision.name == "Mist")
- {
- golem = false;
- ghost = false;
- seamonster = false;
- anim.SetBool("Golem", golem);
- anim.SetBool("Ghost", ghost);
- anim.SetBool("Seamonster", seamonster);
- int ready = Animator.StringToHash("wizard_down");
- anim.Play(ready);
- moveSpeed = wizardSpeed;
- if (!bottleParticles.activeSelf)
- {
- bottleParticles.SetActive(true);
- sfx.PlayOneShot(effect);
- }
- }
- }
- void DrinkPotion (int potion)
- {
- if (potion==1 && !golem && potion1Count>0)
- {
- potion1Count--;
- updateUI();
- golem = true;
- ghost = false;
- seamonster = false;
- anim.SetBool("Golem", golem);
- anim.SetBool("Ghost", ghost);
- anim.SetBool("Seamonster", seamonster);
- int ready = Animator.StringToHash("golem_down");
- anim.Play(ready);
- moveSpeed = golemSpeed;
- bottleParticles.SetActive(false);
- golemParticles.SetActive(true);
- ghostParticles.SetActive(false);
- seaParticles.SetActive(false);
- sfx.PlayOneShot(effect);
- }
- if (potion == 2 && !ghost && potion2Count > 0)
- {
- potion2Count--;
- updateUI();
- golem = false;
- ghost = true;
- seamonster = false;
- anim.SetBool("Golem", golem);
- anim.SetBool("Ghost", ghost);
- anim.SetBool("Seamonster", seamonster);
- int ready = Animator.StringToHash("ghost_down");
- anim.Play(ready);
- moveSpeed = ghostSpeed;
- bottleParticles.SetActive(false);
- golemParticles.SetActive(false);
- ghostParticles.SetActive(true);
- seaParticles.SetActive(false);
- sfx.PlayOneShot(effect);
- }
- if (potion == 3 && !seamonster && potion3Count > 0)
- {
- potion3Count--;
- updateUI();
- golem = false;
- ghost = false;
- seamonster = true;
- anim.SetBool("Golem", golem);
- anim.SetBool("Ghost", ghost);
- anim.SetBool("Seamonster", seamonster);
- int ready = Animator.StringToHash("seamonster_down");
- anim.Play(ready);
- moveSpeed = seaSpeed;
- bottleParticles.SetActive(false);
- golemParticles.SetActive(false);
- ghostParticles.SetActive(false);
- seaParticles.SetActive(true);
- sfx.PlayOneShot(effect);
- }
- }
- void updateUI()
- {
- potion1Display.text = "x"+ potion1Count;
- potion2Display.text = "x" + potion2Count;
- potion3Display.text = "x" + potion3Count;
- }
- IEnumerator NextLevel()
- {
- sfx.PlayOneShot(exit);
- //moveSpeed = 0;
- yield return new WaitForSeconds(0.1f);
- moveSpeed = 0;
- yield return new WaitForSeconds(1f);
- int nextLevel = Application.loadedLevel + 1;
- Application.LoadLevel(nextLevel);
- }
- void ResetLevel()
- {
- Application.LoadLevel(Application.loadedLevel);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement