Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class HeroMove : MonoBehaviour
- {
- Rigidbody2D rb;
- public GameObject SpentObj;
- public GameObject GreenDoorObj;
- public GameObject GreenKeyObj;
- public GameObject BlueDoorObj;
- public GameObject BlueKeyObj;
- public GameObject EmptyObj;
- public GameObject HeroObj;
- int CurY;
- int CurX;
- int[,] LevelInfo;
- public int Color;
- public int KeyX = 100, KeyY = 100;
- public int DoorX = 100, DoorY = 100;
- public int DoorColor;
- // Use this for initialization
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- LevelInfo = Camera.main.GetComponent<LevelMatrix>().Level;
- CurX = Camera.main.GetComponent<LevelMatrix>().HeroX;
- CurY = Camera.main.GetComponent<LevelMatrix>().HeroY;
- rb.GetComponent<SpriteRenderer>().sprite = HeroObj.GetComponent<SpriteRenderer>().sprite;
- }
- // Update is called once per frame
- void Update()
- {
- //gameObject.getComponent<SpriteRenderer>().sprite = ..
- //Movement, Door/Key checks and delete if necessery
- if (KeyCheck(CurX, CurY) != -1)
- {
- Color = KeyCheck(CurX, CurY);
- if (Color == 2)
- {
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- if (Color == 3)
- {
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- // Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- }
- if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
- {
- if (check(CurX + 1, CurY))
- {
- if (DoorCheck(CurX + 1, CurY) != -1)
- {
- DoorColor = DoorCheck(CurX + 1, CurY);
- }
- if (KeyCheck(CurX + 1, CurY) != -1)
- {
- Color = KeyCheck(CurX + 1, CurY);
- if (Color == 2)
- {
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- if (Color == 3)
- {
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- // Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- }
- if (DoorColor == Color)
- {
- Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[DoorY, DoorX] = 0;
- }
- Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[CurY, CurX] = 1;
- rb.transform.Translate(0.32f, 0, 0);
- CurX++;
- }
- }
- if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
- {
- if (check(CurX, CurY + 1))
- {
- if (DoorCheck(CurX, CurY + 1) != -1)
- {
- DoorColor = DoorCheck(CurX, CurY + 1);
- }
- if (KeyCheck(CurX, CurY + 1) != -1)
- {
- Color = KeyCheck(CurX, CurY + 1);
- if (Color == 2)
- {
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- if (Color == 3)
- {
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- // Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- }
- if (DoorColor == Color)
- {
- Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[DoorY, DoorX] = 0;
- }
- Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[CurY, CurX] = 1;
- rb.transform.Translate(0, 0.32f, 0);
- CurY++;
- }
- }
- if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
- {
- if (check(CurX - 1, CurY))
- {
- if (DoorCheck(CurX - 1, CurY) != -1)
- {
- DoorColor = DoorCheck(CurX - 1, CurY);
- }
- if (KeyCheck(CurX - 1, CurY) != -1)
- {
- Color = KeyCheck(CurX - 1, CurY);
- if (Color == 2)
- {
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- if (Color == 3)
- {
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- // Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- }
- if (DoorColor == Color)
- {
- Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[DoorY, DoorX] = 0;
- }
- Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[CurY, CurX] = 1;
- rb.transform.Translate(-0.32f, 0, 0);
- CurX--;
- }
- }
- if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
- {
- if (check(CurX, CurY - 1))
- {
- if (DoorCheck(CurX, CurY - 1) != -1)
- {
- DoorColor = DoorCheck(CurX, CurY - 1);
- }
- if (KeyCheck(CurX, CurY - 1) != -1)
- {
- Color = KeyCheck(CurX, CurY - 1);
- if (Color == 2)
- {
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- //Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- if (Color == 3)
- {
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- // Instantiate(EmptyObj, new Vector3(0.32f * KeyX - 3.3f, 0.32f * KeyY - 2.2f, 0f), Quaternion.identity);
- }
- }
- if (DoorColor == Color)
- {
- Instantiate(EmptyObj, new Vector3(0.32f * DoorX - 3.3f, 0.32f * DoorY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[DoorY, DoorX] = 0;
- }
- Instantiate(SpentObj, new Vector3(0.32f * CurX - 3.3f, 0.32f * CurY - 2.2f, 0f), Quaternion.identity);
- LevelInfo[CurY, CurX] = 1;
- rb.transform.Translate(0, -0.32f, 0);
- CurY--;
- }
- }
- //Restart
- if (Input.GetKeyDown(KeyCode.R))
- {
- Application.LoadLevel("Level");
- }
- }
- void FixedUpdate()
- {
- //rb.velocity = new Vector2(Input.GetAxis("Horizontal") * 10f, rb.velocity.y);
- }
- int DoorCheck(int i, int j)
- {
- if (LevelInfo[j, i - 1] == 2)
- {
- DoorY = j;
- DoorX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j, i + 1] == 2)
- {
- DoorY = j;
- DoorX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i] == 2)
- {
- DoorY = j - 1;
- DoorX = i;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i] == 2)
- {
- DoorY = j + 1;
- DoorX = i;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i - 1] == 2)
- {
- DoorY = j - 1;
- DoorX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i + 1] == 2)
- {
- DoorY = j + 1;
- DoorX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i + 1] == 2)
- {
- DoorY = j - 1;
- DoorX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i - 1] == 2)
- {
- DoorY = j + 1;
- DoorX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j, i - 1] == 3)
- {
- DoorY = j;
- DoorX = i - 1;
- return 3;
- }
- else
- if (LevelInfo[j, i + 1] == 3)
- {
- DoorY = j;
- DoorX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i] == 3)
- {
- DoorY = j - 1;
- DoorX = i;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i] == 3)
- {
- DoorY = j + 1;
- DoorX = i;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i - 1] == 3)
- {
- DoorY = j - 1;
- DoorX = i - 1;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i + 1] == 3)
- {
- DoorY = j + 1;
- DoorX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i + 1] == 3)
- {
- DoorY = j - 1;
- DoorX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i - 1] == 3)
- {
- DoorY = j + 1;
- DoorX = i - 1;
- return 3;
- }
- if (LevelInfo[j + 1, i] == 4)
- {
- DoorY = j + 1;
- DoorX = i;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i] == 4)
- {
- DoorY = j - 1;
- DoorX = i;
- return 4;
- }
- else
- if (LevelInfo[j, i - 1] == 4)
- {
- DoorY = j;
- DoorX = i - 1;
- return 4;
- }
- else
- if (LevelInfo[j, i + 1] == 4)
- {
- DoorY = j;
- DoorX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i - 1] == 4)
- {
- DoorY = j - 1;
- DoorX = i - 1;
- return 4;
- }
- else
- if (LevelInfo[j + 1, i + 1] == 4)
- {
- DoorY = j + 1;
- DoorX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i + 1] == 4)
- {
- DoorY = j - 1;
- DoorX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j + 1, i - 1] == 4)
- {
- DoorY = j + 1;
- DoorX = i - 1;
- return 4;
- }
- else
- {
- return -1;
- }
- }
- int KeyCheck(int i, int j)
- {
- if (LevelInfo[j, i - 1] == -2)
- {
- KeyY = j;
- KeyX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j, i + 1] == -2)
- {
- KeyY = j;
- KeyX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i] == -2)
- {
- KeyY = j - 1;
- KeyX = i;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i] == -2)
- {
- KeyY = j + 1;
- KeyX = i;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i - 1] == -2)
- {
- KeyY = j - 1;
- KeyX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i + 1] == -2)
- {
- KeyY = j + 1;
- KeyX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j - 1, i + 1] == -2)
- {
- KeyY = j - 1;
- KeyX = i + 1;
- return 2;
- }
- else
- if (LevelInfo[j + 1, i - 1] == -2)
- {
- KeyY = j + 1;
- KeyX = i - 1;
- return 2;
- }
- else
- if (LevelInfo[j, i - 1] == -3)
- {
- KeyY = j;
- KeyX = i - 1;
- return 3;
- }
- else
- if (LevelInfo[j, i + 1] == -3)
- {
- KeyY = j;
- KeyX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i] == -3)
- {
- KeyY = j - 1;
- KeyX = i;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i] == -3)
- {
- KeyY = j + 1;
- KeyX = i;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i - 1] == -3)
- {
- KeyY = j - 1;
- KeyX = i - 1;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i + 1] == -3)
- {
- KeyY = j + 1;
- KeyX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j - 1, i + 1] == -3)
- {
- KeyY = j - 1;
- KeyX = i + 1;
- return 3;
- }
- else
- if (LevelInfo[j + 1, i - 1] == -3)
- {
- KeyY = j + 1;
- KeyX = i - 1;
- return 3;
- }
- if (LevelInfo[j + 1, i] == -4)
- {
- KeyY = j + 1;
- KeyX = i;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i] == -4)
- {
- KeyY = j - 1;
- KeyX = i;
- return 4;
- }
- else
- if (LevelInfo[j, i - 1] == -4)
- {
- KeyY = j;
- KeyX = i - 1;
- return 4;
- }
- else
- if (LevelInfo[j, i + 1] == -4)
- {
- KeyY = j;
- KeyX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i - 1] == -4)
- {
- KeyY = j - 1;
- KeyX = i - 1;
- return 4;
- }
- else
- if (LevelInfo[j + 1, i + 1] == -4)
- {
- KeyY = j + 1;
- KeyX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j - 1, i + 1] == -4)
- {
- KeyY = j - 1;
- KeyX = i + 1;
- return 4;
- }
- else
- if (LevelInfo[j + 1, i - 1] == -4)
- {
- KeyY = j + 1;
- KeyX = i - 1;
- return 4;
- }
- else
- {
- return -1;
- }
- }
- bool check(int i, int j)
- {
- return (LevelInfo[j, i] != 1 && LevelInfo[j, i] != 2 && LevelInfo[j, i] != 3 && LevelInfo[j, i] != -2 && LevelInfo[j, i] != -3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement