nbarber20

ComplexInventory.cpp

Dec 21st, 2018
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "ComplexInventory.h"
  4. #include <Engine/World.h>
  5. #include "Attachment.h"
  6. #include "FireArm.h"
  7.  
  8. // Sets default values for this component's properties
  9. UComplexInventory::UComplexInventory()
  10. {
  11.     PrimaryComponentTick.bCanEverTick = true;
  12.  
  13. }
  14.  
  15. // Called when the game starts
  16. void UComplexInventory::BeginPlay()
  17. {
  18.     Super::BeginPlay();
  19.     InitHeldItemsArray();
  20. }
  21.  
  22. void UComplexInventory::InitHeldItemsArray()
  23. {
  24.     for (int i = 0; i < HeldItems.Num(); i++) {
  25.         HeldItems[i].parent = i;
  26.     }
  27. }
  28.  
  29. bool UComplexInventory::HasClipFor(AFireArm* Firearm)
  30. {
  31.     for (int i = 0; i < HeldItems.Num(); i++) {
  32.         if (HeldItems[i].item) {
  33.             if (HeldItems[i].item->GetClass() == Firearm->AmmoType) {
  34.                 return true;
  35.             }
  36.         }
  37.     }
  38.     return false;
  39. }
  40.  
  41. void UComplexInventory::RemoveClipFor(AFireArm* Firearm)
  42. {
  43.     for (int i = 0; i < HeldItems.Num(); i++) {
  44.         if (HeldItems[i].item) {
  45.             if (HeldItems[i].item->GetClass() == Firearm->AmmoType) {
  46.                 HeldItems[i].item->Stack--;
  47.                 if (HeldItems[i].item->Stack <= 0) {
  48.                     AItem* clip = HeldItems[i].item;
  49.                     RemoveItemFromSlot(clip, i); //destroy the clip
  50.                     clip->Destroy();
  51.                 }
  52.                 return;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. void  UComplexInventory::AddItemToInventory(AItem* item)
  59. {
  60.     int desiredSlot = FindSuitableInventorySlot(item);
  61.     if (desiredSlot != -1) { // there is a valid slot for this item
  62.         AddItemToSlot(item, desiredSlot, -1);
  63.     }
  64. }
  65.  
  66. int  UComplexInventory::FindSuitableInventorySlot(AItem* item)
  67. {
  68.     for (int i = 0; i < HeldItems.Num(); i++) {
  69.         if (DoesSlotFit(item, i, -1)) {
  70.             return(i);
  71.         }
  72.     }
  73.     return -1;
  74. }
  75.  
  76. bool  UComplexInventory::DoesSlotFit(AItem* item, int Slot, int ignoreSlot)
  77. {
  78.     if (item) {
  79.         bool isvalid = true;
  80.         if (HeldItems[Slot].item) {
  81.             if (item->GetClass() == HeldItems[Slot].item->GetClass()) { //this is the same item
  82.                 if (item->Stack + HeldItems[Slot].item->Stack <= item->MaxStackSize) {// these two items can be stacked together
  83.                     return true;
  84.                 }
  85.             }
  86.         }
  87.         for (int x = 0; x < item->SlotSizeX; x++) {
  88.             if (Slot + x > HeldItems.Num() - 1) { //item goes out of bottom of inventory
  89.                 return false;
  90.             }
  91.             if (7 - (Slot % 7) < item->SlotSizeX) { // rolls over
  92.                 isvalid = false;
  93.             }
  94.             if (HeldItems[Slot + x].isfull) { // overlaps x
  95.                 if (HeldItems[Slot].parent != ignoreSlot && Slot != ignoreSlot) {
  96.                     isvalid = false;
  97.                 }
  98.             }
  99.             for (int y = 0; y < item->SlotSizeY; y++) {
  100.                 if (Slot + x + (y * 7) > HeldItems.Num() - 1) { //item goes out of bottom of inventory
  101.                     return false;
  102.                 }
  103.                 if (HeldItems[Slot + x + (y * 7)].isfull) { //overlaps y
  104.                     if (HeldItems[Slot + x + (y * 7)].parent != ignoreSlot && Slot != ignoreSlot) {
  105.                         isvalid = false;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         if (isvalid) {
  111.             return true;
  112.         }
  113.     }
  114.     return false;
  115. }
  116.  
  117. bool  UComplexInventory::AddItemToSlot(AItem* item, int Slot, int ignoreSlot)
  118. {
  119.     if (item) {
  120.         //check to see if this is an attachable item
  121.         AAttachment* attachment = Cast<AAttachment>(item);
  122.         if (attachment) {
  123.             if (HeldItems[HeldItems[Slot].parent].item) {
  124.                 AFireArm* firearm = Cast<AFireArm>(HeldItems[HeldItems[Slot].parent].item);
  125.                 if (firearm) {
  126.                     firearm->Attachment = attachment->AttachmentType;
  127.                     firearm->UpdateAttachment();
  128.                     return true;
  129.                 }
  130.             }
  131.         }
  132.  
  133.         if (DoesSlotFit(item, Slot, ignoreSlot)) {
  134.             if (HeldItems[Slot].item) { // there is already an item in this stack so we must be stacking two items together
  135.                 item->Stack += HeldItems[Slot].item->Stack;
  136.                 HeldItems[Slot].item->Destroy();
  137.             }
  138.             HeldItems[Slot].item = item;
  139.             HeldItems[Slot].isfull = true;
  140.             //make child slots full
  141.             for (int i = 0; i < item->SlotSizeX; i++) {
  142.                 HeldItems[Slot + i].isfull = true;
  143.                 HeldItems[Slot + i].parent = Slot;
  144.                 for (int y = 0; y < item->SlotSizeY; y++) {
  145.                     HeldItems[Slot + i + y * 7].isfull = true;
  146.                     HeldItems[Slot + i + y * 7].parent = Slot;
  147.                 }
  148.             }
  149.             return true;
  150.         }
  151.     }
  152.     return false;
  153. }
  154.  
  155. void  UComplexInventory::RemoveItemFromSlot(AItem* item, int Slot)
  156. {
  157.     if (item) {
  158.         HeldItems[Slot].item = NULL;
  159.         HeldItems[Slot].isfull = false;
  160.         //make child slots empty
  161.         for (int i = 0; i < item->SlotSizeX; i++) {
  162.             HeldItems[Slot + i].isfull = false;
  163.             HeldItems[Slot + i].parent = Slot + i;
  164.             for (int y = 0; y < item->SlotSizeY; y++) {
  165.                 HeldItems[Slot + i + (y * 7)].isfull = false;
  166.                 HeldItems[Slot + i + (y * 7)].parent = Slot + y * 7;
  167.             }
  168.         }
  169.     }
  170. }
  171.  
  172. void  UComplexInventory::RemoveStackFromSlot(AItem* item, int Slot, int ammount)
  173. {
  174.     if (item) {
  175.         HeldItems[Slot].item->Stack -= ammount;
  176.         if (HeldItems[Slot].item->Stack <= 0) {
  177.             AItem* clip = HeldItems[Slot].item;
  178.             RemoveItemFromSlot(clip, Slot); //destroy the clip
  179.             clip->Destroy();
  180.         }
  181.     }
  182. }
  183.  
  184. bool  UComplexInventory::AddStacktoSlot(AItem* item, int Slot)
  185. {
  186.     if (item) {
  187.         if (DoesSlotFit(item, Slot, -1)) {
  188.             if (HeldItems[Slot].item) { // there is already an item in this stack
  189.                 if (HeldItems[Slot].item->Stack + 1 <= item->MaxStackSize) {
  190.                     HeldItems[Slot].item->Stack++;
  191.                 }
  192.             }
  193.             else { // i am placing this actor into a new slot, so a new world actor needs to be spawned
  194.                 FActorSpawnParameters SpawnInfo;
  195.                 SpawnInfo.bNoFail = true;
  196.                 FRotator myRot(0, 0, 0);
  197.                 FVector myLoc(0, 0, 0);
  198.                 AItem* WorldInstance = GetWorld()->SpawnActor<AItem>(item->GetClass(), myLoc, myRot, SpawnInfo);
  199.                 WorldInstance->CanPickup = false;
  200.                 WorldInstance->AttachToComponent(Mesh1p, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
  201.                 WorldInstance->SetActorHiddenInGame(true);
  202.                 WorldInstance->SetOwner(this->GetOwner());
  203.                 HeldItems[Slot].item = WorldInstance;
  204.             }
  205.  
  206.             HeldItems[Slot].isfull = true;
  207.             //make child slots full
  208.             for (int i = 0; i < item->SlotSizeX; i++) {
  209.                 HeldItems[Slot + i].isfull = true;
  210.                 HeldItems[Slot + i].parent = Slot;
  211.                 for (int y = 0; y < item->SlotSizeY; y++) {
  212.                     HeldItems[Slot + i + y * 7].isfull = true;
  213.                     HeldItems[Slot + i + y * 7].parent = Slot;
  214.                 }
  215.             }
  216.             return true;
  217.         }
  218.     }
  219.     return false;
  220. }
  221.  
  222. // Called every frame
  223. void UComplexInventory::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  224. {
  225.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment