2017-09-13 12:40:42 -05:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections;
|
2017-09-18 12:43:50 -05:00
|
|
|
|
using UnityEngine.AI;
|
2017-09-13 12:40:42 -05:00
|
|
|
|
|
2017-09-21 04:59:13 -05:00
|
|
|
|
public class SimpleNPCBehavior : Photon.PunBehaviour
|
2017-09-13 12:40:42 -05:00
|
|
|
|
{
|
2017-09-21 04:59:13 -05:00
|
|
|
|
private bool setTarget = true;
|
2017-09-13 12:40:42 -05:00
|
|
|
|
|
2017-09-21 04:59:13 -05:00
|
|
|
|
enum State {
|
2017-09-18 15:04:18 -05:00
|
|
|
|
idle,
|
|
|
|
|
walking,
|
|
|
|
|
talking
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-27 23:56:52 -05:00
|
|
|
|
void Start()
|
2017-09-13 12:40:42 -05:00
|
|
|
|
{
|
2017-09-26 16:27:21 -05:00
|
|
|
|
//agent = gameObject.AddComponent<NavMeshAgent>();
|
2017-09-21 04:59:13 -05:00
|
|
|
|
if (PhotonNetwork.isMasterClient) {
|
|
|
|
|
photonView.RPC("TeleportToTarget", PhotonTargets.All, GetRandomLocation());
|
|
|
|
|
photonView.RPC("SetColorRPC", PhotonTargets.All, new Vector3(Random.value, Random.value, Random.value));
|
2017-09-27 23:40:27 -05:00
|
|
|
|
Debug.Log("setting color");
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 12:40:42 -05:00
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2017-09-21 04:59:13 -05:00
|
|
|
|
if (!PhotonNetwork.isMasterClient)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (setTarget)
|
|
|
|
|
{
|
|
|
|
|
setTarget = false;
|
|
|
|
|
StartCoroutine(UpdateDestination());
|
|
|
|
|
}
|
2017-09-13 12:40:42 -05:00
|
|
|
|
}
|
2017-09-21 04:59:13 -05:00
|
|
|
|
|
2017-09-27 23:56:52 -05:00
|
|
|
|
NavMeshAgent GetAgent()
|
|
|
|
|
{
|
|
|
|
|
return GetComponent<NavMeshAgent>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 04:59:13 -05:00
|
|
|
|
IEnumerator UpdateDestination()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(Random.Range(0.1f, 10.0f));
|
|
|
|
|
Vector3 location = GetRandomLocation();
|
|
|
|
|
|
|
|
|
|
photonView.RPC("SetTarget", PhotonTargets.All, location);
|
|
|
|
|
setTarget = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector3 GetRandomLocation()
|
|
|
|
|
{
|
2017-09-26 16:27:21 -05:00
|
|
|
|
var randTarget = new Vector3(5.0f - (10.0f * Random.value), 0.0f, 5.0f - (10.0f * Random.value));
|
2017-09-21 04:59:13 -05:00
|
|
|
|
NavMeshHit hit;
|
|
|
|
|
if (NavMesh.SamplePosition(randTarget, out hit, 1.0f, NavMesh.AllAreas))
|
2017-09-26 16:27:21 -05:00
|
|
|
|
//return hit.position;
|
2017-09-26 16:44:11 -05:00
|
|
|
|
return new Vector3 (hit.position.x, 1.0f, hit.position.z);
|
2017-09-21 04:59:13 -05:00
|
|
|
|
else
|
|
|
|
|
return GetRandomLocation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[PunRPC]
|
|
|
|
|
void SetTarget(Vector3 target)
|
|
|
|
|
{
|
2017-09-27 23:56:52 -05:00
|
|
|
|
GetAgent().destination = target;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[PunRPC]
|
|
|
|
|
void TeleportToTarget(Vector3 target)
|
|
|
|
|
{
|
2017-09-27 23:56:52 -05:00
|
|
|
|
GetAgent().Warp(target);
|
|
|
|
|
GetAgent().destination = target;
|
2017-09-21 04:59:13 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[PunRPC]
|
|
|
|
|
void SetColorRPC(Vector3 color)
|
|
|
|
|
{
|
|
|
|
|
transform.Find("Body").GetComponent<Renderer>().material.color = new Color(color.x, color.y, color.z);
|
|
|
|
|
}
|
2017-09-13 12:40:42 -05:00
|
|
|
|
}
|