Created
July 16, 2014 04:48
-
-
Save ashblue/4b60b9656ea7b78e5cb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Force inject a sprite renderer before initialization (if not present) | |
[RequireComponent(typeof(SpriteRenderer))] | |
// @TODO Warning, does not take into account parallaxed weight, needs to be added manually | |
// @NOTE Must have a parent container | |
public class TilerChild : MonoBehaviour { | |
public bool repeatX = false; | |
public bool repeatY = false; | |
// Tracking mechanism to discover the current whereabouts of a tile | |
public int xAxis = 0; | |
public int yAxis = 0; | |
public int offsetX = 2; | |
public int offsetY = 2; | |
// Draw checks | |
bool hasARightBuddy = false; | |
bool hasALeftBuddy = false; | |
bool hasATopBuddy = false; | |
bool hasABottomBuddy = false; | |
public bool reverseScaleX = false; | |
public bool reverseScaleY = false; | |
float spriteWidth; | |
float spriteHeight; | |
Camera cam; | |
public Tiler tileApi; | |
void Awake () { | |
cam = Camera.main; | |
} | |
void Start () { | |
SpriteRenderer sRenderer = GetComponent<SpriteRenderer>(); | |
spriteWidth = sRenderer.sprite.bounds.size.x; | |
spriteHeight = sRenderer.sprite.bounds.size.y; | |
} | |
void Update () { | |
if (repeatX && (hasALeftBuddy == false || hasARightBuddy == false)) { | |
// Calculate half the width of what the camera can see (world coords, not pixels) | |
float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height; | |
// Calculate positions where the camera can see the edge of the sprite | |
float edgeVisiblePositionRight = (transform.position.x + spriteWidth / 2) - camHorizontalExtend; | |
float edgeVisiblePostionLeft = (transform.position.x - spriteWidth / 2) + camHorizontalExtend; | |
// Is left or right edge visible? | |
if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false) { | |
if (tileApi.IsTile(xAxis + 1, yAxis) == false) { | |
MakeNewBuddyX(1); | |
tileApi.AddTile(xAxis + 1, yAxis); | |
} | |
hasARightBuddy = true; | |
} else if (cam.transform.position.x <= edgeVisiblePostionLeft + offsetX && hasALeftBuddy == false) { | |
if (tileApi.IsTile(xAxis - 1, yAxis) == false) { | |
MakeNewBuddyX(-1); | |
tileApi.AddTile(xAxis - 1, yAxis); | |
} | |
hasALeftBuddy = true; | |
} | |
} else if (repeatY && (hasATopBuddy == false || hasABottomBuddy == false)) { | |
float camVerticalExtend = cam.orthographicSize * Screen.height / Screen.width; | |
// Calculate top and bottom visible positions | |
float edgeVisiblePositionTop = (transform.position.y + spriteHeight / 2) - camVerticalExtend; | |
float edgeVisiblePositionBottom = (transform.position.y - spriteHeight / 2) + camVerticalExtend; | |
// Is top or bottom edge visible? | |
if (cam.transform.position.y >= edgeVisiblePositionTop - offsetY && hasATopBuddy == false) { | |
if (tileApi.IsTile(xAxis, yAxis + 1) == false) { | |
MakeNewBuddyY(1); | |
tileApi.AddTile(xAxis, yAxis + 1); | |
} | |
hasATopBuddy = true; | |
} else if (cam.transform.position.y <= edgeVisiblePositionBottom + offsetY && hasABottomBuddy == false) { | |
if (tileApi.IsTile(xAxis, yAxis - 1) == false) { | |
MakeNewBuddyY(-1); | |
tileApi.AddTile(xAxis, yAxis - 1); | |
} | |
hasABottomBuddy = true; | |
} | |
} | |
} | |
void MakeNewBuddyX (int rightOrLeft) { | |
// Calculate new pos | |
Vector3 newPosition = new Vector3(transform.position.x + spriteWidth * rightOrLeft, transform.position.y, transform.position.z); | |
Transform newBuddy = Instantiate(transform, newPosition, transform.rotation) as Transform; | |
// Flip the tiled item in reverse (usually for non-tiled items) | |
if (reverseScaleX == true) { | |
newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z); | |
} | |
if (reverseScaleY == true) { | |
newBuddy.localScale = new Vector3(newBuddy.localScale.x, newBuddy.localScale.y * -1, newBuddy.localScale.z); | |
} | |
// Indicate pre-existing buddy is available to the element JUST created | |
newBuddy.parent = transform.parent; | |
if (rightOrLeft > 0) { | |
newBuddy.GetComponent<TilerChild>().hasALeftBuddy = true; | |
} else { | |
newBuddy.GetComponent<TilerChild>().hasARightBuddy = true; | |
} | |
// Update the axis position properly | |
newBuddy.GetComponent<TilerChild>().xAxis += rightOrLeft; | |
} | |
void MakeNewBuddyY (int topOrBottom) { | |
// Calculate new pos | |
Vector3 newPosition = new Vector3(transform.position.x, transform.position.y + spriteHeight * topOrBottom, transform.position.z); | |
Transform newBuddy = Instantiate(transform, newPosition, transform.rotation) as Transform; | |
if (reverseScaleX == true) { | |
newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z); | |
} | |
if (reverseScaleY == true) { | |
newBuddy.localScale = new Vector3(newBuddy.localScale.x, newBuddy.localScale.y * -1, newBuddy.localScale.z); | |
} | |
newBuddy.parent = transform.parent; | |
if (topOrBottom > 0) { | |
newBuddy.GetComponent<TilerChild>().hasABottomBuddy = true; | |
} else { | |
newBuddy.GetComponent<TilerChild>().hasATopBuddy = true; | |
} | |
// Update the axis position properly | |
newBuddy.GetComponent<TilerChild>().yAxis += topOrBottom; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment