123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 Jiang Yin. All rights reserved.
- // Homepage: https://gameframework.cn/
- // Feedback: mailto:ellan@gameframework.cn
- //------------------------------------------------------------
- using GameFramework;
- using UnityEngine;
- using UnityGameFramework.Runtime;
- namespace MetaClient
- {
- /// <summary>
- /// 武器类。
- /// </summary>
- public class Weapon : Entity
- {
- private const string AttachPoint = "Weapon Point";
- [SerializeField]
- private WeaponData m_WeaponData = null;
- private float m_NextAttackTime = 0f;
- #if UNITY_2017_3_OR_NEWER
- protected override void OnInit(object userData)
- #else
- protected internal override void OnInit(object userData)
- #endif
- {
- base.OnInit(userData);
- }
- #if UNITY_2017_3_OR_NEWER
- protected override void OnShow(object userData)
- #else
- protected internal override void OnShow(object userData)
- #endif
- {
- base.OnShow(userData);
- m_WeaponData = userData as WeaponData;
- if (m_WeaponData == null)
- {
- Log.Error("Weapon data is invalid.");
- return;
- }
- GameEntry.Entity.AttachEntity(Entity, m_WeaponData.OwnerId, AttachPoint);
- }
- #if UNITY_2017_3_OR_NEWER
- protected override void OnAttachTo(EntityLogic parentEntity, Transform parentTransform, object userData)
- #else
- protected internal override void OnAttachTo(EntityLogic parentEntity, Transform parentTransform, object userData)
- #endif
- {
- base.OnAttachTo(parentEntity, parentTransform, userData);
- Name = Utility.Text.Format("Weapon of {0}", parentEntity.Name);
- CachedTransform.localPosition = Vector3.zero;
- }
- public void TryAttack()
- {
- if (Time.time < m_NextAttackTime)
- {
- return;
- }
- m_NextAttackTime = Time.time + m_WeaponData.AttackInterval;
- GameEntry.Entity.ShowBullet(new BulletData(GameEntry.Entity.GenerateSerialId(), m_WeaponData.BulletId, m_WeaponData.OwnerId, m_WeaponData.OwnerCamp, m_WeaponData.Attack, m_WeaponData.BulletSpeed)
- {
- Position = CachedTransform.position,
- });
- GameEntry.Sound.PlaySound(m_WeaponData.BulletSoundId);
- }
- }
- }
|