]> git.localhorst.tv Git - l2e.git/blob - src/map/Area.cpp
implemented pushable entities and pushing
[l2e.git] / src / map / Area.cpp
1 /*
2  * Area.cpp
3  *
4  *  Created on: Sep 26, 2012
5  *      Author: holy
6  */
7
8 #include "Area.h"
9
10 #include "Tile.h"
11 #include "../graphics/Sprite.h"
12 #include "../sdl/utility.h"
13
14 #include <stdexcept>
15
16 using geometry::Vector;
17
18 namespace map {
19
20 Area::Area()
21 : battlebg(0)
22 , tiles(0)
23 , numTiles(0)
24 , width(0) {
25
26 }
27
28
29 Tile *Area::TileAt(const geometry::Vector<int> &offset) {
30         int tileIndex(offset.Y() * width + offset.X());
31         if (tileIndex < numTiles) {
32                 return tiles +tileIndex;
33         } else {
34                 return 0;
35         }
36 }
37
38 const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
39         int tileIndex(offset.Y() * width + offset.X());
40         if (tileIndex < numTiles) {
41                 return tiles +tileIndex;
42         } else {
43                 return 0;
44         }
45 }
46
47
48 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
49         for (int i(0); i < numTiles; ++i) {
50                 Vector<int> offset(
51                                 inOffset.X() + (i % width) * tileset->Width(),
52                                 inOffset.Y() + (i / width) * tileset->Height());
53                 const Tile &tile(tiles[i]);
54                 tileset->Draw(dest, offset, tile.Offset().X(), tile.Offset().Y());
55         }
56 }
57
58 void Area::RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
59         SDL_Rect destRect;
60         destRect.x = inOffset.X();
61         destRect.y = inOffset.Y();
62         destRect.w = Width() * tileset->Width();
63         destRect.h = Height() * tileset->Height();
64         sdl::OutlineRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0x00, 0xFF));
65
66         Uint32 color(SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
67
68         for (int i(0); i < numTiles; ++i) {
69                 Vector<int> offset(
70                                 inOffset.X() + (i % width) * tileset->Width(),
71                                 inOffset.Y() + (i / width) * tileset->Height());
72                 const Tile &tile(tiles[i]);
73
74                 if (tile.BlocksNorth()) {
75                         sdl::HorizontalLine(dest, offset, tileset->Width(), color);
76                 }
77                 if (tile.BlocksEast()) {
78                         sdl::VerticalLine(dest, Vector<int>(offset.X() + tileset->Width() - 1, offset.Y()), tileset->Height(), color);
79                 }
80                 if (tile.BlocksSouth()) {
81                         sdl::HorizontalLine(dest, Vector<int>(offset.X(), offset.Y() + tileset->Height() - 1), tileset->Width(), color);
82                 }
83                 if (tile.BlocksWest()) {
84                         sdl::VerticalLine(dest, offset, tileset->Height(), color);
85                 }
86         }
87 }
88
89 }