Commit bbd6373744e6dbf6a4175c2444d7893940b6cd1f

Authored by Danish Siddiqui
1 parent d0242802d5
Exists in master

Terrain Generator added, object pool refactored with object creator

Showing 23 changed files with 324 additions and 173 deletions Side-by-side Diff

Assets/_Prefabs/ObstacleCreator.prefab
No preview for this file type
Assets/_Prefabs/Powerups/Coin.prefab
No preview for this file type
Assets/_Prefabs/TerrainCreator.prefab
No preview for this file type
Assets/_Prefabs/TerrainCreator.prefab.meta
  1 +fileFormatVersion: 2
  2 +guid: 9adaca67eae924c1d98fa8ef9315290d
  3 +timeCreated: 1432624430
  4 +licenseType: Free
  5 +NativeFormatImporter:
  6 + userData:
  7 + assetBundleName:
  8 + assetBundleVariant:
Assets/_Prefabs/_Chunks/_Green/AccaciaSavannah.prefab
No preview for this file type
Assets/_Prefabs/_Chunks/_Plain/PlainSavannah.prefab
No preview for this file type
Assets/_Prefabs/_Obstacles/AfricanKid.prefab
No preview for this file type
Assets/_Prefabs/_Obstacles/Chicken.prefab
No preview for this file type
Assets/_Prefabs/_Obstacles/OldTruck.prefab
No preview for this file type
Assets/_Prefabs/_Players/WhiteBoy.prefab
No preview for this file type
Assets/_Scenes/Savanna_Level_Design_Template.unity
No preview for this file type
Assets/_Scripts/BaseObjectCreator.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +/*
  5 + * Works as a baseclass for creator classes
  6 + * Inherits and gets objects from object pool present in BaseObjectPool
  7 + */
  8 +public abstract class BaseObjectCreator : BaseObjectPool {
  9 +
  10 + // Time between each object instantiation
  11 + [Tooltip("Time between each object instantiation")]
  12 + public float SpawningInterval = 3f;
  13 +
  14 + // Time after spawining starts
  15 + [Tooltip("Time after spawining starts")]
  16 + public float SpawnDelay = 5f;
  17 +
  18 + /*
  19 + * Initialises the pool and starts spawining objects with interval
  20 + */
  21 + new void Start(){
  22 + base.Start();
  23 + InvokeRepeating("Spawn", SpawnDelay, SpawningInterval);
  24 + }
  25 +
  26 + abstract protected void Spawn();
  27 +
  28 +
  29 +}
Assets/_Scripts/BaseObjectCreator.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: 22e19bd659d284bf6a30688a57163e07
  3 +timeCreated: 1432616343
  4 +licenseType: Free
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
Assets/_Scripts/GameConstants.cs
... ... @@ -5,5 +5,6 @@
5 5  
6 6 public static readonly string TERRAIN_TAG = "Terrain";
7 7 public static readonly string COIN_TAG = "Coin";
  8 + public static readonly float TERRAIN_SIZE = 100f;
