view Assets/Application/Scripts/Player.cs @ 3:2dd40b4412e4

Create game base.
author Kazuma
date Mon, 07 Nov 2016 18:42:01 +0900
parents e5ef0342d00b
children 2878be4487ec
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour { // Singleton
	// --------------------------------------
	public static Player Instance;
	private GameObject eye;
	private CharacterController cc;
	private Vector3 Direction;
	// 今見ているフィールドのアイテム
	public GameObject LookedObject;
	// セットする前のアイテム
	public int HaveObjectNumber;

	public int GlassItem = 0;
	public int SandItem = 0;
	public int WoodItem = 0;

	public System.Collections.Generic.List<Stage> HaveItemList = new System.Collections.Generic.List<Stage>();

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

	// -------------------------------------
	private void Start () {
		if (Instance == null) {
			Instance = this;
		}
		eye = this.transform.FindChild ("Eye").gameObject;
		cc = this.GetComponent<CharacterController> ();
	}

	private void Update () {
		Gravity ();
	}

	public void Move (bool b) {
		if (b) {
			Direction += this.transform.forward * Time.deltaTime;
		} 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 (int num) {
		HaveObjectNumber = num;
	}

	public void GetItem () {
		if (LookedObject != null) {
			Stage stage = LookedObject.GetComponent<Stage> ();
			HaveItemList.Add (stage);
			CheckList ();
			stage.Delete ();
		}
	}

	public void SetItem () {
		if (LookedObject != null) {
			StageManager.Instance.CreateStage (HaveObjectNumber, new Vector3 (LookedObject.transform.position.x, LookedObject.transform.position.y + 1f, LookedObject.transform.position.z));
			CheckList ();
		}
	}

	public void CheckList () { // 増えたときに面倒なので動的に増えてもいいように書き直す必要あり
		int g = 0;
		int w = 0;
		int s = 0;
		foreach (var item in HaveItemList) {
			if (Stage.Type.GRASS == item.Attribute) {
				g++;
			} else if (Stage.Type.SAND == item.Attribute) {
				s++;
			} else if (Stage.Type.WOOD == item.Attribute) {
				w++;
			}
		}
		SetItemNum (g, w, s);
	}

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

	public void SetItemNum (int glass, int wood, int sand) {
		this.GlassItem = glass;
		this.WoodItem = wood;
		this.SandItem = sand;
		this.callback (sand, wood, glass);
		print (glass + ", " + wood + ", " + sand);
	}
}