Created
March 30, 2017 15:14
-
-
Save kazumalab/1492e7d789fa2d875c9b6e1f2933373b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Enemy : MonoBehaviour { | |
private readonly float LookedDistance = 2f; | |
private Player player; | |
private CharacterControl cc; | |
public enum Status { | |
None, | |
Warning, | |
Discovery | |
}; | |
public Status mystatus; | |
public GameObject NoticeObject; | |
// Use this for initialization | |
void Start () { | |
cc = GetComponent<CharacterControl> (); | |
player = GameObject.Find ("Player").GetComponent<Player> (); | |
SetCallback (CreateNotice); | |
} | |
// Update is called once per frame | |
void Update () { | |
Search (); | |
} | |
public float getDistance () { | |
Vector3 dv = player.transform.position - transform.position; | |
return Mathf.Pow ((dv.x * dv.x + dv.y * dv.y + dv.z * dv.z), 0.5f); | |
} | |
public void Search () { | |
if (mystatus != Status.Discovery) { | |
// 目線 | |
RaycastHit hit; | |
if (Physics.Raycast (transform.position, transform.forward * LookedDistance, out hit)) { | |
if (hit.collider.tag == "Player") { | |
if (hit.distance < LookedDistance / 2f) { | |
OnChangeStatus (Status.Discovery); | |
} else { | |
OnChangeStatus (Status.Warning); | |
} | |
} | |
} else { | |
OnChangeStatus (Status.None); | |
} | |
// 距離と足音 | |
if (getDistance () < LookedDistance) { | |
if (player.getWalkVolumeStatus () == Volume.Small) | |
OnChangeStatus (Status.Warning); | |
if (player.getWalkVolumeStatus () == Volume.Loud) | |
OnChangeStatus (Status.Discovery); | |
} else { | |
OnChangeStatus (Status.None); | |
} | |
} else { | |
transform.LookAt (player.transform.position); | |
if (getDistance () > LookedDistance) { | |
OnChangeStatus (Status.None); | |
} | |
} | |
print (mystatus); | |
} | |
public delegate void Callback (Status st); | |
public Callback callback; | |
public void SetCallback (Callback c) { | |
callback = c; | |
} | |
public void OnChangeStatus (Status st) { | |
mystatus = st; | |
if (mystatus != Status.None) { | |
if (callback != null) { | |
callback (mystatus); | |
} | |
} | |
} | |
public void CreateNotice (Status st) { | |
if (transform.FindChild ("Notice(Clone)") != null) { | |
transform.FindChild ("Notice(Clone)").GetComponent<Notice> ().ChangeStatus (st); | |
return; | |
} | |
GameObject obj = Instantiate (NoticeObject); | |
obj.transform.SetParent (transform); | |
obj.transform.localPosition = Vector3.up * (cc.getHeight () + 1f); | |
obj.GetComponent<Notice> ().Created (st); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment