Guest User

Untitled

a guest
Apr 3rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Generation : MonoBehaviour {
  5.  
  6. GameObject PlayerObject;
  7. GameObject EnemyObject;
  8.  
  9. public GameObject EnemyPrefab;
  10. public GameObject PlayerPrefab;
  11. public GameObject GridPrefab;
  12. public GameObject WallPrefab;
  13.  
  14. public int width;
  15. public int length;
  16.  
  17. [Range(0,1)]
  18. public float gridSpacing;
  19.  
  20. public Vector2 enemyGridPosition;
  21. public Vector2 gridPosition = new Vector2(0,0);
  22.  
  23. public Transform[,] gridTransforms;
  24.  
  25. void CreateStage() {
  26. Camera.main.transform.position = new Vector3(width / 2, length*width, length / 2);
  27. Camera.main.orthographicSize = (length + width) / 2;
  28.  
  29. for(int x = -1; x < width +1; x++)
  30. {
  31. for(int y = -1; y < length+1; y++)
  32. {
  33. if(x == width || x == -1 || y == length || y == -1)
  34. {
  35. GameObject wall;
  36. wall = (GameObject)Instantiate (WallPrefab, new Vector3 (x+(gridSpacing*x), 0, y+(gridSpacing*y)), Quaternion.identity);
  37. wall.name = "Wall X:" + x + "Y:" + y;
  38. }
  39. else
  40. {
  41. GameObject block;
  42. block = (GameObject)Instantiate (GridPrefab, new Vector3 (x+(gridSpacing*x), 0, y+(gridSpacing*y)), Quaternion.identity);
  43. block.name = "X:"+ x + " " + "Y:" + y;
  44.  
  45. gridTransforms[y,x] = block.transform;
  46. }
  47. }
  48. }
  49. }
  50.  
  51. void MoveEnemy()
  52. {
  53. if(Mathf.Abs(gridPosition.x - enemyGridPosition.x) > float.Epsilon)
  54. {
  55. if(gridPosition.y > enemyGridPosition.y)
  56. {
  57. enemyGridPosition.y++;
  58. }
  59. else
  60. {
  61. enemyGridPosition.y--;
  62. }
  63. }
  64. if(Mathf.Abs(gridPosition.y - enemyGridPosition.y) > float.Epsilon)
  65. {
  66. if(gridPosition.x > enemyGridPosition.x)
  67. {
  68. enemyGridPosition.x++;
  69. }
  70. else
  71. {
  72. enemyGridPosition.x--;
  73. }
  74. }
  75. }
  76.  
  77. bool MovePlayer()
  78. {
  79. if(Input.GetKeyDown(KeyCode.UpArrow))
  80. {
  81. if(gridPosition.y != length - 1)
  82. {
  83. gridPosition.y++;
  84. MoveEnemy();
  85. return true;
  86. }
  87. }
  88. if(Input.GetKeyDown(KeyCode.DownArrow))
  89. {
  90. if(gridPosition.y != 0)
  91. {
  92. gridPosition.y--;
  93. MoveEnemy();
  94. return true;
  95. }
  96. }
  97. if(Input.GetKeyDown(KeyCode.RightArrow))
  98. {
  99. if(gridPosition.x != width - 1)
  100. {
  101. gridPosition.x++;
  102. MoveEnemy();
  103. return true;
  104. }
  105. }
  106. if(Input.GetKeyDown(KeyCode.LeftArrow))
  107. {
  108. if(gridPosition.x != 0)
  109. {
  110. gridPosition.x--;
  111. MoveEnemy();
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117.  
  118. void SpawnEnemy()
  119. {
  120. Transform enemySpawnPosition = gridTransforms[length - 1, width - 1];
  121. GameObject spawnEnemy;
  122.  
  123. spawnEnemy = (GameObject)Instantiate(EnemyPrefab, new Vector3(enemySpawnPosition.transform.position.x, 2, enemySpawnPosition.transform.position.z), Quaternion.identity);
  124. spawnEnemy.name = ("Enemy");
  125. enemyGridPosition = new Vector2(width - 1, length - 1);
  126. }
  127.  
  128. void SpawnPlayer()
  129. {
  130. Transform playerSpawnPosition = gridTransforms[0,0];
  131. GameObject spawnPlayer;
  132.  
  133. spawnPlayer = (GameObject)Instantiate(PlayerPrefab, new Vector3(playerSpawnPosition.transform.position.x, 2, playerSpawnPosition.transform.position.z), Quaternion.identity);
  134. spawnPlayer.name = ("Player");
  135. gridPosition = new Vector2(0, 0);
  136. }
  137.  
  138. void Start() {
  139. gridTransforms = new Transform[length,width];
  140.  
  141. CreateStage();
  142. SpawnEnemy();
  143. SpawnPlayer();
  144. }
  145.  
  146. void FixedUpdate() {
  147.  
  148. GameObject FindEnemy;
  149. Transform GridTileEnemyIsOn;
  150.  
  151. GridTileEnemyIsOn = gridTransforms[(int)enemyGridPosition.y,(int)enemyGridPosition.x];
  152. FindEnemy = GameObject.Find("Enemy");
  153. FindEnemy.transform.position = new Vector3(GridTileEnemyIsOn.transform.position.x, 2, GridTileEnemyIsOn.transform.position.z);
  154.  
  155.  
  156. GameObject FindPlayer;
  157. Transform GridTilePlayerIsOn;
  158. if(MovePlayer() == true)
  159. {
  160. GridTilePlayerIsOn = gridTransforms[(int)gridPosition.y,(int)gridPosition.x];
  161. FindPlayer = GameObject.Find("Player");
  162. FindPlayer.transform.position = new Vector3(GridTilePlayerIsOn.transform.position.x, 2, GridTilePlayerIsOn.transform.position.z);
  163. }
  164. }
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment