view 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 source

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;
	private GameObject baseStage;

	public delegate void StageCreateEndCallback ();
	public StageCreateEndCallback callback;

	private Jungle jungle;
	private int TotalIndex = 0;

	void Awake () {
		if (Instance == null) {
			Instance = this;
		}
	}

	// Use this for initialization
	void Start () {
	}

	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 ();
	}

	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 ();
	}

	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 ();
	}

	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++;
	}

	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);
		}

		//位置設定
		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;
	}
}