view Assets/Application/Scripts/Player.cs @ 12:b55d586dd4eb

change bind from fmap.
author Kazuma Takeda
date Tue, 07 Feb 2017 20:48:57 +0900
parents cf20add31466
children
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using JungleDB;
using System.Text;

public class Player : MonoBehaviour { // Singleton
	// --------------------------------------

	private readonly int BASEHP = 10;

	public static Player Instance;
	private GameObject eye;
	private CharacterController cc;
	private Vector3 Direction;
	// 今見ているフィールドのアイテム
	public GameObject LookedObject;
	// セットする前のアイテム
	public string haveItemType;

	public DeathZone deathZone;

	public delegate void Callback(int n0, int n1, int n2);
	public Callback callback;

	/* paramator */
	public int HP = 10;

	private Jungle jungle;
	// -------------------------------------

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

	private void Start () {
		eye    = this.transform.FindChild ("Eye").gameObject;
		cc     = this.GetComponent<CharacterController> ();
		deathZone.SetHitCallback (Damege);
	}

	private void Update () {
		Gravity ();
		if (Input.GetMouseButtonUp (1)) {
			SetItem ();
		}
		ChangeGage ();
	}

	public void Move (bool b) {
		if (b) {
			Direction += this.transform.forward * Time.deltaTime / 10f;
		} else {
			Direction.x = 0;
			Direction.z = 0;
		}
		cc.Move (Direction);
	}

	public void EyeMove (Vector3 v3) {
		eye.transform.Rotate (-v3.x, 0, 0);
		this.transform.Rotate (0, v3.y, 0);
	}

	public void Gravity () {
		Direction.y = -0.1f;
	}

	public void SetLookedObject (GameObject obj) {
		LookedObject = obj;
	}

	public void SetHaveObjectNumber (string type) {
		haveItemType = type;
	}

	public void GetItem () {
		if (LookedObject != null) {
			Item stage = LookedObject.GetComponent<Item> ();
			stage.Delete ();
		}
	}

	public void SetItem () {
		if (LookedObject != null) {
			StageManager.Instance.CreateItem (LookedObject.transform.position + Vector3.up, 1);
		}
	}

	public void SetCallback (Callback c) {
		this.callback = c;
	}

	public void ChangeGage () {
		// print (HP / BASEHP);
		GameObject.Find ("MoveHP").GetComponent<Image> ().fillAmount = (float)HP / (float)BASEHP;
	}

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

		NodePath playerpath = new DefaultNodePath ().add (0);
		edt = edt.putAttribute (playerpath, "HP", Encoding.UTF8.GetBytes (HP.ToString())).b();
		edt = edt.commit ().b();
	}

	private void OnControllerColliderHit (ControllerColliderHit hit) {
		SetPlayerNode ();
	}

	public void Damege (int d) {
		this.HP -= d;
		UpdatePlayerNode ();
	}

	public void Recovery (int d) {
		if (this.HP < BASEHP) {
			this.HP += d;
			UpdatePlayerNode ();
		}
	}

	private void UpdatePlayerNode () {
		jungle = SaveData.jungle;
		JungleTree tree = jungle.getTreeByName ("SceneTree");
		JungleTreeEditor edt = tree.getTreeEditor ();

		NodePath playerpath = new DefaultNodePath ().add (0);
		edt.putAttribute (playerpath, this).b ();
	}

}