Commit ec6eef23 authored by Luke A Smith's avatar Luke A Smith

Merge branch 'borders' into 'master'

Borders

See merge request !15
parents 36b87a7e 9d269d1e
......@@ -37,6 +37,11 @@ public static class World
setupWithGOs();
}
public static void setup()
{
setup(50, 50 , 3);
}
private static void instantiateMap()
{
map = new GameObject[width][][];
......@@ -113,7 +118,7 @@ public static class World
{
if(x < 0 || y < 0 || z < 0 || x >= width || y >= height || z >= depth)
{
Debug.LogWarning("Pos out of bounds!");
//Debug.LogWarning("Pos out of bounds!");
return false;
}
return true;
......
using UnityEngine;
using UnityEditor;
public class Borders : MonoBehaviour
{
public int top = 2, right = 2, bottom = 2, left = 2; // Border sizes by pixel
private enum Direction{none, t, r, rt, b, bt, br, brt, l, lt, lr, lrt, lb, lbt, lbr, lbrt}
// Binary representation:
// lbrt
// 0000
// Use this for initialization
void Start ()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if(sr != null)
{
Texture2D texOriginal = sr.sprite.texture;
Texture2D tex = (Texture2D)Instantiate(texOriginal);
Color32[] pixels = tex.GetPixels32();
tex.SetPixels32(setBorders(pixels, tex.width, tex.height, checkAdjacency()));
tex.Apply(false);
sr.sprite = Sprite.Create(tex, sr.sprite.rect, new Vector2(.5f, .5f), sr.sprite.pixelsPerUnit);
}
}
Direction checkAdjacency()
{
Direction d = Direction.none;
Vector3 pos = transform.position;
if(!World.Ready())
{
World.setup();
}
if(!World.posInBounds(pos + Vector3.up) || World.posIsThis(pos + Vector3.up, (GameObject)PrefabUtility.GetPrefabObject(this)))
{
d |= Direction.t;
}
if(!World.posInBounds(pos + Vector3.left) ||World.posIsThis(pos + Vector3.left, (GameObject)PrefabUtility.GetPrefabObject(this)))
{
d |= Direction.r;
}
if(!World.posInBounds(pos + Vector3.down) ||World.posIsThis(pos + Vector3.down, (GameObject)PrefabUtility.GetPrefabObject(this)))
{
d |= Direction.b;
}
if(!World.posInBounds(pos + Vector3.right) ||World.posIsThis(pos + Vector3.right, (GameObject)PrefabUtility.GetPrefabObject(this)))
{
d |= Direction.l;
}
return d;
}
Color32[] setBorders(Color32[] pixels, int width, int height, Direction d)
{
if((d & Direction.t) > 0) // top
{
for(int i = left; i < width - right; i++)
{
for(int j = height - top*2; j < height - top; j++)
{
pixels[j*width + i] = pixels[(j+top)*width + i];
}
}
}
if((d & Direction.r) > 0) // right
{
for(int j = bottom; j < height-top; j++)
{
for(int i = left; i < left*2; i++)
{
pixels[j*width + i] = pixels[j*width + (i-left)];
}
}
}
if((d & Direction.b) > 0) // bottom
{
for(int i = left; i < width - right; i++)
{
for(int j = bottom; j < bottom*2; j++)
{
pixels[j*width + i] = pixels[(j-bottom)*width + i];
}
}
}
if((d & Direction.l) > 0) // left
{
for(int j = bottom; j < height-top; j++)
{
for(int i = width - right*2; i < width-right; i++)
{
pixels[j*width + i] = pixels[j*width + (i+right)];
}
}
}
return pixels;
}
}
fileFormatVersion: 2
guid: 99517dc8d8fd9524a90fa289b59bf635
timeCreated: 1487785654
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment