miepzerino
2025-03-28 b7518b77da6511a396501b51b5046ed86ce8d98b
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[RequireComponent(typeof(SpriteRenderer))]
public class Tiling : MonoBehaviour
{
    [System.Serializable]
    public class HeightSpritePair
    {
        public float minHeight;
        public float maxHeight;
        public Sprite sprite;
    }
 
    public int offsetX = 2;
    public int offsetY = 2;
    public HeightSpritePair[] heightSprites;
 
    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 SpriteRenderer spriteRenderer;
 
    private void Awake()
    {
        cam = Camera.main;
        myTransform = transform;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
 
    void Start()
    {
        UpdateSpriteForHeight();
        spriteWidth = spriteRenderer.sprite.bounds.size.x;
        spriteHeight = spriteRenderer.sprite.bounds.size.y;
    }
 
    void Update()
    {
        if (CheckOffscreenAndDestroy()) return; // Return early if destroyed
        CheckHorizontalTiling();
        CheckVerticalTiling();
    }
 
    private void UpdateSpriteForHeight()
    {
        if (heightSprites == null || heightSprites.Length == 0) return;
 
        float worldY = transform.position.y;
        Sprite oldSprite = spriteRenderer.sprite;
 
        foreach (HeightSpritePair pair in heightSprites)
        {
            if (pair.sprite == null) continue; // Skip invalid sprites
 
            if ((pair.minHeight == 0 && pair.maxHeight == 0) ||
                (worldY >= pair.minHeight && worldY <= pair.maxHeight))
            {
                if (oldSprite != pair.sprite)
                {
                    spriteRenderer.sprite = pair.sprite;
                    // Update sprite dimensions if sprite changed
                    spriteWidth = spriteRenderer.sprite.bounds.size.x;
                    spriteHeight = spriteRenderer.sprite.bounds.size.y;
                }
                break;
            }
        }
    }
 
    private void CheckHorizontalTiling()
    {
        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)
            {
                MakeNewBuddy(1, 0);
                hasRightBuddy = true;
            }
            else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && !hasLeftBuddy)
            {
                MakeNewBuddy(-1, 0);
                hasLeftBuddy = true;
            }
        }
    }
 
    private void CheckVerticalTiling()
    {
        if (!hasTopBuddy || !hasBottomBuddy)
        {
            float camVerticalExtend = cam.orthographicSize;
            float edgeVisiblePositionTop = (transform.position.y + spriteHeight / 2) - camVerticalExtend;
            float edgeVisiblePositionBottom = (transform.position.y - spriteHeight / 2) + camVerticalExtend;
 
            if (cam.transform.position.y >= edgeVisiblePositionTop - offsetY && !hasTopBuddy)
            {
                MakeNewBuddy(0, 1);
                hasTopBuddy = true;
            }
            else if (cam.transform.position.y <= edgeVisiblePositionBottom + offsetY && !hasBottomBuddy)
            {
                MakeNewBuddy(0, -1);
                hasBottomBuddy = true;
            }
        }
    }
 
    private void MakeNewBuddy(int rightOrLeft, int topOrBottom)
    {
        Vector3 newPosition = new Vector3(
            myTransform.position.x + spriteWidth * rightOrLeft,
            myTransform.position.y + spriteHeight * topOrBottom,
            myTransform.position.z
        );
 
        Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation);
        Tiling buddyTiling = newBuddy.GetComponent<Tiling>();
 
        if (reverseScale)
        {
            newBuddy.localScale = new Vector3(
                rightOrLeft != 0 ? newBuddy.localScale.x * -1 : newBuddy.localScale.x,
                topOrBottom != 0 ? newBuddy.localScale.y * -1 : newBuddy.localScale.y,
                newBuddy.localScale.z
            );
        }
 
        newBuddy.parent = myTransform.parent;
        newBuddy.name = myTransform.name;
 
        if (rightOrLeft > 0) buddyTiling.hasLeftBuddy = true;
        else if (rightOrLeft < 0) buddyTiling.hasRightBuddy = true;
        if (topOrBottom > 0) buddyTiling.hasBottomBuddy = true;
        else if (topOrBottom < 0) buddyTiling.hasTopBuddy = true;
 
        buddyTiling.UpdateSpriteForHeight();
    }
 
    private bool CheckOffscreenAndDestroy()
    {
        Vector3 viewportPosition = cam.WorldToViewportPoint(transform.position);
        float buffer = 1.5f;
 
        if (viewportPosition.x < -buffer || viewportPosition.x > 1 + buffer ||
            viewportPosition.y < -buffer || viewportPosition.y > 1 + buffer)
        {
            NotifyNeighbors();
            Destroy(gameObject);
            return true;
        }
        return false;
    }
    private void NotifyNeighbors()
    {
        // Use a slightly smaller size than the sprite to ensure we only hit immediate neighbors
        Vector2 checkSize = new Vector2(spriteWidth * 0.9f, spriteHeight * 0.9f);
        Vector2 position = transform.position;
        ContactFilter2D filter = new ContactFilter2D().NoFilter();
        Collider2D[] results = new Collider2D[1];
 
        // Check each direction using OverlapBox
        // Right neighbor
        if (Physics2D.OverlapBox(position + new Vector2(spriteWidth, 0), 
            checkSize, 0f,
            filter, results) > 0)
        {
            Tiling rightTile = results[0].GetComponent<Tiling>();
            if (rightTile != null) rightTile.hasLeftBuddy = false;
        }
 
        // Left neighbor
        if (Physics2D.OverlapBox(position + new Vector2(-spriteWidth, 0),
            checkSize, 0f,
            filter, results) > 0)
        {
            Tiling leftTile = results[0].GetComponent<Tiling>();
            if (leftTile != null) leftTile.hasRightBuddy = false;
        }
 
        // Top neighbor
        if (Physics2D.OverlapBox(position + new Vector2(0, spriteHeight),
            checkSize, 0f,
            filter, results) > 0)
        {
            Tiling topTile = results[0].GetComponent<Tiling>();
            if (topTile != null) topTile.hasBottomBuddy = false;
        }
 
        // Bottom neighbor
        if (Physics2D.OverlapBox(position + new Vector2(0, -spriteHeight),
            checkSize, 0f,
            filter, results) > 0)
        {
            Tiling bottomTile = results[0].GetComponent<Tiling>();
            if (bottomTile != null) bottomTile.hasTopBuddy = false;
        }
    }
    //private void OnDrawGizmos()
    //{
    //    if (!Application.isPlaying) return;
 
    //    Vector2 checkSize = new Vector2(spriteWidth * 0.9f, spriteHeight * 0.9f);
    //    Gizmos.color = Color.yellow;
 
    //    // Draw detection boxes for each direction
    //    Gizmos.DrawWireCube((Vector2)transform.position + new Vector2(spriteWidth, 0), checkSize);
    //    Gizmos.DrawWireCube((Vector2)transform.position + new Vector2(-spriteWidth, 0), checkSize);
    //    Gizmos.DrawWireCube((Vector2)transform.position + new Vector2(0, spriteHeight), checkSize);
    //    Gizmos.DrawWireCube((Vector2)transform.position + new Vector2(0, -spriteHeight), checkSize);
    //}
}