Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Builder : MonoBehaviour {
- //Private vars
- private GameObject plane1;
- private GameObject plane2;
- private GameObject plane3;
- public GameObject obstacle;
- public GameObject coin;
- //Generates random vector3 within given range
- public Vector3 generateRandomVector(float xMin, float xMax, float zMin, float zMax){
- return new Vector3 (Random.Range (xMin,xMax),0,Random.Range (zMin,zMax)) ;
- }
- // Use this for initialization
- void Start () {
- plane1 = Resources.Load ("Plane1") as GameObject;
- plane2 = Resources.Load ("Plane2") as GameObject;
- plane3 = Resources.Load ("Plane3") as GameObject;
- }
- //Updates the position of the gameObject, this is the location where the next plane will be spawned
- public void updatePosition(){
- Vector3 newPosition = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 10);
- gameObject.transform.position = newPosition;
- }
- // This will pick up random plate and add the corresponding nnumber of other objects(coins, obstacles and powerups)
- public void generateTerrain(){
- if (Random.Range (0, 6) > 3) {
- //Clone the plane
- GameObject newPlate = Instantiate (plane3, transform.position, transform.rotation) as GameObject;
- //Generate objects
- generateObjects(newPlate, 7);
- } else if (Random.Range (0, 4) > 2) {
- //Clone the plane
- GameObject newPlate = Instantiate (plane2, transform.position, transform.rotation) as GameObject;
- //Generate objects
- generateObjects(newPlate, 5);
- } else {
- //Clone the plane
- GameObject newPlate = Instantiate (plane1, transform.position, transform.rotation) as GameObject;
- //Generate objects
- generateObjects(newPlate, 3);
- }
- //Updating the position so that the next element won't be on the same position as this one
- updatePosition();
- }
- //Generates obstales, coins and powerups
- public void generateObjects(GameObject parent, int numOfObjects){
- for(int i = 0; i < numOfObjects; i++){
- // Generate what kind of object will be spawned
- int rand = Random.Range (0, 10);
- //Generate random location within the parent
- float xMin, xMax,zMin,zMax;
- Renderer parentRenderer = parent.GetComponent<Renderer>();
- xMin = parentRenderer.bounds.min.x;
- zMin = parentRenderer.bounds.min.z;
- xMax = parentRenderer.bounds.max.x;
- zMax = parentRenderer.bounds.max.z;
- Vector3 pos = generateRandomVector(xMin, xMax, zMin, zMax);
- //TODO add powerups
- if(rand < 6){
- //Instantiate obstacle
- GameObject newObstale = Instantiate(obstacle, pos, parent.transform.rotation) as GameObject;
- newObstale.transform.parent = parent.transform;
- }else{
- //Instantiate coin
- GameObject newObstale = Instantiate(coin, pos, parent.transform.rotation) as GameObject;
- newObstale.transform.parent = parent.transform;
- }
- }
- }
- }
- public class PlayerTrigger : MonoBehaviour {
- private gameData gameDataInstance = new gameData();
- void OnTriggerEnter(Collider collider){
- if (collider.gameObject.tag == "Coin") {
- gameDataInstance.addCoins(1);
- }
- if (collider.gameObject.tag == "Obstacle") {
- Debug.Log ("Obstacle");
- //StorePersisten.storePersistenInstance.Save(gameDataInstance);
- }
- if (collider.gameObject.tag == "PowerUp") {
- // powerup
- Debug.Log("powerUp");
- }
- }
- }
- public class Destroyer : MonoBehaviour {
- private Builder builder;
- private bool isColliding;
- void Start() {
- builder = (Builder)FindObjectOfType(typeof(Builder));
- Debug.Log("Destroyer");
- }
- void Update(){
- isColliding = false;
- }
- void OnTriggerEnter(Collider collider){
- if (isColliding)
- return;
- isColliding = true;
- if (collider.gameObject.transform.parent != null) {
- Destroy (collider.gameObject.transform.parent.gameObject);
- Debug.Log ("Destroyed parent");
- } else {
- Destroy(collider.gameObject);
- }
- if (builder) {
- builder.generateTerrain ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement