Building movable, Position save, Overlapse prevention
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
public class Building : MonoBehaviour
|
||||
@@ -9,26 +7,44 @@ public class Building : MonoBehaviour
|
||||
public int curLevel;
|
||||
public Outline[] outlines;
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.color = Color.blue;
|
||||
//Gizmos.DrawWireSphere(transform.position + new Vector3(0,spaceRadius,0), spaceRadius);
|
||||
Collider col = GetComponent<Collider>();
|
||||
Gizmos.DrawWireCube(col.bounds.center, col.bounds.size);
|
||||
}
|
||||
|
||||
void Awake(){
|
||||
//if(outline == null){outline = GetComponent<Outline>();}
|
||||
outlines = GetComponentsInChildren<Outline>();
|
||||
ToggleOutlines(false);
|
||||
Selector.OnSelectedChanged.AddListener(OnSelectedChanged);
|
||||
transform.tag = "Building";
|
||||
}
|
||||
|
||||
void OnSelectedChanged(){
|
||||
if(Selector.selectedBuilding == null){ToggleOutlines(false); return;}
|
||||
|
||||
ToggleOutlines(Selector.selectedBuilding == this);
|
||||
ToggleOutlines(Selector.selectedBuilding == this);
|
||||
//GetComponent<Collider>().isTrigger=(Selector.movingBuilding==this);
|
||||
|
||||
}
|
||||
|
||||
void ToggleOutlines(bool value){
|
||||
bool isMoving = Selector.movingBuilding==this;
|
||||
foreach(Outline outline in outlines){
|
||||
outline.enabled = value;
|
||||
outline.OutlineColor = (isMoving) ? Color.green : Color.yellow;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Upgrade(){
|
||||
public void ChangeOutlineColor(Color color){
|
||||
foreach(Outline outline in outlines){
|
||||
outline.OutlineColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
public void Upgrade(){
|
||||
if(curLevel >= buildingData.levels.Count-1){Debug.Log("Already max");return;}
|
||||
int cost = buildingData.levels[curLevel+1].price;
|
||||
// Debug.Log("Upgrading " + buildingData.buildingName + " for " + cost + " coins");
|
||||
@@ -40,11 +56,41 @@ public class Building : MonoBehaviour
|
||||
}
|
||||
|
||||
|
||||
await (DBmanager.SetCoins(DBmanager.Coins-cost));
|
||||
DBmanager.SetCoins(DBmanager.Coins-cost);
|
||||
Mathf.Clamp(curLevel++,0, buildingData.levels.Count-1);
|
||||
|
||||
await DBmanager.UpgradeBuilding(buildingData.buildingName, curLevel);
|
||||
DBmanager.UpgradeBuilding(buildingData.buildingName, curLevel);
|
||||
}
|
||||
//
|
||||
// public List<Collider> buildingsInsideMe = new List<Collider>();
|
||||
// public bool locationInvalid {get{return buildingsInsideMe.Count == 0;}}
|
||||
//
|
||||
// void OnTriggerEnter(Collider other) {
|
||||
// Debug.Log("Trigger enter : " + other.name);
|
||||
// if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
|
||||
// if(buildingsInsideMe.Contains(other)){
|
||||
// //Already got him
|
||||
// }else{
|
||||
// buildingsInsideMe.Add(other);ChangeOutlineColor(locationInvalid ? Color.red : Color.green);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// void OnTriggerExit(Collider other){
|
||||
// Debug.Log("Trigger exit : " + other.name);
|
||||
// if(other.GetComponent<Collider>().tag == "Building" && other != GetComponent<Collider>()){
|
||||
// if(buildingsInsideMe.Contains(other)){
|
||||
// buildingsInsideMe.Remove(other);
|
||||
// ChangeOutlineColor(locationInvalid ? Color.green : Color.red);
|
||||
// }else{
|
||||
// //Nothing to remove
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
/* */
|
||||
}
|
||||
@@ -54,9 +100,11 @@ public class Building : MonoBehaviour
|
||||
public class BuildingState{
|
||||
public string id;
|
||||
public int level;
|
||||
public Vector3 position;
|
||||
|
||||
public BuildingState(string m_id, int m_level){
|
||||
public BuildingState(string m_id, int m_level, Vector3 m_position){
|
||||
id = m_id;
|
||||
level = m_level;
|
||||
position = m_position;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ public class BuildingManager : MonoBehaviour
|
||||
if(buildingState.id == building.buildingData.buildingName){
|
||||
buildingExists =true;
|
||||
building.curLevel = buildingState.level;
|
||||
building.transform.position = (buildingState.position != Vector3.zero) ? buildingState.position : building.transform.position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,27 +22,21 @@ public class CameraController : MonoBehaviour
|
||||
private Vector3 cameraStartPos = Vector2.zero;
|
||||
public bool moving = false;
|
||||
public void OnMouseDown(BaseEventData e){
|
||||
#if UNITY_EDITOR
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
mouseStartPos = ped.position;
|
||||
cameraStartPos = cam.transform.position;
|
||||
moving=true;
|
||||
#endif
|
||||
}
|
||||
public void OnMouseUp(BaseEventData e){
|
||||
#if UNITY_EDITOR
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
moving=false;
|
||||
#endif
|
||||
}
|
||||
public void OnMouseMove(BaseEventData e){
|
||||
#if UNITY_EDITOR
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
if(moving){
|
||||
if(moving && !Selector.isMovingBuilding){
|
||||
Vector3 offset = (mouseStartPos-ped.position) * sensitivity;
|
||||
cam.transform.position = cameraStartPos + new Vector3(offset.x,0,offset.y);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ public class DBmanager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
buildingStates.Add(new BuildingState(buildingData.buildingName, 0));
|
||||
buildingStates.Add(new BuildingState(buildingData.buildingName, 0,Vector3.zero));
|
||||
Debug.Log("Added new building "+ buildingData.buildingName);
|
||||
await UpdateBuildingsToServer();
|
||||
|
||||
@@ -114,6 +114,20 @@ public class DBmanager : MonoBehaviour
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static Task RelocateBuilding(string id, Vector3 newPosition){
|
||||
for(int i=0; i < buildingStates.Count; i++){
|
||||
if(buildingStates[i].id == id){
|
||||
buildingStates[i].position = newPosition;
|
||||
Debug.Log("Relocating " + id + " to " + newPosition);
|
||||
// break;
|
||||
}
|
||||
}
|
||||
Debug.Log("Going to update to server" + JsonConvert.SerializeObject(buildingStates));
|
||||
await UpdateBuildingsToServer();
|
||||
|
||||
OnStateChanged.Invoke();
|
||||
}
|
||||
|
||||
public async static Task UpdateBuildingsToServer(){
|
||||
string buildingsJson = JsonConvert.SerializeObject(buildingStates);
|
||||
Debug.Log(buildingsJson);
|
||||
|
||||
@@ -26,7 +26,7 @@ LightingSettings:
|
||||
m_CompAOExponent: 1
|
||||
m_CompAOExponentDirect: 0
|
||||
m_ExtractAO: 0
|
||||
m_MixedBakeMode: 0
|
||||
m_MixedBakeMode: 1
|
||||
m_LightmapsBakeMode: 1
|
||||
m_FilterMode: 1
|
||||
m_LightmapParameters: {fileID: 15201, guid: 0000000000000000f000000000000000, type: 0}
|
||||
|
||||
@@ -23,8 +23,8 @@ class ScrollAndPinch : MonoBehaviour
|
||||
{
|
||||
|
||||
//Update Plane
|
||||
if (Input.touchCount >= 1)
|
||||
Plane.SetNormalAndPosition(transform.up, transform.position);
|
||||
// if (Input.touchCount >= 1)
|
||||
//Plane.SetNormalAndPosition(transform.up, transform.position);
|
||||
|
||||
var Delta1 = Vector3.zero;
|
||||
var Delta2 = Vector3.zero;
|
||||
|
||||
@@ -88,10 +88,10 @@ public class SelectedItemMenu : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
async void OnUpgrade()
|
||||
void OnUpgrade()
|
||||
{
|
||||
upgradeBtn.interactable= false;
|
||||
await Selector.selectedBuilding.Upgrade();
|
||||
Selector.selectedBuilding.Upgrade();
|
||||
OnUpgradeMenuClicked();
|
||||
}
|
||||
|
||||
|
||||
@@ -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