Mercurial > projects > myrrdin
annotate import/myrrdin/tilemap.d @ 6:510541745cd1
turned myrrdin into package.
author | fred@reichbier.de |
---|---|
date | Sat, 19 Jul 2008 15:10:56 +0200 |
parents | f4b89014ad39 |
children | 806b3781f4e4 |
rev | line source |
---|---|
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
1 /* |
6 | 2 myrrdin, a 2d tile engine |
3 Copyright (c) 2008 Friedrich Weber | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
4 |
6 | 5 Permission is hereby granted, free of charge, to any person obtaining a copy |
6 of this software and associated documentation files (the Software), to deal | |
7 in the Software without restriction, including without limitation the rights | |
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 copies of the Software, and to permit persons to whom the Software is | |
10 furnished to do so, subject to the following conditions: | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
11 |
6 | 12 The above copyright notice and this permission notice shall be included in |
13 all copies or substantial portions of the Software. | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
14 |
6 | 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 THE SOFTWARE. | |
22 */ | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
23 |
0 | 24 module tilemap; |
25 | |
26 import tango.math.Math; | |
27 | |
28 import dsfml.window.all; | |
29 import dsfml.system.all; | |
30 import dsfml.graphics.all; | |
31 | |
6 | 32 import myrrdin.tileset; |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
33 import tango.io.Stdout; |
0 | 34 |
35 typedef Sprite[] SpriteArray; | |
36 | |
37 class Tilemap { | |
1 | 38 public int[int][int][int] map; // Layer: x: y: Tile-ID |
3 | 39 public Vector2i[int] layer_tsizes; // Layer: Tile size |
4
292df259cc85
view + sprite consumers, animated sprite working
fred@reichbier.de
parents:
3
diff
changeset
|
40 public Tileset tileset; |
0 | 41 public int width, height, tilewidth, tileheight; |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
42 private View view; |
0 | 43 |
44 this(Tileset tileset, int width, int height, int tilewidth, int tileheight) { | |
45 this.tileset = tileset; | |
46 this.width = width; | |
47 this.height = height; | |
48 this.tilewidth = tilewidth; | |
49 this.tileheight = tileheight; | |
50 } | |
51 | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
52 /* 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
|
53 void set_view(View view) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
54 this.view = view; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
55 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
56 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
57 /* translate On-Screen -> With-View */ |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
58 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
|
59 Vector2i vec = Vector2i(real_x, real_y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
60 if(this.view) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
61 FloatRect rect = this.view.getRect(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
62 vec.x += rect.getLeft(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
63 vec.y += rect.getTop(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
64 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
65 return vec; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
66 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
67 |
3 | 68 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
|
69 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
|
70 return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y))); |
0 | 71 } |
72 | |
3 | 73 Vector2i tile_to_real(int layer, int tile_x, int tile_y) { |
74 return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y); | |
0 | 75 } |
76 | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
77 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
|
78 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
|
79 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
80 |
0 | 81 SpriteArray get_sprites() { |
82 SpriteArray sprites; | |
83 Sprite sprite; | |
3 | 84 int layer_id=0; |
85 Vector2i pos; | |
1 | 86 foreach(int[int][int] layer; map) { |
87 for(int x=0; x<width; x++) { | |
88 if(x in layer) { | |
89 for(int y=0; y<height; y++) { | |
90 if(y in layer[x]) { | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
91 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
|
92 sprite = new Sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
93 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
|
94 sprite.setX(pos.x); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
95 sprite.setY(pos.y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
96 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
|
97 sprites ~= sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
98 } |
1 | 99 } |
0 | 100 } |
101 } | |
102 } | |
3 | 103 layer_id++; |
0 | 104 } |
105 return sprites; | |
106 } | |
107 } |