miepzerino
2023-12-29 701522f3235f47987ec2979ac145ec28c6c9decf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[RequireComponent(typeof(SpriteRenderer))]
public class Tiling : MonoBehaviour
{
    public int offsetX = 2;
    public int offsetY = 2;
 
    public bool hasRightBuddy = false;
    public bool hasLeftBuddy = false;
    public bool hasTopBuddy = false;
    public bool hasBottomBuddy = false;
 
    public bool reverseScale = false;
 
    private float spriteWidth = 0f;
    private float spriteHeight = 0f;
    private Camera cam;
    private Transform myTransform;
 
    private void Awake()
    {
        cam = Camera.main;
        myTransform = transform;
    }
    // Start is called before the first frame update
    void Start()
    {
        SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
        spriteWidth = spriteRenderer.sprite.bounds.size.x;
        spriteHeight = spriteRenderer.sprite.bounds.size.y;
 
        //GenerateBackground();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (!hasLeftBuddy || !hasRightBuddy)
        {
            float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
 
            float edgeVisiblePositionRight = (transform.position.x + spriteWidth / 2) - camHorizontalExtend;
            float edgeVisiblePositionLeft = (transform.position.x - spriteWidth / 2) + camHorizontalExtend;
 
            if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && !hasRightBuddy)
            {
                MakeNewRightOrLeftBuddy(1);
                hasRightBuddy = true;
            }
            else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && !hasLeftBuddy)
            {
                MakeNewRightOrLeftBuddy(-1);
                hasLeftBuddy = true;
            }
        }
    }
 
    private void MakeNewRightOrLeftBuddy(int rightOrLeft)
    {
        Vector3 newPosition = new Vector3(myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
        Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation);
 
        if (reverseScale)
        {
            newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z);
        }
 
        newBuddy.parent = myTransform.parent;
        newBuddy.name = myTransform.name;
 
        if (rightOrLeft > 0)
        {
            newBuddy.GetComponent<Tiling>().hasLeftBuddy = true;
        }
        else if (rightOrLeft < 0)
        {
            newBuddy.GetComponent<Tiling>().hasRightBuddy = true;
        }
    }
}