97 lines
2.4 KiB
C++
97 lines
2.4 KiB
C++
#include <cstdio>
|
|
#include "libtcod.hpp"
|
|
#include "Actor.h"
|
|
#include "Map.h"
|
|
#include "Engine.h"
|
|
#include "Ai.h"
|
|
#include "Destructible.h"
|
|
#include "Attacker.h"
|
|
#include "Container.h"
|
|
#include "Pickable.h"
|
|
|
|
Actor::Actor(int x, int y, std::string_view ch, std::string name, const TCOD_ColorRGB& col) :
|
|
x(x), y(y), ch(ch), col(col), name(name),
|
|
blocks(true), attacker(nullptr), destructible(nullptr), ai(nullptr), pickable(nullptr), container(nullptr), fovOnly(true) {
|
|
|
|
}
|
|
|
|
void Actor::render(TCOD_Console& cons) const {
|
|
int offset_x = engine->player->x - Engine::VIEW_WIDTH / 2;
|
|
int offset_y = engine->player->y - Engine::VIEW_HEIGHT / 2;
|
|
if (offset_x > x || Engine::VIEW_WIDTH + offset_x < x || offset_y > y || Engine::VIEW_HEIGHT + offset_y < y) {
|
|
return;
|
|
}
|
|
tcod::print(cons, { x - offset_x, y - offset_y}, ch, col, std::nullopt);
|
|
}
|
|
|
|
void Actor::update() {
|
|
if (ai) {
|
|
ai->update(this);
|
|
}
|
|
}
|
|
|
|
Actor::~Actor() {
|
|
if (attacker) delete attacker;
|
|
if (destructible) delete destructible;
|
|
if (ai) delete ai;
|
|
if (pickable) delete pickable;
|
|
if (container) delete container;
|
|
}
|
|
|
|
float Actor::getDistance(int cx, int cy) const {
|
|
int dx = x - cx;
|
|
int dy = y - cy;
|
|
return sqrtf((float)(dx * dx + dy * dy));
|
|
}
|
|
|
|
void Actor::save(TCODZip& zip) {
|
|
zip.putInt(x);
|
|
zip.putInt(y);
|
|
zip.putString(std::string(ch).c_str());
|
|
zip.putInt(col.r);
|
|
zip.putInt(col.g);
|
|
zip.putInt(col.b);
|
|
zip.putString(name.c_str());
|
|
zip.putInt(blocks);
|
|
zip.putInt(attacker != NULL);
|
|
zip.putInt(destructible != NULL);
|
|
zip.putInt(ai != NULL);
|
|
zip.putInt(pickable != NULL);
|
|
zip.putInt(container != NULL);
|
|
if (attacker) attacker->save(zip);
|
|
if (destructible) destructible->save(zip);
|
|
if (ai) ai->save(zip);
|
|
if (pickable) pickable->save(zip);
|
|
if (container) container->save(zip);
|
|
}
|
|
|
|
void Actor::load(TCODZip& zip) {
|
|
x = zip.getInt();
|
|
y = zip.getInt();
|
|
ch = std::string(zip.getString());
|
|
col = TCOD_ColorRGB(zip.getInt(), zip.getInt(), zip.getInt());
|
|
name = zip.getString();
|
|
blocks = zip.getInt();
|
|
bool hasAttacker = zip.getInt();
|
|
bool hasDestructible = zip.getInt();
|
|
bool hasAi = zip.getInt();
|
|
bool hasPickable = zip.getInt();
|
|
bool hasContainer = zip.getInt();
|
|
if (hasAttacker) {
|
|
attacker = new Attacker(0.0f);
|
|
attacker->load(zip);
|
|
}
|
|
if (hasDestructible) {
|
|
destructible = Destructible::create(zip);
|
|
}
|
|
if (hasAi) {
|
|
ai = Ai::create(zip);
|
|
}
|
|
if (hasPickable) {
|
|
pickable = Pickable::create(zip);
|
|
}
|
|
if (hasContainer) {
|
|
container = new Container(0);
|
|
container->load(zip);
|
|
}
|
|
} |