view Assets/Application/Scripts/Test/SaveDataTest.cs @ 11:cf20add31466

change putAttribute -> use fmap.
author Kazuma Takeda
date Sat, 28 Jan 2017 19:15:44 +0900
parents 3fefb9f9025d
children
line wrap: on
line source

using UnityEngine;
using UnityEditor;
using System.Collections;
using UnityEngine.SceneManagement;
using JungleDB;
using System.Text;
using Default = System.Collections.Generic;

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


		Either<Error, JungleTreeEditor> e = edt.putAttribute (rootPath, "TreeName", "Item");

		System.Func<JungleTreeEditor, JungleTreeEditor> f =  (JungleTreeEditor arg) => {
			edt = arg;
			return edt;
		};

		e.fmap (f, edt.addNewChildAt (rootPath, 0));
		e.fmap (f, edt.putAttribute ("Category", "Box"));

		e.fmap (f, edt.addNewChildAt (rootPath, 1));
		e.fmap (f, edt.putAttribute ("Category", "Food"));

		NodePath path = rootPath.add(0);

		Default.List<ItemInfo> infoList = new Default.List<ItemInfo> ();
		infoList.Add (new ItemInfo (1, 2, "Grass", "#019540FF"));
		infoList.Add (new ItemInfo (2, 4, "Wood", "#7F3C01FF"));
		infoList.Add (new ItemInfo (3, 1, "Sand", "#D4500EFF"));
		infoList.Add (new ItemInfo (4, 5, "Water", "#2432ADFF"));

		int i = 0;
		foreach (var info in infoList) {
			e.fmap (f, edt.addNewChildAt (path, i));
			e.fmap (f, edt.putAttribute (info));
			i++;
		}

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