Browse Source

started on inventory stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
6cec8cea31

+ 2
- 1
libswan/CMakeLists.txt View File

@@ -23,7 +23,8 @@ install(TARGETS libswan DESTINATION swan/libswan)

add_executable(test_libswan EXCLUDE_FROM_ALL
test/lib/test.cc
test/util.t.cc)
test/util.t.cc
test/ItemStack.t.cc)
target_link_libraries(test_libswan libswan)
target_include_directories(test_libswan
PRIVATE "include/swan")

+ 62
- 0
libswan/include/swan/ItemStack.h View File

@@ -0,0 +1,62 @@
#pragma once

#include <optional>

#include "log.h"

namespace Swan {

class Item;

class ItemStack {
public:
static constexpr int MAX_COUNT = 64;

ItemStack(Item *item, int count = 0): item_(item), count_(count) {

// We don't want a "partially empty" state.
if (item == nullptr || count == 0) {
item = nullptr;
count = 0;
}
}

Item *item() { return item_; }
int count() { return count_; }
bool empty() { return item_ == nullptr; }

// Insert as much of 'st' as possible, returning the leftovers
ItemStack insert(ItemStack st) {

// If this is an empty item stack, just copy over st
if (empty()) {
item_ = st.item_;
count_ = st.count_;
st.item_ = nullptr;
st.count_ = 0;
return st;
}

// If st is a stack of a different kind of item, we don't want it
if (st.item_ != item_)
return st;

// Merge
count_ += st.count_;
if (count_ > MAX_COUNT) {
st.count_ = count_ - MAX_COUNT;
count_ = MAX_COUNT;
} else {
st.count_ = 0;
st.item_ = nullptr;
}

return st;
}

private:
Item *item_;
int count_;
};

}

+ 1
- 0
libswan/include/swan/swan.h View File

@@ -7,6 +7,7 @@
#include <swan/Entity.h>
#include <swan/Game.h>
#include <swan/Item.h>
#include <swan/ItemStack.h>
#include <swan/Mod.h>
#include <swan/PerfCounter.h>
#include <swan/OS.h>

+ 22
- 0
libswan/include/swan/traits/InventoryTrait.h View File

@@ -0,0 +1,22 @@
#pragma once

#include "../Vector2.h"

namespace Swan {

class ItemStack;

namespace InventoryTrait {

class Inventory;
class HasInventory {
public:
virtual Inventory &getInventory() = 0;
};

class Inventory {
public:
};

}
}

+ 62
- 0
libswan/test/ItemStack.t.cc View File

@@ -0,0 +1,62 @@
#include "ItemStack.h"

#include "lib/test.h"

// Most of these tests need two ItemStacks.
// ItemStack requires an Item pointer, and only ever looks at the pointer.
// Therefore, we'll just give the ItemStacks pointers to ints.
static int itint1, itint2;
static Swan::Item *item1 = (Swan::Item *)&itint1;
static Swan::Item *item2 = (Swan::Item *)&itint2;

test("Basic insert") {
Swan::ItemStack s1(item1, 0);
Swan::ItemStack s2(item1, 10);
s2 = s1.insert(s2);

expecteq(s1.count(), 10);
expecteq(s2.count(), 0);
}

test("Insert rejects different items") {
Swan::ItemStack s1(item1, 5);
Swan::ItemStack s2(item2, 10);
Swan::ItemStack ret = s1.insert(s2);

expecteq(s1.count(), 5);
expecteq(ret.count(), 10);
expecteq(ret.count(), s2.count());
expecteq(ret.item(), s2.item());
}

test("Insert never overflows") {
Swan::ItemStack s1(item1, 40);
Swan::ItemStack s2(item1, 40);
s2 = s1.insert(s2);

expecteq(s1.count(), Swan::ItemStack::MAX_COUNT);
expecteq(s2.count(), 80 - Swan::ItemStack::MAX_COUNT);
}


test("When insert empties an ItemStack, it should have its item nulled out") {
Swan::ItemStack s1(item1, 10);
Swan::ItemStack s2(item1, 10);
s2 = s1.insert(s2);

expecteq(s1.count(), 20);
expecteq(s2.count(), 0);
expecteq(s2.item(), nullptr);
expect(s2.empty());
}

test("Insert on an empty item stack") {
Swan::ItemStack s1(nullptr, 0);
Swan::ItemStack s2(item1, 10);
s2 = s1.insert(s2);

expecteq(s1.count(), 10);
expecteq(s1.item(), item1);
expecteq(s2.count(), 0);
expect(s2.empty());
}

+ 1
- 0
libswan/test/lib/test.h View File

@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <iostream>

namespace testlib {


+ 0
- 2
libswan/test/util.t.cc View File

@@ -1,7 +1,5 @@
#include "util.h"

#include <iostream>

#include "lib/test.h"

test("map") {

Loading…
Cancel
Save