Commit c1d61a206b4af6d4dd82faabc368452b70ef255a

Authored by Danish Siddiqui
1 parent 79b1f39878
Exists in master

Scripts added without prefabs,

Showing 8 changed files with 238 additions and 4 deletions Side-by-side Diff

Assets/_Scripts/BaseObjectPool.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +using System.Collections.Generic;
  4 +
  5 +public abstract class BaseObjectPool : MonoBehaviour {
  6 +
  7 + public List<GameObject> PooledObjectPrefabs;
  8 +
  9 + private List<GameObject> _pool;
  10 +
  11 + [SerializeField]
  12 + int CopyCount; //Initial Number of instances to pool
  13 +
  14 + [SerializeField]
  15 + bool IsDynamic; //Should grow number of copies if not found in pool
  16 +
  17 + // Use this for initialization
  18 + public void Start () {
  19 + _pool = new List<GameObject>();
  20 +
  21 + //create a pool of objects with each object having CopyCount instances
  22 + for(int i=0; i<CopyCount; i++){
  23 + foreach (GameObject prefab in PooledObjectPrefabs) {
  24 + GameObject newObject = Instantiate(prefab);
  25 + newObject.SetActive(false);
  26 + _pool.Add(newObject);
  27 + }
  28 + }
  29 + }
  30 +
  31 + /* returns the first free object found
  32 + * doesnt create the new object even if dynamic
  33 + */
  34 + public virtual GameObject GetPoolObject(){
  35 +
  36 + foreach (GameObject obj in _pool) {
  37 + if(!obj.activeInHierarchy){
  38 + obj.SetActive(true);
  39 + return obj;
  40 + }
  41 + }
  42 +
  43 + return null;
  44 + }
  45 +
  46 + /*
  47 + * returns the first free object found with the Tag tag
  48 + * else if list is dynamic creates and returns the new object
  49 + * and adds it to the pool
  50 + * else returns null
  51 + */
  52 + public virtual GameObject GetPoolObject(string tag){
  53 +
  54 + foreach (GameObject obj in _pool) {
  55 + if(!obj.activeInHierarchy && (obj.tag.CompareTo(tag) == 0)){
  56 + obj.SetActive(true);
  57 + return obj;
  58 + }
  59 + }
  60 +
  61 + if(IsDynamic){
  62 + foreach (GameObject prefab in PooledObjectPrefabs) {
  63 + if(prefab.tag.CompareTo(tag) == 0){
  64 + GameObject newObject = Instantiate(prefab);
  65 + _pool.Add(newObject);
  66 + newObject.SetActive(true);
  67 + return newObject;
  68 + }
  69 + }
  70 + }
  71 +
  72 + return null;
  73 + }
  74 +
  75 +}
Assets/_Scripts/BaseObjectPool.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: abca5670a522c4ed9b4d44bf64ed2606
  3 +timeCreated: 1431328566
  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/FollowTarget.cs
... ... @@ -4,8 +4,14 @@
4 4 public class FollowTarget : MonoBehaviour {
5 5  
6 6 public GameObject Target;
7   - public float Distance;
8 7  
  8 + public float DistanceY;
  9 + public float DistanceZ;
  10 +
  11 + [SerializeField] bool FollowX;
  12 + [SerializeField] bool FollowY;
  13 + [SerializeField] bool FollowZ;
  14 +
9 15 // Use this for initialization
10 16 void Start () {
11 17 SwipeController.SwipeEvent += Swipe;
12 18  
13 19  
... ... @@ -13,16 +19,29 @@
13 19  
14 20 // Update is called once per frame
15 21 void LateUpdate () {
16   - float distance = Mathf.Abs(Vector3.Distance(Target.transform.position, transform.position));
17   - if(distance != Distance){
18   - transform.position = new Vector3(Target.transform.position.x, transform.position.y, Target.transform.position.z - Distance);
  22 +
  23 + if(FollowZ){
  24 + float distance = Target.transform.position.z - transform.position.z;
  25 + if(distance != DistanceZ){
  26 + transform.position = new Vector3(Target.transform.position.x, transform.position.y, Target.transform.position.z - DistanceZ);
  27 + }
19 28 }
20 29  
  30 + if(FollowY){
  31 + float distance = transform.position.y - Target.transform.position.y;
  32 + if(distance != DistanceY){
  33 + transform.position = new Vector3(Target.transform.position.x, DistanceY + Target.transform.position.y, transform.position.z);
  34 + }
  35 + }
21 36 }
22 37  
23 38 // -1 for left
24 39 // 1 fpr right
25 40 void Swipe(SwipeController.DIRECTION direction){
  41 +
  42 + if(!FollowX)
  43 + return;
  44 +
26 45 switch(direction){
27 46 case SwipeController.DIRECTION.LEFT:
28 47 StartCoroutine(MoveSideways(-1));
Assets/_Scripts/_ObstacleScripts.meta
  1 +fileFormatVersion: 2
  2 +guid: 419886dcc3eba4aa1b3a399c184914e6
  3 +folderAsset: yes
  4 +timeCreated: 1431501768
  5 +licenseType: Free
  6 +DefaultImporter:
  7 + userData:
  8 + assetBundleName:
  9 + assetBundleVariant:
Assets/_Scripts/_ObstacleScripts/ObstacleController.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +public class ObstacleController : MonoBehaviour {
  5 +
  6 + // Use this for initialization
  7 + [SerializeField]
  8 + float DestroyAfterSeconds = 15f;
  9 +
  10 + void OnCollisionEnter(Collision collision){
  11 + print (collision.gameObject.tag);
  12 + }
  13 +
  14 + void Start () {
  15 + StartCoroutine(Disable());
  16 + }
  17 +
  18 + IEnumerator Disable(){
  19 + yield return new WaitForSeconds(DestroyAfterSeconds);
  20 + this.gameObject.SetActive(false);
  21 + }
  22 +
  23 +}
Assets/_Scripts/_ObstacleScripts/ObstacleController.cs.meta
  1 +fileFormatVersion: 2
  2 +guid: 72b3a15df8f224cfca0e0b680840924b
  3 +timeCreated: 1431501805
  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/ObstacleCreater.cs
  1 +using UnityEngine;
  2 +using System.Collections;
  3 +
  4 +public class ObstacleCreater : BaseObjectPool {
  5 +
  6 + public float SpawningInterval = 3f;
  7 + public float SpawnDelay = 5f;
  8 +
  9 + //distance of left or right lanes from center
  10 + //best case: should be equal to the distance of player movement
  11 + [SerializeField, Tooltip("Make this equal to player lane switching distance.")]
  12 + float LaneDistance = 2.1f;
  13 +
  14 + //used to increase some variations and randomness
  15 + /*
  16 + * -1 : Left
  17 + * 0 : Middle
  18 + * 1 : Right
  19 + * last lane obstacle placed in
  20 + */
  21 + int _lastLanePlaced;
  22 + //last obstacle instantiated
  23 + int _lastObjectCreatedIndex;
  24 +
  25 + // Use this for initialization
  26 + new void Start () {
  27 + base.Start();
  28 + InvokeRepeating("SpawnObject", SpawnDelay, SpawningInterval);
  29 + }
  30 +
  31 +
  32 + //Donot use Instaniate to create new Objects, rather use GetPoolObject
  33 + void SpawnObject(){
  34 +
  35 + //get new random object
  36 + int newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
  37 + while (newObstacleIndex == _lastObjectCreatedIndex) {
  38 + newObstacleIndex = Random.Range(0,PooledObjectPrefabs.Count);
  39 + }
  40 + _lastObjectCreatedIndex = newObstacleIndex;
  41 +
  42 + GameObject newObstacle = GetPoolObject(PooledObjectPrefabs[newObstacleIndex].tag);
  43 + //check if object is available in pool
  44 + if(newObstacle == null)
  45 + return;
  46 +
  47 + //get new Random Lane
  48 + int newLane = Random.Range(-1,2);
  49 + while(newLane == _lastLanePlaced){
  50 + newLane = Random.Range(-1,2);
  51 + }
  52 + _lastLanePlaced = newLane;
  53 +
  54 + Vector3 position = Vector3.zero;
  55 +
  56 + switch (newLane) {
  57 + case -1:
  58 + position = new Vector3(transform.position.x - LaneDistance, newObstacle.transform.position.y, transform.position.z);
  59 + break;
  60 + case 0:
  61 + position = new Vector3(transform.position.x, newObstacle.transform.position.y, transform.position.z);
  62 + break;
  63 + case 1:
  64 + position = new Vector3(transform.position.x + LaneDistance, newObstacle.transform.position.y, transform.position.z);
  65 + break;
  66 + default:
  67 + break;
  68 + }
  69 + newObstacle.transform.position = position;
  70 +
  71 + }
  72 +}
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: