From 8973398675a1ea012da316f791448d9437fc8528 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 27 Apr 2019 20:21:50 +0200 Subject: Added destruction of obstacles --- nGJ2019/Assets/Scripts/LevelScrolling.cs | 104 +++++++++++++++++-------------- 1 file changed, 57 insertions(+), 47 deletions(-) (limited to 'nGJ2019/Assets/Scripts') diff --git a/nGJ2019/Assets/Scripts/LevelScrolling.cs b/nGJ2019/Assets/Scripts/LevelScrolling.cs index 0122a69..fac0a13 100644 --- a/nGJ2019/Assets/Scripts/LevelScrolling.cs +++ b/nGJ2019/Assets/Scripts/LevelScrolling.cs @@ -4,66 +4,76 @@ using UnityEngine; public class LevelScrolling : MonoBehaviour { - public int UpdateRate = -5; - public List Obstacles; - public Transform Background; + public int UpdateRate = -5; + public List Obstacles; + public Transform Background; - private Vector3 initialBgPos; - private float[] backgroundSize; + private Vector3 initialBgPos; + private float[] backgroundSize; - // Start is called before the first frame update - void Start() - { - ResizeBackground(); - } + // Start is called before the first frame update + void Start() + { + ResizeBackground(); + } + + // Update is called once per frame + void Update() + { - // Update is called once per frame - void Update() - { + } + + void FixedUpdate() + { + MoveBackground(); + MoveObstacles(); + DestroyObstacles(); + } - } + void ResizeBackground() + { + var sr = Background.GetComponent(); + if (sr == null) return; - void FixedUpdate() - { - MoveBackground(); - MoveObstacles(); - } + Background.localScale = new Vector3(1, 1, 1); - private void ResizeBackground() - { - var sr = Background.GetComponent(); - if (sr == null) return; + var width = sr.sprite.bounds.size.x; + var height = sr.sprite.bounds.size.y; - Background.localScale = new Vector3(1, 1, 1); + var worldScreenHeight = Camera.main.orthographicSize * 2.0; + var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width; - var width = sr.sprite.bounds.size.x; - var height = sr.sprite.bounds.size.y; + var finalHeight = (float)(worldScreenHeight / height); + var finalWidth = (float)(worldScreenWidth / width); - var worldScreenHeight = Camera.main.orthographicSize * 2.0; - var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width; + Background.localScale = new Vector3(finalHeight, finalHeight, 1); - var finalHeight = (float)(worldScreenHeight / height); - var finalWidth = (float)(worldScreenWidth / width); + var viewportX = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).x; + Background.position = new Vector3(viewportX * -1, 0, 0); + initialBgPos = Background.position; + } - Background.localScale = new Vector3(finalHeight, finalHeight, 1); + void MoveObstacles() + { + for (int i = 0; i < Obstacles.Count; i++) + { + Obstacles[i].transform.Translate(0.01f * UpdateRate, 0, 0); + } + } - var viewportX = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, 0)).x; - Background.position = new Vector3(viewportX * -1, 0, 0); - initialBgPos = Background.position; - } + void DestroyObstacles() + { + if (Obstacles[0].transform.position.x < -30) + { + Destroy(Obstacles[0]); + Obstacles.RemoveAt(0); + } + } - private void MoveObstacles() - { - foreach (var o in Obstacles) + void MoveBackground() { - o.Translate(0.01f * UpdateRate, 0, 0); + if (Background.position.x > -initialBgPos.x) + Background.Translate(0.01f * UpdateRate, 0, 0); + else Background.position = initialBgPos; } - } - - private void MoveBackground() - { - if (Background.position.x > -initialBgPos.x) - Background.Translate(0.01f * UpdateRate, 0, 0); - else Background.position = initialBgPos; - } } -- cgit v1.2.3 From a227d09d68187f80d620e2021be2c84979498710 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 27 Apr 2019 20:27:35 +0200 Subject: Fixed messaging breaking spawning --- nGJ2019/Assets/Scripts/EventTimeline.cs | 7 +++++-- nGJ2019/Assets/Scripts/Messenger.cs | 1 + nGJ2019/Assets/Scripts/ObstacleSpawner.cs | 5 ++--- 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'nGJ2019/Assets/Scripts') diff --git a/nGJ2019/Assets/Scripts/EventTimeline.cs b/nGJ2019/Assets/Scripts/EventTimeline.cs index b864ae7..18b87cc 100644 --- a/nGJ2019/Assets/Scripts/EventTimeline.cs +++ b/nGJ2019/Assets/Scripts/EventTimeline.cs @@ -60,17 +60,20 @@ public class EventTimeline { currentTime += deltaTime; - while(futureEvents.Count > 0 && currentTime > futureEvents[0].time && futureMessages.Count > 0 && currentTime > futureMessages[0].time) + while(futureEvents.Count > 0 && currentTime > futureEvents[0].time) { SpawnEvent e = futureEvents[0]; futureEvents.RemoveAt(0); if(OnSpawnEvent != null) OnSpawnEvent(e); + } + while(futureMessages.Count > 0 && currentTime > futureMessages[0].time) + { MessageEvent m = futureMessages[0]; futureMessages.RemoveAt(0); if(OnMessageEvent != null) OnMessageEvent(m); - } + } } } diff --git a/nGJ2019/Assets/Scripts/Messenger.cs b/nGJ2019/Assets/Scripts/Messenger.cs index ca2ffe1..21d2f8d 100644 --- a/nGJ2019/Assets/Scripts/Messenger.cs +++ b/nGJ2019/Assets/Scripts/Messenger.cs @@ -16,6 +16,7 @@ public class Messenger : MonoBehaviour timeline = Spawner.GetEventTimeline(); timeline.Add(1, "Test"); + timeline.Add(3, ""); timeline.OnMessageEvent += MessageOnEvent; } diff --git a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs index f438e85..0e6b3ac 100644 --- a/nGJ2019/Assets/Scripts/ObstacleSpawner.cs +++ b/nGJ2019/Assets/Scripts/ObstacleSpawner.cs @@ -8,7 +8,6 @@ public class ObstacleSpawner : MonoBehaviour public GameObject caveWallsPrefab, rockTopPrefab, rockBottomPrefab, narrowPassagePrefab, rockJawsPrefab; public LevelScrolling scrolling; - public Messenger messenger; public float spawnLine; @@ -35,8 +34,8 @@ public class ObstacleSpawner : MonoBehaviour break; } - var transformT = ((GameObject)Instantiate(prefab, new Vector3(spawnLine, e.height, 0), Quaternion.identity)).transform; - scrolling.Obstacles.Add(transformT); + var o = Instantiate(prefab, new Vector3(spawnLine, e.height, 0), Quaternion.identity); + scrolling.Obstacles.Add(o); } void Start() -- cgit v1.2.3