vardgrig

ObstacleController

Jun 30th, 2022 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. using System.IO;
  5.  
  6. public class ObstacleController : MonoBehaviour
  7. {
  8.     public float speed = 3.0f;
  9.     public GameObject pipe;
  10.     public GameObject rock;
  11.     public GameObject win;
  12.     public GameObject tree;
  13.     public GameObject coin;
  14.     public TextAsset JSON;
  15.     public LevelController _levelController;
  16.    
  17.     [System.Serializable]
  18.     public class Obstacle{
  19.         public string name {get; set;}
  20.         public float x {get; set;}
  21.         public float y {get; set;}
  22.     }
  23.  
  24.     [System.Serializable]
  25.     public class ObstacleList{
  26.         public List<Obstacle> Obstacle { get; set;}
  27.     }
  28.  
  29.     void Start()
  30.     {
  31.         _levelController.CreateJsonList();
  32.         JSON = _levelController.JsonList[_levelController.levelIndex];
  33.         Debug.Log(JSON.name);
  34.         ReadFromJson();
  35.     }
  36.  
  37.     void Update()
  38.     {
  39.         Roller();
  40.     }
  41.  
  42.     private void Roller()
  43.     {
  44.         transform.position += Vector3.left * speed * Time.deltaTime;
  45.     }
  46.  
  47.     public void ReadFromJson()
  48.     {
  49.         //string jsonPath = $"{Application.dataPath}/JSONs/Values.json";
  50.         //string json = File.ReadAllText(jsonPath);
  51.  
  52.         var obstacleList = JsonConvert.DeserializeObject<ObstacleList>(JSON.text);
  53.         foreach (var item in obstacleList.Obstacle)
  54.         {
  55.             switch (item.name)
  56.             {
  57.                 case "Win":
  58.                     Instantiate(win, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
  59.                     break;
  60.                 case "Coin":
  61.                     Instantiate(coin, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
  62.                     break;
  63.                 case "Rock":
  64.                     Instantiate(rock, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
  65.                     break;
  66.                 case "Tree":
  67.                     Instantiate(tree, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
  68.                     break;
  69.                 case "Pipe":
  70.                     Instantiate(pipe, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
  71.                     break;
  72.             }
  73.         }
  74.     }
  75.    
  76.  
  77. }
Add Comment
Please, Sign In to add comment