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"
|
|
|
|
#include <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;
|
|
|
|
tilemap = new unsigned int[width*height]{0};
|
|
|
|
}
|
|
|
|
|
|
|
|
Tilemap::~Tilemap()
|
|
|
|
{
|
|
|
|
delete tilemap;
|
2017-09-17 20:07:38 +02:00
|
|
|
for (auto var : actors) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<vec2i> Tilemap::getNeighbours(int x, int y, int range)
|
|
|
|
{
|
|
|
|
std::vector<vec2i> neigh;
|
|
|
|
if (range == 0)
|
|
|
|
{
|
|
|
|
neigh.push_back({x,y});
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
neigh.push_back({x+dx,y+dy});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return neigh;
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:44:05 +02:00
|
|
|
void Tilemap::set_tile(int x, int y, int tile)
|
2017-09-17 13:43:13 +02:00
|
|
|
{
|
|
|
|
if (IsInsideBounds(x, y))
|
|
|
|
{
|
|
|
|
tilemap[GetIndex(x, y)] = tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tilemap::GetTile(int x, int y)
|
|
|
|
{
|
|
|
|
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-17 20:07:38 +02:00
|
|
|
for (Actor* var : actors) {
|
2017-09-21 22:44:05 +02:00
|
|
|
vec2i pos = var->get_position();
|
2017-09-17 13:43:13 +02:00
|
|
|
if (var->IsAlive() && pos.x == x && pos.y == y) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:44:05 +02:00
|
|
|
void Tilemap::add_actor(Actor *actor) {
|
2017-09-17 20:07:38 +02:00
|
|
|
for (Actor* var : actors) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if (var == actor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actors.push_back(actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tilemap::RemoveActor(Actor * actor) {
|
|
|
|
for (auto it = actors.begin(); it != actors.end(); it++) {
|
|
|
|
if ((*it) == actor) {
|
|
|
|
actors.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Actor * Tilemap::GetActor(int x, int y, Actors type) {
|
|
|
|
vec2i pos = { x,y };
|
2017-09-17 20:07:38 +02:00
|
|
|
for (Actor* act : actors) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if (act->isTypeOf(type)) {
|
2017-09-21 22:44:05 +02:00
|
|
|
vec2i apos = act->get_position();
|
2017-09-17 13:43:13 +02:00
|
|
|
if (apos == pos) {
|
|
|
|
return act;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Actor*> Tilemap::GetActors(int x, int y, int range, Actors type) {
|
|
|
|
std::vector<Actor*> found;
|
|
|
|
std::vector<vec2i> neigh = getNeighbours(x, y, range);
|
2017-09-17 20:07:38 +02:00
|
|
|
for (Actor* act : actors) {
|
|
|
|
for (vec2i pos : neigh) {
|
2017-09-17 13:43:13 +02:00
|
|
|
if (act->isTypeOf(type)) {
|
2017-09-21 22:44:05 +02:00
|
|
|
vec2i apos = act->get_position();
|
2017-09-17 13:43:13 +02:00
|
|
|
if (apos == pos) {
|
|
|
|
found.push_back(act);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2017-09-21 22:44:05 +02:00
|
|
|
std::vector<Actor*>* Tilemap::get_actor_list() {
|
2017-09-17 13:43:13 +02:00
|
|
|
return &actors;
|
|
|
|
}
|
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);
|
|
|
|
renderer->draw_sprite(tileset->get_sprite(GetTile(ax, ay)), x + ix * w, y + iy * h);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
printf("\t%d", GetTile(x, y));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|