Building movable, Position save, Overlapse prevention
This commit is contained in:
8
Assets/Game/Scenes/GameScene/GameScene.meta
Normal file
8
Assets/Game/Scenes/GameScene/GameScene.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57a9dfba5a6399f839b2e0d779de61ee
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load Diff
2294
Assets/Game/Scenes/GameScene/GameScene/OcclusionCullingData.asset
Normal file
2294
Assets/Game/Scenes/GameScene/GameScene/OcclusionCullingData.asset
Normal file
File diff suppressed because one or more lines are too long
@@ -1,8 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f56e18467d914a7c39317263b81852b6
|
||||
guid: 0f864d91a51b9b4428f54c9afa78d64a
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 112000000
|
||||
mainObjectFileID: 36300000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,96 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7d12ab5d9daab245931f1a47f0609e5
|
||||
TextureImporter:
|
||||
internalIDToNameTable: []
|
||||
externalObjects: {}
|
||||
serializedVersion: 11
|
||||
mipmaps:
|
||||
mipMapMode: 0
|
||||
enableMipMap: 1
|
||||
sRGBTexture: 1
|
||||
linearTexture: 0
|
||||
fadeOut: 0
|
||||
borderMipMap: 0
|
||||
mipMapsPreserveCoverage: 0
|
||||
alphaTestReferenceValue: 0.5
|
||||
mipMapFadeDistanceStart: 1
|
||||
mipMapFadeDistanceEnd: 3
|
||||
bumpmap:
|
||||
convertToNormalMap: 0
|
||||
externalNormalMap: 0
|
||||
heightScale: 0.25
|
||||
normalMapFilter: 0
|
||||
isReadable: 0
|
||||
streamingMipmaps: 0
|
||||
streamingMipmapsPriority: 0
|
||||
vTOnly: 0
|
||||
grayScaleToAlpha: 0
|
||||
generateCubemap: 6
|
||||
cubemapConvolution: 1
|
||||
seamlessCubemap: 1
|
||||
textureFormat: 1
|
||||
maxTextureSize: 2048
|
||||
textureSettings:
|
||||
serializedVersion: 2
|
||||
filterMode: 2
|
||||
aniso: 0
|
||||
mipBias: 0
|
||||
wrapU: 1
|
||||
wrapV: 1
|
||||
wrapW: 1
|
||||
nPOTScale: 1
|
||||
lightmap: 0
|
||||
compressionQuality: 50
|
||||
spriteMode: 0
|
||||
spriteExtrude: 1
|
||||
spriteMeshType: 1
|
||||
alignment: 0
|
||||
spritePivot: {x: 0.5, y: 0.5}
|
||||
spritePixelsToUnits: 100
|
||||
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
||||
spriteGenerateFallbackPhysicsShape: 1
|
||||
alphaUsage: 1
|
||||
alphaIsTransparency: 0
|
||||
spriteTessellationDetail: -1
|
||||
textureType: 0
|
||||
textureShape: 2
|
||||
singleChannelComponent: 0
|
||||
flipbookRows: 1
|
||||
flipbookColumns: 1
|
||||
maxTextureSizeSet: 0
|
||||
compressionQualitySet: 0
|
||||
textureFormatSet: 0
|
||||
ignorePngGamma: 0
|
||||
applyGammaDecoding: 0
|
||||
platformSettings:
|
||||
- serializedVersion: 3
|
||||
buildTarget: DefaultTexturePlatform
|
||||
maxTextureSize: 2048
|
||||
resizeAlgorithm: 0
|
||||
textureFormat: -1
|
||||
textureCompression: 1
|
||||
compressionQuality: 100
|
||||
crunchedCompression: 0
|
||||
allowsAlphaSplitting: 0
|
||||
overridden: 0
|
||||
androidETC2FallbackOverride: 0
|
||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||
spriteSheet:
|
||||
serializedVersion: 2
|
||||
sprites: []
|
||||
outline: []
|
||||
physicsShape: []
|
||||
bones: []
|
||||
spriteID:
|
||||
internalID: 0
|
||||
vertices: []
|
||||
indices:
|
||||
edges: []
|
||||
weights: []
|
||||
secondaryTextures: []
|
||||
spritePackingTag:
|
||||
pSDRemoveMatte: 0
|
||||
pSDShowRemoveMatteOption: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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