Building movable, Position save, Overlapse prevention
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class WorldItemSelector : MonoBehaviour
|
||||
{
|
||||
public float moveThreshold = 1;
|
||||
public float moveThreshold = 10;
|
||||
public LayerMask layerMask;
|
||||
public LayerMask gndMask;
|
||||
|
||||
public void SelectScreenPoint(Vector2 screenPoint){
|
||||
Ray ray = Camera.main.ScreenPointToRay(screenPoint);
|
||||
@@ -16,38 +16,102 @@ public class WorldItemSelector : MonoBehaviour
|
||||
|
||||
if(Physics.Raycast(ray,out hit, Mathf.Infinity, layerMask)){
|
||||
Building selectedB = hit.collider.GetComponent<Building>();
|
||||
if(selectedB!=null){
|
||||
Debug.Log("Selected building : " + selectedB.buildingData.buildingName);
|
||||
Selector.selectBuilding(selectedB);
|
||||
}else{
|
||||
Debug.Log("No target here, Unselecting");
|
||||
Selector.selectBuilding(null);
|
||||
}
|
||||
// if(selectedB!=null){
|
||||
// Debug.Log("Selected building : " + selectedB.buildingData.buildingName);
|
||||
//
|
||||
// }else{
|
||||
// Debug.Log("No target here, Unselecting");
|
||||
// //Selector.selectBuilding(null);
|
||||
// }
|
||||
Selector.selectBuilding(selectedB);
|
||||
}else{
|
||||
Selector.selectBuilding(null);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3? GetTouchingWorldPoint(Vector2 screenPoint){
|
||||
Ray ray = Camera.main.ScreenPointToRay(screenPoint);
|
||||
RaycastHit hit = new RaycastHit();
|
||||
|
||||
if(Physics.Raycast(ray,out hit, Mathf.Infinity, gndMask)){
|
||||
return hit.point;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Vector2 startedPos= Vector2.zero;
|
||||
|
||||
private Vector2? startedPos= null;
|
||||
private bool isPointerDown => startedPos!=null;
|
||||
private bool holdingPointer = false;
|
||||
public float dragDelay = 0.85f;
|
||||
private long pointerDownTimestamp;
|
||||
public void OnPointerDown(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
startedPos = ped.position;
|
||||
holdingPointer =true;
|
||||
Debug.Log($"Pointer timestamp : {pointerDownTimestamp}");
|
||||
StartCoroutine(startDragCheck());
|
||||
}
|
||||
|
||||
IEnumerator startDragCheck(){
|
||||
yield return new WaitForSeconds(dragDelay);
|
||||
|
||||
if(holdingPointer){
|
||||
Debug.Log("Initiating Drag");
|
||||
SelectScreenPoint(startedPos ?? Vector2.zero);
|
||||
|
||||
Selector.setMoving(Selector.selectedBuilding);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerMove(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
|
||||
if(isPointerDown){
|
||||
|
||||
float pointerDiff = Mathf.Abs(ped.position.magnitude - (startedPos ?? Vector2.zero).magnitude);
|
||||
|
||||
holdingPointer=(pointerDiff < moveThreshold);
|
||||
|
||||
if(Selector.isMovingBuilding){
|
||||
// Debug.Log("Gnd position: " + GetTouchingWorldPoint(ped.position));
|
||||
Vector3 worldPos = GetTouchingWorldPoint(ped.position) ?? Selector.movingBuilding.transform.position;
|
||||
//validatePoint
|
||||
Selector.movingPointValid = true;
|
||||
Collider[] buildingsInside = Physics.OverlapBox(Selector.movingBuilding.GetComponent<Collider>().bounds.center, Selector.movingBuilding.GetComponent<Collider>().bounds.extents);
|
||||
foreach(Collider collider in buildingsInside){
|
||||
if((collider.tag == "Building" || collider.tag == "Obstacle" ) && collider.transform != Selector.movingBuilding.transform){
|
||||
Selector.movingPointValid =false;
|
||||
}
|
||||
}
|
||||
Selector.movingBuilding.ChangeOutlineColor(Selector.movingPointValid ? Color.green : Color.red);
|
||||
Selector.movingBuilding.transform.position = worldPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerUp(BaseEventData e){
|
||||
|
||||
Selector.resetMoving();
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
float pointerDiff = Mathf.Abs(ped.position.magnitude - startedPos.magnitude);
|
||||
float pointerDiff = Mathf.Abs(ped.position.magnitude - (startedPos ?? Vector2.zero).magnitude);
|
||||
if(pointerDiff < moveThreshold){
|
||||
SelectScreenPoint(ped.position);
|
||||
}else{
|
||||
Debug.Log("Pointer moved (" + pointerDiff+ "), Not gonna select item");
|
||||
}
|
||||
startedPos = null;
|
||||
holdingPointer=false;
|
||||
Selector.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Selector{
|
||||
public static bool movingPointValid;
|
||||
public static Building movingBuilding;
|
||||
public static bool isMovingBuilding => movingBuilding!=null;
|
||||
public static Building selectedBuilding;
|
||||
public static BuildingData selectedData => selectedBuilding.buildingData;
|
||||
public static UnityEvent OnSelectedChanged = new UnityEvent();
|
||||
@@ -56,4 +120,28 @@ public static class Selector{
|
||||
selectedBuilding = e;
|
||||
OnSelectedChanged.Invoke();
|
||||
}
|
||||
|
||||
public static void resetMoving(){
|
||||
if(movingBuilding!=null ){
|
||||
if(movingPointValid) {
|
||||
DBmanager.RelocateBuilding(movingBuilding.buildingData.buildingName, movingBuilding.transform.position);
|
||||
}else{
|
||||
movingBuilding.transform.position = movingStartedPosition;
|
||||
}
|
||||
}
|
||||
movingBuilding=null;
|
||||
refresh();
|
||||
}
|
||||
|
||||
private static Vector3 movingStartedPosition;
|
||||
|
||||
public static void setMoving(Building e){
|
||||
movingBuilding=e;
|
||||
if(e!=null){ movingStartedPosition=e.transform.position;}
|
||||
refresh();
|
||||
}
|
||||
|
||||
public static void refresh(){
|
||||
OnSelectedChanged.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user