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
|
11 // パーリンノイズを使ったマップか
|
8
|
12 [SerializeField]
|
|
13 private bool _isPerlinNoiseMap = true;
|
|
14
|
11
|
15 // 起伏の激しさ
|
8
|
16 [SerializeField]
|
|
17 private float _relief = 15f;
|
|
18
|
11
|
19 // Y座標を滑らかにするか(小数点以下をそのままにする)
|
8
|
20 [SerializeField]
|
|
21 private bool _isSmoothness = false;
|
|
22
|
11
|
23 // マップの大きさ
|
8
|
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
|
12
|
54 jungle = SaveData.jungle;
|
8
|
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 ();
|
12
|
88 BoxItemInfo iteminfo = attr.get<BoxItemInfo> ("BoxItemInfo");
|
8
|
89
|
|
90 GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/Box");
|
|
91 GameObject obj = Instantiate (item);
|
12
|
92 obj.GetComponent<BoxItem> ().SetItem (iteminfo ,TotalIndex);
|
8
|
93 obj.transform.position = new Vector3 (v.x, v.y, v.z);
|
|
94 obj.transform.SetParent (baseStage.transform);
|
12
|
95 obj.GetComponent<BoxItem> ().SetCreateTreeNode ();
|
|
96
|
|
97 AddTotalIndex ();
|
|
98 }
|
|
99
|
|
100 public void CreateFood (GameObject target, int ItemID) {
|
|
101 TreeNode food_node = getTypeItem (1);
|
|
102 Attributes attr = food_node.getAttributes ();
|
|
103 string type = attr.getString ("Category");
|
|
104
|
|
105 TreeNode item_node = getItem (food_node, ItemID);
|
|
106
|
|
107 attr = item_node.getAttributes ();
|
|
108 FoodItemInfo iteminfo = attr.get<FoodItemInfo> ("FoodItemInfo");
|
|
109
|
|
110 GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/" + iteminfo.Type);
|
|
111 GameObject obj = Instantiate (item);
|
|
112 obj.GetComponent<FoodItem> ().SetItem (iteminfo , TotalIndex);
|
|
113 obj.transform.position = target.transform.position + Vector3.up;
|
|
114 obj.transform.SetParent (baseStage.transform);
|
|
115 obj.GetComponent<FoodItem> ().SetCreateTreeNode ();
|
8
|
116
|
|
117 AddTotalIndex ();
|
0
|
118 }
|
|
119
|
8
|
120 public void CreateStageItem (Vector3 v) {
|
|
121 TreeNode box_node = getTypeItem (0);
|
|
122 Attributes attr = box_node.getAttributes ();
|
|
123
|
|
124 string type = attr.getString ("Category");
|
|
125 GameObject obj = Instantiate (Resources.Load<GameObject> ("Prefabs/" + type + "/Box"));
|
|
126 obj.transform.position = new Vector3 (v.x, v.y, v.z);
|
0
|
127 obj.transform.SetParent (baseStage.transform);
|
12
|
128 obj.GetComponent<BoxItem> ().SetCreateTreeNode ();
|
8
|
129 SetY(obj, box_node);
|
|
130 AddTotalIndex ();
|
0
|
131 }
|
|
132
|
12
|
133 // Box is 0, Food is 1.
|
8
|
134 public TreeNode getTypeItem (int typeID) {
|
12
|
135 jungle = SaveData.jungle;
|
8
|
136 JungleTree tree = jungle.getTreeByName ("ItemTree");
|
|
137 TreeNode node = tree.getRootNode ();
|
|
138 Children child = node.getChildren ();
|
|
139 return child.at (typeID).b ();
|
|
140 }
|
|
141
|
|
142 public TreeNode getItem (TreeNode node, int itemID) {
|
12
|
143 Children child = node.getChildren ();
|
|
144 TreeNode item_node = child.at (itemID).b ();
|
8
|
145 return item_node;
|
|
146 }
|
|
147
|
|
148 public void AddTotalIndex () {
|
|
149 TotalIndex++;
|
0
|
150 }
|
4
|
151
|
8
|
152 private void SetY(GameObject cube, TreeNode node){
|
|
153 float y = 0;
|
|
154
|
|
155 //パーリンノイズを使って高さを決める場合
|
|
156 if(_isPerlinNoiseMap){
|
|
157 float xSample = (cube.transform.localPosition.x) / _relief;
|
|
158 float zSample = (cube.transform.localPosition.z) / _relief;
|
|
159
|
|
160 float noise = Mathf.PerlinNoise(xSample, zSample);
|
|
161
|
|
162 y = _maxHeight * noise;
|
|
163 }
|
|
164 //完全ランダムで高さを決める場合
|
|
165 else{
|
|
166 y = UnityEngine.Random.Range (0, _maxHeight);
|
|
167 }
|
|
168
|
|
169 //滑らかに変化しない場合はyを四捨五入
|
|
170 if(!_isSmoothness){
|
|
171 y = Mathf.Round (y);
|
4
|
172 }
|
8
|
173
|
|
174 //位置設定
|
|
175 cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z);
|
|
176
|
|
177 //高さによって色を段階的に変更
|
|
178 Color color = Color.black;//岩盤っぽい色
|
|
179 int item_id = 1;
|
|
180
|
|
181 if(y > _maxHeight * 0.3f){ // grass
|
|
182 item_id = 0;
|
12
|
183 // Randomで回復をつくる
|
|
184 CreateRandomFood(cube);
|
8
|
185 }
|
|
186 else if(y > _maxHeight * 0.2f){ // maguma
|
|
187 item_id = 1;
|
|
188 }
|
|
189 else if(y > _maxHeight * 0.1f){ // water
|
|
190 item_id = 3;
|
|
191 }
|
|
192
|
|
193 TreeNode item_node = getItem (node, item_id);
|
|
194 Attributes attr = item_node.getAttributes ();
|
|
195
|
12
|
196 BoxItemInfo item = attr.get<BoxItemInfo>("BoxItemInfo");
|
|
197
|
|
198 cube.GetComponent<BoxItem> ().SetItem (item, TotalIndex);
|
|
199 }
|
8
|
200
|
12
|
201 public void CreateRandomFood(GameObject obj) {
|
|
202 int ran = UnityEngine.Random.Range (1, 11);
|
8
|
203
|
12
|
204 if (ran == 10) {
|
|
205 CreateFood (obj, 0);
|
|
206 }
|
0
|
207 }
|
|
208 }
|