8 9 }
Assets/_Scripts/_ObstacleScripts/ObstacleCreater.cs
1   -using UnityEngine;
2   -using System.Collections;
3   -
4   -/*
5   - * Works as a pool for GameObjects
6   - * Creating and Destroying 3D objects is heavy operation so its better to set
7   - * objects in the pool and reuse them.
8   - */
9   -public class ObstacleCreater : BaseObjectPool {
10   -
11   - public float SpawningInterval = 3f;
12   - public float SpawnDelay = 5f;
13   -
14   - private int _spawnCount = 1;
15   - private float _coinSpacing = 1.5f;
16   -
17   - private string [] _powerUpTags = new string[]{
18   - GameConstants.COIN_TAG
19   - };
20   -
21   - /*
22   - * distance of left or right lanes from center
23   - * best case: should be equal to the distance of player movement
24   - */
25   - [SerializeField, Tooltip("Make this equal to player lane switching distance.")]
26   - float LaneDistance = 2.1f;
27   -
28   -
29   - /*
30   - * -1 : Left
31   - * 0 : Middle
32   - * 1 : Right
33   - * used to increase some variations and randomness
34   - * last lane obstacle placed in
35   - */
36   -
37   - int _lastLanePlaced; //lane: left or right or middile
38   -
39   - int _lastObjectCreatedIndex; //last obstacle instantiated
40   -
41   - // overridng the base's implementation of start
42   - new void Start () {
43   - base.Start();
44   - InvokeRepeating("SpawnObject", SpawnDelay, SpawningInterval);
45   - }
46   -
47   -
48   - /*
49   - * Gets objects from the pool and places them in the lane
50   - * Donot use Instaniate to create new Objects, rather use GetPoolObject
51   - */
52   - void SpawnObject(){
53   -
54   - float raycastLength = 100f;
55   - RaycastHit hit;
56   -
57   - // 1. Check if there is terrain below
58   - if (Physics.Raycast(new Ray(transform.position, Vector3.down), out hit, raycastLength)){
59   -
60   - if(hit.collider.tag == GameConstants.TERRAIN_TAG){
61   - if(_spawnCount % 1 == 0){
62   - SpawnPowerUp();
63   - }else{
64   - SpawnCollidableObject(hit);
65   - }
66   - }
67   - }
68   -
69   - _spawnCount++;
70   - }
71   -
72   -
73   - /*
74   - * Spawns stopping objects
75   - */
76   - void SpawnCollidableObject(RaycastHit hit){
77   -
78   - // 2. get new random object
79   - int newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
80   - while (newObstacleIndex == _lastObjectCreatedIndex || System.Array.IndexOf(_powerUpTags, PooledObjectPrefabs[newObstacleIndex].tag) != -1) {
81   - newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
82   - }
83   - _lastObjectCreatedIndex = newObstacleIndex;
84   -
85   - // 3. check if object is available in pool
86   - GameObject newObstacle = GetPoolObject(PooledObjectPrefabs[newObstacleIndex].tag);
87   -
88   - if(newObstacle == null)
89   - return;
90   -
91   - // 4. get new Random Lane
92   - Vector3 position = GetRandomLanePosition();
93   - position.y = hit.point.y;
94   -
95   - // 6. bring the object above ground
96   - newObstacle.GetComponent<ObstacleController>().SetPositionWithPivot(position);
97   - }
98   -
99   - /*
100   - * Spawns collectible object
101   - */
102   - void SpawnPowerUp(){
103   -
104   - RaycastHit hit;
105   - int coinCount = 5;
106   -
107   - Vector3 position = GetRandomLanePosition();
108   - for(int i = 0; i<coinCount; i++){
109   -
110   - GameObject Coin = GetPoolObject(GameConstants.COIN_TAG);
111   -
112   - if(Coin != null) {
113   -
114   - if(Physics.Raycast(new Ray(this.transform.position, Vector3.down), out hit)){
115   - print(transform.position.z + _coinSpacing);
116   - print(hit.point.z);
117   - position.y = hit.point.y;
118   -// print (hit.point);
119   - position.z = hit.point.z;
120   - Coin.GetComponent<ObstacleController>().SetPositionWithPivot(position);
121   - transform.Translate(new Vector3(0, 0, _coinSpacing));
122   -
123   - }
124   - }
125   -
126   - }
127   - }
128   -
129   - Vector3 GetRandomLanePosition(){
130   - int newLane = Random.Range(-1,2);
131   - while(newLane == _lastLanePlaced){
132   - newLane = Random.Range(-1,2);
133   - }
134   - _lastLanePlaced = newLane;
135   -
136   - // 5. Set position in lane
137   - Vector3 position = Vector3.zero;
138   -
139   - switch (newLane) {
140   - case -1:
141   - position = new Vector3(transform.position.x - LaneDistance, transform.position.y, transform.position.z);
142   - break;
143   - case 0:
144   - position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
145   - break;
146   - case 1:
147   - position = new Vector3(transform.position.x + LaneDistance, transform.position.y, transform.position.z);
148   - break;
149   - default:
150   - break;
151   - }
152   - return position;
153   - }
154   -}
Assets/_Scripts/_ObstacleScripts/ObstacleCreater.cs.meta
1   -fileFormatVersion: 2
2   -guid: eee2387c078bf416faa8441045f60889
3   -timeCreated: 1431328586
4   -licenseType: Free
5   -MonoImporter:
6   - serializedVersion: 2
7   - defaultReferences: []
8   - executionOrder: 0
9   - icon: {instanceID: 0}
10   - userData:
11   - assetBundleName:
12   - assetBundleVariant:
Assets/_Scripts/_ObstacleScripts/ObstacleCreator.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +/*
  5 + * Works as a pool for GameObjects
  6 + * Creating and Destroying 3D objects is heavy operation so its better to set
  7 + * objects in the pool and reuse them.
  8 + */
  9 +public class ObstacleCreator : BaseObjectCreator {
  10 +
  11 +
  12 + private int _spawnCount = 1;
  13 + private float _coinSpacing = 1.5f;
  14 +
  15 + private string [] _powerUpTags = new string[]{
  16 + GameConstants.COIN_TAG
  17 + };
  18 +
  19 + /* distance of left or right lanes from center
  20 + * best case: should be equal to the distance of player movement
  21 + */
  22 + [SerializeField, Tooltip("Make this equal to player lane switching distance.")]
  23 + float LaneDistance = 2.1f;
  24 +
  25 +
  26 + /*
  27 + * -1 : Left
  28 + * 0 : Middle
  29 + * 1 : Right
  30 + * used to increase some variations and randomness
  31 + * last lane obstacle placed in
  32 + */
  33 +
  34 + int _lastLanePlaced; //lane: left or right or middile
  35 +
  36 + int _lastObjectCreatedIndex; //last obstacle instantiated
  37 +
  38 + /*
  39 + * Gets objects from the pool and places them in the lane
  40 + * Donot use Instaniate to create new Objects, rather use GetPoolObject
  41 + */
  42 + override protected void Spawn(){
  43 +
  44 + float raycastLength = 100f;
  45 + RaycastHit hit;
  46 +
  47 + // 1. Check if there is terrain below
  48 + if (Physics.Raycast(new Ray(transform.position, Vector3.down), out hit)){
  49 +
  50 + if(hit.collider.tag == GameConstants.TERRAIN_TAG){
  51 + if(_spawnCount % 4 == 0){
  52 + SpawnPowerUp();
  53 + }else{
  54 + SpawnCollidableObject(hit);
  55 + }
  56 + }
  57 + }
  58 +
  59 + _spawnCount++;
  60 + }
  61 +
  62 +
  63 + /*
  64 + * Spawns stopping objects
  65 + */
  66 + private void SpawnCollidableObject(RaycastHit hit){
  67 +
  68 + // 2. get new random object
  69 + int newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
  70 + while (newObstacleIndex == _lastObjectCreatedIndex || System.Array.IndexOf(_powerUpTags, PooledObjectPrefabs[newObstacleIndex].tag) != -1) {
  71 + newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
  72 + }
  73 + _lastObjectCreatedIndex = newObstacleIndex;
  74 +
  75 + // 3. check if object is available in pool
  76 + GameObject newObstacle = GetPoolObject(PooledObjectPrefabs[newObstacleIndex].tag);
  77 +
  78 + if(newObstacle == null)
  79 + return;
  80 +
  81 + // 4. get new Random Lane
  82 + Vector3 position = GetRandomLanePosition();
  83 + position.y = hit.point.y;
  84 +
  85 + // 5. bring the object above ground
  86 + newObstacle.GetComponent<ObstacleController>().SetPositionWithPivot(position);
  87 + }
  88 +
  89 + /*
  90 + * Spawns collectible object
  91 + */
  92 + private void SpawnPowerUp(){
  93 +
  94 + RaycastHit hit;
  95 + int coinCount = 5;
  96 +
  97 + Vector3 position = GetRandomLanePosition();
  98 + for(int i = 0; i<coinCount; i++){
  99 +
  100 + GameObject Coin = GetPoolObject(GameConstants.COIN_TAG);
  101 +
  102 + if(Coin != null) {
  103 +
  104 + if(Physics.Raycast(new Ray(this.transform.position, Vector3.down), out hit)){
  105 + position.y = hit.point.y;
  106 + position.z = hit.point.z;
  107 + Coin.GetComponent<ObstacleController>().SetPositionWithPivot(position);
  108 + transform.Translate(new Vector3(0, 0, _coinSpacing));
  109 + }
  110 + }
  111 +
  112 + }
  113 + }
  114 +
  115 + private Vector3 GetRandomLanePosition(){
  116 + int newLane = Random.Range(-1,2);
  117 + while(newLane == _lastLanePlaced){
  118 + newLane = Random.Range(-1,2);
  119 + }
  120 + _lastLanePlaced = newLane;
  121 +
  122 + Vector3 position = Vector3.zero;
  123 +
  124 + switch (newLane) {
  125 + case -1:
  126 + position = new Vector3(transform.position.x - LaneDistance, transform.position.y, transform.position.z);
  127 + break;
  128 + case 0:
  129 + position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
  130 + break;
  131 + case 1:
  132 + position = new Vector3(transform.position.x + LaneDistance, transform.position.y, transform.position.z);
  133 + break;
  134 + default:
  135 + break;
  136 + }
  137 + return position;
  138 + }
  139 +}
