Files
DefendNJ/Assets/_DefendNJ/Scripts/AI/NPC.cs
2025-09-27 18:45:59 +08:00

91 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using DragonLi.Core;
using Mirror;
using UnityEngine;
public enum NpcType
{
Run,
Fall,
Die,
Over,
}
public class NPC : Agent
{
private Collider _collider;
public Collider selfCollider
{
get
{
if (_collider == null) _collider = GetComponent<Collider>();
return _collider;
}
}
public bool isUserFall;
public NpcType state;
public int id;
public bool isOver;
[Server]
public virtual void OnSpawn(int curId,GameObject target,bool curIsUserFall)
{
base.OnSpawn();
id = curId;
health = 10;
originHealth = 10;
aiPath.enabled = true;
aiPath.maxSpeed = 3;
isUserFall = curIsUserFall;
// 初始设置server
if (isServer)
{
state = NpcType.Run;
if (aiPath != null) aiPath.enabled = true;
if (rvoController != null) rvoController.enabled = true;
if (selfCollider != null) selfCollider.enabled = true;
// MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
// {
// ChangeState(NpcType.Fall);
// }, 4f, this);
}
}
public void ChangeState(NpcType npcType)
{
state = npcType;
switch (state)
{
case NpcType.Fall:
if (isUserFall)
{
AnimatorComponent.SetInteger("state",1);
aiPath.maxSpeed = 1;
}
break;
case NpcType.Die:
AnimatorComponent.SetBool("dead",true);
isOver = true;
break;
case NpcType.Over:
isOver = true;
NetworkServer.Destroy(gameObject);
break;
}
}
[ServerCallback]
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("SafeDoor"))
{
ChangeState(NpcType.Over);
GameManager.Ins.CreateExplosion(transform,EnemyType.Npc);
}
}
}