Collectables online, Main hall Join
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -12,11 +12,13 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 0f0adcd9d9fe1cd78b1cc91fd454aa46, type: 3}
|
||||
m_Name: Mine
|
||||
m_EditorClassIdentifier:
|
||||
buildingName: Mine
|
||||
buildingName: Rocket Repair
|
||||
levels:
|
||||
- level: 1
|
||||
stats:
|
||||
- name: Production rate
|
||||
value: 10%
|
||||
price: 8500
|
||||
description: This is a gold mine
|
||||
description: This is a Rocket Repair
|
||||
collectable: 0
|
||||
productinoRates: []
|
||||
|
||||
@@ -22,3 +22,6 @@ MonoBehaviour:
|
||||
value: 10
|
||||
price: 7900
|
||||
description: This is a Moon Minerals Mine
|
||||
collectable: 1
|
||||
productinoRates:
|
||||
- 0.5
|
||||
|
||||
@@ -22,3 +22,6 @@ MonoBehaviour:
|
||||
value: 2
|
||||
price: 20000
|
||||
description: This is an Oxygen Mine
|
||||
collectable: 1
|
||||
productinoRates:
|
||||
- 1
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Building : MonoBehaviour
|
||||
@@ -6,6 +6,7 @@ public class Building : MonoBehaviour
|
||||
public BuildingData buildingData;
|
||||
public int curLevel;
|
||||
public Outline[] outlines;
|
||||
public DateTime lastCollected;
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.color = Color.blue;
|
||||
@@ -101,10 +102,12 @@ public class BuildingState{
|
||||
public string id;
|
||||
public int level;
|
||||
public Vector3 position;
|
||||
public DateTime lastCollectedTimestamp;
|
||||
|
||||
public BuildingState(string m_id, int m_level, Vector3 m_position){
|
||||
id = m_id;
|
||||
level = m_level;
|
||||
position = m_position;
|
||||
lastCollectedTimestamp = DBmanager.GetNetworkTime();
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ public class BuildingData : ScriptableObject
|
||||
};
|
||||
|
||||
public string description;
|
||||
public bool collectable;
|
||||
public float[] productinoRates;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using EasyButtons;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -26,6 +27,7 @@ public class BuildingManager : MonoBehaviour
|
||||
buildingExists =true;
|
||||
building.curLevel = buildingState.level;
|
||||
building.transform.position = (buildingState.position != Vector3.zero) ? buildingState.position : building.transform.position;
|
||||
building.lastCollected = buildingState.lastCollectedTimestamp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class CameraManager : MonoBehaviour
|
||||
{
|
||||
public Transform cam;
|
||||
public float sensitivity;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private Vector2 mouseStartPos = Vector2.zero;
|
||||
private Vector3 cameraStartPos = Vector2.zero;
|
||||
public bool moving = false;
|
||||
public void OnMouseDown(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
mouseStartPos = ped.position;
|
||||
cameraStartPos = cam.transform.position;
|
||||
moving=true;
|
||||
}
|
||||
public void OnMouseUp(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
moving=false;
|
||||
}
|
||||
public void OnMouseMove(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
if(moving && !Selector.isMovingBuilding){
|
||||
Vector3 offset = (mouseStartPos-ped.position) * sensitivity;
|
||||
cam.transform.position = cameraStartPos + new Vector3(offset.x,0,offset.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
79
Assets/Game/Scripts/CameraManager.cs
Normal file
79
Assets/Game/Scripts/CameraManager.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class CameraManager : MonoBehaviour
|
||||
{
|
||||
public Transform cam;
|
||||
public float sensitivity;
|
||||
|
||||
[Header("Main Hall")]
|
||||
public float camTransformSpeed=0.1f;
|
||||
public Transform mainHallTarget;
|
||||
private Vector3 defaultPos;
|
||||
private Quaternion defaultRot;
|
||||
public GameObject leaveHallBtn;
|
||||
public bool isInsideHall => leaveHallBtn.activeSelf;
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private Vector2 mouseStartPos = Vector2.zero;
|
||||
private Vector3 cameraStartPos = Vector2.zero;
|
||||
private Vector3 mainHallCamRotationStart = Vector3.zero;
|
||||
public bool moving = false;
|
||||
public void OnMouseDown(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
mouseStartPos = ped.position;
|
||||
cameraStartPos = cam.transform.position;
|
||||
mainHallCamRotationStart = cam.transform.eulerAngles;
|
||||
moving=true;
|
||||
}
|
||||
public void OnMouseUp(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
moving=false;
|
||||
}
|
||||
public void OnMouseMove(BaseEventData e){
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
|
||||
Vector3 offset = (mouseStartPos-ped.position) * sensitivity;
|
||||
if(moving && !Selector.isMovingBuilding && !isInsideHall){
|
||||
cam.transform.position = cameraStartPos + new Vector3(offset.x,0,offset.y);
|
||||
}else if(isInsideHall){
|
||||
cam.eulerAngles = new Vector3(mainHallCamRotationStart.x - offset.y, mainHallCamRotationStart.y + offset.x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void JoinMainHall(){
|
||||
Selector.enabled=false;
|
||||
Selector.selectBuilding(null);
|
||||
leaveHallBtn.SetActive(true);
|
||||
defaultPos = cam.position;
|
||||
defaultRot = cam.rotation;
|
||||
StartCoroutine(ToggleMainHallCamera(true));
|
||||
}
|
||||
|
||||
public void LeaveMainHall(){
|
||||
Selector.enabled=true;
|
||||
leaveHallBtn.SetActive(false);
|
||||
StartCoroutine(ToggleMainHallCamera(false));
|
||||
}
|
||||
|
||||
IEnumerator ToggleMainHallCamera(bool value){
|
||||
Vector3 targetPos = (value) ? mainHallTarget.position : defaultPos;
|
||||
Quaternion targetRot = (value) ? mainHallTarget.rotation : defaultRot;
|
||||
|
||||
while(Vector3.Distance(cam.position, targetPos) > 2 || (cam.rotation * Quaternion.Inverse(targetRot)).eulerAngles.magnitude > 5){
|
||||
//Debug.Log("Current rot diff : " + (cam.rotation * Quaternion.Inverse(targetRot)).eulerAngles.magnitude);
|
||||
cam.position = Vector3.Lerp(cam.position, targetPos,camTransformSpeed);
|
||||
cam.rotation = Quaternion.Lerp(cam.rotation, targetRot, camTransformSpeed);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
27
Assets/Game/Scripts/CollectBtn.cs
Normal file
27
Assets/Game/Scripts/CollectBtn.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class CollectBtn : MonoBehaviour
|
||||
{
|
||||
public DateTime lastCollected;
|
||||
public float productionRate;
|
||||
public double collectableAmount;
|
||||
public Button btn;
|
||||
public TMP_Text txt;
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
collectableAmount=((DateTime.Now - lastCollected).TotalSeconds * productionRate);
|
||||
txt.text = ((int)collectableAmount).ToString();
|
||||
|
||||
btn.interactable = collectableAmount > 1;
|
||||
}
|
||||
|
||||
public void Set(DateTime _lastCollected, float _productionRate){
|
||||
lastCollected = _lastCollected;
|
||||
productionRate=_productionRate;
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/CollectBtn.cs.meta
Normal file
11
Assets/Game/Scripts/CollectBtn.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0780243d2e94ca4c88eafaf9c28ed56e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,10 +1,13 @@
|
||||
using System;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.Networking;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
public class DBmanager : MonoBehaviour
|
||||
{
|
||||
public static string phpRoot = "http://38.242.232.13/upf/";
|
||||
@@ -24,6 +27,67 @@ public class DBmanager : MonoBehaviour
|
||||
username = null;
|
||||
}
|
||||
|
||||
public static DateTime GetNetworkTime()
|
||||
{
|
||||
//default Windows time server
|
||||
const string ntpServer = "time.windows.com";
|
||||
|
||||
// NTP message size - 16 bytes of the digest (RFC 2030)
|
||||
var ntpData = new byte[48];
|
||||
|
||||
//Setting the Leap Indicator, Version Number and Mode values
|
||||
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
|
||||
|
||||
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
|
||||
|
||||
//The UDP port number assigned to NTP is 123
|
||||
var ipEndPoint = new IPEndPoint(addresses[0], 123);
|
||||
//NTP uses UDP
|
||||
|
||||
using(var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
||||
{
|
||||
socket.Connect(ipEndPoint);
|
||||
|
||||
//Stops code hang if NTP is blocked
|
||||
socket.ReceiveTimeout = 3000;
|
||||
|
||||
socket.Send(ntpData);
|
||||
socket.Receive(ntpData);
|
||||
socket.Close();
|
||||
}
|
||||
|
||||
//Offset to get to the "Transmit Timestamp" field (time at which the reply
|
||||
//departed the server for the client, in 64-bit timestamp format."
|
||||
const byte serverReplyTime = 40;
|
||||
|
||||
//Get the seconds part
|
||||
ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
|
||||
|
||||
//Get the seconds fraction
|
||||
ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
|
||||
|
||||
//Convert From big-endian to little-endian
|
||||
intPart = SwapEndianness(intPart);
|
||||
fractPart = SwapEndianness(fractPart);
|
||||
|
||||
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
|
||||
|
||||
//**UTC** time
|
||||
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
|
||||
|
||||
return networkDateTime.ToLocalTime();
|
||||
}
|
||||
|
||||
// stackoverflow.com/a/3294698/162671
|
||||
static uint SwapEndianness(ulong x)
|
||||
{
|
||||
return (uint) (((x & 0x000000ff) << 24) +
|
||||
((x & 0x0000ff00) << 8) +
|
||||
((x & 0x00ff0000) >> 8) +
|
||||
((x & 0xff000000) >> 24));
|
||||
}
|
||||
|
||||
|
||||
public async static Task SetCoins(int newValue, bool justOffline = false){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", username);
|
||||
|
||||
@@ -8,6 +8,9 @@ public class SelectedItemMenu : MonoBehaviour
|
||||
public Text nameTxt;
|
||||
public Button upgradeMenuBtn;
|
||||
public Button infoBtn;
|
||||
public CollectBtn collectBtn;
|
||||
public Button joinHallBtn;
|
||||
public Button repairRocketsBtn;
|
||||
|
||||
[Header("info menu")]
|
||||
public GameObject infoMenu;
|
||||
@@ -18,12 +21,17 @@ public class SelectedItemMenu : MonoBehaviour
|
||||
public Transform upgrade_statParent;
|
||||
public Button upgradeBtn;
|
||||
|
||||
[Header("Special buildings")]
|
||||
public BuildingData mainHall;
|
||||
public BuildingData rocketRepair;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Selector.OnSelectedChanged.AddListener(OnSelectionChanged);
|
||||
upgradeMenuBtn.onClick.AddListener(OnUpgradeMenuClicked);
|
||||
infoBtn.onClick.AddListener(OnInfoClicked);
|
||||
upgradeBtn.onClick.AddListener(OnUpgrade);
|
||||
collectBtn.btn.onClick.AddListener(OnCollect);
|
||||
}
|
||||
|
||||
void OnSelectionChanged()
|
||||
@@ -39,11 +47,26 @@ public class SelectedItemMenu : MonoBehaviour
|
||||
{
|
||||
upgradeMenuBtn.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
|
||||
if(Selector.selectedData.collectable){
|
||||
collectBtn.gameObject.SetActive(true);
|
||||
Debug.Log("Last collected : " +Selector.selectedBuilding.lastCollected );
|
||||
collectBtn.Set(Selector.selectedBuilding.lastCollected,Selector.selectedData.productinoRates[Selector.selectedBuilding.curLevel]);
|
||||
}else{
|
||||
collectBtn.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
joinHallBtn.gameObject.SetActive((Selector.selectedData == mainHall));
|
||||
repairRocketsBtn.gameObject.SetActive((Selector.selectedData == rocketRepair));
|
||||
}
|
||||
else
|
||||
{
|
||||
upgradeMenuBtn.gameObject.SetActive(false);
|
||||
infoBtn.gameObject.SetActive(false);
|
||||
collectBtn.gameObject.SetActive(false);
|
||||
joinHallBtn.gameObject.SetActive(false);
|
||||
repairRocketsBtn.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
upgradeMenu.SetActive(false);
|
||||
@@ -102,4 +125,9 @@ public class SelectedItemMenu : MonoBehaviour
|
||||
infoMenu.SetActive(true);
|
||||
descriptionTxt.text = Selector.selectedBuilding.buildingData.description;
|
||||
}
|
||||
|
||||
void OnCollect(){
|
||||
Debug.Log("Collecting from : " + Selector.selectedData.buildingName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -109,6 +109,7 @@ public class WorldItemSelector : MonoBehaviour
|
||||
}
|
||||
|
||||
public static class Selector{
|
||||
public static bool enabled=true;
|
||||
public static bool movingPointValid;
|
||||
public static Building movingBuilding;
|
||||
public static bool isMovingBuilding => movingBuilding!=null;
|
||||
@@ -118,6 +119,7 @@ public static class Selector{
|
||||
|
||||
public static void selectBuilding(Building e){
|
||||
selectedBuilding = e;
|
||||
if(!enabled){selectedBuilding=null;}
|
||||
OnSelectedChanged.Invoke();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user