comparison import/myrrdin/tilemap.d @ 5:f4b89014ad39

added moving figure stuff + animated sprites. not usable atm.
author fred@reichbier.de
date Sat, 19 Jul 2008 14:33:08 +0200
parents src/tilemap.d@292df259cc85
children 510541745cd1
comparison
equal deleted inserted replaced
4:292df259cc85 5:f4b89014ad39
1 /*
2 This file is part of myrrdin.
3
4 myrrdin is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of
7 the License, or (at your option) any later version.
8
9 myrrdin is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with myrrdin. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 module tilemap;
19
20 import tango.math.Math;
21
22 import dsfml.window.all;
23 import dsfml.system.all;
24 import dsfml.graphics.all;
25
26 import tileset;
27 import tango.io.Stdout;
28
29 typedef Sprite[] SpriteArray;
30
31 class Tilemap {
32 public int[int][int][int] map; // Layer: x: y: Tile-ID
33 public Vector2i[int] layer_tsizes; // Layer: Tile size
34 public Tileset tileset;
35 public int width, height, tilewidth, tileheight;
36 private View view;
37
38 this(Tileset tileset, int width, int height, int tilewidth, int tileheight) {
39 this.tileset = tileset;
40 this.width = width;
41 this.height = height;
42 this.tilewidth = tilewidth;
43 this.tileheight = tileheight;
44 }
45
46 /* set the view. you should do this if you use a sf::View */
47 void set_view(View view) {
48 this.view = view;
49 }
50
51 /* translate On-Screen -> With-View */
52 Vector2i translate_with_view(int real_x, int real_y) {
53 Vector2i vec = Vector2i(real_x, real_y);
54 if(this.view) {
55 FloatRect rect = this.view.getRect();
56 vec.x += rect.getLeft();
57 vec.y += rect.getTop();
58 }
59 return vec;
60 }
61
62 Vector2i real_to_tile(int layer, int real_x, int real_y) {
63 Vector2i vec = this.translate_with_view(real_x, real_y);
64 return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y)));
65 }
66
67 Vector2i tile_to_real(int layer, int tile_x, int tile_y) {
68 return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y);
69 }
70
71 bool has_tile(int layer, int x, int y) {
72 return (layer in this.map && x in this.map[layer] && y in this.map[layer][x]);
73 }
74
75 SpriteArray get_sprites() {
76 SpriteArray sprites;
77 Sprite sprite;
78 int layer_id=0;
79 Vector2i pos;
80 foreach(int[int][int] layer; map) {
81 for(int x=0; x<width; x++) {
82 if(x in layer) {
83 for(int y=0; y<height; y++) {
84 if(y in layer[x]) {
85 if (layer[x][y] != 0) { // is no transparent tile?
86 sprite = new Sprite;
87 pos = tile_to_real(layer_id, x, y);
88 sprite.setX(pos.x);
89 sprite.setY(pos.y);
90 sprite.setImage(this.tileset.tiles[layer[x][y]]);
91 sprites ~= sprite;
92 }
93 }
94 }
95 }
96 }
97 layer_id++;
98 }
99 return sprites;
100 }
101 }