Advertisement
Guest User

Untitled

a guest
May 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 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 PlayerX = 100, PlayerY = 100;
  24.     public int DoorX = 100, DoorY = 100;
  25.     public int DoorColor;
  26.  
  27.     int[] dx = new[] { 0, 0, 1, -1, 1, -1, -1, 1 };
  28.     int[] dy = new[] { 1, -1, 0, 0, 1, 1, -1, -1 };
  29.  
  30.     // Use this for initialization
  31.     void Start()
  32.     {
  33.         rb = GetComponent<Rigidbody2D>();
  34.         LevelInfo = Camera.main.GetComponent<LevelMatrix>().Level;
  35.         CurX = Camera.main.GetComponent<LevelMatrix>().HeroX;
  36.         CurY = Camera.main.GetComponent<LevelMatrix>().HeroY;
  37.         rb.GetComponent<SpriteRenderer>().sprite = HeroObj.GetComponent<SpriteRenderer>().sprite;
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void Update()
  42.     {
  43.         //gameObject.getComponent<SpriteRenderer>().sprite = ..
  44.  
  45.         //Movement, Door/Key checks and delete if necessary
  46.  
  47.  
  48.         if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  49.         {
  50.             jump(1, 0);
  51.         }
  52.  
  53.         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  54.         {
  55.             jump(0, 1);
  56.         }
  57.  
  58.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  59.         {
  60.             jump(-1, 0);
  61.         }
  62.  
  63.         if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  64.         {
  65.             jump(0, -1);
  66.         }
  67.  
  68.         //Restart
  69.         if (Input.GetKeyDown(KeyCode.R))
  70.         {
  71.             Application.LoadLevel("Level");
  72.         }
  73.  
  74.         for (int i = 0; i < 15; ++i)
  75.         {
  76.             for (int j = 0; j < 20; ++j)
  77.             {
  78.                 if (LevelInfo[i, j] == 2 || LevelInfo[i, j] == 3)
  79.                 {
  80.                     if (Color == LevelInfo[i, j])
  81.                     {
  82.                         for (int e = 0; e < 8; ++e)
  83.                         {
  84.                             if (CoordCheck(i + dx[e], j + dy[e]) == 6)
  85.                             {
  86.                                 Instantiate(EmptyObj, new Vector3(0.32f * j - 3.3f, 0.32f * i - 2.2f, 0f), Quaternion.identity);
  87.                                 LevelInfo[i, j] = 0;
  88.                             }
  89.                         }
  90.                     }
  91.                 }
  92.                 if (LevelInfo[i, j] == -2 || LevelInfo[i, j] == -3)
  93.                 {
  94.                     for (int e = 0; e < 8; ++e)
  95.                     {
  96.                         if (CoordCheck(i + dx[e], j + dy[e]) == 6)
  97.                         {
  98.                             Color = -LevelInfo[i, j];
  99.                             switch(LevelInfo[i, j])
  100.                             {
  101.                                 case -2:
  102.                                     rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
  103.                                     break;
  104.  
  105.                                 case -3:
  106.                                     rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
  107.                                     break;
  108.                             }
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     int CoordCheck(int x, int y)
  117.     {
  118.         if (x >= 0 && x <= 14 && y >= 0 && y <= 19)
  119.         {
  120.             return LevelInfo[x, y];
  121.         }
  122.         return 47;
  123.     }
  124.  
  125.     bool check(int i, int j)
  126.     {
  127.         return (LevelInfo[j, i] != 1 && LevelInfo[j, i] != 2 && LevelInfo[j, i] != 3 && LevelInfo[j, i] != -2 && LevelInfo[j, i] != -3);
  128.     }
  129.  
  130.     void jump(int i, int j)
  131.     {
  132.         if (check(CurX + i, CurY + j))
  133.         {
  134.             Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
  135.             LevelInfo[CurY, CurX] = 1;
  136.             rb.transform.Translate(0.32f * i, 0.32f * j, 0);
  137.             CurX += i;
  138.             CurY += j;
  139.             LevelInfo[CurY, CurX] = 6;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement