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

put Attribute class.
author Kazuma Takeda
date Fri, 20 Jan 2017 07:30:26 +0900
parents 599bd8ddb72b
children cf20add31466
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) {
			Destroy (this.gameObject);
			return;
		}

		nowBroken -= 1;
	}

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