Assets/_Scripts/_ObstacleScripts/ObstacleCreator.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: d37026a74538d4677a9c5abf63e2192a
  3 +timeCreated: 1432615593
  4 +licenseType: Free
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
Assets/_Scripts/_TerrainScripts.meta
  1 +fileFormatVersion: 2
  2 +guid: 76b32608803bc4ef69ad38070107d801
  3 +folderAsset: yes
  4 +timeCreated: 1432615211
  5 +licenseType: Free
  6 +DefaultImporter:
  7 + userData:
  8 + assetBundleName:
  9 + assetBundleVariant:
Assets/_Scripts/_TerrainScripts/TerrainChunkController.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +public class TerrainChunkController : MonoBehaviour {
  5 +
  6 + float _colliderHeight = 6f;
  7 + public GameObject TerrainObject;
  8 +
  9 + // Use this for initialization
  10 + void Start () {
  11 +
  12 + BoxCollider collider = gameObject.AddComponent<BoxCollider>();
  13 +
  14 +// foreach(Transform childTransform in gameObject.transform){
  15 +// if(childTransform.tag.CompareTo(GameConstants.TERRAIN_TAG) == 0)
  16 +// TerrainObject = childTransform.gameObject.GetComponent<Terrain>();
  17 +// }
  18 +
  19 + //Setting the collider to end of chunk
  20 + //Hardcoded values were copied from the inspector
  21 + //After setting the collider at right place
  22 + collider.size = new Vector3(9f, 3f, 0.1f);
  23 + collider.center = new Vector3(0f, 3.48f, GameConstants.TERRAIN_SIZE);
  24 + collider.isTrigger = true;
  25 +
  26 + }
  27 +
  28 + void OnTriggerEnter(Collider collider){
  29 + StartCoroutine(Disable());
  30 + }
  31 +
  32 + IEnumerator Disable(){
  33 + yield return new WaitForEndOfFrame();
  34 + gameObject.SetActive(false);
  35 + }
  36 +}
