aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/InfiniteBackground.cs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-04-08 17:24:16 -0500
committerToby Vincent <tobyv13@gmail.com>2022-04-08 17:24:16 -0500
commitfb2703b646d22bd4502fab6f0ca58cb922d1ed2c (patch)
tree37a9584cd0d194082749161fef417cc4c4e965fe /Assets/Scripts/InfiniteBackground.cs
parenta16d177920a16d4364e0b1ceff8dc7639ba6ca17 (diff)
wip: no regerts
fix jump; race condition(ish) in jump/fall/land events (?) impl trigger animations on events refactor control, (follow) camera, anim handlers into interfaces impl controller pattern for controls (FSM using lambdas + interfaces) come up with the rest of the todos
Diffstat (limited to 'Assets/Scripts/InfiniteBackground.cs')
-rw-r--r--Assets/Scripts/InfiniteBackground.cs41
1 files changed, 41 insertions, 0 deletions
diff --git a/Assets/Scripts/InfiniteBackground.cs b/Assets/Scripts/InfiniteBackground.cs
new file mode 100644
index 0000000..d587040
--- /dev/null
+++ b/Assets/Scripts/InfiniteBackground.cs
@@ -0,0 +1,41 @@
+using System.Collections;
+using UnityEngine;
+
+public class InfiniteBackground : MonoBehaviour
+{
+ [SerializeField] GameObject cam;
+ [SerializeField] float parallaxEffect = 0.3f;
+
+ internal Vector2 pos;
+ internal float spriteExtentX;
+
+ void Start()
+ {
+ Vector2 pos = transform.position;
+ spriteExtentX = GetComponent<SpriteRenderer>().bounds.size.x / 2;
+ }
+
+ void FixedUpdate()
+ {
+ float camExtentX = Camera.main.orthographicSize * Screen.width / Screen.height;
+ float newPosX = pos.x + (cam.transform.position.x * parallaxEffect);
+
+ if (newPosX + spriteExtentX <= Camera.main.transform.position.x + camExtentX)
+ {
+ var clone = Instantiate(this.gameObject);
+ clone.transform.position = new Vector3(newPosX + spriteExtentX, transform.position.y, transform.position.z);
+ }
+ else if (newPosX - spriteExtentX >= Camera.main.transform.position.x - camExtentX)
+ {
+ var clone = Instantiate(gameObject);
+ clone.transform.position = new Vector3(newPosX - spriteExtentX, transform.position.y, transform.position.z);
+ }
+
+ transform.position = new Vector3(newPosX, transform.position.y, transform.position.z);
+ }
+
+ void OnBecameInvisible()
+ {
+ Destroy(gameObject);
+ }
+} \ No newline at end of file