using UnityEngine;
using System.Collections;
public class DjNPC : MonoBehaviour {
//是否绘制敌人血条
bool showBlood = false;
public Texture2D tex_red;
public Texture2D tex_black;
//血值
private int HP = 100;
//主角对象
private GameObject hero;
// Use this for initialization
void Start ()
{
//获取主角对象
hero = GameObject.Find("/Hero");
}
// Update is called once per frame
void Update ()
{
//始终朝向主角
transform.LookAt(hero.transform);
}
void OnGUI()
{
if(showBlood)
{
//绘制敌人血条
int blood_width = tex_red.width * HP / 100;
GUI.DrawTexture(new Rect(5,5,tex_black.width,tex_black.height),tex_black);
GUI.DrawTexture(new Rect(5,5,blood_width,tex_red.height),tex_red);
}
}
void OnMouseDown()
{
//击打中敌人目标
if(HP > 0)
{
//减血
HP -= 5;
//受打击后先后退
transform.Translate(Vector3.back * 0.1f);
}
else
{
//如果没有血了就消失
Destroy(gameObject);
}
}
void OnMouseUp()
{
//后退之后再向前就是还原
transform.Translate(Vector3.forward * 0.1f);
}
void OnMouseOver()
{
//开始绘制血条
showBlood = true;
}
void OnMouseExit()
{
//鼠标离开后不绘制
showBlood = false;
}
}