DropItemAction.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 DropItemAction : IAction
  9. {
  10. ItemContainer itemContainer; // Reference to the character that will move
  11. ItemRef itemRef;
  12. public DropItemAction(ItemContainer itemContainer, ItemRef itemRef)
  13. {
  14. this.itemContainer = itemContainer;
  15. this.itemRef = itemRef;
  16. }
  17. public void Start()
  18. {
  19. itemContainer.RemoveItem(itemRef);
  20. Vector3 pos = itemContainer.transform.position;
  21. pos.y += 1.5f;
  22. pos.x += Random.Range(-1, 1);
  23. pos.z += Random.Range(-1, 1);
  24. GameObject obj = GameObject.Instantiate(itemRef.item.prefab, pos, itemContainer.transform.rotation);
  25. WorldItem worldItem = obj.GetComponent<WorldItem>();
  26. worldItem.itemName = itemRef.item.title;
  27. worldItem.quantity = itemRef.quantity;
  28. worldItem.lastUsed = itemRef.lastUsed;
  29. SerializerWorldItem serializer = obj.GetComponent<SerializerWorldItem>();
  30. serializer.loadedFromData = true;
  31. if(itemRef.item.category == ItemType.firearm)
  32. {
  33. ItemFirearmRef firearmRef = (ItemFirearmRef)itemRef;
  34. worldItem.itemAmmoName = firearmRef.ammoType.title;
  35. worldItem.ammoQuantity = firearmRef.ammo;
  36. }
  37. CharacterController character = itemContainer.GetComponent<CharacterController>();
  38. if(character !=null)
  39. {
  40. if(character.GetEquipedItem() == itemRef)
  41. {
  42. GameObject.Destroy(character.GetEquipedGameObject());
  43. character.SetEquipedItem(null);
  44. character.SetEquipedGameObject(null);
  45. character.animator.SetInteger("Mode", 0);
  46. }
  47. }
  48. }
  49. public void Update()
  50. {
  51. }
  52. public void End()
  53. {
  54. }
  55. public bool IsDone()
  56. {
  57. return true;
  58. }
  59. }
  60. }