comparison src/tilemap.d @ 0:a2d653eb9e99

first working version.
author fred@reichbier.de
date Thu, 17 Jul 2008 18:52:55 +0200
parents
children f193d0c14685
comparison
equal deleted inserted replaced
-1:000000000000 0:a2d653eb9e99
1 module tilemap;
2
3 import tango.math.Math;
4
5 import dsfml.window.all;
6 import dsfml.system.all;
7 import dsfml.graphics.all;
8
9 import tileset;
10
11 typedef Sprite[] SpriteArray;
12
13 class Tilemap {
14 public int[int][int] map;
15 private Tileset tileset;
16 public int width, height, tilewidth, tileheight;
17
18 this(Tileset tileset, int width, int height, int tilewidth, int tileheight) {
19 this.tileset = tileset;
20 this.width = width;
21 this.height = height;
22 this.tilewidth = tilewidth;
23 this.tileheight = tileheight;
24 }
25
26 void real_to_tile(int real_x, int real_y, inout int tile_x, inout int tile_y) {
27 tile_x = rndint(floor(real_x / this.tilewidth));
28 tile_y = rndint(floor(real_y / this.tileheight));
29 }
30
31 void tile_to_real(int tile_x, int tile_y, inout int real_x, inout int real_y) {
32 real_x = tile_x*this.tilewidth;
33 real_y = tile_y*this.tileheight;
34 }
35
36 SpriteArray get_sprites() {
37 SpriteArray sprites;
38 Sprite sprite;
39 int real_x, real_y;
40 for(int x=0; x<width; x++) {
41 if(x in this.map) {
42 for(int y=0; y<height; y++) {
43 if(y in this.map[x]) {
44 sprite = new Sprite;
45 tile_to_real(x, y, real_x, real_y);
46 sprite.setX(real_x);
47 sprite.setY(real_y);
48 sprite.setImage(this.tileset.tiles[this.map[x][y]]);
49 sprites ~= sprite;
50 }
51 }
52 }
53 }
54 return sprites;
55 }
56 }