120 lines
3.6 KiB
C#
120 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class Enemy : MonoBehaviour
|
|
{
|
|
public NavMeshAgent _agent;
|
|
public float bloodvolume;
|
|
public Transform temp;
|
|
public Transform player;//玩家
|
|
public Transform build;//建筑
|
|
public GameObject wupinPrefab;//僚机
|
|
public EnemySpawn enemySpawn;//怪物复活点
|
|
public GameObject BloodPrefab;//流血特效
|
|
public float attack;//攻击力
|
|
private float attackTimer = 0f;
|
|
private const float attackInterval = 2f;
|
|
private Animator animator;
|
|
public string aniNow;
|
|
private float attackCityTimer = 0f;
|
|
private const float attackCityInterval = 10f;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
_agent = GetComponent<NavMeshAgent>();
|
|
player=GameObject.FindWithTag("Player").transform;
|
|
build = GameObject.FindWithTag("Build").transform;
|
|
animator =GetComponent<Animator>();
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (player.GetComponent<playercontroller>().IsCity())
|
|
{
|
|
//Debug.Log("怪物攻城");
|
|
setMoveTo(build);
|
|
AttackCity();
|
|
}
|
|
else
|
|
{
|
|
setMoveTo(player);
|
|
AutoAttack();
|
|
}
|
|
if (bloodvolume <= 0)
|
|
{
|
|
//ToDo
|
|
enemySpawn.MinusNum();
|
|
GameObject Blood = Instantiate(BloodPrefab,this.transform.position + new Vector3(0,5,0), this.transform.rotation);
|
|
Destroy(Blood,0.5f);
|
|
GameObject wupin = Instantiate(wupinPrefab);
|
|
wupin.transform.position = this.transform.position+ new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
|
|
Destroy(this.gameObject);
|
|
}
|
|
|
|
}
|
|
public void EnemyHpDown(float hp)
|
|
{
|
|
bloodvolume -= hp;
|
|
}
|
|
public void setMoveTo(Transform target)
|
|
{
|
|
//Debug.Log(""+target.position);
|
|
_agent.SetDestination(target.position+new Vector3(0,-10,0));
|
|
}
|
|
public void AttackCity()
|
|
{
|
|
if (build != null)
|
|
{
|
|
float distance = Vector3.Distance(build.position, transform.position);
|
|
//Debug.Log("distance:" + distance);
|
|
if (distance <= 20f)
|
|
{
|
|
attackTimer += Time.deltaTime;
|
|
animator.Play(aniNow);
|
|
if (attackTimer >= attackInterval)
|
|
{
|
|
//对玩家产生伤害
|
|
bool isDeath = build.GetComponent<Doors>().TakeDamage(attack);
|
|
Debug.Log("发起攻击!");
|
|
if (isDeath)
|
|
{
|
|
|
|
build = null;
|
|
}
|
|
attackTimer = 0f;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AutoAttack()
|
|
{
|
|
if (player != null)
|
|
{
|
|
float distance = Vector3.Distance(player.position, transform.position);
|
|
//Debug.Log("distance:" + distance);
|
|
if (distance <= 20f)
|
|
{
|
|
attackTimer += Time.deltaTime;
|
|
animator.Play(aniNow);
|
|
if (attackTimer >= attackInterval)
|
|
{
|
|
//对玩家产生伤害
|
|
bool isDeath = player.GetComponent<playercontroller>().TakeDamage(attack);
|
|
Debug.Log("发起攻击!");
|
|
if (isDeath)
|
|
{
|
|
player = null;
|
|
}
|
|
attackTimer = 0f;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|