view Assets/Application/Scripts/Test/SaveDataTest.cs @ 10:3fefb9f9025d

put Attribute class.
author Kazuma Takeda
date Fri, 20 Jan 2017 07:30:26 +0900
parents bbab930748c4
children cf20add31466
line wrap: on
line source

using UnityEngine;
using UnityEditor;
using System.Collections;
using UnityEngine.SceneManagement;
using JungleDB;
using System.Text;

public class SaveDataTest : MonoBehaviour {

	public static SaveDataTest instance;

	public SceneNode root;
	public System.Collections.Generic.List<GameObject> objList;
	public StageManager stageManager;

	public static Jungle jungle;
	private JungleTree tree;
	private JungleTreeEditor editor;
	private NodePath rootPath;

	public delegate void Callback ();
	public Callback callback;

	/// <summary>
	/// Jungleの木を構築し終わった時に実行するメソッドをセット
	/// </summary>
	public void AddCallback (Callback c) {
		this.callback = c;
		print ("Add : " + callback.Method);
	}

	private void initJungle () {
		jungle = new DefaultJungle (null, "Game", new DefaultTreeEditor(new DefaultTraverser()));
		tree = jungle.createNewTree ("Scene");
		rootPath = new DefaultNodePath ();
		// 編集を可能にする
		editor = tree.getTreeEditor ();
		// editor = editor.addNewChildAt (rootPath, 0).b();
	}

	private void Awake () {
		if (instance == null)
			instance = this;
	}

	private void Start () {
		initJungle ();
		CreateSceneTree ();
		CreateItemTree ();
	}

	private void CreateTree () {
		GameObject rootObj = new GameObject ();
		rootObj.name = "Root";
		root = new SceneNode(rootObj, null);
	}

	private void Check () {
		int i = 0;
		foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) {
			string path = AssetDatabase.GetAssetOrScenePath (obj);
			bool isScene = path.Contains (".unity");
			if (isScene) { // このシーンの中のオブジェクトかどうか。
				if (obj.transform.childCount == 0 && obj.transform.parent == null || obj.transform.childCount > 0 && obj.transform.parent == null) { // 親を取得
					objList.Add(obj);
					i++;
				}
			}
		}

		foreach (var obj in objList) {
			SceneNode node = new SceneNode (obj, root);
			root.AddChild (node);
			CheckChild (obj.transform, node);
		}
	}

	private void CheckChild (Transform obj, SceneNode parent) {
		if (obj.childCount == 0)
			return;

		for (int i = 0; i < obj.childCount; i++) {
			SceneNode node = new SceneNode (obj.GetChild (i).gameObject, parent);
			parent.AddChild (node);
			CheckChild (obj.GetChild (i), node);
		}
	}

	private bool CreateNode (NodePath path, int num) {
		Either<Error, JungleTreeEditor> e = editor.addNewChildAt (path, num);
		if (e.isA ()) {
			e.a ();
			return false;
		}
		editor = e.b ();
		return true;
	}

	private bool SetAttribute (NodePath path, GameObject obj) {
		// Either<Error, TreeEditor> e = editor.putAttribute (path, key, Encoding.UTF8.GetBytes(val));
		Either<Error, JungleTreeEditor> e = editor.putAttribute (path, "NodeName", Encoding.UTF8.GetBytes(obj.name));
		if (e.isA ()) {
			e.a ();
			return false;
		}
		editor = e.b ();
		return true;
	}

	private void CreateItemTree () {
		JungleTree tree = jungle.createNewTree ("ItemTree");
		JungleTreeEditor edt = tree.getTreeEditor ();

		edt = edt.putAttribute (rootPath, "TreeName", "Item").b ();

		edt = edt.addNewChildAt (rootPath, 0).b ();
		edt = edt.putAttribute ("Category", "Box").b ();

		edt = edt.addNewChildAt (rootPath, 1).b ();
		edt = edt.putAttribute ("Category", "Food").b ();

		NodePath path = rootPath.add(0);

		edt = edt.addNewChildAt (path, 0).b ();

		ItemInfo item_info = new ItemInfo (1, 2, "Grass", "#019540FF");
		edt = edt.putAttribute (item_info).b ();

		edt = edt.addNewChildAt (path, 1).b ();
		item_info = new ItemInfo (2, 4, "Wood", "#7F3C01FF");
		edt = edt.putAttribute (item_info).b ();

		edt = edt.addNewChildAt (path, 2).b ();
		item_info = new ItemInfo (3, 1, "Sand", "#D4500EFF");
		edt = edt.putAttribute (item_info).b ();


		edt = edt.addNewChildAt (path, 3).b ();
		item_info = new ItemInfo (4, 5, "Water", "#2432ADFF");
		edt = edt.putAttribute (item_info).b ();
		edt.commit ();

		StageManager.Instance.Init ();
	}

	private void CreateSceneTree () {
		JungleTree tree = jungle.createNewTree ("SceneTree");
		JungleTreeEditor edt = tree.getTreeEditor ();

		edt = edt.addNewChildAt (rootPath, 0).b ();
		edt = edt.putAttribute ("NodeName", "Player").b ();

		NodePath playerpath = rootPath.add (0);

		edt = edt.addNewChildAt (playerpath, 0).b();
		edt = edt.putAttribute ("NodeName", "HaveItems").b();

		edt = edt.addNewChildAt (rootPath, 1).b ();
		edt = edt.putAttribute ("NodeName", "Stage").b ();

		edt = edt.commit ().b();
	}
}