Mercurial > hg > Members > kazuma > JungleforUnity
comparison Assets/Application/Scripts/StageManager.cs @ 8:599bd8ddb72b
Create Item Tree and Create Stage.
author | Kazuma Takeda |
---|---|
date | Tue, 17 Jan 2017 19:57:19 +0900 |
parents | 2878be4487ec |
children | bbab930748c4 |
comparison
equal
deleted
inserted
replaced
7:ad4729c5eec4 | 8:599bd8ddb72b |
---|---|
1 using UnityEngine; | 1 using UnityEngine; |
2 using System.Collections; | 2 using System.Collections; |
3 using System.Collections.Generic; | 3 using System.Collections.Generic; |
4 using JungleDB; | |
5 using System; | |
4 | 6 |
5 public class StageManager : MonoBehaviour { | 7 public class StageManager : MonoBehaviour { |
6 | 8 |
9 private float _maxHeight = 10; | |
10 | |
11 //パーリンノイズを使ったマップか | |
12 [SerializeField] | |
13 private bool _isPerlinNoiseMap = true; | |
14 | |
15 //起伏の激しさ | |
16 [SerializeField] | |
17 private float _relief = 15f; | |
18 | |
19 //Y座標を滑らかにするか(小数点以下をそのままにする) | |
20 [SerializeField] | |
21 private bool _isSmoothness = false; | |
22 | |
23 //マップの大きさ | |
24 [SerializeField] | |
25 private float _mapSize = 1f; | |
26 | |
7 public static StageManager Instance; | 27 public static StageManager Instance; |
8 // stages["Attribute"]で取れる | |
9 public System.Collections.Generic.Dictionary<Stage.Type, GameObject> stages = new System.Collections.Generic.Dictionary<Stage.Type, GameObject>(); | |
10 private GameObject baseStage; | 28 private GameObject baseStage; |
29 | |
30 public delegate void StageCreateEndCallback (); | |
31 public StageCreateEndCallback callback; | |
32 | |
33 private Jungle jungle; | |
34 private int TotalIndex = 0; | |
11 | 35 |
12 void Awake () { | 36 void Awake () { |
13 if (Instance == null) { | 37 if (Instance == null) { |
14 Instance = this; | 38 Instance = this; |
15 } | 39 } |
16 } | 40 } |
17 | 41 |
18 // Use this for initialization | 42 // Use this for initialization |
19 void Start () { | 43 void Start () { |
20 Init (); | |
21 SetStage (); | |
22 CreateStages (10); | |
23 } | 44 } |
24 | 45 |
25 void Init () { | 46 public void Init () { |
47 | |
48 | |
26 baseStage = new GameObject (); | 49 baseStage = new GameObject (); |
27 baseStage.name = "stage"; | 50 baseStage.name = "stage"; |
28 baseStage.transform.position = Vector3.zero; | 51 baseStage.transform.position = Vector3.zero; |
29 GameObject.FindGameObjectWithTag ("Player").transform.SetParent (baseStage.transform); | 52 GameObject.FindGameObjectWithTag ("Player").transform.SetParent (baseStage.transform); |
53 | |
54 jungle = SaveDataTest.jungle; | |
55 CreateStage (15); | |
30 } | 56 } |
31 | 57 |
32 void SetStage () { | 58 public void SetCallback (StageCreateEndCallback c) { |
33 object[] objects = Resources.LoadAll ("Prefabs/Object"); | 59 this.callback = c; |
34 foreach (var obj in objects) { | 60 print ("Set : "+ callback.Method); |
35 GameObject gbj = (GameObject)obj; | |
36 stages.Add(gbj.GetComponent<Stage>().Attribute, gbj); | |
37 } | |
38 } | 61 } |
39 | 62 |
40 // One. | 63 public void AddCallback (StageCreateEndCallback c) { |
41 public void CreateStage (Stage.Type attr, Vector3 pos) { | 64 this.callback += c; |
42 GameObject obj = Instantiate (stages[attr]); | 65 print ("Add : " + callback.Method); |
43 obj.GetComponent<Stage> ().SetPosition (pos); | |
44 obj.transform.SetParent (baseStage.transform); | |
45 } | 66 } |
46 | 67 |
47 // Any. | 68 public void CreateStage (int n) { |
48 public void CreateStages (int count) { | 69 for (int i = 0; i < n; i++) { |
49 for (int x = 0; x < count; x++) { | 70 for (int j = 0; j < n; j++) { |
50 for (int z = 0; z < count; z++) { | 71 CreateStageItem (new Vector3(i, 0, j)); |
51 CreateStage (GetTypes(Random.Range(0,3)), new Vector3 (x, Random.Range(-1, 1), z)); | |
52 } | 72 } |
53 } | 73 } |
74 if(callback != null) | |
75 callback (); | |
54 } | 76 } |
55 | 77 |
56 public Stage.Type GetTypes (int n) { | 78 public void CreateItem (Vector3 v, int ItemID) { |
57 if (n == 0) { | 79 |
58 return Stage.Type.SAND; | 80 TreeNode box_node = getTypeItem (0); |
59 } else if (n == 1) { | 81 |
60 return Stage.Type.WOOD; | 82 Attributes attr = box_node.getAttributes (); |
61 } else { | 83 string type = attr.getString ("Category"); |
62 return Stage.Type.GRASS; | 84 |
85 TreeNode item_node = getItem (box_node, ItemID); | |
86 | |
87 attr = item_node.getAttributes (); | |
88 string subType = attr.getString ("Type"); | |
89 string broken = attr.getString ("Broken"); | |
90 string itemID = attr.getString ("ID"); | |
91 string color_code = attr.getString ("Color"); | |
92 | |
93 GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/Box"); | |
94 GameObject obj = Instantiate (item); | |
95 obj.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, itemID, TotalIndex); | |
96 obj.transform.position = new Vector3 (v.x, v.y, v.z); | |
97 obj.transform.SetParent (baseStage.transform); | |
98 obj.GetComponent<Item> ().SetCreateTreeNode (); | |
99 | |
100 Color color = Color.black; | |
101 ColorUtility.TryParseHtmlString(color_code, out color);//草っぽい色 | |
102 obj.GetComponent<MeshRenderer> ().material.color = color; | |
103 | |
104 AddTotalIndex (); | |
105 } | |
106 | |
107 public void CreateStageItem (Vector3 v) { | |
108 TreeNode box_node = getTypeItem (0); | |
109 Attributes attr = box_node.getAttributes (); | |
110 | |
111 string type = attr.getString ("Category"); | |
112 GameObject obj = Instantiate (Resources.Load<GameObject> ("Prefabs/" + type + "/Box")); | |
113 obj.transform.position = new Vector3 (v.x, v.y, v.z); | |
114 obj.transform.SetParent (baseStage.transform); | |
115 obj.GetComponent<Item> ().SetCreateTreeNode (); | |
116 SetY(obj, box_node); | |
117 AddTotalIndex (); | |
118 } | |
119 | |
120 public TreeNode getTypeItem (int typeID) { | |
121 jungle = SaveDataTest.jungle; | |
122 JungleTree tree = jungle.getTreeByName ("ItemTree"); | |
123 TreeNode node = tree.getRootNode (); | |
124 Attributes attr = node.getAttributes (); | |
125 Children child = node.getChildren (); | |
126 return child.at (typeID).b (); | |
127 } | |
128 | |
129 public TreeNode getItem (TreeNode node, int itemID) { | |
130 Children box_child = node.getChildren (); | |
131 TreeNode item_node = box_child.at (itemID).b (); | |
132 return item_node; | |
133 } | |
134 | |
135 public void AddTotalIndex () { | |
136 TotalIndex++; | |
137 } | |
138 | |
139 private void SetY(GameObject cube, TreeNode node){ | |
140 float y = 0; | |
141 | |
142 //パーリンノイズを使って高さを決める場合 | |
143 if(_isPerlinNoiseMap){ | |
144 float xSample = (cube.transform.localPosition.x) / _relief; | |
145 float zSample = (cube.transform.localPosition.z) / _relief; | |
146 | |
147 float noise = Mathf.PerlinNoise(xSample, zSample); | |
148 | |
149 y = _maxHeight * noise; | |
63 } | 150 } |
64 } | 151 //完全ランダムで高さを決める場合 |
65 | 152 else{ |
66 // Update is called once per frame | 153 y = UnityEngine.Random.Range (0, _maxHeight); |
67 void Update () { | 154 } |
68 | 155 |
156 //滑らかに変化しない場合はyを四捨五入 | |
157 if(!_isSmoothness){ | |
158 y = Mathf.Round (y); | |
159 } | |
160 | |
161 //位置設定 | |
162 cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z); | |
163 | |
164 //高さによって色を段階的に変更 | |
165 Color color = Color.black;//岩盤っぽい色 | |
166 int item_id = 1; | |
167 | |
168 if(y > _maxHeight * 0.3f){ // grass | |
169 item_id = 0; | |
170 } | |
171 else if(y > _maxHeight * 0.2f){ // maguma | |
172 item_id = 1; | |
173 } | |
174 else if(y > _maxHeight * 0.1f){ // water | |
175 item_id = 3; | |
176 } | |
177 | |
178 TreeNode item_node = getItem (node, item_id); | |
179 Attributes attr = item_node.getAttributes (); | |
180 | |
181 string subType = attr.getString ("Type"); | |
182 string broken = attr.getString ("Broken"); | |
183 string itemID = attr.getString ("ID"); | |
184 string color_code = attr.getString ("Color"); | |
185 | |
186 cube.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, item_id.ToString(), TotalIndex); | |
187 | |
188 ColorUtility.TryParseHtmlString(color_code, out color);//草っぽい色 | |
189 cube.GetComponent<MeshRenderer> ().material.color = color; | |
69 } | 190 } |
70 } | 191 } |