Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "ComplexInventory.h"
- #include <Engine/World.h>
- #include "Attachment.h"
- #include "FireArm.h"
- // Sets default values for this component's properties
- UComplexInventory::UComplexInventory()
- {
- PrimaryComponentTick.bCanEverTick = true;
- }
- // Called when the game starts
- void UComplexInventory::BeginPlay()
- {
- Super::BeginPlay();
- InitHeldItemsArray();
- }
- void UComplexInventory::InitHeldItemsArray()
- {
- for (int i = 0; i < HeldItems.Num(); i++) {
- HeldItems[i].parent = i;
- }
- }
- bool UComplexInventory::HasClipFor(AFireArm* Firearm)
- {
- for (int i = 0; i < HeldItems.Num(); i++) {
- if (HeldItems[i].item) {
- if (HeldItems[i].item->GetClass() == Firearm->AmmoType) {
- return true;
- }
- }
- }
- return false;
- }
- void UComplexInventory::RemoveClipFor(AFireArm* Firearm)
- {
- for (int i = 0; i < HeldItems.Num(); i++) {
- if (HeldItems[i].item) {
- if (HeldItems[i].item->GetClass() == Firearm->AmmoType) {
- HeldItems[i].item->Stack--;
- if (HeldItems[i].item->Stack <= 0) {
- AItem* clip = HeldItems[i].item;
- RemoveItemFromSlot(clip, i); //destroy the clip
- clip->Destroy();
- }
- return;
- }
- }
- }
- }
- void UComplexInventory::AddItemToInventory(AItem* item)
- {
- int desiredSlot = FindSuitableInventorySlot(item);
- if (desiredSlot != -1) { // there is a valid slot for this item
- AddItemToSlot(item, desiredSlot, -1);
- }
- }
- int UComplexInventory::FindSuitableInventorySlot(AItem* item)
- {
- for (int i = 0; i < HeldItems.Num(); i++) {
- if (DoesSlotFit(item, i, -1)) {
- return(i);
- }
- }
- return -1;
- }
- bool UComplexInventory::DoesSlotFit(AItem* item, int Slot, int ignoreSlot)
- {
- if (item) {
- bool isvalid = true;
- if (HeldItems[Slot].item) {
- if (item->GetClass() == HeldItems[Slot].item->GetClass()) { //this is the same item
- if (item->Stack + HeldItems[Slot].item->Stack <= item->MaxStackSize) {// these two items can be stacked together
- return true;
- }
- }
- }
- for (int x = 0; x < item->SlotSizeX; x++) {
- if (Slot + x > HeldItems.Num() - 1) { //item goes out of bottom of inventory
- return false;
- }
- if (7 - (Slot % 7) < item->SlotSizeX) { // rolls over
- isvalid = false;
- }
- if (HeldItems[Slot + x].isfull) { // overlaps x
- if (HeldItems[Slot].parent != ignoreSlot && Slot != ignoreSlot) {
- isvalid = false;
- }
- }
- for (int y = 0; y < item->SlotSizeY; y++) {
- if (Slot + x + (y * 7) > HeldItems.Num() - 1) { //item goes out of bottom of inventory
- return false;
- }
- if (HeldItems[Slot + x + (y * 7)].isfull) { //overlaps y
- if (HeldItems[Slot + x + (y * 7)].parent != ignoreSlot && Slot != ignoreSlot) {
- isvalid = false;
- }
- }
- }
- }
- if (isvalid) {
- return true;
- }
- }
- return false;
- }
- bool UComplexInventory::AddItemToSlot(AItem* item, int Slot, int ignoreSlot)
- {
- if (item) {
- //check to see if this is an attachable item
- AAttachment* attachment = Cast<AAttachment>(item);
- if (attachment) {
- if (HeldItems[HeldItems[Slot].parent].item) {
- AFireArm* firearm = Cast<AFireArm>(HeldItems[HeldItems[Slot].parent].item);
- if (firearm) {
- firearm->Attachment = attachment->AttachmentType;
- firearm->UpdateAttachment();
- return true;
- }
- }
- }
- if (DoesSlotFit(item, Slot, ignoreSlot)) {
- if (HeldItems[Slot].item) { // there is already an item in this stack so we must be stacking two items together
- item->Stack += HeldItems[Slot].item->Stack;
- HeldItems[Slot].item->Destroy();
- }
- HeldItems[Slot].item = item;
- HeldItems[Slot].isfull = true;
- //make child slots full
- for (int i = 0; i < item->SlotSizeX; i++) {
- HeldItems[Slot + i].isfull = true;
- HeldItems[Slot + i].parent = Slot;
- for (int y = 0; y < item->SlotSizeY; y++) {
- HeldItems[Slot + i + y * 7].isfull = true;
- HeldItems[Slot + i + y * 7].parent = Slot;
- }
- }
- return true;
- }
- }
- return false;
- }
- void UComplexInventory::RemoveItemFromSlot(AItem* item, int Slot)
- {
- if (item) {
- HeldItems[Slot].item = NULL;
- HeldItems[Slot].isfull = false;
- //make child slots empty
- for (int i = 0; i < item->SlotSizeX; i++) {
- HeldItems[Slot + i].isfull = false;
- HeldItems[Slot + i].parent = Slot + i;
- for (int y = 0; y < item->SlotSizeY; y++) {
- HeldItems[Slot + i + (y * 7)].isfull = false;
- HeldItems[Slot + i + (y * 7)].parent = Slot + y * 7;
- }
- }
- }
- }
- void UComplexInventory::RemoveStackFromSlot(AItem* item, int Slot, int ammount)
- {
- if (item) {
- HeldItems[Slot].item->Stack -= ammount;
- if (HeldItems[Slot].item->Stack <= 0) {
- AItem* clip = HeldItems[Slot].item;
- RemoveItemFromSlot(clip, Slot); //destroy the clip
- clip->Destroy();
- }
- }
- }
- bool UComplexInventory::AddStacktoSlot(AItem* item, int Slot)
- {
- if (item) {
- if (DoesSlotFit(item, Slot, -1)) {
- if (HeldItems[Slot].item) { // there is already an item in this stack
- if (HeldItems[Slot].item->Stack + 1 <= item->MaxStackSize) {
- HeldItems[Slot].item->Stack++;
- }
- }
- else { // i am placing this actor into a new slot, so a new world actor needs to be spawned
- FActorSpawnParameters SpawnInfo;
- SpawnInfo.bNoFail = true;
- FRotator myRot(0, 0, 0);
- FVector myLoc(0, 0, 0);
- AItem* WorldInstance = GetWorld()->SpawnActor<AItem>(item->GetClass(), myLoc, myRot, SpawnInfo);
- WorldInstance->CanPickup = false;
- WorldInstance->AttachToComponent(Mesh1p, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
- WorldInstance->SetActorHiddenInGame(true);
- WorldInstance->SetOwner(this->GetOwner());
- HeldItems[Slot].item = WorldInstance;
- }
- HeldItems[Slot].isfull = true;
- //make child slots full
- for (int i = 0; i < item->SlotSizeX; i++) {
- HeldItems[Slot + i].isfull = true;
- HeldItems[Slot + i].parent = Slot;
- for (int y = 0; y < item->SlotSizeY; y++) {
- HeldItems[Slot + i + y * 7].isfull = true;
- HeldItems[Slot + i + y * 7].parent = Slot;
- }
- }
- return true;
- }
- }
- return false;
- }
- // Called every frame
- void UComplexInventory::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- }
Advertisement
Add Comment
Please, Sign In to add comment