0
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
8
|
4 using JungleDB;
|
|
5 using System;
|
0
|
6
|
|
7 public class StageManager : MonoBehaviour {
|
|
8
|
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
|
3
|
27 public static StageManager Instance;
|
0
|
28 private GameObject baseStage;
|
|
29
|
8
|
30 public delegate void StageCreateEndCallback ();
|
|
31 public StageCreateEndCallback callback;
|
|
32
|
|
33 private Jungle jungle;
|
|
34 private int TotalIndex = 0;
|
|
35
|
3
|
36 void Awake () {
|
|
37 if (Instance == null) {
|
|
38 Instance = this;
|
|
39 }
|
|
40 }
|
|
41
|
0
|
42 // Use this for initialization
|
|
43 void Start () {
|
|
44 }
|
|
45
|
8
|
46 public void Init () {
|
|
47
|
|
48
|
0
|
49 baseStage = new GameObject ();
|
|
50 baseStage.name = "stage";
|
|
51 baseStage.transform.position = Vector3.zero;
|
4
|
52 GameObject.FindGameObjectWithTag ("Player").transform.SetParent (baseStage.transform);
|
8
|
53
|
|
54 jungle = SaveDataTest.jungle;
|
|
55 CreateStage (15);
|
|
56 }
|
|
57
|
|
58 public void SetCallback (StageCreateEndCallback c) {
|
|
59 this.callback = c;
|
|
60 print ("Set : "+ callback.Method);
|
|
61 }
|
|
62
|
|
63 public void AddCallback (StageCreateEndCallback c) {
|
|
64 this.callback += c;
|
|
65 print ("Add : " + callback.Method);
|
|
66 }
|
|
67
|
|
68 public void CreateStage (int n) {
|
|
69 for (int i = 0; i < n; i++) {
|
|
70 for (int j = 0; j < n; j++) {
|
|
71 CreateStageItem (new Vector3(i, 0, j));
|
|
72 }
|
|
73 }
|
|
74 if(callback != null)
|
|
75 callback ();
|
0
|
76 }
|
|
77
|
8
|
78 public void CreateItem (Vector3 v, int ItemID) {
|
|
79
|
|
80 TreeNode box_node = getTypeItem (0);
|
|
81
|
|
82 Attributes attr = box_node.getAttributes ();
|
|
83 string type = attr.getString ("Category");
|
|
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 ();
|
0
|
105 }
|
|
106
|
8
|
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);
|
0
|
114 obj.transform.SetParent (baseStage.transform);
|
8
|
115 obj.GetComponent<Item> ().SetCreateTreeNode ();
|
|
116 SetY(obj, box_node);
|
|
117 AddTotalIndex ();
|
0
|
118 }
|
|
119
|
8
|
120 public TreeNode getTypeItem (int typeID) {
|
|
121 jungle = SaveDataTest.jungle;
|
|
122 JungleTree tree = jungle.getTreeByName ("ItemTree");
|
|
123 TreeNode node = tree.getRootNode ();
|
|
124 Children child = node.getChildren ();
|
|
125 return child.at (typeID).b ();
|
|
126 }
|
|
127
|
|
128 public TreeNode getItem (TreeNode node, int itemID) {
|
|
129 Children box_child = node.getChildren ();
|
|
130 TreeNode item_node = box_child.at (itemID).b ();
|
|
131 return item_node;
|
|
132 }
|
|
133
|
|
134 public void AddTotalIndex () {
|
|
135 TotalIndex++;
|
0
|
136 }
|
4
|
137
|
8
|
138 private void SetY(GameObject cube, TreeNode node){
|
|
139 float y = 0;
|
|
140
|
|
141 //パーリンノイズを使って高さを決める場合
|
|
142 if(_isPerlinNoiseMap){
|
|
143 float xSample = (cube.transform.localPosition.x) / _relief;
|
|
144 float zSample = (cube.transform.localPosition.z) / _relief;
|
|
145
|
|
146 float noise = Mathf.PerlinNoise(xSample, zSample);
|
|
147
|
|
148 y = _maxHeight * noise;
|
|
149 }
|
|
150 //完全ランダムで高さを決める場合
|
|
151 else{
|
|
152 y = UnityEngine.Random.Range (0, _maxHeight);
|
|
153 }
|
|
154
|
|
155 //滑らかに変化しない場合はyを四捨五入
|
|
156 if(!_isSmoothness){
|
|
157 y = Mathf.Round (y);
|
4
|
158 }
|
8
|
159
|
|
160 //位置設定
|
|
161 cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z);
|
|
162
|
|
163 //高さによって色を段階的に変更
|
|
164 Color color = Color.black;//岩盤っぽい色
|
|
165 int item_id = 1;
|
|
166
|
|
167 if(y > _maxHeight * 0.3f){ // grass
|
|
168 item_id = 0;
|
|
169 }
|
|
170 else if(y > _maxHeight * 0.2f){ // maguma
|
|
171 item_id = 1;
|
|
172 }
|
|
173 else if(y > _maxHeight * 0.1f){ // water
|
|
174 item_id = 3;
|
|
175 }
|
|
176
|
|
177 TreeNode item_node = getItem (node, item_id);
|
|
178 Attributes attr = item_node.getAttributes ();
|
|
179
|
|
180 string subType = attr.getString ("Type");
|
|
181 string broken = attr.getString ("Broken");
|
|
182 string itemID = attr.getString ("ID");
|
|
183 string color_code = attr.getString ("Color");
|
|
184
|
|
185 cube.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, item_id.ToString(), TotalIndex);
|
|
186
|
|
187 ColorUtility.TryParseHtmlString(color_code, out color);//草っぽい色
|
|
188 cube.GetComponent<MeshRenderer> ().material.color = color;
|
0
|
189 }
|
|
190 }
|