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);
|
10
|
95 obj.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, itemID, TotalIndex, color_code);
|
8
|
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 AddTotalIndex ();
|
0
|
101 }
|
|
102
|
8
|
103 public void CreateStageItem (Vector3 v) {
|
|
104 TreeNode box_node = getTypeItem (0);
|
|
105 Attributes attr = box_node.getAttributes ();
|
|
106
|
|
107 string type = attr.getString ("Category");
|
|
108 GameObject obj = Instantiate (Resources.Load<GameObject> ("Prefabs/" + type + "/Box"));
|
|
109 obj.transform.position = new Vector3 (v.x, v.y, v.z);
|
0
|
110 obj.transform.SetParent (baseStage.transform);
|
8
|
111 obj.GetComponent<Item> ().SetCreateTreeNode ();
|
|
112 SetY(obj, box_node);
|
|
113 AddTotalIndex ();
|
0
|
114 }
|
|
115
|
8
|
116 public TreeNode getTypeItem (int typeID) {
|
|
117 jungle = SaveDataTest.jungle;
|
|
118 JungleTree tree = jungle.getTreeByName ("ItemTree");
|
|
119 TreeNode node = tree.getRootNode ();
|
|
120 Children child = node.getChildren ();
|
|
121 return child.at (typeID).b ();
|
|
122 }
|
|
123
|
|
124 public TreeNode getItem (TreeNode node, int itemID) {
|
|
125 Children box_child = node.getChildren ();
|
|
126 TreeNode item_node = box_child.at (itemID).b ();
|
|
127 return item_node;
|
|
128 }
|
|
129
|
|
130 public void AddTotalIndex () {
|
|
131 TotalIndex++;
|
0
|
132 }
|
4
|
133
|
8
|
134 private void SetY(GameObject cube, TreeNode node){
|
|
135 float y = 0;
|
|
136
|
|
137 //パーリンノイズを使って高さを決める場合
|
|
138 if(_isPerlinNoiseMap){
|
|
139 float xSample = (cube.transform.localPosition.x) / _relief;
|
|
140 float zSample = (cube.transform.localPosition.z) / _relief;
|
|
141
|
|
142 float noise = Mathf.PerlinNoise(xSample, zSample);
|
|
143
|
|
144 y = _maxHeight * noise;
|
|
145 }
|
|
146 //完全ランダムで高さを決める場合
|
|
147 else{
|
|
148 y = UnityEngine.Random.Range (0, _maxHeight);
|
|
149 }
|
|
150
|
|
151 //滑らかに変化しない場合はyを四捨五入
|
|
152 if(!_isSmoothness){
|
|
153 y = Mathf.Round (y);
|
4
|
154 }
|
8
|
155
|
|
156 //位置設定
|
|
157 cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z);
|
|
158
|
|
159 //高さによって色を段階的に変更
|
|
160 Color color = Color.black;//岩盤っぽい色
|
|
161 int item_id = 1;
|
|
162
|
|
163 if(y > _maxHeight * 0.3f){ // grass
|
|
164 item_id = 0;
|
|
165 }
|
|
166 else if(y > _maxHeight * 0.2f){ // maguma
|
|
167 item_id = 1;
|
|
168 }
|
|
169 else if(y > _maxHeight * 0.1f){ // water
|
|
170 item_id = 3;
|
|
171 }
|
|
172
|
|
173 TreeNode item_node = getItem (node, item_id);
|
|
174 Attributes attr = item_node.getAttributes ();
|
|
175
|
10
|
176 // string subType = attr.getString ("Type");
|
|
177 // string broken = attr.getString ("Broken");
|
|
178 // string color_code = attr.getString ("Color");
|
8
|
179
|
10
|
180 ItemInfo item = attr.get<ItemInfo>("ItemInfo");
|
8
|
181
|
10
|
182 cube.GetComponent<Item> ().SetItem (item, TotalIndex);
|
0
|
183 }
|
|
184 }
|