Advertisement
Guest User

Morphomancer Source

a guest
Apr 18th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.36 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. [RequireComponent(typeof(BoxCollider2D))]
  6. public class Player : MonoBehaviour
  7. {
  8.  
  9.     //public float moveSpeed = 5;
  10.     public bool golem;
  11.     public bool ghost;
  12.     public bool seamonster;
  13.     private const int wizardSpeed = 5;
  14.     private const int golemSpeed = 4;
  15.     private const int ghostSpeed = 6;
  16.     private const int seaSpeed = 5;
  17.     float moveSpeed = wizardSpeed;
  18.  
  19.     Vector3 distance;
  20.     Vector2 input;
  21.  
  22.     const float skinWidth = .015f;
  23.     public int horizontalRayCount = 3;
  24.     public int verticalRayCount = 3;
  25.  
  26.     float horizontalRaySpacing;
  27.     float verticalRaySpacing;
  28.  
  29.     Animator anim;
  30.  
  31.     BoxCollider2D col;
  32.     RaycastOrigins raycastOrigins;
  33.  
  34.     public GameObject exitParticles;
  35.     public GameObject bottleParticles;
  36.     public GameObject golemParticles;
  37.     public GameObject ghostParticles;
  38.     public GameObject seaParticles;
  39.  
  40.     public Text potion1Display;
  41.     public Text potion2Display;
  42.     public Text potion3Display;
  43.  
  44.     public int potion1Count;
  45.     public int potion2Count;
  46.     public int potion3Count;
  47.  
  48.     AudioSource sfx;
  49.     public AudioClip exit;
  50.     public AudioClip effect;
  51.     public AudioClip pickup;
  52.  
  53.     void Awake()
  54.     {
  55.         // set layer to ignore raycast
  56.         gameObject.layer = 2;
  57.         exitParticles.SetActive(false);
  58.         bottleParticles.SetActive(false);
  59.         golemParticles.SetActive(false);
  60.         ghostParticles.SetActive(false);
  61.         seaParticles.SetActive(false);
  62.         anim = GetComponent<Animator>();
  63.         sfx = GetComponent<AudioSource>();
  64.     }
  65.  
  66.     void Start()
  67.     {
  68.         col = GetComponent<BoxCollider2D>();
  69.         CalculateRaySpacing();
  70.         updateUI();
  71.     }
  72.  
  73.     void Update()
  74.     {
  75.         input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  76.         if (Input.GetKey("1"))
  77.         {
  78.             DrinkPotion(1);
  79.         }
  80.         if (Input.GetKey("2"))
  81.         {
  82.             DrinkPotion(2);
  83.         }
  84.         if (Input.GetKey("3"))
  85.         {
  86.             DrinkPotion(3);
  87.         }
  88.         if (Input.GetKey("r"))
  89.         {
  90.             ResetLevel();
  91.         }
  92.  
  93.  
  94.         // Animation Parameters
  95.         Vector2 dir = distance;
  96.         anim.SetFloat("DirX", dir.x);
  97.         anim.SetFloat("DirY", dir.y);
  98.         anim.SetBool("Golem", golem);
  99.         anim.SetBool("Ghost", ghost);
  100.         anim.SetBool("Seamonster", seamonster);
  101.  
  102.     }
  103.  
  104.     void FixedUpdate()
  105.     {
  106.         distance.x = input.x * moveSpeed;
  107.         distance.y = input.y * moveSpeed;
  108.         Move(distance * Time.deltaTime);
  109.     }
  110.  
  111.     public void Move(Vector3 velocity)
  112.     {
  113.         UpdateRaycastOrigins();
  114.         if (velocity.x != 0)
  115.         {
  116.             HorizontalCollisions(ref velocity);
  117.         }
  118.         if (velocity.y != 0)
  119.         {
  120.             VerticalCollisions(ref velocity);
  121.         }
  122.  
  123.         transform.Translate(velocity);
  124.     }
  125.  
  126.     void HorizontalCollisions(ref Vector3 velocity)
  127.     {
  128.         float directionX = Mathf.Sign(velocity.x);
  129.         float rayLength = Mathf.Abs(velocity.x) + skinWidth;
  130.  
  131.         for (int i = 0; i < horizontalRayCount; i++)
  132.         {
  133.             Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight;
  134.             rayOrigin += Vector2.up * (horizontalRaySpacing * i);
  135.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength);
  136.  
  137.             Debug.DrawRay(rayOrigin, Vector2.right * directionX * rayLength, Color.green);
  138.  
  139.             if (hit)
  140.             {
  141.                 if (hit.collider.name == "Exit" || hit.collider.name == "Potion1" || hit.collider.name == "Potion2" || hit.collider.name == "Potion3" || hit.collider.name == "Mist")
  142.                 {
  143.                     return;
  144.                 }
  145.                 if (hit.collider.name == "Lava" && golem)
  146.                 {
  147.                     return;
  148.                 }
  149.                 if (hit.collider.name == "Fence" && ghost)
  150.                 {
  151.                     return;
  152.                 }
  153.                 if (hit.collider.name == "Water" && seamonster)
  154.                 {
  155.                     return;
  156.                 }
  157.                 else
  158.                 {
  159.  
  160.                     //Debug.Log(hit.collider.name);
  161.                     velocity.x = (hit.distance - skinWidth) * directionX;
  162.                     rayLength = hit.distance;
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.     void VerticalCollisions(ref Vector3 velocity)
  169.     {
  170.         float directionY = Mathf.Sign(velocity.y);
  171.         float rayLength = Mathf.Abs(velocity.y) + skinWidth;
  172.  
  173.         for (int i = 0; i < verticalRayCount; i++)
  174.         {
  175.             Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
  176.             rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
  177.             RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength);
  178.  
  179.             ///Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.green);
  180.  
  181.             if (hit)
  182.             {
  183.                 if (hit.collider.name == "Exit" || hit.collider.name == "Potion1" || hit.collider.name == "Potion2" || hit.collider.name == "Potion3" || hit.collider.name == "Mist")
  184.                 {
  185.                     return;
  186.                 }
  187.                 if (hit.collider.name == "Lava" && golem)
  188.                 {
  189.                     return;
  190.                 }
  191.                 if (hit.collider.name == "Fence" && ghost)
  192.                 {
  193.                     return;
  194.                 }
  195.                 if (hit.collider.name == "Water" && seamonster)
  196.                 {
  197.                     return;
  198.                 }
  199.                 else
  200.                 {
  201.                     velocity.y = (hit.distance - skinWidth) * directionY;
  202.                     rayLength = hit.distance;
  203.                 }
  204.             }
  205.         }
  206.     }
  207.  
  208.     void UpdateRaycastOrigins()
  209.     {
  210.         Bounds bounds = col.bounds;
  211.         bounds.Expand(skinWidth * -2);
  212.  
  213.         raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
  214.         raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
  215.         raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
  216.         raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
  217.     }
  218.  
  219.     void CalculateRaySpacing()
  220.     {
  221.         Bounds bounds = col.bounds;
  222.         bounds.Expand(skinWidth * -2);
  223.  
  224.         horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue);
  225.         verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue);
  226.  
  227.         horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
  228.         verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
  229.     }
  230.  
  231.     struct RaycastOrigins
  232.     {
  233.         public Vector2 topLeft, topRight;
  234.         public Vector2 bottomLeft, bottomRight;
  235.     }
  236.  
  237.     void OnTriggerEnter2D(Collider2D collision)
  238.     {
  239.         if (collision.name == "Exit")
  240.         {
  241.             //Debug.Log("Next Level");
  242.             StartCoroutine("NextLevel");
  243.             exitParticles.SetActive(true);
  244.         }
  245.  
  246.         if (collision.name == "Potion1")
  247.         {
  248.             if (potion1Count==9)
  249.             {
  250.                 return;
  251.             }
  252.             collision.gameObject.SetActive(false);
  253.             potion1Count++;
  254.             updateUI();
  255.             sfx.PlayOneShot(pickup);
  256.         }
  257.  
  258.         if (collision.name == "Potion2")
  259.         {
  260.             if (potion2Count == 9)
  261.             {
  262.                 return;
  263.             }
  264.             collision.gameObject.SetActive(false);
  265.             potion2Count++;
  266.             updateUI();
  267.             sfx.PlayOneShot(pickup);
  268.         }
  269.  
  270.         if (collision.name == "Potion3")
  271.         {
  272.             if (potion3Count == 9)
  273.             {
  274.                 return;
  275.             }
  276.             collision.gameObject.SetActive(false);
  277.             potion3Count++;
  278.             updateUI();
  279.             sfx.PlayOneShot(pickup);
  280.         }
  281.  
  282.         if (collision.name == "Mist")
  283.         {
  284.             golem = false;
  285.             ghost = false;
  286.             seamonster = false;
  287.             anim.SetBool("Golem", golem);
  288.             anim.SetBool("Ghost", ghost);
  289.             anim.SetBool("Seamonster", seamonster);
  290.             int ready = Animator.StringToHash("wizard_down");
  291.             anim.Play(ready);
  292.             moveSpeed = wizardSpeed;
  293.             if (!bottleParticles.activeSelf)
  294.             {
  295.                 bottleParticles.SetActive(true);
  296.                 sfx.PlayOneShot(effect);
  297.             }
  298.         }
  299.     }
  300.  
  301.     void DrinkPotion (int potion)
  302.     {
  303.         if (potion==1 && !golem && potion1Count>0)
  304.         {
  305.             potion1Count--;
  306.             updateUI();
  307.             golem = true;
  308.             ghost = false;
  309.             seamonster = false;
  310.             anim.SetBool("Golem", golem);
  311.             anim.SetBool("Ghost", ghost);
  312.             anim.SetBool("Seamonster", seamonster);
  313.             int ready = Animator.StringToHash("golem_down");
  314.             anim.Play(ready);
  315.             moveSpeed = golemSpeed;
  316.             bottleParticles.SetActive(false);
  317.             golemParticles.SetActive(true);
  318.             ghostParticles.SetActive(false);
  319.             seaParticles.SetActive(false);
  320.             sfx.PlayOneShot(effect);
  321.         }
  322.         if (potion == 2 && !ghost && potion2Count > 0)
  323.         {
  324.             potion2Count--;
  325.             updateUI();
  326.             golem = false;
  327.             ghost = true;
  328.             seamonster = false;
  329.             anim.SetBool("Golem", golem);
  330.             anim.SetBool("Ghost", ghost);
  331.             anim.SetBool("Seamonster", seamonster);
  332.             int ready = Animator.StringToHash("ghost_down");
  333.             anim.Play(ready);
  334.             moveSpeed = ghostSpeed;
  335.             bottleParticles.SetActive(false);
  336.             golemParticles.SetActive(false);
  337.             ghostParticles.SetActive(true);
  338.             seaParticles.SetActive(false);
  339.             sfx.PlayOneShot(effect);
  340.         }
  341.         if (potion == 3 && !seamonster && potion3Count > 0)
  342.         {
  343.             potion3Count--;
  344.             updateUI();
  345.             golem = false;
  346.             ghost = false;
  347.             seamonster = true;
  348.             anim.SetBool("Golem", golem);
  349.             anim.SetBool("Ghost", ghost);
  350.             anim.SetBool("Seamonster", seamonster);
  351.             int ready = Animator.StringToHash("seamonster_down");
  352.             anim.Play(ready);
  353.             moveSpeed = seaSpeed;
  354.             bottleParticles.SetActive(false);
  355.             golemParticles.SetActive(false);
  356.             ghostParticles.SetActive(false);
  357.             seaParticles.SetActive(true);
  358.             sfx.PlayOneShot(effect);
  359.         }
  360.     }
  361.  
  362.     void updateUI()
  363.     {
  364.         potion1Display.text = "x"+ potion1Count;
  365.         potion2Display.text = "x" + potion2Count;
  366.         potion3Display.text = "x" + potion3Count;
  367.     }
  368.  
  369.     IEnumerator NextLevel()
  370.     {
  371.         sfx.PlayOneShot(exit);
  372.         //moveSpeed = 0;
  373.         yield return new WaitForSeconds(0.1f);
  374.         moveSpeed = 0;
  375.         yield return new WaitForSeconds(1f);
  376.         int nextLevel = Application.loadedLevel + 1;
  377.         Application.LoadLevel(nextLevel);
  378.     }
  379.  
  380.     void ResetLevel()
  381.     {
  382.         Application.LoadLevel(Application.loadedLevel);
  383.     }
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement