From b1a279b551e2127ff9f530b205f2e1590ae51e9f Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 27 Apr 2019 12:19:37 +0200 Subject: Added level obstacles spawning --- nGJ2019/Assets/Scripts/LevelScrolling.cs | 27 +++++++++++++++------------ nGJ2019/Assets/Scripts/ObstacleSpawner.cs | 12 +++++++----- 2 files changed, 22 insertions(+), 17 deletions(-) (limited to 'nGJ2019/Assets/Scripts') diff --git a/nGJ2019/Assets/Scripts/LevelScrolling.cs b/nGJ2019/Assets/Scripts/LevelScrolling.cs index b40f70e..025bf40 100644 --- a/nGJ2019/Assets/Scripts/LevelScrolling.cs +++ b/nGJ2019/Assets/Scripts/LevelScrolling.cs @@ -4,9 +4,9 @@ using UnityEngine; public class LevelScrolling : MonoBehaviour { - public int updateRate = 1; - public Transform obstacles; - public Transform background; + public int UpdateRate = 1; + public List Obstacles; + public Transform Background; private Vector3 initialBgPos; private float[] backgroundSize; @@ -31,10 +31,10 @@ public class LevelScrolling : MonoBehaviour private void ResizeBackground() { - var sr = background.GetComponent(); + var sr = Background.GetComponent(); if (sr == null) return; - background.localScale = new Vector3(1, 1, 1); + Background.localScale = new Vector3(1, 1, 1); var width = sr.sprite.bounds.size.x; var height = sr.sprite.bounds.size.y; @@ -45,22 +45,25 @@ public class LevelScrolling : MonoBehaviour var finalHeight = (float)(worldScreenHeight / height); var finalWidth = (float)(worldScreenWidth / width); - background.localScale = new Vector3(finalHeight, finalHeight, 1); + Background.localScale = new Vector3(finalHeight, finalHeight, 1); var viewportX = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).x; - background.position = new Vector3(viewportX * -1, 0, 0); - initialBgPos = background.position; + Background.position = new Vector3(viewportX * -1, 0, 0); + initialBgPos = Background.position; } private void MoveObstacles() { - obstacles.Translate(new Vector2(0.1f * updateRate, 0)); + foreach (var o in Obstacles) + { + o.Translate(new Vector2(0.01f * UpdateRate, 0)); + } } private void MoveBackground() { - if (background.position.x > -initialBgPos.x) - background.Translate(new Vector2(0.1f * updateRate, 0)); - else background.position = initialBgPos; + if (Background.position.x > -initialBgPos.x) + Background.Translate(new Vector2(0.01f * UpdateRate, 0)); + else Background.position = initialBgPos; } } diff --git a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs index d007513..7be5b33 100644 --- a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs +++ b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs @@ -8,6 +8,7 @@ public class ObstacleSpawner : MonoBehaviour public GameObject alfaPrefab; public GameObject betaPrefab; + public LevelScrolling scrolling; public float spawnLine; @@ -21,15 +22,16 @@ public class ObstacleSpawner : MonoBehaviour if(e.type == EventTimeline.SpawnEventType.beta) prefab = betaPrefab; - Instantiate(prefab, new Vector3(spawnLine, e.height, 0), Quaternion.identity); + var transformT = ((GameObject) Instantiate(prefab, new Vector3(spawnLine, e.height, 0), Quaternion.identity)).transform; + scrolling.Obstacles.Add(transformT); } void Start() { - timeline.Add(2, 2, EventTimeline.SpawnEventType.alfa); - timeline.Add(4, 2, EventTimeline.SpawnEventType.beta); - timeline.Add(5, -2, EventTimeline.SpawnEventType.beta); - timeline.Add(7, -3, EventTimeline.SpawnEventType.alfa); + for(int i = 0; i < 120; i += 5) + { + timeline.Add(i, 0, EventTimeline.SpawnEventType.alfa); + } timeline.OnSpawnEvent += spawnOnEvent; } -- cgit v1.2.3