| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
|
| 24 |
// 3rd party library includes |
| 25 |
|
| 26 |
// FIFE includes |
| 27 |
// These includes are split up in two parts, separated by one empty line |
| 28 |
// First block: files included from the FIFE root src directory |
| 29 |
// Second block: files included from the same folder |
| 30 |
#include "util/log/logger.h" |
| 31 |
#include "util/structures/purge.h" |
| 32 |
#include "model/metamodel/grids/cellgrid.h" |
| 33 |
|
| 34 |
#include "layer.h" |
| 35 |
#include "instance.h" |
| 36 |
#include "map.h" |
| 37 |
#include "instancetree.h" |
| 38 |
#include "cell.h" |
| 39 |
#include "cellcache.h" |
| 40 |
#include "trigger.h" |
| 41 |
|
| 42 |
namespace FIFE { |
| 43 |
/** Logger to use for this source file. |
| 44 |
* @relates Logger |
| 45 |
*/ |
| 46 |
static Logger _log(LM_STRUCTURES); |
| 47 |
|
| 48 |
Layer::Layer(const std::string& identifier, Map* map, CellGrid* grid) |
| 49 |
: m_id(identifier), |
| 50 |
m_map(map), |
| 51 |
m_instancesVisibility(true), |
| 52 |
m_transparency(0), |
| 53 |
m_instanceTree(new InstanceTree()), |
| 54 |
m_grid(grid), |
| 55 |
m_pathingStrategy(CELL_EDGES_ONLY), |
| 56 |
m_sortingStrategy(SORTING_CAMERA), |
| 57 |
m_walkable(false), |
| 58 |
m_interact(false), |
| 59 |
m_walkableId(""), |
| 60 |
m_cellCache(NULL), |
| 61 |
m_changeListeners(), |
| 62 |
m_changedInstances(), |
| 63 |
m_changed(false), |
| 64 |
m_static(false) { |
| 65 |
} |
| 66 |
|
| 67 |
Layer::~Layer() { |
| 68 |
// if this is a walkable layer |
| 69 |
destroyCellCache(); |
| 70 |
// if this is a interact layer |
| 71 |
if (m_interact) { |
| 72 |
Layer* temp = m_map->getLayer(m_walkableId); |
| 73 |
if (temp) { |
| 74 |
temp->removeInteractLayer(this); |
| 75 |
} |
| 76 |
} |
| 77 |
purge(m_instances); |
| 78 |
delete m_instanceTree; |
| 79 |
} |
| 80 |
|
| 81 |
const std::string& Layer::getId() const { |
| 82 |
return m_id; |
| 83 |
} |
| 84 |
|
| 85 |
void Layer::setId(const std::string& id) { |
| 86 |
m_id = id; |
| 87 |
} |
| 88 |
|
| 89 |
Map* Layer::getMap() const { |
| 90 |
return m_map; |
| 91 |
} |
| 92 |
|
| 93 |
CellGrid* Layer::getCellGrid() const { |
| 94 |
return m_grid; |
| 95 |
} |
| 96 |
|
| 97 |
void Layer::setCellGrid(CellGrid* grid) { |
| 98 |
m_grid = grid; |
| 99 |
} |
| 100 |
|
| 101 |
InstanceTree* Layer::getInstanceTree(void) const { |
| 102 |
return m_instanceTree; |
| 103 |
} |
| 104 |
|
| 105 |
bool Layer::hasInstances() const { |
| 106 |
return !m_instances.empty(); |
| 107 |
} |
| 108 |
|
| 109 |
Instance* Layer::createInstance(Object* object, const ModelCoordinate& p, const std::string& id) { |
| 110 |
ExactModelCoordinate emc(static_cast<double>(p.x), static_cast<double>(p.y), static_cast<double>(p.z)); |
| 111 |
return createInstance(object, emc, id); |
| 112 |
} |
| 113 |
|
| 114 |
Instance* Layer::createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id) { |
| 115 |
Location location(this); |
| 116 |
location.setExactLayerCoordinates(p); |
| 117 |
|
| 118 |
Instance* instance = new Instance(object, location, id); |
| 119 |
if(instance->isActive()) { |
| 120 |
setInstanceActivityStatus(instance, instance->isActive()); |
| 121 |
} |
| 122 |
m_instances.push_back(instance); |
| 123 |
m_instanceTree->addInstance(instance); |
| 124 |
|
| 125 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 126 |
while (i != m_changeListeners.end()) { |
| 127 |
(*i)->onInstanceCreate(this, instance); |
| 128 |
++i; |
| 129 |
} |
| 130 |
m_changed = true; |
| 131 |
return instance; |
| 132 |
} |
| 133 |
|
| 134 |
bool Layer::addInstance(Instance* instance, const ExactModelCoordinate& p){ |
| 135 |
if( !instance ){ |
| 136 |
FL_ERR(_log, "Tried to add an instance to layer, but given instance is invalid"); |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
Location& location = instance->getLocationRef(); |
| 141 |
location.setLayer(this); |
| 142 |
location.setExactLayerCoordinates(p); |
| 143 |
|
| 144 |
m_instances.push_back(instance); |
| 145 |
m_instanceTree->addInstance(instance); |
| 146 |
if(instance->isActive()) { |
| 147 |
setInstanceActivityStatus(instance, instance->isActive()); |
| 148 |
} |
| 149 |
|
| 150 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 151 |
while (i != m_changeListeners.end()) { |
| 152 |
(*i)->onInstanceCreate(this, instance); |
| 153 |
++i; |
| 154 |
} |
| 155 |
m_changed = true; |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
void Layer::removeInstance(Instance* instance) { |
| 160 |
// If the instance is changed and removed on the same pump, |
| 161 |
// it can happen that the instance can not cleanly be removed, |
| 162 |
// to avoid this we have to update the instance first and send |
| 163 |
// the result to the LayerChangeListeners. |
| 164 |
if (instance->isActive()) { |
| 165 |
if (instance->update() != ICHANGE_NO_CHANGES) { |
| 166 |
std::vector<Instance*> updateInstances; |
| 167 |
updateInstances.push_back(instance); |
| 168 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 169 |
while (i != m_changeListeners.end()) { |
| 170 |
(*i)->onLayerChanged(this, updateInstances); |
| 171 |
++i; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 177 |
while (i != m_changeListeners.end()) { |
| 178 |
(*i)->onInstanceDelete(this, instance); |
| 179 |
++i; |
| 180 |
} |
| 181 |
setInstanceActivityStatus(instance, false); |
| 182 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 183 |
for(; it != m_instances.end(); ++it) { |
| 184 |
if(*it == instance) { |
| 185 |
m_instanceTree->removeInstance(*it); |
| 186 |
m_instances.erase(it); |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
m_changed = true; |
| 191 |
} |
| 192 |
|
| 193 |
void Layer::deleteInstance(Instance* instance) { |
| 194 |
// If the instance is changed and deleted on the same pump, |
| 195 |
// it can happen that the instance can not cleanly be removed, |
| 196 |
// to avoid this we have to update the instance first and send |
| 197 |
// the result to the LayerChangeListeners. |
| 198 |
if (instance->isActive()) { |
| 199 |
if (instance->update() != ICHANGE_NO_CHANGES) { |
| 200 |
std::vector<Instance*> updateInstances; |
| 201 |
updateInstances.push_back(instance); |
| 202 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 203 |
while (i != m_changeListeners.end()) { |
| 204 |
(*i)->onLayerChanged(this, updateInstances); |
| 205 |
++i; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 211 |
while (i != m_changeListeners.end()) { |
| 212 |
(*i)->onInstanceDelete(this, instance); |
| 213 |
++i; |
| 214 |
} |
| 215 |
setInstanceActivityStatus(instance, false); |
| 216 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 217 |
for(; it != m_instances.end(); ++it) { |
| 218 |
if(*it == instance) { |
| 219 |
m_instanceTree->removeInstance(*it); |
| 220 |
delete *it; |
| 221 |
m_instances.erase(it); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
m_changed = true; |
| 227 |
} |
| 228 |
|
| 229 |
const std::vector<Instance*>& Layer::getInstances() const { |
| 230 |
return m_instances; |
| 231 |
} |
| 232 |
|
| 233 |
void Layer::setInstanceActivityStatus(Instance* instance, bool active) { |
| 234 |
if(active) { |
| 235 |
m_activeInstances.insert(instance); |
| 236 |
} else { |
| 237 |
m_activeInstances.erase(instance); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
Instance* Layer::getInstance(const std::string& id) { |
| 242 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 243 |
for(; it != m_instances.end(); ++it) { |
| 244 |
if((*it)->getId() == id) |
| 245 |
return *it; |
| 246 |
} |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
std::vector<Instance*> Layer::getInstances(const std::string& id) { |
| 252 |
std::vector<Instance*> matching_instances; |
| 253 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 254 |
for(; it != m_instances.end(); ++it) { |
| 255 |
if((*it)->getId() == id) |
| 256 |
matching_instances.push_back(*it); |
| 257 |
} |
| 258 |
return matching_instances; |
| 259 |
} |
| 260 |
|
| 261 |
std::vector<Instance*> Layer::getInstancesAt(Location& loc, bool use_exactcoordinates) { |
| 262 |
std::vector<Instance*> matching_instances; |
| 263 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 264 |
|
| 265 |
for(; it != m_instances.end(); ++it) { |
| 266 |
if (use_exactcoordinates) { |
| 267 |
if ((*it)->getLocationRef().getExactLayerCoordinatesRef() == loc.getExactLayerCoordinatesRef()) { |
| 268 |
matching_instances.push_back(*it); |
| 269 |
} |
| 270 |
} else { |
| 271 |
if ((*it)->getLocationRef().getLayerCoordinates() == loc.getLayerCoordinates()) { |
| 272 |
matching_instances.push_back(*it); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return matching_instances; |
| 278 |
} |
| 279 |
|
| 280 |
std::list<Instance*> Layer::getInstancesIn(Rect& rec) { |
| 281 |
std::list<Instance*> matching_instances; |
| 282 |
ModelCoordinate mc(rec.x, rec.y); |
| 283 |
m_instanceTree->findInstances(mc, rec.w, rec.h, matching_instances); |
| 284 |
|
| 285 |
return matching_instances; |
| 286 |
} |
| 287 |
|
| 288 |
std::vector<Instance*> Layer::getInstancesInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2) { |
| 289 |
std::vector<Instance*> instances; |
| 290 |
std::list<Instance*> matchingInstances; |
| 291 |
std::vector<ModelCoordinate> coords = m_grid->getCoordinatesInLine(pt1, pt2); |
| 292 |
for (std::vector<ModelCoordinate>::iterator it = coords.begin(); it != coords.end(); ++it) { |
| 293 |
m_instanceTree->findInstances(*it, 0, 0, matchingInstances); |
| 294 |
if (!matchingInstances.empty()) { |
| 295 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 296 |
} |
| 297 |
} |
| 298 |
return instances; |
| 299 |
} |
| 300 |
|
| 301 |
std::vector<Instance*> Layer::getInstancesInCircle(const ModelCoordinate& center, uint16_t radius) { |
| 302 |
std::vector<Instance*> instances; |
| 303 |
std::list<Instance*> matchingInstances; |
| 304 |
//radius power 2 |
| 305 |
uint16_t radiusp2 = (radius+1) * radius; |
| 306 |
|
| 307 |
ModelCoordinate current(center.x-radius, center.y-radius); |
| 308 |
ModelCoordinate target(center.x+radius, center.y+radius); |
| 309 |
for (; current.y < center.y; current.y++) { |
| 310 |
current.x = center.x-radius; |
| 311 |
for (; current.x < center.x; current.x++) { |
| 312 |
uint16_t dx = center.x - current.x; |
| 313 |
uint16_t dy = center.y - current.y; |
| 314 |
uint16_t distance = dx*dx + dy*dy; |
| 315 |
if (distance <= radiusp2) { |
| 316 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 317 |
if (!matchingInstances.empty()) { |
| 318 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 319 |
} |
| 320 |
|
| 321 |
current.x = center.x + dx; |
| 322 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 323 |
if (!matchingInstances.empty()) { |
| 324 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 325 |
} |
| 326 |
|
| 327 |
current.y = center.y + dy; |
| 328 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 329 |
if (!matchingInstances.empty()) { |
| 330 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 331 |
} |
| 332 |
|
| 333 |
current.x = center.x-dx; |
| 334 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 335 |
if (!matchingInstances.empty()) { |
| 336 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 337 |
} |
| 338 |
|
| 339 |
current.y = center.y-dy; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
current.x = center.x; |
| 344 |
current.y = center.y-radius; |
| 345 |
for (; current.y <= target.y; current.y++) { |
| 346 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 347 |
if (!matchingInstances.empty()) { |
| 348 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
current.y = center.y; |
| 353 |
current.x = center.x-radius; |
| 354 |
for (; current.x <= target.x; current.x++) { |
| 355 |
m_instanceTree->findInstances(current, 0, 0, matchingInstances); |
| 356 |
if (!matchingInstances.empty()) { |
| 357 |
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end()); |
| 358 |
} |
| 359 |
} |
| 360 |
return instances; |
| 361 |
} |
| 362 |
|
| 363 |
std::vector<Instance*> Layer::getInstancesInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle) { |
| 364 |
std::vector<Instance*> instances; |
| 365 |
ExactModelCoordinate exactCenter(center.x, center.y); |
| 366 |
std::vector<Instance*> tmpInstances = getInstancesInCircle(center, radius); |
| 367 |
int32_t s = (sangle + 360) % 360; |
| 368 |
int32_t e = (eangle + 360) % 360; |
| 369 |
bool greater = (s > e) ? true : false; |
| 370 |
for (std::vector<Instance*>::iterator it = tmpInstances.begin(); it != tmpInstances.end(); ++it) { |
| 371 |
int32_t angle = getAngleBetween(exactCenter, intPt2doublePt((*it)->getLocationRef().getLayerCoordinates())); |
| 372 |
if (greater) { |
| 373 |
if (angle >= s || angle <= e) { |
| 374 |
instances.push_back(*it); |
| 375 |
} |
| 376 |
} else { |
| 377 |
if (angle >= s && angle <= e) { |
| 378 |
instances.push_back(*it); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
return instances; |
| 383 |
} |
| 384 |
|
| 385 |
void Layer::getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer) const { |
| 386 |
if (!layer) { |
| 387 |
layer = this; |
| 388 |
} |
| 389 |
|
| 390 |
if (m_instances.empty()) { |
| 391 |
min = ModelCoordinate(); |
| 392 |
max = min; |
| 393 |
} else { |
| 394 |
min = m_instances.front()->getLocationRef().getLayerCoordinates(layer); |
| 395 |
max = min; |
| 396 |
|
| 397 |
for (std::vector<Instance*>::const_iterator i = m_instances.begin(); i != m_instances.end(); ++i) { |
| 398 |
ModelCoordinate coord = (*i)->getLocationRef().getLayerCoordinates(layer); |
| 399 |
min.x = std::min(min.x, coord.x); |
| 400 |
max.x = std::max(max.x, coord.x); |
| 401 |
min.y = std::min(min.y, coord.y); |
| 402 |
max.y = std::max(max.y, coord.y); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
float Layer::getZOffset() const { |
| 409 |
static const float globalmax = 100.0; |
| 410 |
static const float globalrange = 200.0; |
| 411 |
int32_t numlayers = m_map->getLayerCount(); |
| 412 |
int32_t thislayer = 1; // we don't need 0 indexed |
| 413 |
|
| 414 |
const std::list<Layer*>& layers = m_map->getLayers(); |
| 415 |
std::list<Layer*>::const_iterator iter = layers.begin(); |
| 416 |
for (; iter != layers.end(); ++iter, ++thislayer) { |
| 417 |
if (*iter == this) { |
| 418 |
break; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
float offset = globalmax - (numlayers - (thislayer - 1)) * (globalrange/numlayers); |
| 423 |
return offset; |
| 424 |
} |
| 425 |
|
| 426 |
uint32_t Layer::getLayerCount() const { |
| 427 |
return m_map->getLayerCount(); |
| 428 |
} |
| 429 |
|
| 430 |
void Layer::setInstancesVisible(bool vis) { |
| 431 |
if (m_instancesVisibility != vis) { |
| 432 |
m_instancesVisibility = vis; |
| 433 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 434 |
for (; it != m_instances.end(); ++it) { |
| 435 |
(*it)->callOnVisibleChange(); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void Layer::setLayerTransparency(uint8_t transparency) { |
| 441 |
if (m_transparency != transparency) { |
| 442 |
m_transparency = transparency; |
| 443 |
std::vector<Instance*>::iterator it = m_instances.begin(); |
| 444 |
for (; it != m_instances.end(); ++it) { |
| 445 |
(*it)->callOnTransparencyChange(); |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
uint8_t Layer::getLayerTransparency() { |
| 451 |
return m_transparency; |
| 452 |
} |
| 453 |
|
| 454 |
void Layer::toggleInstancesVisible() { |
| 455 |
setInstancesVisible(!m_instancesVisibility); |
| 456 |
} |
| 457 |
|
| 458 |
bool Layer::areInstancesVisible() const { |
| 459 |
return m_instancesVisibility; |
| 460 |
} |
| 461 |
|
| 462 |
bool Layer::cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate) { |
| 463 |
bool blockingInstance = false; |
| 464 |
if (m_cellCache) { |
| 465 |
Cell* cell = m_cellCache->getCell(cellCoordinate); |
| 466 |
if (cell) { |
| 467 |
return cell->getCellType() != CTYPE_NO_BLOCKER; |
| 468 |
} |
| 469 |
} else { |
| 470 |
std::list<Instance*> adjacentInstances; |
| 471 |
m_instanceTree->findInstances(cellCoordinate, 0, 0, adjacentInstances); |
| 472 |
for(std::list<Instance*>::const_iterator j = adjacentInstances.begin(); j != adjacentInstances.end(); ++j) { |
| 473 |
if((*j)->isBlocking() && (*j)->getLocationRef().getLayerCoordinates() == cellCoordinate) { |
| 474 |
blockingInstance = true; |
| 475 |
break; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
return blockingInstance; |
| 480 |
} |
| 481 |
|
| 482 |
std::vector<Instance*> Layer::getBlockingInstances(const ModelCoordinate& cellCoordinate) { |
| 483 |
std::vector<Instance*> blockingInstances; |
| 484 |
if (m_cellCache) { |
| 485 |
Cell* cell = m_cellCache->getCell(cellCoordinate); |
| 486 |
if (cell) { |
| 487 |
const std::set<Instance*>& blocker = cell->getInstances(); |
| 488 |
for (std::set<Instance*>::const_iterator it = blocker.begin(); it != blocker.end(); ++it) { |
| 489 |
if ((*it)->isBlocking()) { |
| 490 |
blockingInstances.push_back(*it); |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
} else { |
| 495 |
std::list<Instance*> adjacentInstances; |
| 496 |
m_instanceTree->findInstances(cellCoordinate, 0, 0, adjacentInstances); |
| 497 |
for(std::list<Instance*>::const_iterator j = adjacentInstances.begin(); j != adjacentInstances.end(); ++j) { |
| 498 |
if((*j)->isBlocking() && (*j)->getLocationRef().getLayerCoordinates() == cellCoordinate) { |
| 499 |
blockingInstances.push_back(*j); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
return blockingInstances; |
| 504 |
} |
| 505 |
|
| 506 |
void Layer::setPathingStrategy(PathingStrategy strategy) { |
| 507 |
m_pathingStrategy = strategy; |
| 508 |
m_grid->setAllowDiagonals(m_pathingStrategy != CELL_EDGES_ONLY); |
| 509 |
} |
| 510 |
|
| 511 |
PathingStrategy Layer::getPathingStrategy() const { |
| 512 |
return m_pathingStrategy; |
| 513 |
} |
| 514 |
|
| 515 |
void Layer::setSortingStrategy(SortingStrategy strategy) { |
| 516 |
m_sortingStrategy = strategy; |
| 517 |
} |
| 518 |
|
| 519 |
SortingStrategy Layer::getSortingStrategy() const { |
| 520 |
return m_sortingStrategy; |
| 521 |
} |
| 522 |
|
| 523 |
void Layer::setWalkable(bool walkable) { |
| 524 |
m_walkable = walkable; |
| 525 |
} |
| 526 |
|
| 527 |
bool Layer::isWalkable() { |
| 528 |
return m_walkable; |
| 529 |
} |
| 530 |
|
| 531 |
void Layer::setInteract(bool interact, const std::string& id) { |
| 532 |
m_interact = interact; |
| 533 |
m_walkableId = id; |
| 534 |
} |
| 535 |
|
| 536 |
bool Layer::isInteract() { |
| 537 |
return m_interact; |
| 538 |
} |
| 539 |
|
| 540 |
const std::string& Layer::getWalkableId() { |
| 541 |
return m_walkableId; |
| 542 |
} |
| 543 |
|
| 544 |
void Layer::addInteractLayer(Layer* layer) { |
| 545 |
if (m_walkable) { |
| 546 |
m_interacts.push_back(layer); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
const std::vector<Layer*>& Layer::getInteractLayers() { |
| 551 |
return m_interacts; |
| 552 |
} |
| 553 |
|
| 554 |
void Layer::removeInteractLayer(Layer* layer) { |
| 555 |
if (m_walkable) { |
| 556 |
std::vector<Layer*>::iterator it = m_interacts.begin(); |
| 557 |
for (; it != m_interacts.end(); ++it) { |
| 558 |
if (*it == layer) { |
| 559 |
(*it)->removeChangeListener(m_cellCache->getCellCacheChangeListener()); |
| 560 |
m_interacts.erase(it); |
| 561 |
break; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
void Layer::createCellCache() { |
| 568 |
if (!m_cellCache && m_walkable) { |
| 569 |
m_cellCache = new CellCache(this); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
CellCache* Layer::getCellCache() { |
| 574 |
return m_cellCache; |
| 575 |
} |
| 576 |
|
| 577 |
void Layer::destroyCellCache() { |
| 578 |
if (m_walkable) { |
| 579 |
removeChangeListener(m_cellCache->getCellCacheChangeListener()); |
| 580 |
if (!m_interacts.empty()) { |
| 581 |
std::vector<Layer*>::iterator it = m_interacts.begin(); |
| 582 |
for (; it != m_interacts.end(); ++it) { |
| 583 |
(*it)->removeChangeListener(m_cellCache->getCellCacheChangeListener()); |
| 584 |
(*it)->setInteract(false, ""); |
| 585 |
} |
| 586 |
m_interacts.clear(); |
| 587 |
} |
| 588 |
delete m_cellCache; |
| 589 |
m_cellCache = NULL; |
| 590 |
m_walkable = false; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
bool Layer::update() { |
| 595 |
m_changedInstances.clear(); |
| 596 |
std::vector<Instance*> inactiveInstances; |
| 597 |
std::set<Instance*>::iterator it = m_activeInstances.begin(); |
| 598 |
for(; it != m_activeInstances.end(); ++it) { |
| 599 |
if ((*it)->update() != ICHANGE_NO_CHANGES) { |
| 600 |
m_changedInstances.push_back(*it); |
| 601 |
m_changed = true; |
| 602 |
} else if (!(*it)->isActive()) { |
| 603 |
inactiveInstances.push_back(*it); |
| 604 |
} |
| 605 |
} |
| 606 |
if (!m_changedInstances.empty()) { |
| 607 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 608 |
while (i != m_changeListeners.end()) { |
| 609 |
(*i)->onLayerChanged(this, m_changedInstances); |
| 610 |
++i; |
| 611 |
} |
| 612 |
//std::cout << "Layer named " << Id() << " changed = 1\n"; |
| 613 |
} |
| 614 |
// remove inactive instances from m_activeInstances |
| 615 |
if (!inactiveInstances.empty()) { |
| 616 |
std::vector<Instance*>::iterator i = inactiveInstances.begin(); |
| 617 |
while (i != inactiveInstances.end()) { |
| 618 |
m_activeInstances.erase(*i); |
| 619 |
++i; |
| 620 |
} |
| 621 |
} |
| 622 |
//std::cout << "Layer named " << Id() << " changed = 0\n"; |
| 623 |
bool retval = m_changed; |
| 624 |
m_changed = false; |
| 625 |
return retval; |
| 626 |
} |
| 627 |
|
| 628 |
void Layer::addChangeListener(LayerChangeListener* listener) { |
| 629 |
m_changeListeners.push_back(listener); |
| 630 |
} |
| 631 |
|
| 632 |
void Layer::removeChangeListener(LayerChangeListener* listener) { |
| 633 |
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin(); |
| 634 |
while (i != m_changeListeners.end()) { |
| 635 |
if ((*i) == listener) { |
| 636 |
m_changeListeners.erase(i); |
| 637 |
return; |
| 638 |
} |
| 639 |
++i; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
bool Layer::isChanged() { |
| 644 |
return m_changed; |
| 645 |
} |
| 646 |
|
| 647 |
std::vector<Instance*>& Layer::getChangedInstances() { |
| 648 |
return m_changedInstances; |
| 649 |
} |
| 650 |
|
| 651 |
void Layer::setStatic(bool stati) { |
| 652 |
m_static = stati; |
| 653 |
} |
| 654 |
|
| 655 |
bool Layer::isStatic() { |
| 656 |
return m_static; |
| 657 |
} |
| 658 |
|
| 659 |
} // FIFE |