adg5/Engine.cpp
2025-04-20 17:57:56 +10:00

82 lines
1.9 KiB
C++

#include <SDL3/SDL.h>
#include "libtcod.hpp"
#include "Actor.h"
#include "Map.h"
#include "Engine.h"
#include "Destructible.h"
#include "Attacker.h"
#include "Ai.h"
#include "Gui.h"
Engine::Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod::Console *console) {
this->context = context;
this->console = console;
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
gui = nullptr;
map = nullptr;
player = nullptr;
fovRadius = 10;
computeFov = true;
gameStatus = STARTUP;
}
void Engine::init() {
gui = new Gui(context, console);
player = new Actor(40, 25, "@", "player", TCOD_ColorRGB(255, 255, 255));
player->destructible = new PlayerDestructible(30, 2, "player corpse");
player->attacker = new Attacker(5);
player->ai = new PlayerAi();
actors.push(player);
map = new Map(80, 45);
gui->message(TCOD_ColorRGB(150,0,0),
"Welcome stranger!\nPrepare to perish in the Tombs Andrew's Dunegon Game.");
}
Engine::~Engine() {
actors.clearAndDelete();
delete map;
delete gui;
}
bool Engine::update() {
if (gameStatus == STARTUP) map->computeFov();
gameStatus = IDLE;
player->update();
if (gameStatus == NEW_TURN) {
for (Actor** iterator = actors.begin(); iterator != actors.end();
iterator++) {
Actor* actor = *iterator;
if (actor != player) {
actor->update();
}
}
}
if (gameStatus == QUIT) {
return false;
}
return true;
}
void Engine::render() {
console->clear();
// draw the map
map->render(*console);
// draw the actors
for (Actor** iterator = actors.begin();
iterator != actors.end(); iterator++) {
Actor* actor = *iterator;
if (actor != player && map->isInFov(actor->x, actor->y)) {
actor->render(*console);
}
}
player->render(*console);
gui->render();
context->present(*console);
}
void Engine::sendToBack(Actor* actor) {
actors.remove(actor);
actors.insertBefore(actor, 0);
}