Silly Adventure Point&Click games maker (or supposed to be)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 lines
4.5 KiB

#include "game.h"
#include "features/gamefeatures.h"
#include "features/qw_levelbuilder.h"
#include "features/qw_statemachine.h"
Game::Game()
{
game_features = std::make_unique<GameFeatures>(this);
auto &[view, scene, sound_player, inventory, builder, text_dlg, widget_dlg] = *game_features;
builder->setGameFeatures(game_features);
// Connecting all the UI controls to their features
for (auto &widg : scene->inventoryWidgets())
widg->onConnect(game_features);
connect(scene, SIGNAL(signalClickInventory()), inventory, SLOT(onClicked()));
view->setFixedSize(static_cast<int>(scene->sceneRect().width() ),
static_cast<int>(scene->sceneRect().height()));
builder->initSaveProfile("test", "test");
builder->initLevel(QWLevelBuilder::strInitLevel());
buildStateMachine();
buildHotKeys();
}
Game::~Game() = default;
void Game::buildStateMachine()
{
auto *view = game_features->ptr_view;
auto *scene = game_features->ptr_scene;
QWStateMachine *state_machine = new QWStateMachine(view);
// Connecting UI elements to global state machine
for (auto &widg : scene->inventoryWidgets())
widg->onBuildingStateMachine(state_machine, game_features);
QState *state_menu = new QState;
state_machine->registerState("state_menu", state_menu);
connect(state_menu, SIGNAL(entered()), game_features->ptr_scene, SLOT(onEntryMenu()));
const auto &state_gameplay = state_machine->stateByKey("state_gameplay");
QSignalTransition *enter_gameplay_transition = new QSignalTransition(scene, SIGNAL(signalLeaveMenu()));
enter_gameplay_transition->setTargetState(state_gameplay);
state_menu->addTransition(enter_gameplay_transition);
QSignalTransition *leave_gameplay_transition = new QSignalTransition(scene, SIGNAL(signalEnterMenu()));
leave_gameplay_transition->setTargetState(state_menu);
state_gameplay->addTransition(leave_gameplay_transition);
QState *state_examination = new QState;
state_machine->registerState("state_examination", state_examination);
QSignalTransition *enter_examination_transition = new QSignalTransition(scene, SIGNAL(signalEnterExamination()));
enter_examination_transition->setTargetState(state_examination);
state_gameplay->addTransition(enter_examination_transition);
QSignalTransition *leave_examination_transition = new QSignalTransition(scene, SIGNAL(signalLeaveExamination()));
leave_examination_transition->setTargetState(state_gameplay);
state_examination->addTransition(leave_examination_transition);
QState *state_widget_dialogue = new QState;
state_machine->registerState("state_widget_dialogue", state_widget_dialogue);
connect(state_widget_dialogue, SIGNAL(entered()), game_features->ptr_scene, SLOT(onEntryDialogue()));
QSignalTransition *enter_widget_dialogue_transition = new QSignalTransition(game_features->ptr_widget_dlg, SIGNAL(onEntryDialogueTransition()));
enter_widget_dialogue_transition->setTargetState(state_widget_dialogue);
state_menu->addTransition(enter_widget_dialogue_transition);
state_gameplay->addTransition(enter_widget_dialogue_transition);
QSignalTransition *leave_widget_dialogue_transition = new QSignalTransition(game_features->ptr_widget_dlg, SIGNAL(onLeaveDialogueTransition()));
leave_widget_dialogue_transition->setTargetState(state_gameplay);
state_widget_dialogue->addTransition(leave_widget_dialogue_transition);
state_machine->addState(state_menu);
state_machine->addState(state_widget_dialogue);
state_machine->addState(state_examination);
state_machine->setInitialState(state_gameplay);
state_machine->start();
}
void Game::buildHotKeys()
{
auto *builder = game_features->ptr_builder;
auto *view = game_features->ptr_view;
/* For quick exit */
auto quit_action = new QAction(tr("E&xit"), this);
quit_action->setShortcut(tr("Ctrl+Q"));
connect(quit_action, SIGNAL(triggered()), qApp, SLOT(quit()));
view->addAction(quit_action);
/* For quick save */
auto save_action = new QAction(tr("S&ave"), this);
save_action->setShortcut(tr("Ctrl+S"));
connect(save_action, SIGNAL(triggered()), builder, SLOT(saveGame()));
view->addAction(save_action);
/* For quick load */
auto load_action = new QAction(tr("L&oad"), this);
load_action->setShortcut(tr("Ctrl+L"));
connect(load_action, SIGNAL(triggered()), builder, SLOT(loadGame()));
view->addAction(load_action);
}
void Game::start()
{
game_features->ptr_view->show();
}