remove Blocks
[Tsunagari.git] / src / tile.cpp
blobdb910bdb9eeeb762c8908b77854d78a98098a85c
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** tile.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <boost/foreach.hpp>
9 #include "area.h"
10 #include "tile.h"
11 #include "window.h"
14 Tile::Tile()
15 : type(NULL), flags(0x0)
19 bool Tile::hasFlag(unsigned flag) const
21 if (flags & flag || (type && type->flags & flag))
22 return true;
23 return false;
26 void Tile::onEnterScripts(Resourcer* rc, Entity* triggeredBy)
28 if (flags & hasOnEnter)
29 runScripts(rc, triggeredBy, onEnter, events);
30 if (type && type->flags & hasOnEnter)
31 runScripts(rc, triggeredBy, onEnter, type->events);
34 void Tile::onLeaveScripts(Resourcer* rc, Entity* triggeredBy)
36 if (type && type->flags & hasOnLeave)
37 runScripts(rc, triggeredBy, onLeave,
38 type->events);
39 if (flags & hasOnLeave)
40 runScripts(rc, triggeredBy, onLeave, events);
43 void Tile::runScripts(Resourcer*, Entity*,
44 const TileEventTrigger trigger,
45 const std::vector<TileEvent>& events)
47 for (std::vector<TileEvent>::const_iterator i = events.begin(); i != events.end(); i++) {
48 // BOOST_FOREACH(const TileEvent& e, events) {
49 const TileEvent& e = *i;
50 if (e.trigger == trigger) {
51 /* FIXME: Redo scripts.
52 Script s(rc);
53 bindEntity(&s, entity, "entity");
54 // TODO bindTile(script, tile, "tile");
55 s.run(rc, e.script);
62 TileType::TileType()
63 : flags(0x0)
67 TileType::TileType(TiledImage& img)
68 : flags(0x0)
70 anim.addFrame(img.front());
71 img.pop_front();
74 bool TileType::needsRedraw(const Area& area) const
76 const int millis = GameWindow::getWindow().time();
77 return anim.needsRedraw(millis) &&
78 visibleIn(area, area.visibleTiles());
81 bool TileType::visibleIn(const Area& area, const icube_t& tiles) const
83 for (int z = tiles.z1; z != tiles.z2; z++) {
84 for (int y = tiles.y1; y != tiles.y2; y++) {
85 for (int x = tiles.x1; x != tiles.x2; x++) {
86 icoord pos(x, y, z);
87 // Do this check before passing _tiles_ to fn.
88 if (area.tileExists(pos)) {
89 const Tile& tile = area.getTile(pos);
90 if (tile.type == this)
91 return true;
96 return false;