Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwp committed Jun 4, 2020
1 parent bad2bc9 commit 3d03f2d
Show file tree
Hide file tree
Showing 23 changed files with 306 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ add_executable(${APP} ${APP_SOURCES}
app/view/widgets/ActionWidget.h
app/view/board/SlotView.cpp
app/view/board/SlotView.h
)
app/view/board/CardDeckView.cpp app/view/board/CardDeckView.h)

# ---------------
# Link
Expand Down
9 changes: 8 additions & 1 deletion app/controller/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
Scripting::Scripting() {
host_ = make_unique<LuaHost>();
tick_handles_ = make_unique<vector<luabridge::LuaRef>>();
init_handles_ = make_unique<vector<luabridge::LuaRef>>();

Card::luaRegister(host_->ns());
GameBoard::luaRegister(host_->ns());
GameBoardView::luaRegister(host_->ns());
StackWidget::luaRegister(host_->ns());
}

void Scripting::doScripts(const string &base_path) {
Expand All @@ -38,9 +41,13 @@ void Scripting::doModule(const string &path, const string &name) {
auto descr_table = host_->getGlobal(name);

auto tick_handle = descr_table["onTick"];
auto key_handle = descr_table["onKeyPress"];
auto init_handle = descr_table["onInit"];

if (!tick_handle.isNil()) {
tick_handles_->emplace_back(tick_handle);
}

if (!init_handle.isNil()) {
init_handles_->emplace_back(init_handle);
}
}
7 changes: 7 additions & 0 deletions app/controller/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class Scripting {
private:
unique_ptr<LuaHost> host_;
unique_ptr<vector<luabridge::LuaRef>> init_handles_;
unique_ptr<vector<luabridge::LuaRef>> tick_handles_;

void doModule(const string &path, const string &name);
Expand All @@ -24,6 +25,12 @@ class Scripting {

void doScripts(const string &base_path);

void onInit() {
for (auto &h: *init_handles_) {
host_->doFunction(h);
}
}

void onTick(float dt) {
for (auto &h : *tick_handles_) {
host_->doFunction(h, dt);
Expand Down
5 changes: 4 additions & 1 deletion app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main() {
float hand_scale = 500.f;
hand_view = gui_scene->addChild(GameBoardView());
hand_view->addSlot("corp_hand", glm::vec3(0.f), glm::vec4(100.f, 50.f, 700.f, 50.f) * (1.f / hand_scale));
hand_view->rotation = glm::rotate(hand_view->rotation, glm::vec3((float)-M_PI_2, 0.f, 0.f));
hand_view->rotation = glm::rotate(hand_view->rotation, glm::vec3((float)-M_PI_2, (float)0.1f, 0.f));
hand_view->scale = glm::vec3(hand_scale);

auto gameboard = GameBoard();
Expand All @@ -83,7 +83,10 @@ int main() {

auto scripting = make_unique<Scripting>();
scripting->setGlobal("board", &gameboard);
scripting->setGlobal("hand_view", hand_view.get());
scripting->setGlobal("board_view", board_view.get());
scripting->doScripts("../app/scripts");
scripting->onInit();

while (!glfwWindowShouldClose(window)) {
glClearColor(0.1f, 0.1f, 0.1f, 1.f);
Expand Down
56 changes: 38 additions & 18 deletions app/model/board/GameBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include "GameBoard.h"
#include "../../view/widgets/StackWidget.h"
#include "../../view/board/CardDeckView.h"

GameBoard::GameBoard() {
this->views = make_shared<vector<shared_ptr<GameBoardView>>>();

stack_slots_ = make_unique<std::unordered_map<string, shared_ptr<vector<shared_ptr<Card>>>>>();
single_slots_ = make_unique<std::unordered_map<string, shared_ptr<Card>>>();
GameBoard::GameBoard():
views(make_shared<vector<shared_ptr<GameBoardView>>>()),
cards_(make_unique<std::unordered_map<string, vector<shared_ptr<Card>>>>())
{
}

shared_ptr<GameBoardView> GameBoard::findViewFor(const string &slotid) const {
Expand All @@ -22,6 +22,7 @@ shared_ptr<GameBoardView> GameBoard::findViewFor(const string &slotid) const {
return nullptr;
}

/*
Card* GameBoard::assign(const string& slotid, const Card &card) {
auto card_ptr = make_shared<Card>(card);
single_slots_->insert_or_assign(slotid, card_ptr);
Expand Down Expand Up @@ -53,23 +54,43 @@ void GameBoard::remove(const string& slotid) {
Card *GameBoard::push(const string& slotid, const Card &card) {
auto card_ptr = make_shared<Card>(card);
shared_ptr<CardView> card_view_ptr;
auto stack = (*stack_slots_)[slotid].get();
auto view = findViewFor(slotid);
shared_ptr<StackWidget> stack_widget;
if (view == nullptr) {
return nullptr;
}
if (stack == nullptr) {
auto stack_ptr = make_shared<vector<shared_ptr<Card>>>();
stack = stack_ptr.get();
(*stack_slots_)[slotid] = stack_ptr;
if (auto view = findViewFor(slotid)) {
auto stack_widget = view->addSlotView(slotid, StackWidget());
auto card_view = stack_widget->addChild(CardView::ForCard(card_ptr));
UILayer::registerSceneEntity(card_view);
}
} else if (auto view = findViewFor(slotid)) {
auto widget = view->getSlotView<StackWidget>(slotid);
auto card_view_ptr = widget->addChild(CardView::ForCard(card_ptr));
UILayer::registerSceneEntity(card_view_ptr);
stack_widget = view->addSlotView(slotid, StackWidget());
} else {
stack_widget = view->getSlotView<StackWidget>(slotid);
}
card_view_ptr = stack_widget->addChild(CardView::ForCard(card_ptr));
stack_widget->update();
if (attachments_slot.length() != 0) {
auto x = card_view_ptr->position.x;
auto zmax = card_view_ptr->position.z;
auto zmin = -0.2f;
auto bbox = glm::vec4(x, zmax, x, zmin);
view->addSlot(attachments_slot, glm::vec3(0.f), bbox);
auto attachments_widget = view->addSlotView(attachments_slot, StackWidget());
attachments_widget->orientation = StackWidgetOrientation_Vertical;
attachments_widget->alignment = StackWidgetAlignment_Max;
attachments_widget->addChild(CardView::ForCard(card_ptr));
attachments_widget->addChild(CardView::ForCard(card_ptr));
UILayer::registerSceneEntity(card_view_ptr);
stack->emplace_back(card_ptr);
return card_ptr.get();
}
Expand Down Expand Up @@ -107,14 +128,13 @@ void GameBoard::pull(const string& slotid, int idx) {
}
}
}
*/

void GameBoard::luaRegister(luabridge::Namespace ns) {
ns
.beginClass<GameBoard>("GameBoard")
.addFunction("assign", &GameBoard::assign)
.addFunction("remove", &GameBoard::remove)
.addFunction("push", &GameBoard::push)
.addFunction("pull", &GameBoard::pull)
.addFunction("insertCard", &GameBoard::insert<Card, CardView>)
.addFunction("insertDeck", &GameBoard::insert<CardDeck, CardDeckView>)
.endClass();

}
Expand Down
53 changes: 42 additions & 11 deletions app/model/board/GameBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@

class GameBoard {
private:
unique_ptr<std::unordered_map<string, shared_ptr<vector<shared_ptr<Card>>>>> stack_slots_;
unique_ptr<std::unordered_map<string, shared_ptr<Card>>> single_slots_;
unique_ptr<std::unordered_map<string, vector<shared_ptr<Card>>>> cards_;

static void Copy(GameBoard *a, const GameBoard &b) {
a->views = b.views;
a->stack_slots_ = make_unique<std::unordered_map<string, shared_ptr<vector<shared_ptr<Card>>>>>(*b.stack_slots_);
a->single_slots_ = make_unique<std::unordered_map<string, shared_ptr<Card>>>(*b.single_slots_);
a->cards_ = make_unique<std::unordered_map<string, vector<shared_ptr<Card>>>>(*b.cards_);
}

[[nodiscard]] shared_ptr<GameBoardView> findViewFor(const string& slotid) const;
Expand All @@ -41,20 +39,53 @@ class GameBoard {
}

void addView(GameBoardView &&view) {
views->emplace_back(make_shared<GameBoardView>(move(view)));
views->emplace_back(make_shared<GameBoardView>(move(view)));
}

void addView(const shared_ptr<GameBoardView>& view_ptr) {
views->emplace_back(view_ptr);
}

Card *assign(const string& slotid, const Card &card);
Card *get(const string& slotid);
void remove(const string& slotid);
template <class T, class V>
T *insert(const string &slotid, const T &card, size_t idx = 0) {
auto vec = &(*cards_)[slotid];
auto ptr = make_shared<T>(card);
vec->insert(vec->begin() + idx, ptr);

Card *push(const string& slotid, const Card &card);
Card *find(const string& slotid, int idx);
void pull(const string& slotid, int idx);
if (auto view = findViewFor(slotid)) {
auto stack_widget = view->getSlotView<StackWidget>(slotid);
if (stack_widget == nullptr) {
stack_widget = view->addSlotView(slotid, StackWidget());
}

auto card_view = stack_widget->addChild(V::ForCard(card));
UILayer::registerSceneEntity(card_view);
}

return ptr.get();
}

void erase(const string &slotid, size_t idx = 0) {
auto vec = &(*cards_)[slotid];
vec->erase(vec->begin() + idx);
}

template <class T>
T *emplace_back(const string &slotid, const T &card) {
return this->insert(slotid, card, (*cards_)[slotid].size());
}

template <class T>
T *replace(const string &slotid, const T &card, size_t idx = 0) {
this->erase(slotid, idx);
return this->insert(slotid, card, idx);
}

template <class T>
shared_ptr<T> *get(const string &slotid, int idx = 0) {
auto p = (*cards_)[slotid][idx];
return dynamic_pointer_cast<T>(p);
}

static void luaRegister(luabridge::Namespace);
};
Expand Down
4 changes: 3 additions & 1 deletion app/model/card/Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <engine/Entity.h>
#include <scripting/LuaHost.h>

class Card {
class Item {};

class Card: public Item {
public:
int uid = 0;
int variant = 0;
Expand Down
4 changes: 4 additions & 0 deletions app/model/card/CardDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
//

#include "CardDeck.h"

shared_ptr<Card> CardDeck::topCard() const {
return (*cards_)[0];
}
9 changes: 8 additions & 1 deletion app/model/card/CardDeck.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@


#include <engine/Entity.h>
#include "Card.h"

class CardDeck {
class CardDeck: public Item {
private:
shared_ptr<Entity> entity_;
unique_ptr<vector<shared_ptr<Card>>> cards_;
public:
[[nodiscard]] shared_ptr<Card> topCard() const;

size_t size() { return cards_->size(); }

};


Expand Down
21 changes: 21 additions & 0 deletions app/scripts/game.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
game = {
onInit = function()
local ice_1 = board_view:getSlotStackWidget("corp_remote_1_ice")
ice_1.alignment = 1
ice_1.orientation = 1
ice_1.child_padding = 1.25
ice_1.child_rotation = math.pi / 2
end
}

board:push("corp_hand", Card(200, true))
board:push("corp_hand", Card(201, true))
board:push("corp_hand", Card(202, true))

board:push("corp_remote_1", Card(100, true))

board:push("corp_remote_1_ice", Card(200, false))
board:push("corp_remote_1_ice", Card(200, false))
board:push("corp_remote_1_ice", Card(200, false))
board:assign("corp_hq", Card(101, true))
board:assign("corp_rnd", Card(102, false))
7 changes: 0 additions & 7 deletions app/scripts/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,3 @@ test = {
end
}

board:push("corp_hand", Card(200, true))
board:push("corp_hand", Card(201, true))
board:push("corp_hand", Card(202, true))

board:push("corp_remote", Card(100, true))
board:assign("corp_hq", Card(101, true))
board:assign("corp_rnd", Card(102, false))
49 changes: 49 additions & 0 deletions app/view/board/CardDeckView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by shdwp on 6/4/2020.
//

#include <util/Debug.h>
#include "CardDeckView.h"
#include "CardView.h"

CardDeckView::CardDeckView(shared_ptr<CardDeck> deck, const shared_ptr<Model> &model):
SlotView(model),
deck_(deck)
{
scale = glm::vec3(0.1f, 0.1f, 0.1f);
}

CardDeckView CardDeckView::ForDeck(shared_ptr<CardDeck> deck) {
return CardDeckView(deck, CardView::SharedModel);
}

void CardDeckView::update() {
Entity::update();

scale.y = 0.01f * deck_->size();
}

void CardDeckView::draw(glm::mat4 transform) {
Entity::draw(transform);
uiTransform_ = transform;

if (deck_ != nullptr) {
Debug::Shared->drawText(glm::scale(transform, glm::vec3(10, 100, 10)), format("d{}", deck_->size()));
}
}

glm::vec4 CardDeckView::getArea(const Camera &cam) {
auto a = cam.projection * cam.lookAt() * uiTransform_ * glm::vec4(-0.65f, 0.f, -1.f, 1.f);
auto b = cam.projection * cam.lookAt() * uiTransform_ * glm::vec4(0.65f, 0.f, 1.f, 1.f);

return glm::vec4(
a.x < b.x ? a.x : b.x,
a.y < b.y ? a.y : b.y,
b.x > a.x ? b.x : a.x,
b.y > a.y ? b.y : a.y
);
}

void CardDeckView::clicked(glm::vec3 pos) {

}
Loading

0 comments on commit 3d03f2d

Please sign in to comment.