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 PlayerX = 100, PlayerY = 100;
- public int DoorX = 100, DoorY = 100;
- public int DoorColor;
- int[] dx = new[] { 0, 0, 1, -1, 1, -1, -1, 1 };
- int[] dy = new[] { 1, -1, 0, 0, 1, 1, -1, -1 };
- // 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 necessary
- if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
- {
- jump(1, 0);
- }
- if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
- {
- jump(0, 1);
- }
- if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
- {
- jump(-1, 0);
- }
- if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
- {
- jump(0, -1);
- }
- //Restart
- if (Input.GetKeyDown(KeyCode.R))
- {
- Application.LoadLevel("Level");
- }
- for (int i = 0; i < 15; ++i)
- {
- for (int j = 0; j < 20; ++j)
- {
- if (LevelInfo[i, j] == 2 || LevelInfo[i, j] == 3)
- {
- if (Color == LevelInfo[i, j])
- {
- for (int e = 0; e < 8; ++e)
- {
- if (CoordCheck(i + dx[e], j + dy[e]) == 6)
- {
- Instantiate(EmptyObj, new Vector3(0.32f * j - 3.3f, 0.32f * i - 2.2f, 0f), Quaternion.identity);
- LevelInfo[i, j] = 0;
- }
- }
- }
- }
- if (LevelInfo[i, j] == -2 || LevelInfo[i, j] == -3)
- {
- for (int e = 0; e < 8; ++e)
- {
- if (CoordCheck(i + dx[e], j + dy[e]) == 6)
- {
- Color = -LevelInfo[i, j];
- switch(LevelInfo[i, j])
- {
- case -2:
- rb.GetComponent<SpriteRenderer>().sprite = GreenKeyObj.GetComponent<SpriteRenderer>().sprite;
- break;
- case -3:
- rb.GetComponent<SpriteRenderer>().sprite = BlueKeyObj.GetComponent<SpriteRenderer>().sprite;
- break;
- }
- }
- }
- }
- }
- }
- }
- int CoordCheck(int x, int y)
- {
- if (x >= 0 && x <= 14 && y >= 0 && y <= 19)
- {
- return LevelInfo[x, y];
- }
- return 47;
- }
- 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);
- }
- void jump(int i, int j)
- {
- if (check(CurX + i, CurY + j))
- {
- 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 * i, 0.32f * j, 0);
- CurX += i;
- CurY += j;
- LevelInfo[CurY, CurX] = 6;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement