Refactorings, only update a single actor per turn, support multiple player-controlled actors

This commit is contained in:
Adrian Hedqvist 2017-12-30 20:15:39 +01:00
parent 39ad8c3b34
commit 1ace1cf300
20 changed files with 141 additions and 136 deletions

View file

@ -102,6 +102,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\Shader.cpp" />
<ClCompile Include="src\Actor.cpp" />
<ClCompile Include="src\App.cpp" />
<ClCompile Include="src\AttackEnemyNode.cpp" />
@ -176,6 +177,7 @@
<ClInclude Include="libs\kaguya-1.3.2\include\kaguya\utility.hpp" />
<ClInclude Include="libs\kaguya-1.3.2\include\kaguya\utility_cxx03.hpp" />
<ClInclude Include="libs\kaguya-1.3.2\include\kaguya\utility_cxx11.hpp" />
<ClInclude Include="src\Shader.h" />
<ClInclude Include="src\Actor.h" />
<ClInclude Include="src\App.h" />
<ClInclude Include="src\AttackEnemyNode.h" />

View file

@ -138,6 +138,9 @@
<ClCompile Include="src\Rng.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="libs\kaguya-1.3.2\include\kaguya\another_binding_api.hpp">
@ -380,5 +383,8 @@
<ClInclude Include="src\Rng.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Shader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View file

@ -17,7 +17,7 @@ Actor::Actor(Tilemap *map, vec2i pos) : Entity(map, pos) {
void Actor::update() {
if (!alive) return;
if (bt != nullptr) {
if (!player_controlled && bt != nullptr) {
bt->tick(this);
}
if (health < health_max) {
@ -51,7 +51,7 @@ void Actor::attack(Actor *act) {
void Actor::attack(vec2i dpos) {
if (dpos.dist() <= range) {
vec2i pos = get_position();
auto acts = get_map()->get_entities(pos.x + dpos.x, pos.y + dpos.y, 0, ENTITY_ACTOR);
auto acts = get_map()->get_actors(pos.x + dpos.x, pos.y + dpos.y, 0);
for (Entity* ent : acts) {
auto act = (Actor*)ent;
if (act->is_alive() && act->get_actor_faction() != faction) {

View file

@ -35,8 +35,9 @@ protected:
public:
int id;
std::string name;
bool player_controlled;
Actor(Tilemap *map, vec2i pos, std::string datakey);
//Actor(Tilemap *map, vec2i pos, std::string datakey);
bool is_alive(){ return alive; };
void attack(vec2i dpos); // basic melee attack
void attack(Actor* act);

View file

@ -13,11 +13,10 @@ BehaviourTreeStatus AttackEnemyNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
std::vector<Actor*> visibleEnemies;
for (auto ent : actors) {
auto actor = (Actor*)ent;
for (Actor* actor : actors) {
if (actor == tick->target) continue;

View file

@ -18,9 +18,8 @@ BehaviourTreeStatus FleeNode::tick(BTTick * tick) {
std::vector<vec2i> enemyPos;
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
for (auto ent : actors) {
auto actor = (Actor*)ent;
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
for (Actor* actor : actors) {
if (actor->is_type_of(ACT_HERO) != ishero) {
vec2i pos = actor->get_position();
if (line_of_sight(tick->target->get_map(), tick->target->get_position(), pos)) {

View file

@ -13,10 +13,9 @@ BehaviourTreeStatus HealFriendNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
std::vector<Actor*> friends;
for (auto ent : actors) {
auto actor = (Actor*)ent;
for (Actor* actor : actors) {
if (actor == tick->target) continue;
if (actor->is_type_of(ACT_HERO) == ishero && actor->get_health() < actor->get_health_max()) {

View file

@ -11,6 +11,7 @@
Hero::Hero(Tilemap* map, vec2i pos) : Actor(map, pos) {
name = "Hero";
alive = true;
player_controlled = true;
health = 6;
health_max = 6;
strength = 2;

View file

@ -16,9 +16,8 @@ BehaviourTreeStatus IfNotSeeEnemyNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
for (auto ent : actors) {
auto actor = (Actor*)ent;
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
for (Actor* actor : actors) {
if (actor == tick->target) continue;
if (actor->is_type_of(ACT_HERO) != ishero) {

View file

@ -16,9 +16,8 @@ BehaviourTreeStatus IfSeeEnemyNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
for (auto ent : actors) {
auto actor = (Actor*)ent;
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
for (Actor* actor : actors) {
if (actor == tick->target) continue;
if (actor->is_type_of(ACT_HERO) != ishero) {

View file

@ -17,9 +17,8 @@ BehaviourTreeStatus IfSeeFriendNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
for (auto ent : actors) {
auto actor = (Actor*)ent;
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
for (Actor* actor : actors) {
if (actor == tick->target) continue;
if (actor->is_type_of(ACT_HERO) == ishero) {

View file

@ -65,7 +65,7 @@ void maze_fill(Tilemap& map, int x, int y, Rng &rng) {
}
Tilemap generate_dungeon(int width, int height) {
return generate_dungeon(std::random_device()(), width, height);
return generate_dungeon(Rng::get_random_seed(), width, height);
}
Tilemap generate_dungeon(unsigned int seed, int width, int height) {
@ -186,8 +186,8 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height) {
// Clean up dead ends in the maze
std::vector<vec2i> dead_ends;
for (int y = 0; y < map.get_height(); y++) {
for (int x = 0; x < map.get_width(); x++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
std::vector<vec2i> neigh{vec2i(x + 1, y), vec2i(x, y + 1), vec2i(x - 1, y), vec2i(x, y - 1) };
int count = 0;
for (vec2i pos : neigh) {
@ -200,7 +200,7 @@ Tilemap generate_dungeon(unsigned int seed, int width, int height) {
}
}
}
int pass_amount = 100;
int pass_amount = width + height;
for (int pass = 0; pass < pass_amount; pass++) {
if (dead_ends.empty()) break;
std::vector<vec2i> new_dead_ends;

View file

@ -18,7 +18,7 @@
const int mapwidth = 32;
InputAction action;
InputAction player_action;
void PlayState::load() {
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Creating ascii tileset...\n");
@ -63,37 +63,45 @@ void PlayState::load() {
}
void PlayState::new_game() {
action = ACTION_NONE;
player_action = ACTION_NONE;
if (hero != nullptr) {
delete hero;
hero = nullptr;
if (player_actor != nullptr) {
delete player_actor;
player_actor = nullptr;
}
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Creating tilemap...\n");
Rng rng;
tilemap = generate_dungeon(64, 64);
tilemap = generate_dungeon(128, 128);
vec2i heropos;
do {
heropos.x = rng.get_int(1, tilemap.get_width() - 1);
heropos.y = rng.get_int(1, tilemap.get_width() - 1);
} while (tilemap.get_tile(heropos.x, heropos.y) == '#');
hero = new Hero(&tilemap, vec2i(heropos.x,heropos.y));
tilemap.add_entity(hero);
player_actor = new Hero(&tilemap, vec2i(heropos.x,heropos.y));
tilemap.add_actor(player_actor);
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Done.\n");
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Calculating initial FOV...\n");
fov = FieldOfView(&tilemap);
fov.calc(hero->get_position(), 6);
fov.calc(player_actor->get_position(), 6);
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Done.\n");
current_entity_index = 0;
Actor* actor = tilemap.get_actor_list()->at(current_entity_index);
is_player_turn = actor->player_controlled;
if (is_player_turn) camera_pos = actor->get_position();
}
Gamestate *PlayState::update(double delta) {
if (action != ACTION_NONE) {
if (!is_player_turn || player_action != ACTION_NONE) {
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Starting turn...\n");
if (hero && hero->is_alive()) {
std::vector<Actor*>* actors = tilemap.get_actor_list();
Actor* actor = actors->at(current_entity_index);
if (is_player_turn && actor->is_alive()) {
vec2i dir;
switch (action) {
switch (player_action) {
case ACTION_MOVE_NORTH: dir = {0, -1}; break;
case ACTION_MOVE_NORTHWEST: dir = {-1, -1}; break;
case ACTION_MOVE_NORTHEAST: dir = {1, -1}; break;
@ -103,59 +111,44 @@ Gamestate *PlayState::update(double delta) {
case ACTION_MOVE_SOUTHWEST: dir = {-1, 1}; break;
case ACTION_MOVE_SOUTHEAST: dir = {1, 1}; break;
case ACTION_WAIT: dir = {0, 0}; break;
default: action = ACTION_NONE; SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Turn aborted: no player action.\n"); return nullptr; // abort turn
default: player_action = ACTION_NONE; SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Turn aborted: no player action.\n"); return nullptr; // abort turn
}
if (dir != vec2i(0,0)) {
if (!hero->move(dir.x, dir.y)) {
vec2i heropos = hero->get_position();
auto acts = tilemap.get_entities(heropos.x + dir.x, heropos.y + dir.y, 0, ENTITY_ACTOR);
if (!actor->move(dir.x, dir.y)) {
vec2i heropos = actor->get_position();
auto acts = tilemap.get_actors(heropos.x + dir.x, heropos.y + dir.y, 0);
if(acts.empty()) {
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Turn aborted: invalid player action.\n");
action = ACTION_NONE;
player_action = ACTION_NONE;
return nullptr; // unable to move and nothing to attack == abort turn
}
for (auto ent : acts) {
auto act = (Actor*)ent;
if (act->is_alive() && act->get_actor_faction() != hero->get_actor_faction()) {
hero->attack(act);
if (act->is_alive() && act->get_actor_faction() != actor->get_actor_faction()) {
actor->attack(act);
break;
}
}
}
}
hero->update();
fov.calc(hero->get_position(), 6);
fov.calc(actor->get_position(), 6);
}
actor->update();
auto actors = tilemap.get_entity_list();
for (Entity* var : *actors) {
if (var == hero) continue;
if (var->entity_type() == ENTITY_ACTOR) {
((Actor*)var)->update();
}
player_action = ACTION_NONE;
current_entity_index = (current_entity_index + 1) % actors->size(); // Increase the current actor index
Actor* next = actors->at(current_entity_index);
is_player_turn = next->player_controlled; // Check if next actor is player controlled
if (is_player_turn) {
camera_pos = next->get_position();
player_actor = next;
}
/* // We got enough memory, we can leave the corpses on the field.
unsigned int actor_size = actors->size();
for (unsigned int i = actor_size - 1; i <= actor_size; i--) { // Woo unsigned int underflow abuse!
if (!actors->at(i)->is_alive()) {
if (actors->at(i) == hero) {
hero = nullptr;
}
delete actors->at(i);
actors->erase(actors->begin() + i);
}
}
*/
action = ACTION_NONE;
SDL_LogVerbose(SDL_LOG_CATEGORY_SYSTEM, "Turn finished.\n");
}
return nullptr;
}
bool debug_actors = false;
bool debug_settings = false;
bool debug_disable_fov = false;
void PlayState::draw(double delta) {
if (debug) {
{
@ -186,7 +179,7 @@ void PlayState::draw(double delta) {
if (debug_actors) {
ImGui::Begin("Actors", &debug_actors);
auto actors = tilemap.get_entity_list();
auto actors = tilemap.get_actor_list();
const char* headers[] {
"id", "name", "health", "strength"
};
@ -207,35 +200,31 @@ void PlayState::draw(double delta) {
}
}
vec2i asciisize = {
const vec2i asciisize = {
ascii->get_tile_width(),
ascii->get_tile_height(),
};
vec2i tilesize = {
const vec2i tilesize = {
app->renderer->get_renderer_width() / ascii->get_tile_width(),
app->renderer->get_renderer_height() / ascii->get_tile_height(),
};
vec2i margin = {
const 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),
const vec2i offset = {
(tilesize.x/2-camera_pos.x),
(tilesize.y/2- camera_pos.y),
};
tilemap.draw(app->renderer, ascii, margin.x, margin.y, -offset.x, -offset.y, tilesize.x, tilesize.y, debug_disable_fov ? nullptr : &fov);
auto entities = tilemap.get_entity_list();
auto entities = tilemap.get_actor_list();
Color black = Color(0, 0, 0, 1);
const Color black = Color(0, 0, 0, 1);
// Draw dead actors
for (Entity* var : *entities) {
if (var->entity_type() == ENTITY_ACTOR && ((Actor*)var)->is_alive()) continue;
for (Actor* var : *entities) {
if (var->is_alive()) continue;
vec2i pos = var->get_position();
if (debug_disable_fov || fov.can_see(pos)) {
@ -247,22 +236,20 @@ void PlayState::draw(double delta) {
}
// Draw the rest of the entities
for (Entity* var : *entities) {
if (var->entity_type() == ENTITY_ACTOR && !((Actor*)var)->is_alive()) continue;
for (Actor* var : *entities) {
if (!var->is_alive()) continue;
vec2i pos = var->get_position();
if (debug_disable_fov || fov.can_see(pos)) {
int sprite = var->get_sprite_id();
Color fg = var->get_sprite_color();
app->renderer->draw_sprite(ascii->get_sprite(sprite), fg, black, margin.x + (offset.x + pos.x) * asciisize.x, margin.y + (offset.y + pos.y) * asciisize.y);
}
}
if (hero != nullptr) {
if (player_actor != nullptr) {
Color fg = Color(1, 0.01, 0.01, 1);
for (int i = 0; i < hero->get_health(); i++) {
for (int i = 0; i < player_actor->get_health(); i++) {
app->renderer->draw_sprite(ascii->get_sprite(3), fg, black, (i+1) * asciisize.x, asciisize.y);
}
}
@ -278,7 +265,7 @@ void PlayState::inputevent(InputEvent *event) {
case ACTION_RESET: new_game(); break;
case ACTION_ESCAPE_MENU: break; // TODO
case ACTION_NONE: break;
default: action = event->action; break;
default: player_action = event->action; break;
}
}
}

View file

@ -1,10 +1,4 @@
//
// Created by Adrian on 2017-09-21.
//
#ifndef DUNGEON_PLAYSTATE_H
#define DUNGEON_PLAYSTATE_H
#pragma once
#include "Gamestate.h"
#include "Tilemap.h"
@ -16,10 +10,18 @@ class Actor;
class PlayState : public Gamestate {
Tileset* ascii;
Tilemap tilemap;
Actor * hero;
Actor * player_actor;
FieldOfView fov;
unsigned int current_entity_index;
bool is_player_turn;
vec2i camera_pos;
bool debug;
bool debug_actors = false;
bool debug_settings = false;
bool debug_disable_fov = false;
public:
void new_game();
void load() override;
@ -28,6 +30,3 @@ public:
void quit() override;
void inputevent(InputEvent* event) override;
};
#endif //DUNGEON_PLAYSTATE_H

View file

@ -14,10 +14,9 @@ BehaviourTreeStatus RangedAttackNode::tick(BTTick * tick) {
bool ishero = tick->target->is_type_of(ACT_HERO);
vec2i targetpos = tick->target->get_position();
auto actors = tick->target->get_map()->get_entities(targetpos.x, targetpos.y, 6, ENTITY_ACTOR);
auto actors = tick->target->get_map()->get_actors(targetpos.x, targetpos.y, 6);
std::vector<Actor*> enemies;
for (auto ent : actors) {
auto actor = (Actor*)ent;
for (Actor* actor : actors) {
if (actor == tick->target) continue;
if (actor->is_type_of(ACT_HERO) != ishero) {

View file

@ -40,7 +40,7 @@ GLuint LoadShader(const char* path, GLenum shadertype) {
}
GLboolean result = GL_FALSE;
int infologlength;
int infologlength = 0;
SDL_Log("Compiling shader: %s\n", path);
char const * source = shadersource.c_str();
@ -66,7 +66,7 @@ GLuint CreateShaderProgram(GLuint vertshader, GLuint fragshader) {
glLinkProgram(programId);
GLboolean result = GL_FALSE;
int infologlength;
int infologlength = 0;
glGetShaderiv(programId, GL_LINK_STATUS, &result);
glGetShaderiv(programId, GL_INFO_LOG_LENGTH, &infologlength);

View file

@ -2,9 +2,14 @@
#include <random>
#include <chrono>
std::random_device rd;
unsigned int Rng::get_random_seed() {
return rd();
}
Rng::Rng() {
std::random_device rd;
Rng(rd(), 0);
Rng(get_random_seed(), 0);
}
@ -27,3 +32,12 @@ int Rng::get_int(int min, int max) {
step++;
return std::uniform_int_distribution<int>{min, max}(mte);
}
float Rng::get_float(float max) {
return get_float(0, max);
}
float Rng::get_float(float min, float max) {
step++;
return std::uniform_real_distribution<float>{min, max}(mte);
}

View file

@ -7,11 +7,15 @@ class Rng {
unsigned int step = 0;
std::mt19937 mte;
public:
static unsigned int get_random_seed();
Rng();
Rng(unsigned int seed, unsigned int step = 0);
~Rng();
int get_int(int max);
int get_int(int max = 100);
int get_int(int min, int max);
float get_float(float max = 1);
float get_float(float min, float max);
};

View file

@ -17,7 +17,7 @@ Tilemap::Tilemap(int width, int height) {
}
Tilemap::~Tilemap() {
for (auto var : entities) {
for (auto var : actors) {
delete var;
}
}
@ -68,7 +68,7 @@ bool Tilemap::is_blocked(int x, int y) {
if (tilemap[get_index(x, y)] == '#') { // TODO: Replace hardcoded tiles
return true;
}
for (Entity* var : entities) {
for (Entity* var : actors) {
vec2i pos = var->get_position();
if (var->has_collision() && pos == vec2i(x, y))
return true;
@ -78,19 +78,19 @@ bool Tilemap::is_blocked(int x, int y) {
return true;
}
void Tilemap::add_entity(Entity *actor) {
for (Entity* var : entities) {
void Tilemap::add_actor(Actor *actor) {
for (Actor* var : actors) {
if (var == actor) {
return;
}
}
entities.push_back(actor);
actors.push_back(actor);
}
void Tilemap::remove_entity(Entity * actor) {
for (auto it = entities.begin(); it != entities.end(); it++) {
void Tilemap::remove_actor(Actor * actor) {
for (auto it = actors.begin(); it != actors.end(); it++) {
if ((*it) == actor) {
entities.erase(it);
actors.erase(it);
return;
}
}
@ -98,7 +98,7 @@ void Tilemap::remove_entity(Entity * actor) {
Entity * Tilemap::get_entity(int x, int y, EntityTypes type) {
vec2i pos = { x,y };
for (Entity* ent : entities) {
for (Entity* ent : actors) {
if (ent->entity_type() == type) {
vec2i apos = ent->get_position();
if (apos == pos) {
@ -109,25 +109,23 @@ Entity * Tilemap::get_entity(int x, int y, EntityTypes type) {
return nullptr;
}
std::vector<Entity*> Tilemap::get_entities(int x, int y, int range, EntityTypes type) {
std::vector<Entity*> found;
std::vector<Actor*> Tilemap::get_actors(int x, int y, int range) {
std::vector<Actor*> found;
std::vector<vec2i> neigh = get_neighbours(x, y, range);
for (Entity* ent : entities) {
for (Actor* ent : actors) {
for (vec2i pos : neigh) {
if (ent->entity_type() == type) {
vec2i apos = ent->get_position();
if (apos == pos) {
found.emplace_back(ent);
break;
}
vec2i apos = ent->get_position();
if (apos == pos) {
found.emplace_back(ent);
break;
}
}
}
return found;
}
std::vector<Entity*>* Tilemap::get_entity_list() {
return &entities;
std::vector<Actor*>* Tilemap::get_actor_list() {
return &actors;
}
void Tilemap::draw(Renderer *renderer, Tileset* tileset, int x, int y, int tx, int ty, int tw, int th, FieldOfView* view) {

View file

@ -2,7 +2,7 @@
#include <array>
#include <vector>
#include "Tileset.h"
#include "Entity.h"
#include "Actor.h"
struct vec2i;
class Renderer;
@ -10,7 +10,7 @@ class FieldOfView;
class Tilemap {
std::vector<unsigned int> tilemap;
std::vector<Entity*> entities;
std::vector<Actor*> actors;
int width;
int height;
public:
@ -28,13 +28,13 @@ public:
void draw(Renderer *renderer, Tileset *tileset, int x, int y, int tx, int ty, int tw, int th, FieldOfView* view);
void add_entity(Entity *actor);
void remove_entity(Entity *actor);
void add_actor(Actor *actor);
void remove_actor(Actor *actor);
void debug_print();
Entity* get_entity(int x, int y, EntityTypes type);
std::vector<Entity*> get_entities(int x, int y, int range, EntityTypes type);
std::vector<Entity*>* get_entity_list();
std::vector<Actor*> get_actors(int x, int y, int range);
std::vector<Actor*>* get_actor_list();
};