Commit b30c4bd7 authored by Luke Smith's avatar Luke Smith

Start for drag placement.

parent 6a52a6fb
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// This script should be attached the the prefab of the left side cap.
// Set mid to the prefab that should be placed between the caps.
// The right cap will be the flipped version of the left cap.
public class DragPlacement : MonoBehaviour
{
public GameObject cap;
public GameObject mid;
public static GameObject instance; // Current instance of this script
public void endDrag(Vector3 pos)
{
Vector2 dir = pos - transform.position;
// Moved further left/right
if(Mathf.Abs(dir.x) > Mathf.Abs(dir.y))
{
placeX((int)pos.x);
}
// Moved further up/down
else
{
placeY((int)pos.y);
}
}
private void placeX(int x)
{
// Placeing left
if(x < transform.position.x)
{
// Place start cap and flip
GameObject go = World.set(transform.position, cap);
go.transform.localScale = new Vector3(go.transform.localScale.x * -1,
go.transform.localScale.y,
go.transform.localScale.z);
// Place mid
for(int i = (int)transform.position.x; i > x; i--)
{
World.set(i, transform.position.y, transform.position.z, mid);
}
// Place cap
World.set(x, transform.position.y, transform.position.z, cap);
}
// Placing right
else
{
// Place start cap
World.set(transform.position, cap);
// Place mid
for(int i = (int)transform.position.x; i < x; i++)
{
World.set(i, transform.position.y, transform.position.z, mid);
}
// Place cap
GameObject go = World.set(x, transform.position.y, transform.position.z, cap);
go.transform.localScale = new Vector3(go.transform.localScale.x * -1,
go.transform.localScale.y,
go.transform.localScale.z);
}
}
private void placeY(int y)
{
// Placeing down
if(y < transform.position.y)
{
// Place start cap and flip
GameObject go = World.set(transform.position, cap);
go.transform.localScale = new Vector3(go.transform.localScale.x,
go.transform.localScale.y * -1,
go.transform.localScale.z);
go.transform.Rotate(new Vector3(0, 0, -90));
// Place mid
for(int i = (int)transform.position.y; i > y; i--)
{
go = World.set(transform.position.x, i, transform.position.z, mid);
go.transform.Rotate(new Vector3(0, 0, -90));
}
// Place cap
go = World.set(y, transform.position.y, transform.position.z, cap);
go.transform.Rotate(new Vector3(0, 0, -90));
}
// Placing up
else
{
// Place start cap and flip
GameObject go = World.set(transform.position, cap);
go.transform.Rotate(new Vector3(0, 0, 90));
// Place mid
for(int i = (int)transform.position.y; i < y; i++)
{
go = World.set(transform.position.x, i, transform.position.z, mid);
go.transform.Rotate(new Vector3(0, 0, 90));
}
// Place cap
go = World.set(y, transform.position.y, transform.position.z, cap);
go.transform.localScale = new Vector3(go.transform.localScale.x,
go.transform.localScale.y * -1,
go.transform.localScale.z);
go.transform.Rotate(new Vector3(0, 0, 90));
}
}
}
......@@ -81,7 +81,8 @@ public class MapEditor : MonoBehaviour
{
if(!World.Ready())
{
Debug.LogError("World not initialized! Please set a width, height, and depth in the inspector and click \"Set\".");
Debug.LogError("World not initialized!\n"
+"Please set a width, height, and depth in the inspector and click \"Set\".");
return;
}
......@@ -90,27 +91,40 @@ public class MapEditor : MonoBehaviour
if(Tiles == null)
GameObject.FindObjectOfType<MapEditor>().setupTilesParent();
// Placing non-empty tile
if(Tile != null)
{
if(!World.posIsThis(pos, Tile))
// Not a drag place tile
if(Tile.GetComponent<DragPlacement>() == null)
{
Debug.Log("new");
GameObject go = (GameObject)GameObject.Instantiate(Tile, new Vector3(pos.x, pos.y, Layer), Quaternion.identity);
go = PrefabUtility.ConnectGameObjectToPrefab(go, Tile);
go.transform.position = new Vector3(pos.x, pos.y, Layer);
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
if(sr != null)
if(!World.posIsThis(pos, Tile))
{
sr.sortingOrder = layer * -World.SORTING_ORDER_MULT;
Debug.Log("new");
GameObject go = (GameObject)GameObject.Instantiate(Tile, new Vector3(pos.x, pos.y, Layer), Quaternion.identity);
go = PrefabUtility.ConnectGameObjectToPrefab(go, Tile);
go.transform.position = new Vector3(pos.x, pos.y, Layer);
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
if(sr != null)
{
sr.sortingOrder = layer * -World.SORTING_ORDER_MULT;
}
go.transform.parent = Tiles;
World.set(pos, layer, go);
}
go.transform.parent = Tiles;
World.set(pos, layer, go);
}
// Is a drag place tile
else
Debug.Log("new");
{
if(DragPlacement.instance == null)
{ // Start new drag placement
// Need to detect when not holding mouse to end drag placement
}
}
}
// Placing empty tile (delete)
else
{
World.set(pos, layer, null);
......
......@@ -58,19 +58,19 @@ public static class World
public static void giveMap(GameObject[][][] newMap) { map = newMap; }
public static bool set(Vector3 pos, GameObject go) { return set(pos.x, pos.y, pos.z, go); }
public static bool set(Vector2 pos, float z, GameObject go) { return set(pos.x, pos.y, z, go); }
public static bool set(Vector2 pos, int z, GameObject go) { return set((int)pos.x, (int)pos.y, z, go); }
public static bool set(float x, float y, float z, GameObject go) { return set((int)x, (int)y, (int)z, go); }
public static bool set(int x, int y, int z, GameObject go)
public static GameObject set(Vector3 pos, GameObject go) { return set(pos.x, pos.y, pos.z, go); }
public static GameObject set(Vector2 pos, float z, GameObject go) { return set(pos.x, pos.y, z, go); }
public static GameObject set(Vector2 pos, int z, GameObject go) { return set((int)pos.x, (int)pos.y, z, go); }
public static GameObject set(float x, float y, float z, GameObject go) { return set((int)x, (int)y, (int)z, go); }
public static GameObject set(int x, int y, int z, GameObject go)
{
if(!posInBounds(x, y, z))
return false;
return null;
destroyPos(x, y, z);
map[x][y][z] = go;
return true;
return go;
}
......
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