Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. public class Builder : MonoBehaviour {
  2. //Private vars
  3. private GameObject plane1;
  4. private GameObject plane2;
  5. private GameObject plane3;
  6. public GameObject obstacle;
  7. public GameObject coin;
  8. //Generates random vector3 within given range
  9. public Vector3 generateRandomVector(float xMin, float xMax, float zMin, float zMax){
  10. return new Vector3 (Random.Range (xMin,xMax),0,Random.Range (zMin,zMax)) ;
  11. }
  12. // Use this for initialization
  13. void Start () {
  14. plane1 = Resources.Load ("Plane1") as GameObject;
  15. plane2 = Resources.Load ("Plane2") as GameObject;
  16. plane3 = Resources.Load ("Plane3") as GameObject;
  17. }
  18. //Updates the position of the gameObject, this is the location where the next plane will be spawned
  19. public void updatePosition(){
  20. Vector3 newPosition = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 10);
  21. gameObject.transform.position = newPosition;
  22. }
  23. // This will pick up random plate and add the corresponding nnumber of other objects(coins, obstacles and powerups)
  24. public void generateTerrain(){
  25. if (Random.Range (0, 6) > 3) {
  26. //Clone the plane
  27. GameObject newPlate = Instantiate (plane3, transform.position, transform.rotation) as GameObject;
  28. //Generate objects
  29. generateObjects(newPlate, 7);
  30.  
  31. } else if (Random.Range (0, 4) > 2) {
  32. //Clone the plane
  33. GameObject newPlate = Instantiate (plane2, transform.position, transform.rotation) as GameObject;
  34. //Generate objects
  35. generateObjects(newPlate, 5);
  36. } else {
  37. //Clone the plane
  38. GameObject newPlate = Instantiate (plane1, transform.position, transform.rotation) as GameObject;
  39. //Generate objects
  40. generateObjects(newPlate, 3);
  41. }
  42. //Updating the position so that the next element won't be on the same position as this one
  43. updatePosition();
  44. }
  45. //Generates obstales, coins and powerups
  46. public void generateObjects(GameObject parent, int numOfObjects){
  47. for(int i = 0; i < numOfObjects; i++){
  48. // Generate what kind of object will be spawned
  49. int rand = Random.Range (0, 10);
  50. //Generate random location within the parent
  51. float xMin, xMax,zMin,zMax;
  52. Renderer parentRenderer = parent.GetComponent<Renderer>();
  53. xMin = parentRenderer.bounds.min.x;
  54. zMin = parentRenderer.bounds.min.z;
  55. xMax = parentRenderer.bounds.max.x;
  56. zMax = parentRenderer.bounds.max.z;
  57. Vector3 pos = generateRandomVector(xMin, xMax, zMin, zMax);
  58. //TODO add powerups
  59. if(rand < 6){
  60. //Instantiate obstacle
  61. GameObject newObstale = Instantiate(obstacle, pos, parent.transform.rotation) as GameObject;
  62. newObstale.transform.parent = parent.transform;
  63. }else{
  64. //Instantiate coin
  65. GameObject newObstale = Instantiate(coin, pos, parent.transform.rotation) as GameObject;
  66. newObstale.transform.parent = parent.transform;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. public class PlayerTrigger : MonoBehaviour {
  73. private gameData gameDataInstance = new gameData();
  74. void OnTriggerEnter(Collider collider){
  75. if (collider.gameObject.tag == "Coin") {
  76. gameDataInstance.addCoins(1);
  77. }
  78. if (collider.gameObject.tag == "Obstacle") {
  79. Debug.Log ("Obstacle");
  80. //StorePersisten.storePersistenInstance.Save(gameDataInstance);
  81. }
  82. if (collider.gameObject.tag == "PowerUp") {
  83. // powerup
  84. Debug.Log("powerUp");
  85. }
  86. }
  87. }
  88.  
  89. public class Destroyer : MonoBehaviour {
  90. private Builder builder;
  91. private bool isColliding;
  92. void Start() {
  93. builder = (Builder)FindObjectOfType(typeof(Builder));
  94. Debug.Log("Destroyer");
  95. }
  96. void Update(){
  97. isColliding = false;
  98. }
  99. void OnTriggerEnter(Collider collider){
  100. if (isColliding)
  101. return;
  102. isColliding = true;
  103. if (collider.gameObject.transform.parent != null) {
  104. Destroy (collider.gameObject.transform.parent.gameObject);
  105. Debug.Log ("Destroyed parent");
  106. } else {
  107. Destroy(collider.gameObject);
  108. }
  109. if (builder) {
  110. builder.generateTerrain ();
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement