149 lines
3.5 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MinionNPCController : BasicEnemyNPCController
{
protected bool isReady = false;
public LayerMask movementMask;
Camera cam;
Plane plane = new Plane(Vector3.down, 0);
Ray ray;
RaycastHit hit;
protected override void Start()
{
cam = Camera.main;
base.Start();
isReady = false;
}
public virtual void OnSummonAnimationEnded()
{
isReady = true;
}
protected override void Update()
{
if (!photonView.IsMine) return;
if (isDead) return;
if (!isReady)
{
SetAgentMoving(false);
return;
}
counter += Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f, movementMask))
{
FollowInstruction(hit.point);
}
else
{
if (plane.Raycast(ray, out float distance))
{
FollowInstruction(ray.GetPoint(distance));
}
}
}
else
{
if (HasTarget())
{
ChasingUpdate();
}
else
{
PatrollingUpdate();
}
}
}
protected override void PatrollingUpdate()
{
if (!HasAvailableTargets())
{
//Debug.Log("BRAIN: !HasAVailableTargetsInside");
if (currentTarget != null)
{
//Debug.Log("BRAIN: TargetWasNull");
currentTarget = null;
}
if (agent.destination == null)
{
//Debug.Log("BRAIN: DEstination Null");
FollowOwner();
}
else if (agent.remainingDistance < distanceToChangePatrolDestination)
{
//Debug.Log("BRAIN: StoppingDistanceReached");
FollowOwner();
}
else
{
//Debug.Log("BRAIN: Else");
FollowOwner();
}
}
else
{
//Debug.Log("BRAIN: Else Else");
FollowOwner();
if (currentTarget == null)
{
UpdateCurrentTarget(GetClosestTarget());
}
}
}
protected virtual void FollowOwner()
{
agent.speed = patrolAgentSpeed;
patrolDestination = ((RiftPlayer)photonView.Owner.TagObject).transform.position;
patrolDestination.y = 0f;
UpdatePatrolTarget(patrolDestination);
SetAgentMoving(true);
//Debug.Log("BRAIN: Following owner");
}
protected virtual void FollowInstruction(Vector3 instructionPosition)
{
agent.speed = patrolAgentSpeed;
patrolDestination = instructionPosition;
patrolDestination.y = 0f;
UpdatePatrolTarget(patrolDestination);
SetAgentMoving(true);
}
[PunRPC]
protected override void RPC_OnDeath(bool lootDropped)
{
if (isDead) return;
//Debug.Log($"{this.gameObject.name} died!");
isDead = true;
agent.enabled = false;
//experienceOnDeath.Raise(health.GetMaxValue() * GameConstants.GameBalancing.HealthIntoExperienceMultiplier);
//dropTable.DropLoot(lootDropped);
animatorController.SetDead();
if (!photonView.IsMine) return;
}
}