2017-09-17 13:43:13 +02:00
|
|
|
#include "Tilemap.h"
|
|
|
|
#include "vec2i.h"
|
|
|
|
#include "Actor.h"
|
2017-09-21 22:17:06 +02:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include "Tileset.h"
|
|
|
|
#include "FieldOfView.h"
|
2017-12-27 17:50:44 +01:00
|
|
|
#include <SDL2/SDL.h>
|
2017-09-17 13:43:13 +02:00
|
|
|
|
|
|
|
int Tilemap::GetIndex(int x, int y)
|
|
|
|
{
|
|
|
|
return y * width + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tilemap::Tilemap(int width, int height)
|
|
|
|
{
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
2017-12-27 14:35:45 +01:00
|
|
|
tilemap = std::vector<unsigned int>(width*height, 0);
|
2017-09-17 13:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Tilemap::~Tilemap()
|
|
|
|
{
|
2017-09-26 15:49:11 +02:00
|
|
|
for (auto var : entities) {
|
2017-09-17 13:43:13 +02:00
|
|
|
delete var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tilemap::GetWidth()
|
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tilemap::GetHeight()
|
|
|
|
{
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tilemap::IsInsideBounds(int x, int y)
|
|
|
|
{
|
|
|
|
return x >= 0 && x < width && y >= 0 && y < height;
|
|
|
|
}
|
|
|
|
|
2017-10-18 12:22:28 +02:00
|
|
|
std::vector<vec2i> Tilemap::get_neighbours(int x, int y, int range)
|
2017-09-17 13:43:13 +02:00
|
|
|
{
|
|
|
|
std::vector<vec2i> neigh;
|
|
|
|
if (range == 0)
|
|
|
|
{
|
2017-09-26 15:49:11 +02:00
|
|
|
neigh.emplace_back(x,y);
|
2017-09-17 13:43:13 +02:00
|
|
|
return neigh;
|
|
|
|
}
|
|
|
|
for (int dx = -range; dx <= range; dx++)
|
|
|
|
{
|
|
|
|
for (int dy = -range; dy <= range; dy++)
|
|
|
|
{
|
|
|
|
if ((dx != 0 || dy != 0) && IsInsideBounds(x + dx, y + dy))
|
|
|
|
{
|
2017-09-26 15:49:11 +02:00
|
|
|
neigh.emplace_back(x+dx,y+dy);
|
2017-09-17 13:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return neigh;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
void Tilemap::set_tile(int x, int y, unsigned int tile)
|
2017-09-17 13:43:13 +02:00
|
|
|
{
|
|
|
|
if (IsInsideBounds(x, y))
|
|
|
|
{
|
|
|
|
tilemap[GetIndex(x, y)] = tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 12:22:28 +02:00
|
|
|
int Tilemap::get_tile(int x, int y)
|
2017-09-17 13:43:13 +02:00
|
|
|
{
|
|
|
|
if (IsInsideBounds(x, y))
|
|
|
|
{
|
|
|
|
return tilemap[GetIndex(x, y)];
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tilemap::IsBlocked(int x, int y)
|
|
|
|
{
|
|
|
|
if (IsInsideBounds(x, y))
|
|
|
|
{
|
|
|
|
if (tilemap[GetIndex(x,y)] == '#') { // TODO: Replace hardcoded tiles
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-26 15:49:11 +02:00
|
|
|
for (Entity* var : entities) {
|
2017-09-21 22:44:05 +02:00
|
|
|
vec2i pos = var->get_position();
|
2017-09-26 15:49:11 +02:00
|
|
|
if (var->has_collision() && pos == vec2i(x, y))
|
2017-09-17 13:43:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
void Tilemap::add_entity(Entity *actor) {
|
|
|
|
for (Entity* var : entities) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if (var == actor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 15:49:11 +02:00
|
|
|
entities.push_back(actor);
|
2017-09-17 13:43:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
void Tilemap::remove_entity(Entity * actor) {
|
|
|
|
for (auto it = entities.begin(); it != entities.end(); it++) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if ((*it) == actor) {
|
2017-09-26 15:49:11 +02:00
|
|
|
entities.erase(it);
|
2017-09-17 13:43:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
Entity * Tilemap::get_entity(int x, int y, EntityTypes type) {
|
2017-09-17 13:43:13 +02:00
|
|
|
vec2i pos = { x,y };
|
2017-09-26 15:49:11 +02:00
|
|
|
for (Entity* ent : entities) {
|
|
|
|
if (ent->entity_type() == type) {
|
|
|
|
vec2i apos = ent->get_position();
|
2017-09-17 13:43:13 +02:00
|
|
|
if (apos == pos) {
|
2017-09-26 15:49:11 +02:00
|
|
|
return ent;
|
2017-09-17 13:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
std::vector<Entity*> Tilemap::get_entities(int x, int y, int range, EntityTypes type) {
|
|
|
|
std::vector<Entity*> found;
|
2017-10-18 12:22:28 +02:00
|
|
|
std::vector<vec2i> neigh = get_neighbours(x, y, range);
|
2017-09-26 15:49:11 +02:00
|
|
|
for (Entity* ent : entities) {
|
2017-09-17 20:07:38 +02:00
|
|
|
for (vec2i pos : neigh) {
|
2017-09-26 15:49:11 +02:00
|
|
|
if (ent->entity_type() == type) {
|
|
|
|
vec2i apos = ent->get_position();
|
2017-09-17 13:43:13 +02:00
|
|
|
if (apos == pos) {
|
2017-09-26 15:49:11 +02:00
|
|
|
found.emplace_back(ent);
|
2017-09-17 13:43:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2017-09-26 15:49:11 +02:00
|
|
|
std::vector<Entity*>* Tilemap::get_entity_list() {
|
|
|
|
return &entities;
|
2017-09-17 13:43:13 +02:00
|
|
|
}
|
2017-09-21 22:17:06 +02:00
|
|
|
|
2017-09-23 22:06:36 +02:00
|
|
|
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);
|
2017-10-18 12:22:28 +02:00
|
|
|
renderer->draw_sprite(tileset->get_sprite(get_tile(ax, ay)), x + ix * w, y + iy * h);
|
2017-09-23 22:06:36 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-09-21 22:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tilemap::debug_print() {
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
for (int x = 0; x < width; ++x) {
|
2017-10-18 12:22:28 +02:00
|
|
|
printf("\t%d", get_tile(x, y));
|
2017-09-21 22:17:06 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|