Mercurial > projects > myrrdin
annotate import/myrrdin/tilemap.d @ 11:5866d9f2ca75 default tip
fixed director + tilemap stuff
author | fred@reichbier.de |
---|---|
date | Wed, 13 Aug 2008 18:31:10 +0200 |
parents | 806b3781f4e4 |
children |
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 |
7 | 24 module myrrdin.tilemap; |
0 | 25 |
26 import tango.math.Math; | |
27 | |
28 import dsfml.window.all; | |
29 import dsfml.system.all; | |
30 import dsfml.graphics.all; | |
31 | |
11 | 32 import myrrdin.director; |
6 | 33 import myrrdin.tileset; |
11 | 34 |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
35 import tango.io.Stdout; |
0 | 36 |
37 typedef Sprite[] SpriteArray; | |
38 | |
39 class Tilemap { | |
1 | 40 public int[int][int][int] map; // Layer: x: y: Tile-ID |
3 | 41 public Vector2i[int] layer_tsizes; // Layer: Tile size |
4
292df259cc85
view + sprite consumers, animated sprite working
fred@reichbier.de
parents:
3
diff
changeset
|
42 public Tileset tileset; |
0 | 43 public int width, height, tilewidth, tileheight; |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
44 private View view; |
0 | 45 |
46 this(Tileset tileset, int width, int height, int tilewidth, int tileheight) { | |
47 this.tileset = tileset; | |
48 this.width = width; | |
49 this.height = height; | |
50 this.tilewidth = tilewidth; | |
51 this.tileheight = tileheight; | |
11 | 52 this.view = Director.renderer.app.getView(); |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
53 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
54 |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
55 /* translate On-Screen -> With-View */ |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
56 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
|
57 Vector2i vec = Vector2i(real_x, real_y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
58 if(this.view) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
59 FloatRect rect = this.view.getRect(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
60 vec.x += rect.getLeft(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
61 vec.y += rect.getTop(); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
62 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
63 return vec; |
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 |
3 | 66 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
|
67 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
|
68 return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y))); |
0 | 69 } |
70 | |
3 | 71 Vector2i tile_to_real(int layer, int tile_x, int tile_y) { |
72 return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y); | |
0 | 73 } |
74 | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
75 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
|
76 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
|
77 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
78 |
0 | 79 SpriteArray get_sprites() { |
80 SpriteArray sprites; | |
81 Sprite sprite; | |
3 | 82 int layer_id=0; |
83 Vector2i pos; | |
1 | 84 foreach(int[int][int] layer; map) { |
85 for(int x=0; x<width; x++) { | |
86 if(x in layer) { | |
87 for(int y=0; y<height; y++) { | |
88 if(y in layer[x]) { | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
89 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
|
90 sprite = new Sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
91 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
|
92 sprite.setX(pos.x); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
93 sprite.setY(pos.y); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
94 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
|
95 sprites ~= sprite; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
4
diff
changeset
|
96 } |
1 | 97 } |
0 | 98 } |
99 } | |
100 } | |
3 | 101 layer_id++; |
0 | 102 } |
103 return sprites; | |
104 } | |
105 } |