CharacterEquipItemAction.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. using KairoEngine.Inventory;
  6. namespace KairoEngine.CharacterSystem
  7. {
  8. public class CharacterEquipItemAction : IAction
  9. {
  10. ItemRef itemRef;
  11. CharacterController character; // Reference to the character that will move
  12. // Constructor
  13. public CharacterEquipItemAction(ItemRef itemRef, CharacterController character)
  14. {
  15. this.itemRef = itemRef;
  16. this.character = character;
  17. }
  18. public void Start()
  19. {
  20. //Debug.Log("Equipping Item");
  21. if(itemRef.item.category != ItemType.firearm && itemRef.item.category != ItemType.handWeapon) return;
  22. if(character.HasEquipedItem())
  23. {
  24. GameObject.Destroy(character.GetEquipedGameObject());
  25. character.SetEquipedItem(null);
  26. }
  27. GameObject weaponObj;
  28. ItemBaseHoldable holdableItem = (ItemBaseHoldable)itemRef.item;
  29. weaponObj = GameObject.Instantiate(holdableItem.holdablePrefab, character.rightHand.transform);
  30. holdableItem.Setup(weaponObj, 1f);
  31. character.animator.SetInteger("Mode", holdableItem.animationMode);
  32. character.SetEquipedItem(itemRef);
  33. character.SetEquipedGameObject(weaponObj);
  34. if(itemRef.item.category == ItemType.firearm)
  35. {
  36. ItemBaseFirearm weaponItem = (ItemBaseFirearm)itemRef.item;
  37. if(weaponItem.firearmType == FirearmType.pistol) character.animator.SetBool("Pistol", true);
  38. else character.animator.SetBool("Pistol", false);
  39. Firearm firearm = character.GetEquipedGameObject().GetComponent<Firearm>();
  40. if(firearm != null)
  41. {
  42. ItemFirearmRef firearmRef = itemRef as ItemFirearmRef;
  43. if(firearmRef == null)
  44. {
  45. Debug.LogError("Cast is not working for item " + itemRef.item.title);
  46. //firearmRef = new ItemFirearmRef(weaponItem, itemRef.quantity, weaponItem.ammoType, 0);
  47. //itemRef = firearmRef;
  48. //character.itemContainer.UpdateItem(itemRef);
  49. }
  50. firearm.itemFirearmRef = firearmRef;
  51. }
  52. }
  53. else if(itemRef.item.category == ItemType.handWeapon)
  54. {
  55. ItemBaseHandWeapon weaponItem = (ItemBaseHandWeapon)itemRef.item;
  56. DamageEmitter damageEmitter = character.GetEquipedGameObject().GetComponent<DamageEmitter>();
  57. damageEmitter.character = character;
  58. damageEmitter.damage = weaponItem.damage;
  59. HandWeapon handWeapon = weaponObj.GetComponent<HandWeapon>();
  60. if(handWeapon != null)
  61. {
  62. handWeapon.weaponCollider.enabled = false;
  63. handWeapon.damageTrigger.enabled = true;
  64. }
  65. }
  66. }
  67. public void Update() { }
  68. public void End() { }
  69. public bool IsDone()
  70. {
  71. return true;
  72. }
  73. }
  74. }