Optimizations for AI and map rendering

This commit is contained in:
Adrian Hedqvist 2017-09-23 22:06:36 +02:00
parent abc1c8fbce
commit a2e3b4efda
10 changed files with 93 additions and 59 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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);
}
}
}

View file

@ -18,8 +18,6 @@ class PlayState : public Gamestate {
Tilemap* tilemap;
Actor * hero;
FieldOfView *fov;
float delay = .5;
double timer;
bool debug;
public:

View file

@ -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;

View file

@ -149,16 +149,22 @@ std::vector<Actor*>* 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);
}
}
}
}

View file

@ -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);

View file

@ -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;
}

View file

@ -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);
};