-
-
Save hawksprite/54efbb05937eee19980ec9a06530d756 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 System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Net; | |
using DG.Tweening; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
public class MouseController : MonoBehaviour | |
{ | |
public GameObject circleCursorPrefab; | |
private Tile tileUnderMouse; | |
// The world-position of the mouse last frame. | |
Vector3 lastFramePosition; | |
Vector3 currFramePosition; | |
Vector3 dragStartPosition; | |
List<GameObject> dragPreviewGameObjects; | |
private GameObject cameraParent; | |
private TileController tc; | |
private BuildModeController bmc; | |
enum MouseMode | |
{ | |
SELECT, | |
BUILD, | |
DECONSTRUCT | |
} | |
MouseMode currentMode = MouseMode.SELECT; | |
private Ray ray; | |
public Tile GetMouseOverTile() | |
{ | |
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); | |
mousePos.y = 0f; | |
return WorldController.Instance.GetTileAtWorldCoord(mousePos); | |
} | |
// Use this for initialization | |
void Start() | |
{ | |
dragPreviewGameObjects = new List<GameObject>(); | |
bmc = FindObjectOfType<BuildModeController>(); | |
tc = FindObjectOfType<TileController>(); | |
cameraParent = GameObject.Find("Main Camera"); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
} | |
RaycastHit _hit; | |
if (Physics.Raycast(ray, out _hit)) | |
{ | |
currFramePosition = _hit.point; | |
} | |
UpdateDragging(); | |
UpdateCameraMovement(); | |
lastFramePosition = currFramePosition; | |
} | |
public Tile GetTileUnderMouse() | |
{ | |
return WorldController.Instance.GetTileAtWorldCoord(currFramePosition); | |
} | |
private bool DrawOutsideOnly = true; | |
private bool isDragging = false; | |
void UpdateDragging() | |
{ | |
//We're over a UI Element. | |
if (EventSystem.current.IsPointerOverGameObject()) return; | |
//Clean up the old previews. | |
while (dragPreviewGameObjects.Count > 0) | |
{ | |
GameObject go = dragPreviewGameObjects[0]; | |
dragPreviewGameObjects.RemoveAt(0); | |
SimplePool.Despawn(go); | |
} | |
if (currentMode != MouseMode.BUILD) | |
{ | |
SetVisualizeBuildGridToolThings(null, null); | |
return; | |
} | |
//Start Drag | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
dragStartPosition = currFramePosition; | |
isDragging = true; | |
} | |
else if (isDragging == false) | |
{ | |
dragStartPosition = currFramePosition; | |
} | |
if (bmc.IsObjectDraggable() == false) | |
{ | |
dragStartPosition = currFramePosition; | |
} | |
if (Input.GetMouseButtonUp(1) || Input.GetKeyUp(KeyCode.Escape)) | |
{ | |
// The RIGHT mouse button was released, so we | |
// are cancelling any dragging/build mode. | |
isDragging = false; | |
} | |
if (bmc.IsObjectDraggable() == false) | |
{ | |
dragStartPosition = currFramePosition; | |
} | |
int start_x = Mathf.FloorToInt(dragStartPosition.x + 0.5f); | |
int end_x = Mathf.FloorToInt(currFramePosition.x + 0.5f); | |
int start_y = Mathf.FloorToInt(dragStartPosition.z + 0.5f); | |
int end_y = Mathf.FloorToInt(currFramePosition.z + 0.5f); | |
// We may be dragging in the "wrong" direction, so flip things if needed. | |
if (end_x < start_x) | |
{ | |
int tmp = end_x; | |
end_x = start_x; | |
start_x = tmp; | |
} | |
if (end_y < start_y) | |
{ | |
int tmp = end_y; | |
end_y = start_y; | |
start_y = tmp; | |
} | |
for (int x = start_x; x <= end_x; x++) | |
{ | |
for (int y = start_y; y <= end_y; y++) | |
{ | |
Tile t = WorldController.Instance.world.GetTileAt(x, y); | |
if (t != null) | |
{ | |
if (bmc.buildMode == BuildMode.EQUIPMENT) | |
{ | |
MoveEquipmentOnTile(bmc.buildModeObjectType, t); | |
SetVisualizeBuildGridToolThings(bmc.buildModeObjectType, t); | |
} | |
if (bmc.buildMode == BuildMode.FOUNDATION) | |
{ | |
GameObject go = SimplePool.Spawn(circleCursorPrefab, new Vector3(x, 0.001f, y), | |
Quaternion.Euler(90f, 0f, 0f)); | |
go.transform.SetParent(this.transform, true); | |
dragPreviewGameObjects.Add(go); | |
} | |
//if (!DrawOutsideOnly || ((x == start_x || x == end_x) || (y == start_y || y == end_y))) | |
//{ | |
// // show the generic dragging visuals | |
// GameObject go = SimplePool.Spawn(circleCursorPrefab, new Vector3(x, 0.1f, y), | |
// Quaternion.Euler(90f, 0f, 0f)); | |
// go.transform.SetParent(this.transform, true); | |
// dragPreviewGameObjects.Add(go); | |
//} | |
} | |
} | |
} | |
//End Drag | |
if (isDragging && Input.GetMouseButtonUp(0)) | |
{ | |
isDragging = false; | |
for (int x = start_x; x <= end_x; x++) | |
{ | |
for (int y = start_y; y <= end_y; y++) | |
{ | |
Tile t = WorldController.Instance.world.GetTileAt(x, y); | |
if (t != null) | |
{ | |
if (bmc.DoBuild(t)) | |
{ | |
} | |
else | |
{ | |
Debug.LogFormat("Tried to place: {0}, but it failed.", bmc.buildModeObjectType); | |
} | |
} | |
} | |
} | |
if (_tmp != null) | |
{ | |
Destroy( | |
_tmp); //TODO: Maybe instead of destroying, we disable it from a list to be reused? | |
_tmp = null; | |
currentMode = MouseMode.SELECT; | |
bmc.buildModeObjectType = null; | |
} | |
} | |
//if (isDragging && Input.GetMouseButtonUp(0) && currentMode == MouseMode.BUILD && !true) | |
//{ | |
// isDragging = false; | |
// for (int x = start_x; x <= end_x; x++) | |
// { | |
// for (int y = start_y; y <= end_y; y++) | |
// { | |
// Tile t = WorldController.Instance.world.GetTileAt(x, y); | |
// if (t != null) | |
// { | |
// if (t.Type == TileType.Grass) { t.Type = TileType.Foundation; } else if (t.Type == TileType.Foundation) t.Type = TileType.Grass; | |
// } | |
// } | |
// } | |
//} | |
} | |
List<int[]> generateGirdOutFrom(Tile root, int width, int height){ | |
List<int[]> buffer = new List<int[]>(); | |
int center_x = width / 2; | |
int center_y = height / 2; | |
// The first placement is the root tile that the mouse is out, then we want to crawl up and down | |
for (int x = 0; x < (width); x++){ | |
for (int y = 0; y < (height); y++){ | |
int diff_x = x - center_x; | |
int diff_y = y - center_y; | |
buffer.append(new int[]{root.X + diff_x, root.Y + diff_y}); | |
} | |
} | |
if ((width % 2) != 0){ | |
// An odd width need to account with potentially an extra tile | |
} | |
if ((height % 2) != 0){ | |
// Same for height | |
} | |
return buffer; | |
} | |
public GameObject visualizeGridPrefab; | |
List<GameObject> visualizeBuildGrid = new List<GameObject>(); | |
void SetVisualizeBuildGridToolThings(string ObjectID, Tile tile) | |
{ | |
while (visualizeBuildGrid.Count > 0) | |
{ | |
GameObject go = visualizeBuildGrid[0]; | |
visualizeBuildGrid.RemoveAt(0); | |
SimplePool.Despawn(go); | |
} | |
if (string.IsNullOrEmpty(bmc.buildModeObjectType) || currentMode == MouseMode.SELECT) return; //Returns after the reset of the visualization. | |
Equipment obj = World.current.equipmentPrototypes[bmc.buildModeObjectType]; | |
int equipmentWidth = obj.Width; | |
int equipmentHeight = obj.Height; | |
List<int[]> placementSpace = generateGirdOutFrom(tile, equipmentWidth, equipmentHeight); | |
foreach(int[] placements in placementSpace){ | |
int x_off = placements[0]; | |
int y_off = placements[1]; | |
Tile t = World.current.GetTileAt(x_off, y_off); | |
if (t != null) | |
{ | |
GameObject go = SimplePool.Spawn(visualizeGridPrefab, | |
new Vector3(t.X, 0.01f, t.Y), Quaternion.Euler(90f, 0f, 0f)); | |
go.transform.SetParent(this.transform, true); | |
visualizeBuildGrid.Add(go); | |
Color clr = go.GetComponent<SpriteRenderer>().color; | |
if ((t.Type != TileType.Foundation && obj.needsFoundation) || t.equipment != null) | |
{ | |
if (clr != Color.red) | |
{ | |
go.GetComponent<SpriteRenderer>().color = Color.red; | |
} | |
} | |
else | |
{ | |
if (clr != Color.green) | |
{ | |
go.GetComponent<SpriteRenderer>().color = Color.green; | |
} | |
} | |
} | |
} | |
} | |
private GameObject _tmp; | |
void MoveEquipmentOnTile(string ObjectID, Tile tile) | |
{ | |
if (string.IsNullOrEmpty(bmc.buildModeObjectType)) return; | |
if (World.current.equipmentPrototypes.ContainsKey(bmc.buildModeObjectType)) | |
{ | |
Equipment obj = World.current.equipmentPrototypes[bmc.buildModeObjectType]; | |
int equipmentWidth = obj.Width; | |
int equipmentHeight = obj.Height; | |
if (_tmp == null) | |
{ | |
_tmp = Instantiate(WorldController.Instance.Equipment[bmc.buildModeObjectType], | |
new Vector3(Mathf.Round(tile.X + equipmentWidth / 2), WorldController.Instance.Equipment[bmc.buildModeObjectType].transform.position.y, | |
Mathf.Round(tile.Y + equipmentHeight / 2)), Quaternion.Euler(0, 180f, 0)); | |
} | |
else | |
{ | |
//_tmp.transform.DOMove(new Vector3(tile.X, bmc.CNCPrefab.transform.position.y, tile.Y), 0.5f, true); | |
_tmp.transform.position = new Vector3(Mathf.Round(tile.X + equipmentWidth / 2), | |
WorldController.Instance.Equipment[bmc.buildModeObjectType].transform.position.y, Mathf.Round(tile.Y + equipmentHeight / 2)); | |
} | |
} | |
else | |
{ | |
Debug.LogError("MoveEquipmentOnTile: OBJ is null. Requested: " + ObjectID); | |
return; | |
} | |
} | |
public void StartBuildMode() | |
{ | |
currentMode = MouseMode.BUILD; | |
} | |
void UpdateCameraMovement() | |
{ | |
if (Input.GetMouseButton(1) || Input.GetMouseButton(2)) //Middle or Right Mouse Button | |
{ | |
//Vector3 diff = lastFramePosition - currFramePosition; | |
//Camera.main.transform.Translate(diff); | |
float h = Input.GetAxis("Mouse Y") * 1f; | |
float v = Input.GetAxis("Mouse X") * 1f; | |
//Camera.main.transform.Translate(v, h, 0f); | |
cameraParent.transform.Translate(v, h, 0f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment