Unity之黑暗之光按教程学习第九天

     今天完成了部分装备系统,装备系统用到了前面的脚本,和新建的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;
				}
		}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李曼竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值