view Assets/Application/Scripts/Item.cs @ 11:cf20add31466

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

using UnityEngine;
using System.Collections;
using JungleDB;
using System.Text;
using System;
public class Item : MonoBehaviour {

	public string Type;
	public GameObject ItemImage;
	public int Broken = 2;
	private int nowBroken = 2;
	public int indexID = 0;
	public int itemID = 1;
	public string ColorCode = "";

	private Jungle jungle;

	public void SetItem (ItemInfo item, int index) {
		this.Broken = item.Broken;
		nowBroken   = Broken;
		Type        = item.Type;
		itemID      = item.itemID;
		indexID     = index;
		ColorCode   = item.ColorCode;
		SetColor (this.ColorCode);
	}

	public void SetItem (int broken, string type, string item_id, int index, string code) {
		this.Broken = broken;
		nowBroken   = Broken;
		Type        = type;
		itemID      = Convert.ToInt32(item_id);
		indexID     = index;
		SetColor (code);
	}

	public void SetPosition (Vector3 pos) {
		this.transform.position = pos;
		// print ("Set (" + pos.x + ", " + pos.y + ", " + pos.z + ")");
	}

	public void SetColor (string code) {
		Color color;
		ColorUtility.TryParseHtmlString(code, out color);
		this.GetComponent<MeshRenderer> ().material.color = color;
	}

	public void Delete() {
		if (nowBroken == 0) {
			DeleteTreeNode ();
			Destroy (this.gameObject);
			return;
		}

		nowBroken -= 1;
	}

	public void DeleteTreeNode () {
		jungle = SaveDataTest.jungle;
		JungleTree tree      = jungle.getTreeByName ("SceneTree");
		JungleTreeEditor edt = tree.getTreeEditor ();

		NodePath root        = new DefaultNodePath ();
		NodePath stagePath   = root.add (1);

		Either<Error, JungleTreeEditor> e = edt.deleteChildAt (stagePath, indexID);
		if (e.isA ()) {
			print ("[Error] Try again!");
			SetCreateTreeNode ();
		}
		edt = e.b ();
		edt.commit ();
	}


	public void SetCreateTreeNode () {
		jungle = SaveDataTest.jungle;
		JungleTree tree      = jungle.getTreeByName ("SceneTree");
		JungleTreeEditor edt = tree.getTreeEditor ();

		NodePath root        = new DefaultNodePath ();
		NodePath stagePath   = root.add (1);
		edt                  = edt.addNewChildAt(stagePath, indexID).b();

		Either<Error, JungleTreeEditor> e = edt.putAttribute (stagePath.add (indexID), "Item_ID", Encoding.UTF8.GetBytes(itemID.ToString()));
		if (e.isA ()) {
			print ("[Error] Try again!");
			SetCreateTreeNode ();
		}
		edt = e.b ();
		edt.commit ();
	}
}