Mercurial > projects > myrrdin
annotate 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 |
rev | line source |
---|---|
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
1 /* |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
2 This file is part of myrrdin. |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
3 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
4 myrrdin is free software: you can redistribute it and/or modify |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
5 it under the terms of the GNU Lesser General Public License as |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
6 published by the Free Software Foundation, either version 3 of |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
7 the License, or (at your option) any later version. |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
8 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
9 myrrdin is distributed in the hope that it will be useful, |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
12 GNU Lesser General Public License for more details. |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
13 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
14 You should have received a copy of the GNU Lesser General Public |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
15 License along with myrrdin. If not, see <http://www.gnu.org/licenses/>. |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
16 */ |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
17 |
0 | 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; | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
27 import tango.io.Stdout; |
0 | 28 |
29 typedef Sprite[] SpriteArray; | |
30 | |
31 class Tilemap { | |
1 | 32 public int[int][int][int] map; // Layer: x: y: Tile-ID |
3 | 33 public Vector2i[int] layer_tsizes; // Layer: Tile size |
4
292df259cc85
view + sprite consumers, animated sprite working
fred@reichbier.de
parents:
3
diff
changeset
|
34 public Tileset tileset; |
0 | 35 public int width, height, tilewidth, tileheight; |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
36 private View view; |
0 | 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 | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
46 /* set the view. you should do this if you use a sf::View */ |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
47 void set_view(View view) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
48 this.view = view; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
49 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
50 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
51 /* translate On-Screen -> With-View */ |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
52 Vector2i translate_with_view(int real_x, int real_y) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
53 Vector2i vec = Vector2i(real_x, real_y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
54 if(this.view) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
55 FloatRect rect = this.view.getRect(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
56 vec.x += rect.getLeft(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
57 vec.y += rect.getTop(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
58 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
59 return vec; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
60 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
61 |
3 | 62 Vector2i real_to_tile(int layer, int real_x, int real_y) { |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
63 Vector2i vec = this.translate_with_view(real_x, real_y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
64 return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y))); |
0 | 65 } |
66 | |
3 | 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); | |
0 | 69 } |
70 | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
71 bool has_tile(int layer, int x, int y) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
72 return (layer in this.map && x in this.map[layer] && y in this.map[layer][x]); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
73 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
74 |
0 | 75 SpriteArray get_sprites() { |
76 SpriteArray sprites; | |
77 Sprite sprite; | |
3 | 78 int layer_id=0; |
79 Vector2i pos; | |
1 | 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]) { | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
85 if (layer[x][y] != 0) { // is no transparent tile? |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
86 sprite = new Sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
87 pos = tile_to_real(layer_id, x, y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
88 sprite.setX(pos.x); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
89 sprite.setY(pos.y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
90 sprite.setImage(this.tileset.tiles[layer[x][y]]); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
91 sprites ~= sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
92 } |
1 | 93 } |
0 | 94 } |
95 } | |
96 } | |
3 | 97 layer_id++; |
0 | 98 } |
99 return sprites; | |
100 } | |
101 } |