Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Generation : MonoBehaviour {
- GameObject PlayerObject;
- GameObject EnemyObject;
- public GameObject EnemyPrefab;
- public GameObject PlayerPrefab;
- public GameObject GridPrefab;
- public GameObject WallPrefab;
- public int width;
- public int length;
- [Range(0,1)]
- public float gridSpacing;
- public Vector2 enemyGridPosition;
- public Vector2 gridPosition = new Vector2(0,0);
- public Transform[,] gridTransforms;
- void CreateStage() {
- Camera.main.transform.position = new Vector3(width / 2, length*width, length / 2);
- Camera.main.orthographicSize = (length + width) / 2;
- for(int x = -1; x < width +1; x++)
- {
- for(int y = -1; y < length+1; y++)
- {
- if(x == width || x == -1 || y == length || y == -1)
- {
- GameObject wall;
- wall = (GameObject)Instantiate (WallPrefab, new Vector3 (x+(gridSpacing*x), 0, y+(gridSpacing*y)), Quaternion.identity);
- wall.name = "Wall X:" + x + "Y:" + y;
- }
- else
- {
- GameObject block;
- block = (GameObject)Instantiate (GridPrefab, new Vector3 (x+(gridSpacing*x), 0, y+(gridSpacing*y)), Quaternion.identity);
- block.name = "X:"+ x + " " + "Y:" + y;
- gridTransforms[y,x] = block.transform;
- }
- }
- }
- }
- void MoveEnemy()
- {
- if(Mathf.Abs(gridPosition.x - enemyGridPosition.x) > float.Epsilon)
- {
- if(gridPosition.y > enemyGridPosition.y)
- {
- enemyGridPosition.y++;
- }
- else
- {
- enemyGridPosition.y--;
- }
- }
- if(Mathf.Abs(gridPosition.y - enemyGridPosition.y) > float.Epsilon)
- {
- if(gridPosition.x > enemyGridPosition.x)
- {
- enemyGridPosition.x++;
- }
- else
- {
- enemyGridPosition.x--;
- }
- }
- }
- bool MovePlayer()
- {
- if(Input.GetKeyDown(KeyCode.UpArrow))
- {
- if(gridPosition.y != length - 1)
- {
- gridPosition.y++;
- MoveEnemy();
- return true;
- }
- }
- if(Input.GetKeyDown(KeyCode.DownArrow))
- {
- if(gridPosition.y != 0)
- {
- gridPosition.y--;
- MoveEnemy();
- return true;
- }
- }
- if(Input.GetKeyDown(KeyCode.RightArrow))
- {
- if(gridPosition.x != width - 1)
- {
- gridPosition.x++;
- MoveEnemy();
- return true;
- }
- }
- if(Input.GetKeyDown(KeyCode.LeftArrow))
- {
- if(gridPosition.x != 0)
- {
- gridPosition.x--;
- MoveEnemy();
- return true;
- }
- }
- return false;
- }
- void SpawnEnemy()
- {
- Transform enemySpawnPosition = gridTransforms[length - 1, width - 1];
- GameObject spawnEnemy;
- spawnEnemy = (GameObject)Instantiate(EnemyPrefab, new Vector3(enemySpawnPosition.transform.position.x, 2, enemySpawnPosition.transform.position.z), Quaternion.identity);
- spawnEnemy.name = ("Enemy");
- enemyGridPosition = new Vector2(width - 1, length - 1);
- }
- void SpawnPlayer()
- {
- Transform playerSpawnPosition = gridTransforms[0,0];
- GameObject spawnPlayer;
- spawnPlayer = (GameObject)Instantiate(PlayerPrefab, new Vector3(playerSpawnPosition.transform.position.x, 2, playerSpawnPosition.transform.position.z), Quaternion.identity);
- spawnPlayer.name = ("Player");
- gridPosition = new Vector2(0, 0);
- }
- void Start() {
- gridTransforms = new Transform[length,width];
- CreateStage();
- SpawnEnemy();
- SpawnPlayer();
- }
- void FixedUpdate() {
- GameObject FindEnemy;
- Transform GridTileEnemyIsOn;
- GridTileEnemyIsOn = gridTransforms[(int)enemyGridPosition.y,(int)enemyGridPosition.x];
- FindEnemy = GameObject.Find("Enemy");
- FindEnemy.transform.position = new Vector3(GridTileEnemyIsOn.transform.position.x, 2, GridTileEnemyIsOn.transform.position.z);
- GameObject FindPlayer;
- Transform GridTilePlayerIsOn;
- if(MovePlayer() == true)
- {
- GridTilePlayerIsOn = gridTransforms[(int)gridPosition.y,(int)gridPosition.x];
- FindPlayer = GameObject.Find("Player");
- FindPlayer.transform.position = new Vector3(GridTilePlayerIsOn.transform.position.x, 2, GridTilePlayerIsOn.transform.position.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment