Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HeroMove : MonoBehaviour
  6. {
  7.  
  8.     Rigidbody2D rb;
  9.  
  10.     public GameObject SpentObj;
  11.     public GameObject GreenDoorObj;
  12.     public GameObject GreenKeyObj;
  13.     public GameObject BlueDoorObj;
  14.     public GameObject BlueKeyObj;
  15.     public GameObject EmptyObj;
  16.     public GameObject HeroObj;
  17.  
  18.     int CurY;
  19.     int CurX;
  20.     int[,] LevelInfo;
  21.     public int Color;
  22.     public int KeyX = 100, KeyY = 100;
  23.     public int DoorX = 100, DoorY = 100;
  24.     public int DoorColor;
  25.  
  26.     // Use this for initialization
  27.     void Start()
  28.     {
  29.         rb = GetComponent<Rigidbody2D>();
  30.         LevelInfo = Camera.main.GetComponent<LevelMatrix>().Level;
  31.         CurX = Camera.main.GetComponent<LevelMatrix>().HeroX;
  32.         CurY = Camera.main.GetComponent<LevelMatrix>().HeroY;
  33.         rb.GetComponent<SpriteRenderer>().sprite = HeroObj.GetComponent<SpriteRenderer>().sprite;
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         //gameObject.getComponent<SpriteRenderer>().sprite = ..
  40.        
  41.         //Movement, Door/Key checks and delete if necessery
  42.         if (KeyCheck(CurX, CurY) != -1)
  43.         {
  44.             Color = KeyCheck(CurX, CurY);
  45.             if (Color == 2)
  46.             {
  47.                 rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  48.                 //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  49.             }
  50.             if (Color == 3)
  51.             {
  52.                 rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  53.                 //    Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  54.             }
  55.         }
  56.  
  57.         if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  58.         {
  59.             if (check(CurX + 1, CurY))
  60.             {
  61.                 if (DoorCheck(CurX + 1, CurY) != -1)
  62.                 {
  63.                     DoorColor = DoorCheck(CurX + 1, CurY);
  64.                 }
  65.                 if (KeyCheck(CurX + 1, CurY) != -1)
  66.                 {
  67.                     Color = KeyCheck(CurX + 1, CurY);
  68.                     if (Color == 2)
  69.                     {
  70.                         rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  71.                         //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  72.                     }
  73.                     if (Color == 3)
  74.                     {
  75.                         rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  76.                         //    Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  77.                     }
  78.                 }
  79.                 if (DoorColor == Color)
  80.                 {
  81.                     Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
  82.                     LevelInfo[DoorY, DoorX] = 0;
  83.                 }
  84.                 Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
  85.                 LevelInfo[CurY, CurX] = 1;
  86.                 rb.transform.Translate(0.32f, 0, 0);
  87.                 CurX++;
  88.             }
  89.         }
  90.         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  91.         {
  92.             if (check(CurX, CurY + 1))
  93.             {
  94.                 if (DoorCheck(CurX, CurY + 1) != -1)
  95.                 {
  96.                     DoorColor = DoorCheck(CurX, CurY + 1);
  97.                 }
  98.                 if (KeyCheck(CurX, CurY + 1) != -1)
  99.                 {
  100.                     Color = KeyCheck(CurX, CurY + 1);
  101.                     if (Color == 2)
  102.                     {
  103.                         rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  104.                         //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  105.                     }
  106.                     if (Color == 3)
  107.                     {
  108.                         rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  109.                         //    Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  110.                     }
  111.                 }
  112.                 if (DoorColor == Color)
  113.                 {
  114.                     Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
  115.                     LevelInfo[DoorY, DoorX] = 0;
  116.                 }
  117.                 Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
  118.                 LevelInfo[CurY, CurX] = 1;
  119.                 rb.transform.Translate(0, 0.32f, 0);
  120.                 CurY++;
  121.             }
  122.         }
  123.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  124.         {
  125.             if (check(CurX - 1, CurY))
  126.             {
  127.                 if (DoorCheck(CurX - 1, CurY) != -1)
  128.                 {
  129.                     DoorColor = DoorCheck(CurX - 1, CurY);
  130.                 }
  131.                 if (KeyCheck(CurX - 1, CurY) != -1)
  132.                 {
  133.                     Color = KeyCheck(CurX - 1, CurY);
  134.                     if (Color == 2)
  135.                     {
  136.                         rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  137.                         //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  138.                     }
  139.                     if (Color == 3)
  140.                     {
  141.                         rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  142.                         //    Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  143.                     }
  144.                 }
  145.                 if (DoorColor == Color)
  146.                 {
  147.                     Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
  148.                     LevelInfo[DoorY, DoorX] = 0;
  149.                 }
  150.                 Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
  151.                 LevelInfo[CurY, CurX] = 1;
  152.                 rb.transform.Translate(-0.32f, 0, 0);
  153.                 CurX--;
  154.             }
  155.         }
  156.         if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  157.         {
  158.             if (check(CurX, CurY - 1))
  159.             {
  160.                 if (DoorCheck(CurX, CurY - 1) != -1)
  161.                 {
  162.                     DoorColor = DoorCheck(CurX, CurY - 1);
  163.                 }
  164.                 if (KeyCheck(CurX, CurY - 1) != -1)
  165.                 {
  166.                     Color = KeyCheck(CurX, CurY - 1);
  167.                     if (Color == 2)
  168.                     {
  169.                         rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  170.                         //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  171.                     }
  172.                     if (Color == 3)
  173.                     {
  174.                         rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  175.                         //    Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
  176.                     }
  177.                 }
  178.                 if (DoorColor == Color)
  179.                 {
  180.                     Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
  181.                     LevelInfo[DoorY, DoorX] = 0;
  182.                 }
  183.                 Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
  184.                 LevelInfo[CurY, CurX] = 1;
  185.                 rb.transform.Translate(0, -0.32f, 0);
  186.                 CurY--;
  187.             }
  188.         }
  189.         //Restart
  190.         if (Input.GetKeyDown(KeyCode.R))
  191.         {
  192.             Application.LoadLevel("Level");
  193.         }
  194.  
  195.     }
  196.  
  197.     void FixedUpdate()
  198.     {
  199.         //rb.velocity = new Vector2(Input.GetAxis("Horizontal") * 10f, rb.velocity.y);
  200.     }
  201.  
  202.     int DoorCheck(int i, int j)
  203.     {
  204.         if (LevelInfo[j, i - 1] == 2)
  205.         {
  206.             DoorY = j;
  207.             DoorX = i - 1;
  208.             return 2;
  209.         }
  210.         else
  211.         if (LevelInfo[j, i + 1] == 2)
  212.         {
  213.             DoorY = j;
  214.             DoorX = i + 1;
  215.             return 2;
  216.         }
  217.         else
  218.         if (LevelInfo[j - 1, i] == 2)
  219.         {
  220.             DoorY = j - 1;
  221.             DoorX = i;
  222.             return 2;
  223.         }
  224.         else
  225.         if (LevelInfo[j + 1, i] == 2)
  226.         {
  227.             DoorY = j + 1;
  228.             DoorX = i;
  229.             return 2;
  230.         }
  231.         else
  232.         if (LevelInfo[j - 1, i - 1] == 2)
  233.         {
  234.             DoorY = j - 1;
  235.             DoorX = i - 1;
  236.             return 2;
  237.         }
  238.         else
  239.         if (LevelInfo[j + 1, i + 1] == 2)
  240.         {
  241.             DoorY = j + 1;
  242.             DoorX = i + 1;
  243.             return 2;
  244.         }
  245.         else
  246.         if (LevelInfo[j - 1, i + 1] == 2)
  247.         {
  248.             DoorY = j - 1;
  249.             DoorX = i + 1;
  250.             return 2;
  251.         }
  252.         else
  253.         if (LevelInfo[j + 1, i - 1] == 2)
  254.         {
  255.             DoorY = j + 1;
  256.             DoorX = i - 1;
  257.             return 2;
  258.         }
  259.         else
  260.         if (LevelInfo[j, i - 1] == 3)
  261.         {
  262.             DoorY = j;
  263.             DoorX = i - 1;
  264.             return 3;
  265.         }
  266.         else
  267.         if (LevelInfo[j, i + 1] == 3)
  268.         {
  269.             DoorY = j;
  270.             DoorX = i + 1;
  271.             return 3;
  272.         }
  273.         else
  274.         if (LevelInfo[j - 1, i] == 3)
  275.         {
  276.             DoorY = j - 1;
  277.             DoorX = i;
  278.             return 3;
  279.         }
  280.         else
  281.         if (LevelInfo[j + 1, i] == 3)
  282.         {
  283.             DoorY = j + 1;
  284.             DoorX = i;
  285.             return 3;
  286.         }
  287.         else
  288.         if (LevelInfo[j - 1, i - 1] == 3)
  289.         {
  290.             DoorY = j - 1;
  291.             DoorX = i - 1;
  292.             return 3;
  293.         }
  294.         else
  295.         if (LevelInfo[j + 1, i + 1] == 3)
  296.         {
  297.             DoorY = j + 1;
  298.             DoorX = i + 1;
  299.             return 3;
  300.         }
  301.         else
  302.         if (LevelInfo[j - 1, i + 1] == 3)
  303.         {
  304.             DoorY = j - 1;
  305.             DoorX = i + 1;
  306.             return 3;
  307.         }
  308.         else
  309.         if (LevelInfo[j + 1, i - 1] == 3)
  310.         {
  311.             DoorY = j + 1;
  312.             DoorX = i - 1;
  313.             return 3;
  314.         }
  315.         if (LevelInfo[j + 1, i] == 4)
  316.         {
  317.             DoorY = j + 1;
  318.             DoorX = i;
  319.             return 4;
  320.         }
  321.         else
  322.         if (LevelInfo[j - 1, i] == 4)
  323.         {
  324.             DoorY = j - 1;
  325.             DoorX = i;
  326.             return 4;
  327.         }
  328.         else
  329.         if (LevelInfo[j, i - 1] == 4)
  330.         {
  331.             DoorY = j;
  332.             DoorX = i - 1;
  333.             return 4;
  334.         }
  335.         else
  336.         if (LevelInfo[j, i + 1] == 4)
  337.         {
  338.             DoorY = j;
  339.             DoorX = i + 1;
  340.             return 4;
  341.         }
  342.         else
  343.         if (LevelInfo[j - 1, i - 1] == 4)
  344.         {
  345.             DoorY = j - 1;
  346.             DoorX = i - 1;
  347.             return 4;
  348.         }
  349.         else
  350.         if (LevelInfo[j + 1, i + 1] == 4)
  351.         {
  352.             DoorY = j + 1;
  353.             DoorX = i + 1;
  354.             return 4;
  355.         }
  356.         else
  357.         if (LevelInfo[j - 1, i + 1] == 4)
  358.         {
  359.             DoorY = j - 1;
  360.             DoorX = i + 1;
  361.             return 4;
  362.         }
  363.         else
  364.         if (LevelInfo[j + 1, i - 1] == 4)
  365.         {
  366.             DoorY = j + 1;
  367.             DoorX = i - 1;
  368.             return 4;
  369.         }
  370.         else
  371.         {
  372.             return -1;
  373.         }
  374.     }
  375.  
  376.     int KeyCheck(int i, int j)
  377.     {
  378.         if (LevelInfo[j, i - 1] == -2)
  379.         {
  380.             KeyY = j;
  381.             KeyX = i - 1;
  382.             return 2;
  383.         }
  384.         else
  385.         if (LevelInfo[j, i + 1] == -2)
  386.         {
  387.             KeyY = j;
  388.             KeyX = i + 1;
  389.             return 2;
  390.         }
  391.         else
  392.         if (LevelInfo[j - 1, i] == -2)
  393.         {
  394.             KeyY = j - 1;
  395.             KeyX = i;
  396.             return 2;
  397.         }
  398.         else
  399.         if (LevelInfo[j + 1, i] == -2)
  400.         {
  401.             KeyY = j + 1;
  402.             KeyX = i;
  403.             return 2;
  404.         }
  405.         else
  406.         if (LevelInfo[j - 1, i - 1] == -2)
  407.         {
  408.             KeyY = j - 1;
  409.             KeyX = i - 1;
  410.             return 2;
  411.         }
  412.         else
  413.         if (LevelInfo[j + 1, i + 1] == -2)
  414.         {
  415.             KeyY = j + 1;
  416.             KeyX = i + 1;
  417.             return 2;
  418.         }
  419.         else
  420.         if (LevelInfo[j - 1, i + 1] == -2)
  421.         {
  422.             KeyY = j - 1;
  423.             KeyX = i + 1;
  424.             return 2;
  425.         }
  426.         else
  427.         if (LevelInfo[j + 1, i - 1] == -2)
  428.         {
  429.             KeyY = j + 1;
  430.             KeyX = i - 1;
  431.             return 2;
  432.         }
  433.         else
  434.         if (LevelInfo[j, i - 1] == -3)
  435.         {
  436.             KeyY = j;
  437.             KeyX = i - 1;
  438.             return 3;
  439.         }
  440.         else
  441.         if (LevelInfo[j, i + 1] == -3)
  442.         {
  443.             KeyY = j;
  444.             KeyX = i + 1;
  445.             return 3;
  446.         }
  447.         else
  448.         if (LevelInfo[j - 1, i] == -3)
  449.         {
  450.             KeyY = j - 1;
  451.             KeyX = i;
  452.             return 3;
  453.         }
  454.         else
  455.         if (LevelInfo[j + 1, i] == -3)
  456.         {
  457.             KeyY = j + 1;
  458.             KeyX = i;
  459.             return 3;
  460.         }
  461.         else
  462.         if (LevelInfo[j - 1, i - 1] == -3)
  463.         {
  464.             KeyY = j - 1;
  465.             KeyX = i - 1;
  466.             return 3;
  467.         }
  468.         else
  469.         if (LevelInfo[j + 1, i + 1] == -3)
  470.         {
  471.             KeyY = j + 1;
  472.             KeyX = i + 1;
  473.             return 3;
  474.         }
  475.         else
  476.         if (LevelInfo[j - 1, i + 1] == -3)
  477.         {
  478.             KeyY = j - 1;
  479.             KeyX = i + 1;
  480.             return 3;
  481.         }
  482.         else
  483.         if (LevelInfo[j + 1, i - 1] == -3)
  484.         {
  485.             KeyY = j + 1;
  486.             KeyX = i - 1;
  487.             return 3;
  488.         }
  489.         if (LevelInfo[j + 1, i] == -4)
  490.         {
  491.             KeyY = j + 1;
  492.             KeyX = i;
  493.             return 4;
  494.         }
  495.         else
  496.         if (LevelInfo[j - 1, i] == -4)
  497.         {
  498.             KeyY = j - 1;
  499.             KeyX = i;
  500.             return 4;
  501.         }
  502.         else
  503.         if (LevelInfo[j, i - 1] == -4)
  504.         {
  505.             KeyY = j;
  506.             KeyX = i - 1;
  507.             return 4;
  508.         }
  509.         else
  510.         if (LevelInfo[j, i + 1] == -4)
  511.         {
  512.             KeyY = j;
  513.             KeyX = i + 1;
  514.             return 4;
  515.         }
  516.         else
  517.         if (LevelInfo[j - 1, i - 1] == -4)
  518.         {
  519.             KeyY = j - 1;
  520.             KeyX = i - 1;
  521.             return 4;
  522.         }
  523.         else
  524.         if (LevelInfo[j + 1, i + 1] == -4)
  525.         {
  526.             KeyY = j + 1;
  527.             KeyX = i + 1;
  528.             return 4;
  529.         }
  530.         else
  531.         if (LevelInfo[j - 1, i + 1] == -4)
  532.         {
  533.             KeyY = j - 1;
  534.             KeyX = i + 1;
  535.             return 4;
  536.         }
  537.         else
  538.         if (LevelInfo[j + 1, i - 1] == -4)
  539.         {
  540.             KeyY = j + 1;
  541.             KeyX = i - 1;
  542.             return 4;
  543.         }
  544.         else
  545.         {
  546.             return -1;
  547.         }
  548.     }
  549.  
  550.     bool check(int i, int j)
  551.     {
  552.         return (LevelInfo[j, i] != 1 && LevelInfo[j, i] != 2 && LevelInfo[j, i] != 3 && LevelInfo[j, i] != -2 && LevelInfo[j, i] != -3);
  553.     }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement