diff 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
line wrap: on
line diff
--- a/Assets/Application/Scripts/StageManager.cs	Thu Dec 15 22:45:31 2016 +0900
+++ b/Assets/Application/Scripts/StageManager.cs	Tue Jan 17 19:57:19 2017 +0900
@@ -1,14 +1,38 @@
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
+using JungleDB;
+using System;
 
 public class StageManager : MonoBehaviour {
 
+	private float _maxHeight = 10;
+
+	//パーリンノイズを使ったマップか
+	[SerializeField]
+	private bool _isPerlinNoiseMap = true;
+
+	//起伏の激しさ
+	[SerializeField]
+	private float _relief = 15f;
+
+	//Y座標を滑らかにするか(小数点以下をそのままにする)
+	[SerializeField]
+	private bool _isSmoothness = false;
+
+	//マップの大きさ
+	[SerializeField]
+	private float _mapSize = 1f;
+
 	public static StageManager Instance;
-	// stages["Attribute"]で取れる
-	public System.Collections.Generic.Dictionary<Stage.Type, GameObject> stages = new System.Collections.Generic.Dictionary<Stage.Type, GameObject>();
 	private GameObject baseStage;
 
+	public delegate void StageCreateEndCallback ();
+	public StageCreateEndCallback callback;
+
+	private Jungle jungle;
+	private int TotalIndex = 0;
+
 	void Awake () {
 		if (Instance == null) {
 			Instance = this;
@@ -17,54 +41,151 @@
 
 	// Use this for initialization
 	void Start () {
-		Init ();
-		SetStage ();
-		CreateStages (10);
 	}
 
-	void Init () {
+	public void Init () {
+		
+
 		baseStage = new GameObject ();
 		baseStage.name = "stage";
 		baseStage.transform.position = Vector3.zero;
 		GameObject.FindGameObjectWithTag ("Player").transform.SetParent (baseStage.transform);
+
+		jungle = SaveDataTest.jungle;
+		CreateStage (15);
+	}
+
+	public void SetCallback (StageCreateEndCallback c) {
+		this.callback = c;
+		print ("Set : "+ callback.Method);
+	}
+
+	public void AddCallback (StageCreateEndCallback c) {
+		this.callback += c;
+		print ("Add : " + callback.Method);
+	}
+
+	public void CreateStage (int n) {
+		for (int i = 0; i < n; i++) {
+			for (int j = 0; j < n; j++) {
+				CreateStageItem (new Vector3(i, 0, j));
+			}
+		}
+		if(callback != null)
+			callback ();
 	}
 
-	void SetStage () {
-		object[] objects = Resources.LoadAll ("Prefabs/Object");
-		foreach (var obj in objects) {
-			GameObject gbj = (GameObject)obj;
-			stages.Add(gbj.GetComponent<Stage>().Attribute, gbj);
-		}
+	public void CreateItem (Vector3 v, int ItemID) {
+
+		TreeNode box_node = getTypeItem (0);
+
+		Attributes attr = box_node.getAttributes ();
+		string type = attr.getString ("Category");
+
+		TreeNode item_node = getItem (box_node, ItemID);
+
+		attr = item_node.getAttributes ();
+		string subType = attr.getString ("Type");
+		string broken = attr.getString ("Broken");
+		string itemID = attr.getString ("ID");
+		string color_code = attr.getString ("Color");
+
+		GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/Box");
+		GameObject obj = Instantiate (item);
+		obj.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, itemID, TotalIndex);
+		obj.transform.position = new Vector3 (v.x, v.y, v.z);
+		obj.transform.SetParent (baseStage.transform);
+		obj.GetComponent<Item> ().SetCreateTreeNode ();
+
+		Color color = Color.black;
+		ColorUtility.TryParseHtmlString(color_code, out color);//草っぽい色
+		obj.GetComponent<MeshRenderer> ().material.color = color;
+
+		AddTotalIndex ();
 	}
 
-	// One.
-	public void CreateStage (Stage.Type attr, Vector3 pos) {
-		GameObject obj = Instantiate (stages[attr]);
-		obj.GetComponent<Stage> ().SetPosition (pos);
+	public void CreateStageItem (Vector3 v) {
+		TreeNode box_node = getTypeItem (0);
+		Attributes attr = box_node.getAttributes ();
+
+		string type = attr.getString ("Category");
+		GameObject obj = Instantiate (Resources.Load<GameObject> ("Prefabs/" + type + "/Box"));
+		obj.transform.position = new Vector3 (v.x, v.y, v.z);
 		obj.transform.SetParent (baseStage.transform);
+		obj.GetComponent<Item> ().SetCreateTreeNode ();
+		SetY(obj, box_node);
+		AddTotalIndex ();
 	}
 
-	// Any.
-	public void CreateStages (int count) {
-		for (int x = 0; x < count; x++) {
-			for (int z = 0; z < count; z++) {
-				CreateStage (GetTypes(Random.Range(0,3)), new Vector3 (x, Random.Range(-1, 1), z));
-			}
-		}
+	public TreeNode getTypeItem (int typeID) {
+		jungle = SaveDataTest.jungle;
+		JungleTree tree = jungle.getTreeByName ("ItemTree");
+		TreeNode node = tree.getRootNode ();
+		Attributes attr = node.getAttributes ();
+		Children child = node.getChildren ();
+		return child.at (typeID).b ();
+	}
+
+	public TreeNode getItem (TreeNode node, int itemID) {
+		Children box_child = node.getChildren ();
+		TreeNode item_node = box_child.at (itemID).b ();
+		return item_node;
+	}
+
+	public void AddTotalIndex () {
+		TotalIndex++;
 	}
 
-	public Stage.Type GetTypes (int n) {
-		if (n == 0) {
-			return Stage.Type.SAND;
-		} else if (n == 1) {
-			return Stage.Type.WOOD;
-		} else {
-			return Stage.Type.GRASS;
+	private void SetY(GameObject cube, TreeNode node){
+		float y = 0;
+
+		//パーリンノイズを使って高さを決める場合
+		if(_isPerlinNoiseMap){
+			float xSample = (cube.transform.localPosition.x) / _relief;
+			float zSample = (cube.transform.localPosition.z) / _relief;
+
+			float noise = Mathf.PerlinNoise(xSample, zSample);
+
+			y = _maxHeight * noise;
+		}
+		//完全ランダムで高さを決める場合
+		else{
+			y = UnityEngine.Random.Range (0, _maxHeight);
+		}
+
+		//滑らかに変化しない場合はyを四捨五入
+		if(!_isSmoothness){
+			y = Mathf.Round (y);
 		}
-	}
-	
-	// Update is called once per frame
-	void Update () {
-	
+
+		//位置設定
+		cube.transform.localPosition = new Vector3 (cube.transform.localPosition.x, y, cube.transform.localPosition.z);
+
+		//高さによって色を段階的に変更
+		Color color = Color.black;//岩盤っぽい色
+		int item_id = 1;
+
+		if(y > _maxHeight * 0.3f){ // grass
+			item_id = 0;
+		}
+		else if(y > _maxHeight * 0.2f){ // maguma
+			item_id = 1;
+		}
+		else if(y > _maxHeight * 0.1f){ // water
+			item_id = 3;
+		}
+
+		TreeNode item_node = getItem (node, item_id);
+		Attributes attr = item_node.getAttributes ();
+
+		string subType = attr.getString ("Type");
+		string broken = attr.getString ("Broken");
+		string itemID = attr.getString ("ID");
+		string color_code = attr.getString ("Color");
+
+		cube.GetComponent<Item> ().SetItem (Convert.ToInt32 (broken), subType, item_id.ToString(), TotalIndex);
+
+		ColorUtility.TryParseHtmlString(color_code, out color);//草っぽい色
+		cube.GetComponent<MeshRenderer> ().material.color = color;
 	}
 }