diff Assets/Application/Scripts/Test/SaveData.cs @ 13:e297afe0889d default tip

Add Prefab.
author Kazuma Takeda
date Tue, 07 Feb 2017 20:49:26 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Assets/Application/Scripts/Test/SaveData.cs	Tue Feb 07 20:49:26 2017 +0900
@@ -0,0 +1,210 @@
+using UnityEngine;
+using UnityEditor;
+using System.Collections;
+using UnityEngine.SceneManagement;
+using JungleDB;
+using System.Text;
+using Default = System.Collections.Generic;
+using System.Linq;
+
+public class SaveData : MonoBehaviour {
+
+	public static SaveData 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> either = edt.putAttribute (rootPath, "TreeName", "Item");
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.addNewChildAt (rootPath, 0);
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.putAttribute ("Category", "Box");
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.addNewChildAt (rootPath, 1);
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.putAttribute ("Category", "Food");
+		});
+
+		NodePath path = rootPath.add(0);
+
+		Default.List<BoxItemInfo> infoList = new Default.List<BoxItemInfo> ();
+		infoList.Add (new BoxItemInfo (1, 2, "Grass", "#019540FF"));
+		infoList.Add (new BoxItemInfo (2, 4, "Wood", "#7F3C01FF"));
+		infoList.Add (new BoxItemInfo (3, 1, "Sand", "#D4500EFF"));
+		infoList.Add (new BoxItemInfo (4, 5, "Water", "#2432ADFF"));
+
+		foreach (var info in infoList.Select((v, i) => new {v, i})) {
+			either = either.bind ((JungleTreeEditor arg) => {
+				return arg.addNewChildAt (path, info.i);
+			});
+
+			either = either.bind ((JungleTreeEditor arg) => {
+				return arg.putAttribute (info.v);
+			});
+		}
+
+
+
+		Default.List<FoodItemInfo> foodinfoList = new Default.List<FoodItemInfo>();
+
+		foodinfoList.Add (new FoodItemInfo (1, 1, "Apple"));
+		foodinfoList.Add (new FoodItemInfo (1, 2, "Apple"));
+
+		path = rootPath.add(1);
+
+		foreach (var info in foodinfoList.Select((v, i) => new {v, i})) {
+			either = either.bind ((JungleTreeEditor arg) => {
+				return arg.addNewChildAt (path, info.i);
+			});
+
+			either = either.bind ((JungleTreeEditor arg) => {
+				return arg.putAttribute (info.v);
+			});
+		}
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.commit();
+		});
+
+		StageManager.Instance.Init ();
+	}
+
+	private void CreateSceneTree () {
+		JungleTree tree = jungle.createNewTree ("SceneTree");
+		JungleTreeEditor edt = tree.getTreeEditor ();
+
+		Either<Error, JungleTreeEditor> either = edt.addNewChildAt (rootPath, 0);
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.putAttribute ("NodeName", "Player");
+		});
+
+		NodePath playerpath = rootPath.add (0);
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.addNewChildAt (playerpath, 0);
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.putAttribute ("NodeName", "HaveItems");
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.addNewChildAt (rootPath, 1);
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.putAttribute ("NodeName", "Stage");
+		});
+
+		either = either.bind ((JungleTreeEditor arg) => {
+			return arg.commit();
+		});
+	}
+}