123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.Networking;
- using System.Linq;
- using ARLocation;
- using Newtonsoft.Json;
- public class Z_get_RW_Objects : MonoBehaviour
- {
- private float elapsed = 0.0f;
- private float period = 1.5f;
- public GameObject gpsPrefab;
- public GameObject serverManager;
- public GameObject playerNumberObject;
- private string playerNumber;
- public List<GameObject> objectsPlay;
- private string serverUrl;
- // Start is called before the first frame update
- void Start()
- {
- this.serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_all_objects_in_game/";
-
- }
- // Update is called once per frame
- void Update()
- {
- elapsed += Time.deltaTime;
-
-
- if (elapsed >= period) {
-
- elapsed = 0.0f;
- string gn = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
- string pn = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
- if(gn == null)
- return;
- if(pn == null)
- return;
-
- StartCoroutine(PlayedObjects(this.serverUrl, gn, pn));
- }
-
- }
- public IEnumerator PlayedObjects(string serverUrl, string gameN, string playerN)
- {
- serverUrl += gameN;
- this.playerNumber = playerN;
-
- UnityWebRequest www = UnityWebRequest.Get(serverUrl);
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- else
- {
- //var result = www.downloadHandler.text.Split(new[] { '},' })
- // .Select(s => s.Split(new[] { ':' }))
- // .ToDictionary(k => k[0].Replace("\"","" ).Replace("}","").Replace("{", "").Trim(), v => v[1].Replace("\"", "").Replace("}", "").Trim());
- //var result_list = result.Values.ToList();
- var values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,string>>>(www.downloadHandler.text);
-
- foreach (var uni in values)
- {
- //you can print values here or add to a list or ...
- string owner= uni.Value["owner"];
- string latitude = uni.Value["latitude"];
- string longitude = uni.Value["longitude"];
- string object_name = uni.Value["object_name"];
-
- if(owner != playerNumber)
- {
- foreach(GameObject obj in objectsPlay) {
- if(object_name.Contains(obj.name))
- {
- GameObject placedObject = Instantiate(obj);
- // SET COLOR
- placedObject.GetComponent<Z_color_Select>().SetObjectColor(playerNumber);
-
- // SET GPS COORDINATES
- var loc = new Location()
- {
- Latitude = Convert.ToDouble(latitude),
- Longitude = Convert.ToDouble(longitude),
- Altitude = 0.5f,
- AltitudeMode = AltitudeMode.GroundRelative
- };
- var opts = new PlaceAtLocation.PlaceAtOptions()
- {
- HideObjectUntilItIsPlaced = true,
- MaxNumberOfLocationUpdates = 2,
- MovementSmoothing = 0.1f,
- UseMovingAverage = false
- };
- PlaceAtLocation.AddPlaceAtComponent(placedObject, loc, opts);
- }
- }
- }
- }
- }
- }
- }
|