diff --git a/src/Actor.cpp b/src/Actor.cpp index c27785d..bd0e47c 100644 --- a/src/Actor.cpp +++ b/src/Actor.cpp @@ -5,6 +5,7 @@ Actor::Actor(Tilemap * map, vec2i pos) { this->map = map; position = pos; + bt = nullptr; } const vec2i Actor::get_position() { @@ -24,7 +25,7 @@ bool Actor::Move(int dx, int dy) { void Actor::update() { if (!alive) return; - if (bt) { + if (bt != nullptr) { bt->tick(this); } if (health < maxhealth) { @@ -40,9 +41,4 @@ void Actor::update() { } } -Actor::~Actor() { - if (bt != nullptr) { - delete bt; - bt = nullptr; - } -} +Actor::~Actor() = default; diff --git a/src/App.cpp b/src/App.cpp index 9d4fe81..e262ed5 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -23,8 +23,8 @@ bool App::init() { Config cfg = Config("dungeon.cfg"); cfg.load(); - int windowWidth = cfg.getInt("ResolutionX", 1280); - int windowHeight = cfg.getInt("ResolutionY", 720); + int windowWidth = cfg.getInt("ResolutionX", 792); + int windowHeight = cfg.getInt("ResolutionY", 600); bool vsync = cfg.getBool("VSync", false); bool wireframe = cfg.getBool("Wireframes", false); diff --git a/src/Goblin.cpp b/src/Goblin.cpp index b0a39eb..6b50bcd 100644 --- a/src/Goblin.cpp +++ b/src/Goblin.cpp @@ -5,22 +5,24 @@ #include "WanderNode.h" #include "RestNode.h" +BehaviourTree* gobtree = nullptr; + Goblin::Goblin(Tilemap* map, vec2i pos) : Actor(map, pos) { alive = true; health = 4; maxhealth = 4; strength = 1; - - BehaviourTreeSelector* root = new BehaviourTreeSelector(nullptr); - bt = new BehaviourTree(root); - { - - - new AttackEnemyNode(root); - new RestNode(root); - new WanderNode(root); + if (gobtree == nullptr) { + auto * root = new BehaviourTreeSelector(nullptr); + gobtree = new BehaviourTree(root); + { + new AttackEnemyNode(root); + new RestNode(root); + new WanderNode(root); + } } + bt = gobtree; } -Goblin::~Goblin() {} +Goblin::~Goblin() = default; diff --git a/src/PlayState.cpp b/src/PlayState.cpp index 761aa2b..7d1fad0 100644 --- a/src/PlayState.cpp +++ b/src/PlayState.cpp @@ -138,7 +138,6 @@ void PlayState::new_game() { } Gamestate *PlayState::update(double delta) { - timer += delta; if (action != ACTION_NONE) { if (hero) { vec2i dir; @@ -184,7 +183,9 @@ Gamestate *PlayState::update(double delta) { actors->erase(actors->begin() + i); } } - fov->calc(hero->get_position(), 6); + if (hero) { + fov->calc(hero->get_position(), 6); + } action = ACTION_NONE; } return nullptr; @@ -214,25 +215,37 @@ void PlayState::draw(double delta) { } ImGui::EndMainMenuBar(); - - ImGui::SliderFloat("turndelay", &delay, .01f, 1); } - vec2i offset; - offset.x = app->renderer->get_renderer_width() / 2; - offset.y = app->renderer->get_renderer_height() / 2; - vec2i heropos = hero->get_position(); - offset.x -= heropos.x * 12; - offset.y -= heropos.y * 12; + vec2i asciisize = { + ascii->get_tile_width(), + ascii->get_tile_height(), + }; + vec2i tilesize = { + app->renderer->get_renderer_width() / ascii->get_tile_width(), + app->renderer->get_renderer_height() / ascii->get_tile_height(), + }; + vec2i margin = { + (app->renderer->get_renderer_width() - tilesize.x * asciisize.x)/2, + (app->renderer->get_renderer_height() - tilesize.y * asciisize.y)/2, + }; + vec2i heropos = {0,0}; + if (hero) { + heropos = hero->get_position(); + } + vec2i offset = { + (tilesize.x/2-heropos.x), + (tilesize.y/2-heropos.y), + }; - tilemap->draw(app->renderer, ascii, offset.x, offset.y, fov); + tilemap->draw(app->renderer, ascii, margin.x, margin.y, -offset.x, -offset.y, tilesize.x, tilesize.y, fov); auto actors = tilemap->get_actor_list(); for (Actor* var : *actors) { vec2i pos = var->get_position(); if (fov == nullptr || fov->can_see(pos)) { app->renderer->set_color(0, 0, 0, 255); - app->renderer->draw_sprite(ascii->get_sprite(219), offset.x + pos.x * 12, offset.y + pos.y * 12); + app->renderer->draw_sprite(ascii->get_sprite(219), margin.x + (offset.x + pos.x) * asciisize.x, margin.y + (offset.y + pos.y) * asciisize.y); int sprite; switch (var->Type()) { @@ -253,16 +266,16 @@ void PlayState::draw(double delta) { sprite = 2; break; } - app->renderer->draw_sprite(ascii->get_sprite(sprite), offset.x + pos.x * 12, offset.y + pos.y * 12); + app->renderer->draw_sprite(ascii->get_sprite(sprite), margin.x + (offset.x + pos.x) * asciisize.x, margin.y + (offset.y + pos.y) * asciisize.y); } } if (hero != nullptr) { app->renderer->set_color(155, 5, 5, 255); for (int i = 0; i < hero->health; i++) { app->renderer->set_color(0, 0, 0, 255); - app->renderer->draw_sprite(ascii->get_sprite(219), i * 12, 0); + app->renderer->draw_sprite(ascii->get_sprite(219), (i+1) * asciisize.x, asciisize.y); app->renderer->set_color(255, 0, 0, 255); - app->renderer->draw_sprite(ascii->get_sprite(3), i * 12, 0); + app->renderer->draw_sprite(ascii->get_sprite(3), (i+1) * asciisize.x, asciisize.y); } } } diff --git a/src/PlayState.h b/src/PlayState.h index 2dd797b..5ad882d 100644 --- a/src/PlayState.h +++ b/src/PlayState.h @@ -18,8 +18,6 @@ class PlayState : public Gamestate { Tilemap* tilemap; Actor * hero; FieldOfView *fov; - float delay = .5; - double timer; bool debug; public: diff --git a/src/Shaman.cpp b/src/Shaman.cpp index 94e6e75..b37bc00 100644 --- a/src/Shaman.cpp +++ b/src/Shaman.cpp @@ -9,26 +9,31 @@ #include "FleeNode.h" +BehaviourTree* shamtree = nullptr; + Shaman::Shaman(Tilemap* map, vec2i pos) : Actor(map, pos) { alive = true; health = 2; maxhealth = 2; strength = 1; - BehaviourTreeSelector* root = new BehaviourTreeSelector(nullptr); - bt = new BehaviourTree(root); - { - IfSeeFriendNode* seefriend = new IfSeeFriendNode(root); - BehaviourTreeSelector* fsel = new BehaviourTreeSelector(seefriend); + if (shamtree == nullptr) { + auto * root = new BehaviourTreeSelector(nullptr); + shamtree = new BehaviourTree(root); { - new HealFriendNode(fsel); - //new RangedAttackNode(fsel); - } + auto * seefriend = new IfSeeFriendNode(root); + auto * fsel = new BehaviourTreeSelector(seefriend); + { + new HealFriendNode(fsel); + //new RangedAttackNode(fsel); + } - new FleeNode(root); - new WanderNode(root); + new FleeNode(root); + new WanderNode(root); + } } + bt = shamtree; } -Shaman::~Shaman() {} +Shaman::~Shaman() = default; diff --git a/src/Tilemap.cpp b/src/Tilemap.cpp index 3cd5a53..b9c5374 100644 --- a/src/Tilemap.cpp +++ b/src/Tilemap.cpp @@ -149,16 +149,22 @@ std::vector* Tilemap::get_actor_list() { return &actors; } -void Tilemap::draw(Renderer *renderer, Tileset* tileset, int ox, int oy, FieldOfView* view) { - for (int x = 0; x < 32; x++) { - for (int y = 0; y < 32; y++) { - if (view == nullptr || view->has_seen({x, y})) { - renderer->set_color(1, 1, 1, 1); - renderer->draw_sprite(tileset->get_sprite(GetTile(x, y)), ox + x * 12, oy + y * 12); +void Tilemap::draw(Renderer *renderer, Tileset* tileset, int x, int y, int tx, int ty, int tw, int th, FieldOfView* view) { + int w = tileset->get_tile_width(); + int h = tileset->get_tile_height(); + for (int ix = 0; ix < tw; ix++) { + for (int iy = 0; iy < th; iy++) { + int ax = tx + ix; + int ay = ty + iy; + if (IsInsideBounds(ax, ay)) { + if (view == nullptr || view->has_seen({ax, ay})) { + renderer->set_color(1, 1, 1, 1); + renderer->draw_sprite(tileset->get_sprite(GetTile(ax, ay)), x + ix * w, y + iy * h); - if (view != nullptr && !view->can_see({x, y})) { - renderer->set_color(0, 0, 0, .6f); - renderer->draw_sprite(tileset->get_sprite(219), ox + x * 12, oy + y * 12); + if (view != nullptr && !view->can_see({ax, ay})) { + renderer->set_color(0, 0, 0, .6f); + renderer->draw_sprite(tileset->get_sprite(219), x + ix * w, y + iy * h); + } } } } diff --git a/src/Tilemap.h b/src/Tilemap.h index 041037b..ffe60cf 100644 --- a/src/Tilemap.h +++ b/src/Tilemap.h @@ -24,7 +24,7 @@ public: int GetTile(int x, int y); bool IsBlocked(int x, int y); // Checks if there is an actor blocking the tile. - void draw(Renderer *renderer, Tileset *tileset, int x, int y, FieldOfView* view); + void draw(Renderer *renderer, Tileset *tileset, int x, int y, int tx, int ty, int tw, int th, FieldOfView* view); void add_actor(Actor *actor); void RemoveActor(Actor* actor); diff --git a/src/Tileset.cpp b/src/Tileset.cpp index 05cc1c4..88c4a99 100644 --- a/src/Tileset.cpp +++ b/src/Tileset.cpp @@ -4,6 +4,8 @@ Tileset::Tileset(Renderer* renderer, std::string imgPath, int imgWidth, int imgHeight, int tileWidth, int tileHeight) { int tilesX = imgWidth / tileWidth; int tilesY = imgHeight / tileHeight; + tile_width = tileWidth; + tile_height = tileHeight; amount = tilesX*tilesY; sprites = new Sprite[amount]; if (renderer->LoadTexture(imgPath) != nullptr) { @@ -22,7 +24,7 @@ Tileset::~Tileset() { delete sprites; } -int Tileset::GetAmount() { +int Tileset::get_amount() { return amount; } @@ -32,3 +34,11 @@ Sprite * Tileset::get_sprite(int tileId) { } return nullptr; } + +int Tileset::get_tile_width() { + return tile_width; +} + +int Tileset::get_tile_height() { + return tile_height; +} diff --git a/src/Tileset.h b/src/Tileset.h index 8ccf7f7..cbdce38 100644 --- a/src/Tileset.h +++ b/src/Tileset.h @@ -8,10 +8,14 @@ struct Sprite; class Tileset { Sprite* sprites; int amount; + int tile_width; + int tile_height; public: Tileset(Renderer* renderer, std::string imgPath, int imgWidth, int imgHeight, int tileWidth, int tileHeight); ~Tileset(); - int GetAmount(); + int get_amount(); + int get_tile_width(); + int get_tile_height(); Sprite* get_sprite(int tileId); };