view Assets/Application/Scripts/StageManager.cs @ 12:b55d586dd4eb

change bind from fmap.
author Kazuma Takeda
date Tue, 07 Feb 2017 20:48:57 +0900
parents cf20add31466
children
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 = SaveData.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 ();
		BoxItemInfo iteminfo = attr.get<BoxItemInfo> ("BoxItemInfo");

		GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/Box");
		GameObject obj = Instantiate (item);
		obj.GetComponent<BoxItem> ().SetItem (iteminfo ,TotalIndex);
		obj.transform.position = new Vector3 (v.x, v.y, v.z);
		obj.transform.SetParent (baseStage.transform);
		obj.GetComponent<BoxItem> ().SetCreateTreeNode ();

		AddTotalIndex ();
	}

	public void CreateFood (GameObject target, int ItemID) {
		TreeNode food_node = getTypeItem (1);
		Attributes attr = food_node.getAttributes ();
		string type = attr.getString ("Category");

		TreeNode item_node = getItem (food_node, ItemID);

		attr = item_node.getAttributes ();
		FoodItemInfo iteminfo = attr.get<FoodItemInfo> ("FoodItemInfo");

		GameObject item = Resources.Load<GameObject> ("Prefabs/" + type + "/" + iteminfo.Type);
		GameObject obj = Instantiate (item);
		obj.GetComponent<FoodItem> ().SetItem (iteminfo , TotalIndex);
		obj.transform.position = target.transform.position + Vector3.up;
		obj.transform.SetParent (baseStage.transform);
		obj.GetComponent<FoodItem> ().SetCreateTreeNode ();

		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<BoxItem> ().SetCreateTreeNode ();
		SetY(obj, box_node);
		AddTotalIndex ();
	}

	// Box is 0, Food is 1.
	public TreeNode getTypeItem (int typeID) {
		jungle = SaveData.jungle;
		JungleTree tree = jungle.getTreeByName ("ItemTree");
		TreeNode node = tree.getRootNode ();
		Children child = node.getChildren ();
		return child.at (typeID).b ();
	}

	public TreeNode getItem (TreeNode node, int itemID) {
		Children child = node.getChildren ();
		TreeNode item_node = 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;
			// Randomで回復をつくる
			CreateRandomFood(cube);
		}
		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 ();

		BoxItemInfo item = attr.get<BoxItemInfo>("BoxItemInfo");

		cube.GetComponent<BoxItem> ().SetItem (item, TotalIndex);
	}

	public void CreateRandomFood(GameObject obj) {
		int ran = UnityEngine.Random.Range (1, 11);

		if (ran == 10) {
			CreateFood (obj, 0);
		}
	}
}