menu
This commit is contained in:
parent
25ba63cea9
commit
89b4bce734
8
Ai.cpp
8
Ai.cpp
@ -20,6 +20,11 @@ void PlayerAi::update(Actor* owner) {
|
|||||||
case SDL_EVENT_QUIT:
|
case SDL_EVENT_QUIT:
|
||||||
engine->gameStatus = Engine::QUIT;
|
engine->gameStatus = Engine::QUIT;
|
||||||
break;
|
break;
|
||||||
|
case SDL_EVENT_KEY_DOWN:
|
||||||
|
if (event.key.key == SDLK_ESCAPE) {
|
||||||
|
engine->gameStatus = Engine::PAUSE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -47,6 +52,9 @@ void PlayerAi::update(Actor* owner) {
|
|||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
dx = 1;
|
dx = 1;
|
||||||
break;
|
break;
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
engine->gameStatus = Engine::PAUSE;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
if (event.key.key >= SDLK_A && event.key.key <= SDLK_Z) {
|
if (event.key.key >= SDLK_A && event.key.key <= SDLK_Z) {
|
||||||
handleActionKey(owner, event.key.key);
|
handleActionKey(owner, event.key.key);
|
||||||
|
48
Engine.cpp
48
Engine.cpp
@ -19,7 +19,6 @@ Engine::Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod::
|
|||||||
player = nullptr;
|
player = nullptr;
|
||||||
fovRadius = 10;
|
fovRadius = 10;
|
||||||
computeFov = true;
|
computeFov = true;
|
||||||
gameStatus = STARTUP;
|
|
||||||
mouse.cx = 0;
|
mouse.cx = 0;
|
||||||
mouse.cy = 0;
|
mouse.cy = 0;
|
||||||
mouse.rbutton_pressed = false;
|
mouse.rbutton_pressed = false;
|
||||||
@ -27,7 +26,6 @@ Engine::Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod::
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::init() {
|
void Engine::init() {
|
||||||
gui = new Gui(context, console);
|
|
||||||
player = new Actor(40, 25, "@", "player", TCOD_ColorRGB(255, 255, 255));
|
player = new Actor(40, 25, "@", "player", TCOD_ColorRGB(255, 255, 255));
|
||||||
player->destructible = new PlayerDestructible(30, 2, "player corpse");
|
player->destructible = new PlayerDestructible(30, 2, "player corpse");
|
||||||
player->attacker = new Attacker(5);
|
player->attacker = new Attacker(5);
|
||||||
@ -38,11 +36,17 @@ void Engine::init() {
|
|||||||
map->init(true);
|
map->init(true);
|
||||||
gui->message(TCOD_ColorRGB(150,0,0),
|
gui->message(TCOD_ColorRGB(150,0,0),
|
||||||
"Welcome stranger!\nPrepare to perish in the Tombs of Andrew's Dunegon.");
|
"Welcome stranger!\nPrepare to perish in the Tombs of Andrew's Dunegon.");
|
||||||
|
gameStatus = STARTUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::term() {
|
||||||
|
actors.clearAndDelete();
|
||||||
|
if (map) delete map;
|
||||||
|
gui->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() {
|
||||||
actors.clearAndDelete();
|
term();
|
||||||
delete map;
|
|
||||||
delete gui;
|
delete gui;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,6 +54,7 @@ bool Engine::update() {
|
|||||||
if (gameStatus == STARTUP) map->computeFov();
|
if (gameStatus == STARTUP) map->computeFov();
|
||||||
gameStatus = IDLE;
|
gameStatus = IDLE;
|
||||||
player->update();
|
player->update();
|
||||||
|
|
||||||
if (gameStatus == NEW_TURN) {
|
if (gameStatus == NEW_TURN) {
|
||||||
for (Actor** iterator = actors.begin(); iterator != actors.end();
|
for (Actor** iterator = actors.begin(); iterator != actors.end();
|
||||||
iterator++) {
|
iterator++) {
|
||||||
@ -58,10 +63,15 @@ bool Engine::update() {
|
|||||||
actor->update();
|
actor->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (gameStatus == QUIT) {
|
||||||
if (gameStatus == QUIT) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
else if (gameStatus == PAUSE) {
|
||||||
|
save();
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,8 +208,28 @@ void Engine::save() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::load() {
|
void Engine::load() {
|
||||||
|
gui = new Gui(context, console);
|
||||||
|
gui->menu.clear();
|
||||||
|
gui->menu.addItem(Menu::NEW_GAME, "New game");
|
||||||
if (TCODSystem::fileExists("game.sav")) {
|
if (TCODSystem::fileExists("game.sav")) {
|
||||||
|
engine->gui->menu.addItem(Menu::CONTINUE, "Continue");
|
||||||
|
}
|
||||||
|
engine->gui->menu.addItem(Menu::EXIT, "Exit");
|
||||||
|
Menu::MenuItemCode menuItem = engine->gui->menu.pick(context, console);
|
||||||
|
|
||||||
|
if (menuItem == Menu::EXIT || menuItem == Menu::NONE) {
|
||||||
|
// Exit or window closed
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else if (menuItem == Menu::NEW_GAME) {
|
||||||
|
// New game
|
||||||
|
term();
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
else if (menuItem == Menu::CONTINUE) {
|
||||||
|
// Continue
|
||||||
TCODZip zip;
|
TCODZip zip;
|
||||||
|
term();
|
||||||
zip.loadFromFile("game.sav");
|
zip.loadFromFile("game.sav");
|
||||||
// load the map
|
// load the map
|
||||||
int width = zip.getInt();
|
int width = zip.getInt();
|
||||||
@ -219,10 +249,8 @@ void Engine::load() {
|
|||||||
nbActors--;
|
nbActors--;
|
||||||
}
|
}
|
||||||
// finally the message log
|
// finally the message log
|
||||||
gui = new Gui(context, console);
|
|
||||||
gui->load(zip);
|
gui->load(zip);
|
||||||
}
|
gameStatus = STARTUP;
|
||||||
else {
|
|
||||||
engine->init();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
2
Engine.h
2
Engine.h
@ -13,6 +13,7 @@ public:
|
|||||||
NEW_TURN,
|
NEW_TURN,
|
||||||
VICTORY,
|
VICTORY,
|
||||||
DEFEAT,
|
DEFEAT,
|
||||||
|
PAUSE,
|
||||||
QUIT
|
QUIT
|
||||||
} gameStatus;
|
} gameStatus;
|
||||||
|
|
||||||
@ -28,6 +29,7 @@ public:
|
|||||||
tcod::Console* console;
|
tcod::Console* console;
|
||||||
Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod::Console *console);
|
Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod::Console *console);
|
||||||
void init();
|
void init();
|
||||||
|
void term();
|
||||||
~Engine();
|
~Engine();
|
||||||
bool update();
|
bool update();
|
||||||
void render(bool present = true);
|
void render(bool present = true);
|
||||||
|
73
Gui.cpp
73
Gui.cpp
@ -1,3 +1,4 @@
|
|||||||
|
#include <SDL3/SDL.h>
|
||||||
#include "Gui.h"
|
#include "Gui.h"
|
||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
#include "Actor.h"
|
#include "Actor.h"
|
||||||
@ -14,10 +15,15 @@ Gui::Gui(tcod::Context *ctx, tcod::Console *root) {
|
|||||||
this->root = root;
|
this->root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gui::~Gui() {
|
void Gui::clear() {
|
||||||
log.clearAndDelete();
|
log.clearAndDelete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Gui::~Gui() {
|
||||||
|
delete con.release();
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
void Gui::render() {
|
void Gui::render() {
|
||||||
// clear the GUI console
|
// clear the GUI console
|
||||||
con.clear();
|
con.clear();
|
||||||
@ -136,4 +142,69 @@ void Gui::load(TCODZip& zip) {
|
|||||||
message(col, text);
|
message(col, text);
|
||||||
nbMessages--;
|
nbMessages--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu::~Menu() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::clear() {
|
||||||
|
items.clearAndDelete();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::addItem(MenuItemCode code, std::string label) {
|
||||||
|
MenuItem* item = new MenuItem();
|
||||||
|
item->code = code;
|
||||||
|
item->label = label;
|
||||||
|
items.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
Menu::MenuItemCode Menu::pick(tcod::Context *ctx, tcod::Console *con) {
|
||||||
|
static TCODImage img("menu_background1.png");
|
||||||
|
int selectedItem = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
img.blit2x(*con, 0, 0);
|
||||||
|
int currentItem = 0;
|
||||||
|
for (MenuItem** it = items.begin(); it != items.end(); it++) {
|
||||||
|
if (currentItem == selectedItem) {
|
||||||
|
tcod::print(*con, { 10, 10 + currentItem * 3 }, (*it)->label,TCOD_ColorRGB(255, 100, 255), std::nullopt);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tcod::print(*con, { 10, 10 + currentItem * 3 }, (*it)->label, TCOD_ColorRGB(200, 200, 200), std::nullopt);
|
||||||
|
}
|
||||||
|
currentItem++;
|
||||||
|
}
|
||||||
|
ctx->present(*con);
|
||||||
|
// check key presses
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
|
while (SDL_PollEvent(&event)) {
|
||||||
|
ctx->convert_event_coordinates(event);
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_EVENT_KEY_DOWN:
|
||||||
|
{
|
||||||
|
if (event.key.key == SDLK_UP) {
|
||||||
|
selectedItem--;
|
||||||
|
if (selectedItem < 0) {
|
||||||
|
selectedItem = items.size() - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (event.key.key == SDLK_DOWN) {
|
||||||
|
selectedItem = (selectedItem + 1) % items.size();
|
||||||
|
}
|
||||||
|
else if (event.key.key == SDLK_RETURN) {
|
||||||
|
return items.get(selectedItem)->code;
|
||||||
|
}
|
||||||
|
else if (event.key.key == SDLK_ESCAPE) {
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_EVENT_QUIT:
|
||||||
|
engine->gameStatus = Engine::QUIT;
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
27
Gui.h
27
Gui.h
@ -2,6 +2,27 @@
|
|||||||
|
|
||||||
#include "libtcod.hpp"
|
#include "libtcod.hpp"
|
||||||
#include "Persistance.h"
|
#include "Persistance.h"
|
||||||
|
|
||||||
|
class Menu {
|
||||||
|
public:
|
||||||
|
enum MenuItemCode {
|
||||||
|
NONE,
|
||||||
|
NEW_GAME,
|
||||||
|
CONTINUE,
|
||||||
|
EXIT
|
||||||
|
};
|
||||||
|
~Menu();
|
||||||
|
void clear();
|
||||||
|
void addItem(MenuItemCode code, std::string label);
|
||||||
|
MenuItemCode pick(tcod::Context* ctx, tcod::Console* con);
|
||||||
|
protected:
|
||||||
|
struct MenuItem {
|
||||||
|
MenuItemCode code;
|
||||||
|
std::string label;
|
||||||
|
};
|
||||||
|
TCODList<MenuItem*> items;
|
||||||
|
};
|
||||||
|
|
||||||
class Gui : public Persistent {
|
class Gui : public Persistent {
|
||||||
public:
|
public:
|
||||||
Gui(tcod::Context *ctx, tcod::Console *root);
|
Gui(tcod::Context *ctx, tcod::Console *root);
|
||||||
@ -10,7 +31,8 @@ public:
|
|||||||
void message(const TCOD_ColorRGB& col, const char* text, ...);
|
void message(const TCOD_ColorRGB& col, const char* text, ...);
|
||||||
void load(TCODZip& zip);
|
void load(TCODZip& zip);
|
||||||
void save(TCODZip& zip);
|
void save(TCODZip& zip);
|
||||||
|
void clear();
|
||||||
|
Menu menu;
|
||||||
protected:
|
protected:
|
||||||
tcod::Console con;
|
tcod::Console con;
|
||||||
tcod::Console* root;
|
tcod::Console* root;
|
||||||
@ -26,4 +48,5 @@ protected:
|
|||||||
};
|
};
|
||||||
|
|
||||||
TCODList<Message*> log;
|
TCODList<Message*> log;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BIN
menu_background1.png
Normal file
BIN
menu_background1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
Loading…
x
Reference in New Issue
Block a user