Assets/_Scripts/_TerrainScripts/TerrainChunkController.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: bad626cc2c50c4503a450dbfb53e228c
  3 +timeCreated: 1432618378
  4 +licenseType: Free
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant:
Assets/_Scripts/_TerrainScripts/TerrainCreator.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +/*
  5 + * Works as a pool for Terrain Prefabs
  6 + * Creating and Destroying Terrain objects is heavy operation so its better to set
  7 + * objects in the pool and reuse them.
  8 + */
  9 +public class TerrainCreator : BaseObjectCreator {
  10 +
  11 + public static Vector3 LastTerrainPosition;
  12 +
  13 +
  14 + new void Start(){
  15 + base.Start();
  16 +
  17 + GameObject firstChunk = GetPoolObject();
  18 + LastTerrainPosition = firstChunk.transform.position;
  19 +
  20 + InvokeRepeating("Spawn", SpawnDelay, SpawningInterval);
  21 +
  22 + }
  23 +
  24 + public override GameObject GetPoolObject () {
  25 + GameObject newObject = base.GetPoolObject ();
  26 +
  27 + if(newObject){
  28 + if(newObject.GetComponent<TerrainChunkController>() == null)
  29 + newObject.AddComponent<TerrainChunkController>();
  30 + }
  31 +
  32 + return newObject;
  33 + }
  34 +
  35 + override protected void Spawn(){
  36 +
  37 + GameObject chunk = GetPoolObject();
  38 +
  39 + if(chunk != null){
  40 +
  41 + chunk.transform.position = new Vector3(LastTerrainPosition.x, LastTerrainPosition.y, LastTerrainPosition.z + GameConstants.TERRAIN_SIZE);
  42 + LastTerrainPosition = chunk.transform.position;
  43 + }
  44 +
  45 + }
  46 +
  47 +}
Assets/_Scripts/_TerrainScripts/TerrainCreator.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: 1823e00d847ca457b96dfc56f1f04b8d
  3 +timeCreated: 1432615224
  4 +licenseType: Free
  5 +MonoImporter:
  6 + serializedVersion: 2
  7 + defaultReferences: []
  8 + executionOrder: 0
  9 + icon: {instanceID: 0}
  10 + userData:
  11 + assetBundleName:
  12 + assetBundleVariant: