Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwp committed Jun 10, 2020
1 parent dfe3fd8 commit c217142
Show file tree
Hide file tree
Showing 23 changed files with 481 additions and 258 deletions.
5 changes: 5 additions & 0 deletions app/controller/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void Scripting::registerClasses() {
.addFunction("append", &Deck::append)
.addFunction("shuffle", &Deck::shuffle)
.addFunction("remove", &Deck::remove)
.addFunction("erase", &Deck::erase)
.addProperty("size", &Deck::size)
.endClass();
}
Expand Down Expand Up @@ -198,6 +199,10 @@ void Scripting::registerClasses() {
.beginClass<CardSelectWidget>("CardSelectWidget")
.TYPE_PROP(CardSelectWidget)
.addFunction("setDeck", &CardSelectWidget::setDeck)
.addFunction("removeCard", &CardSelectWidget::removeCard)
.addProperty("hidden",
std::function([](const CardSelectWidget *ptr) { return ptr->hidden; }),
std::function([](CardSelectWidget *ptr, bool value) { ptr->hidden = value; }))
.endClass();
}

Expand Down
4 changes: 4 additions & 0 deletions app/controller/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum InteractionEvent {
InteractionEvent_AltClick,
InteractionEvent_Drag,
InteractionEvent_Release,
InteractionEvent_Cancel,
};

typedef enum InteractionEvent interaction_event_t;
Expand Down Expand Up @@ -85,6 +86,7 @@ class Scripting {
case InteractionEvent_AltClick: event_str = "altclick"; break;
case InteractionEvent_Drag: event_str = "drag"; break;
case InteractionEvent_Release: event_str = "release"; break;
case InteractionEvent_Cancel: event_str = "cancel"; break;
default: FAIL("Enum error");
}

Expand All @@ -95,6 +97,8 @@ class Scripting {
host_->doFunction(std::get<1>(h), std::get<0>(h), event_str, deckview);
} else if (auto cardview = dynamic_cast<CardView *>(object.get())) {
host_->doFunction(std::get<1>(h), std::get<0>(h), event_str, cardview);
} else {
host_->doFunction(std::get<1>(h), std::get<0>(h), event_str);
}
}
}
Expand Down
27 changes: 13 additions & 14 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <iostream>
#include <chrono>
#include <thread>

Expand All @@ -8,13 +7,9 @@
#include <scripting/LuaHost.h>
#include <ui/Input.h>
#include <util/Debug.h>
#include <ui/UILayer.h>

#include "controller/Scripting.h"
#include "view/widgets/StackWidget.h"
#include "view/materials/CardMaterial.h"
#include "model/board/GameBoard.h"
#include "view/widgets/ActionWidget.h"
#include "view/board/ZoomCardView.h"
#include "view/widgets/CardSelectWidget.h"

Expand Down Expand Up @@ -119,7 +114,6 @@ int main() {
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

scene->updateHierarchy();
scripting->onTick(glfwGetTime());

{
scene->bind();
Expand Down Expand Up @@ -150,7 +144,11 @@ int main() {
glfwSwapBuffers(window);
glfwPollEvents();

card_select_widget->offset += glm::vec2(0.f, (float)Input::Shared->getScrollY() * 10.f);
scripting->onTick(glfwGetTime());

{
card_select_widget->offset += glm::vec2(0.f, (float) Input::Shared->getScrollY() * 10.f);
}

if (Input::Shared->keyPressed(GLFW_MOUSE_BUTTON_LEFT)) {
shared_ptr<UIInteractable> intr;
Expand All @@ -166,6 +164,10 @@ int main() {
}
}

if (Input::Shared->keyPressed(GLFW_KEY_ESCAPE)) {
scripting->onInteraction<UIInteractable>(InteractionEvent_Cancel);
}

if (Input::Shared->keyPressed(GLFW_KEY_R)) {
// force game restart
break;
Expand All @@ -175,19 +177,16 @@ int main() {
shared_ptr<UIInteractable> intr;
if ((intr = gui_scene->ui_layer->traceInputCursor()) || (intr = scene->ui_layer->traceInputCursor())) {
if (auto card_view = dynamic_pointer_cast<CardView>(intr)) {
gui_card_zoomed_view->setCard(card_view->card, scripting->debugMetadataDescription(card_view->card.get()));
gui_card_zoomed_view->hidden = false;
if (!dynamic_pointer_cast<DeckView>(intr)) {
gui_card_zoomed_view->setCard(card_view->card, scripting->debugMetadataDescription(card_view->card.get()));
gui_card_zoomed_view->hidden = false;
}
}
}
} else {
gui_card_zoomed_view->hidden = true;
}

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwTerminate();
return 0;
}

Input::Shared->reset();
}

Expand Down
2 changes: 1 addition & 1 deletion app/model/board/GameBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class GameBoard {
}

template <class O, class T, class V>
T *replace(const string &slotid, O *from, const T &to) {
T *replace(const string &slotid, O *from, const T &to) {
auto idx = this->erase(slotid, from);
if (idx != -1) {
return this->insert<T, V>(slotid, to, idx);
Expand Down
14 changes: 14 additions & 0 deletions app/model/card/Deck.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@ class Deck: public Item {
insert(card, -1);
}

void moveToTop(const Card &card) {
auto val = card;

}

void remove(int idx) {
cards->erase(cards->begin() + idx);
}

void erase(Card *card) {
for (auto i = begin(*cards); i != end(*cards); i++) {
if (card == i->get()) {
*cards->erase(i);
return;
}
}
}

void shuffle() {
std::random_shuffle(begin(*cards), end(*cards));
}
Expand Down
7 changes: 5 additions & 2 deletions app/scripts/cardspec/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ operations = {
["01060"] = { -- Shipment from MirrorMorph
onPlay = function (meta)
for _ = 0, 2 do
return make_interaction:promptStackSelect(SIDE_CORP, SLOT_CORP_HAND, function (card)
return make_interaction:promptSlotSelect(SIDE_CORP, SLOT_CORP_HAND, 1, function (card)
return make_interaction:promptInstall(SIDE_CORP, card)
end)
end
Expand All @@ -37,7 +37,10 @@ operations = {
onPlay = function (meta)
make_interaction:promptDeckSelect(SIDE_CORP, SLOT_CORP_RND, 5, 5, function (card)
card.faceup = false
board:deckAppend(SIDE_CORP, SLOT_CORP_RND, card)

local deck = board:deckGet(SLOT_CORP_RND, 0)
deck:append(card)

return true
end)
end
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/const.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ SLOT_CORP_ARCHIVES = "corp_archives"
-- SLOT_CORP_ = "corp_"

function isSlotRemote(slot)
return starts_with(slot, "corp_remote_") and not ends_with(slot, "_ice")
return string.starts_with(slot, "corp_remote_") and not string.ends_with(slot, "_ice")
end

function isSlotIce(slot)
return starts_with(slot, "corp_remote_") and ends_with(slot, "_ice")
return string.starts_with(slot, "corp_remote_") and string.ends_with(slot, "_ice")
end

33 changes: 27 additions & 6 deletions app/scripts/controller/0_player_controller.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- @class PlayerController
--- @field handling InteractionPhase
--- @field phase InteractionPhase
PlayerController = class()

--- @param child_storage table
Expand All @@ -8,14 +8,35 @@ function PlayerController:New()
return construct(self)
end

function PlayerController:active() return self.handling ~= nil end
function PlayerController:active() return self.phase ~= nil end

--- @param phase InteractionPhase
function PlayerController:handle(phase)
info("Controller %s handling %s of %s", self, phase.type, phase.side)
info("Controller %s phase %s of %s", self, phase.type, phase.side)
self.phase = phase
end

function PlayerController:handled()
self.handling = nil
--- @param amount number amount of phases to pop
function PlayerController:handled(amount)
amount = amount or 1

if game:popIfTop(self.phase) then
info("Controller %s handled %d phases", self, amount)

self.phase = nil
for _ = 2, amount do
game:popPhase()
end

game:cycle()
else
info("Controller %s tried to handle %d phases but had to delegate to the top", self, amount)
self:delegated()
end

end

function PlayerController:delegated()
self.phase = nil
game:cycle()
end
end
142 changes: 126 additions & 16 deletions app/scripts/controller/human/human_controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,142 @@ HumanController = class(PlayerController)

--- @return HumanController
function HumanController:New()
return construct(self, PlayerController:New())
return construct(self, PlayerController:New(), {
last_update = 0,
alert_until = 0,
})
end

function HumanController:handle(phase)
PlayerController.handle(self, phase)

if phase.type == SelectFromDeckPhase.Type then
--- @type SelectFromDeckPhase
local ph = phase

local deck = board:deckGet(ph.slot, 0)
card_select_widget:setDeck(deck, ph.limit)
card_select_widget.hidden = false
end
end

function HumanController:onTick(dt)
if dt - self.last_update > 1 then
status_label:setText(string.format(
"%s, cl%d, cr%d, s%d, b%d",
self.phase.type,
game:countClicks(SIDE_CORP),
game.corp.credits,
game.corp.score,
game.corp.bad_publicity
))

if self.alert_until > dt then
verbose("Alert expired")
alert_label:setText("")
end

self.last_update = dt
end

local update = true
if Input:keyPressed(61) then
game.corp:alterCredits(1)
elseif Input:keyPressed(45) then
game.corp:alterCredits(-1)
elseif Input:keyPressed(48) then
game:alterClicks(SIDE_CORP, 1)
elseif Input:keyPressed(57) then
game.corp:alterClicks(SIDE_CORP, -1)
else
update = false
end

if update then self.last_update = 0 end
end

--- @param type string
--- @param descr SlotInteractable
function HumanController:onInteraction(type, descr)
self.last_update = 0

if type == "click" and descr.card and isSlotRemote(descr.slot) then
local i = cardspec:interactionFromTable(descr.card.meta)
if i == "score" then
game.corp:actionScore(descr.card, descr.slot)
elseif i == "rez" then
game.corp:actionRez(descr.card, descr.slot)
end
end

if not self:active() then
return
end

if self.handling.type == TurnBaseIntr.Type and self.handling.side == SIDE_CORP then
if descr.slot == "corp_rnd" then
verbose("corp click on draw card")
game.corp:actionDrawCard()
return self:handled()

elseif itr.slot == "corp_hand" then
local intr = cardspec:interactionFromHand(itr.card.meta)
verbose()
if intr == "install" then
self.event = itr
elseif intr == "play" then
game.corp:actionOperation(itr.card, itr.slot)
return self:reset()
local ph = self.phase
if ph.side == SIDE_CORP then
if ph.type == TurnBasePhase.Type then
if type == "click" then
if descr.slot == SLOT_CORP_RND then
info("Corp draw card")
game.corp:actionDrawCard()
return self:handled()

elseif descr.slot == SLOT_CORP_HAND then
local card_play_type = cardspec:interactionFromHand(descr.card.meta)
if card_play_type == "install" then
info("Corp installing %s", descr.card.uid)
game:pushPhase(InstallPhase:New(ph.side, descr.slot, descr.card))
return self:delegated()
elseif card_play_type == "play" and game.corp:actionOperation(descr.card, descr.slot) then
info("Corp played %s", descr.card.uid)
return self:handled()
else
error("Uknown play type %s", card_play_type)
end
end
elseif type == "altclick" then
if isSlotRemote(descr.slot) and game.corp:actionAdvance(descr.card, descr.slot) then
return self:handled()
end
end

elseif ph.type == InstallPhase.Type then
if type == "click" then
if isSlotRemote(descr.slot) then
if game.corp:actionInstallRemote(ph.card, ph.slot, descr.slot) then
return self:handled(2)
else
info("Corp failed to actionInstallRemote")
end
else
info("Invalid slot for install %s", descr.slot)
end
elseif type == "cancel" then
return self:handled()
end
elseif ph.type == SelectFromDeckPhase.Type then
--- @type SelectFromDeckPhase
local sel_ph = ph
local deck = board:deckGet(sel_ph.slot, 0)
if type == "click" and descr.card then
info("Corp selected %d, %d left", descr.card.uid, sel_ph.amount - 1)
if sel_ph.cb(descr.card) then
sel_ph.amount = sel_ph.amount - 1
card_select_widget:removeCard(descr.card)
deck:erase(descr.card)

if sel_ph.amount <= 0 then
info("Corp finished selecting cards")
card_select_widget.hidden = true
return self:handled()
end
end
end
elseif ph.type == SelectFromStackPhase.Type then
--- @yype SelectFromStackPhase
local sel_ph = ph

end
end
end

Loading

0 comments on commit c217142

Please sign in to comment.