今天完成了部分装备系统,装备系统用到了前面的脚本,和新建的EquipmentUI和EquiptmentItem 脚本,基本实现,穿戴,脱掉的功能。
public class EquipmentItem : MonoBehaviour {
private UISprite sprite;
public int id;
private bool isHover = false;
private void Awake ()
{
sprite = this.GetComponent<UISprite> ();
}
private void Update ()
{
if (isHover) {//当鼠标在装备栏之上,监测鼠标右键点击
if (Input.GetMouseButtonDown (1)) {//表示卸下装备
EquipmentUI._instance.TakeOff (id,this.gameObject);
}
}
}
public void SetId(int id)
{
this.id = id;
ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById (id);
SetInfo (info);
}
public void SetInfo(ObjectInfo info)
{
this.id = info.id;
sprite.spriteName = info.icon_name;
}
public void OnHover(bool isOver)
{
isHover = isOver;
}
}
public class EquipmentUI : MonoBehaviour {
public static EquipmentUI _instance;
private TweenPosition tween;
private bool isShow = false;
private GameObject Headgear;
private GameObject Armor;
private GameObject RightHand;
private GameObject LeftHand;
private GameObject Shoe;
private GameObject Accessory;
private PlayerStatus playerStatus;
public GameObject equipmentItem;
public int Attack = 0;
public int Def = 0;
public int Speed = 0;
void Awake ()
{
_instance = this;
tween = this.GetComponent<TweenPosition> ();
Headgear = transform.Find ("Headgear").gameObject;
Armor = transform.Find ("Armor").gameObject;
RightHand = transform.Find ("RightHand").gameObject;
LeftHand = transform.Find ("LeftHand").gameObject;
Shoe = transform.Find ("Shoe").gameObject;
Accessory = transform.Find ("Accessory").gameObject;
playerStatus = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus> ();
}
public void TransformState()
{
if (isShow == false) {
tween.PlayForward ();isShow = true;
} else {
tween.PlayReverse ();isShow = false;
}
}
public bool Dress (int id)
{
ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById (id);
if(info.type != ObjectType.Equip) {
return false;//穿戴不成功
}
if(playerStatus.heroType == HeroType.Magician) {
if(info.applicationType == ApplicationType.Swordman) {
return false;
}
}
if(playerStatus.heroType == HeroType.Swordman) {
if(info.applicationType == ApplicationType.Magician) {
return false;
}
}
GameObject parent = null;
switch (info.dressType) {
case DressType.Headgear:
parent = Headgear;
break;
case DressType.Armor:
parent = Armor;
break;
case DressType.RightHand:
parent = RightHand;
break;
case DressType.LeftHand:
parent = LeftHand;
break;
case DressType.Shoe:
parent = Shoe;
break;
case DressType.Accessory:
parent = Accessory;
break;
}
EquipmentItem Item = parent.GetComponentInChildren<EquipmentItem> ();
if(Item != null) {//已经穿戴同样类型装备
Inventory._instance.GetId (Item.id);//把已经穿戴的物品卸下
Item.SetInfo (info);
} else {//没有穿戴
GameObject ItemGo = NGUITools.AddChild (parent, equipmentItem);
ItemGo.transform.localPosition = Vector3.zero;
ItemGo.GetComponent<EquipmentItem> ().SetInfo(info);
}
UpdateProperty ();
return true;
}
public void TakeOff(int id,GameObject go)
{
Inventory._instance.GetId (id);
Destroy (go);
UpdateProperty ();
}
private void UpdateProperty ()
{
this.Attack = 0;
this.Def = 0;
this.Speed = 0;
EquipmentItem headgearItem = Headgear.GetComponentInChildren<EquipmentItem> ();
PlusProperty (headgearItem);
EquipmentItem ArmorItem = Armor.GetComponentInChildren<EquipmentItem> ();
PlusProperty (ArmorItem);
EquipmentItem ShoeItem = Shoe.GetComponentInChildren<EquipmentItem> ();
PlusProperty (ShoeItem);
EquipmentItem RightHandItem = RightHand.GetComponentInChildren<EquipmentItem> ();
PlusProperty (RightHandItem);
EquipmentItem LeftHandItem = LeftHand.GetComponentInChildren<EquipmentItem> ();
PlusProperty (LeftHandItem);
EquipmentItem AccesoryItem = Accessory.GetComponentInChildren<EquipmentItem> ();
PlusProperty (AccesoryItem);
}
void PlusProperty (EquipmentItem item)
{
if (item != null) {
ObjectInfo equipinfo = ObjectsInfo._instance.GetObjectInfoById (item.id);
this.Attack += equipinfo.Attack;
this.Def += equipinfo.Def;
this.Speed += equipinfo.Speed;
}
}
}