Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class PlayerController : MonoBehaviour {
  7. Vector3 pos;
  8. float speed = 1.0f;
  9. private Rigidbody2D rb;
  10.  
  11. void Start () {
  12. pos = transform.position; // pozitia initiala
  13.  
  14. }
  15.  
  16. void OnTriggerEnter2D(Collider2D other)
  17. {
  18. if(other.gameObject.CompareTag("Pickup"))
  19. {
  20. Destroy (other.gameObject);
  21. }
  22. }
  23.  
  24.  
  25. void FixedUpdate () {
  26.  
  27. if(Input.GetKey(KeyCode.A) && transform.position == pos) {
  28.  
  29. transform.Translate(-1.0f, 0.0f, 0.0f);
  30. transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * 2*speed);
  31. }
  32.  
  33. if (Input.GetKey (KeyCode.D) && transform.position == pos) {
  34.  
  35. transform.Translate (1.0f, 0.0f, 0.0f);
  36. transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * 2 * speed);
  37. }
  38.  
  39. if (Input.GetKey (KeyCode.W) && transform.position == pos) {
  40.  
  41. transform.Translate (0.0f, 1.0f, 0.0f);
  42. transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
  43. }
  44.  
  45. if (Input.GetKey (KeyCode.S) && transform.position == pos) {
  46.  
  47. transform.Translate (0.0f, -1.0f, 0.0f);
  48. transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
  49. }
  50.  
  51.  
  52. }
  53. void Update(){
  54. pos = transform.position;
  55. }
  56. }
  57.  
  58. using System.Collections;
  59. using System.Collections.Generic;
  60. using UnityEngine;
  61. using System;
  62.  
  63. public class CarrotsGenerator : MonoBehaviour {
  64.  
  65. [SerializeField] int requiredCarrots = 20;
  66. [SerializeField] GameObject carrotPrefab = null;
  67. [SerializeField] int requiredStones = 5;
  68. [SerializeField] GameObject stonesPrefab = null;
  69. [SerializeField] int requiredTrees= 5;
  70. [SerializeField] GameObject treesPrefab = null;
  71. [SerializeField] int requiredCaves = 5;
  72. [SerializeField] GameObject cavesPrefab = null;
  73.  
  74. private void Start () {
  75. if (carrotPrefab == null || requiredCarrots > 10 * 8) { return; } // protect against some errors
  76. if (stonesPrefab == null || requiredStones > 10 * 8) { return; }
  77. if (treesPrefab == null || requiredTrees> 10 * 8) { return; }
  78. if (cavesPrefab == null || requiredCaves > 10 * 8) { return; }
  79.  
  80. bool[,] occupiedPositions = new bool[10, 8]; // 2D array of bools to store if a position is occupied
  81. occupiedPositions[0, 7] = true; // rabbit position (in reality it is -4.5, 3.5)
  82. GenerateObjects(requiredCarrots,occupiedPositions,carrotPrefab);
  83. GenerateObjects(requiredStones,occupiedPositions,stonesPrefab);
  84. GenerateObjects(requiredTrees,occupiedPositions,treesPrefab);
  85. GenerateObjects(requiredCaves,occupiedPositions,cavesPrefab);
  86. }
  87.  
  88. void GenerateObjects(int numberRequired, bool[,] occupiedPositions, GameObject objectPrefab){
  89. for (int i = 0; i < numberRequired; ++i) {
  90. int x, y;
  91. do {
  92. x = UnityEngine.Random.Range (0, 10);
  93. y = UnityEngine.Random.Range (0, 8);
  94. } while (occupiedPositions [x, y]);
  95. if (!objectPrefab.Equals (carrotPrefab)) { // avoid cases when the rabbit is stuck at the very beginning
  96. if (!(((x == 1 && y == 7) && occupiedPositions [0, 6].Equals (true)) ||
  97. ((x == 0 && y == 6) && occupiedPositions [1, 7].Equals (true))))
  98. occupiedPositions [x, y] = true;
  99. Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity);
  100. } else {
  101. occupiedPositions [x, y] = true;
  102. Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity);
  103. }
  104.  
  105. }
  106. }
  107. // Update is called once per frame
  108. void Update () {
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement