Weapon.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using UnityEngine;
  9. using UnityGameFramework.Runtime;
  10. namespace MetaClient
  11. {
  12. /// <summary>
  13. /// 武器类。
  14. /// </summary>
  15. public class Weapon : Entity
  16. {
  17. private const string AttachPoint = "Weapon Point";
  18. [SerializeField]
  19. private WeaponData m_WeaponData = null;
  20. private float m_NextAttackTime = 0f;
  21. #if UNITY_2017_3_OR_NEWER
  22. protected override void OnInit(object userData)
  23. #else
  24. protected internal override void OnInit(object userData)
  25. #endif
  26. {
  27. base.OnInit(userData);
  28. }
  29. #if UNITY_2017_3_OR_NEWER
  30. protected override void OnShow(object userData)
  31. #else
  32. protected internal override void OnShow(object userData)
  33. #endif
  34. {
  35. base.OnShow(userData);
  36. m_WeaponData = userData as WeaponData;
  37. if (m_WeaponData == null)
  38. {
  39. Log.Error("Weapon data is invalid.");
  40. return;
  41. }
  42. GameEntry.Entity.AttachEntity(Entity, m_WeaponData.OwnerId, AttachPoint);
  43. }
  44. #if UNITY_2017_3_OR_NEWER
  45. protected override void OnAttachTo(EntityLogic parentEntity, Transform parentTransform, object userData)
  46. #else
  47. protected internal override void OnAttachTo(EntityLogic parentEntity, Transform parentTransform, object userData)
  48. #endif
  49. {
  50. base.OnAttachTo(parentEntity, parentTransform, userData);
  51. Name = Utility.Text.Format("Weapon of {0}", parentEntity.Name);
  52. CachedTransform.localPosition = Vector3.zero;
  53. }
  54. public void TryAttack()
  55. {
  56. if (Time.time < m_NextAttackTime)
  57. {
  58. return;
  59. }
  60. m_NextAttackTime = Time.time + m_WeaponData.AttackInterval;
  61. GameEntry.Entity.ShowBullet(new BulletData(GameEntry.Entity.GenerateSerialId(), m_WeaponData.BulletId, m_WeaponData.OwnerId, m_WeaponData.OwnerCamp, m_WeaponData.Attack, m_WeaponData.BulletSpeed)
  62. {
  63. Position = CachedTransform.position,
  64. });
  65. GameEntry.Sound.PlaySound(m_WeaponData.BulletSoundId);
  66. }
  67. }
  68. }