Mercurial > projects > myrrdin
view 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 |
line wrap: on
line source
/* This file is part of myrrdin. myrrdin is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. myrrdin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with myrrdin. If not, see <http://www.gnu.org/licenses/>. */ module tilemap; import tango.math.Math; import dsfml.window.all; import dsfml.system.all; import dsfml.graphics.all; import tileset; import tango.io.Stdout; typedef Sprite[] SpriteArray; class Tilemap { public int[int][int][int] map; // Layer: x: y: Tile-ID public Vector2i[int] layer_tsizes; // Layer: Tile size public Tileset tileset; public int width, height, tilewidth, tileheight; private View view; this(Tileset tileset, int width, int height, int tilewidth, int tileheight) { this.tileset = tileset; this.width = width; this.height = height; this.tilewidth = tilewidth; this.tileheight = tileheight; } /* set the view. you should do this if you use a sf::View */ void set_view(View view) { this.view = view; } /* translate On-Screen -> With-View */ Vector2i translate_with_view(int real_x, int real_y) { Vector2i vec = Vector2i(real_x, real_y); if(this.view) { FloatRect rect = this.view.getRect(); vec.x += rect.getLeft(); vec.y += rect.getTop(); } return vec; } Vector2i real_to_tile(int layer, int real_x, int real_y) { Vector2i vec = this.translate_with_view(real_x, real_y); return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y))); } Vector2i tile_to_real(int layer, int tile_x, int tile_y) { return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y); } bool has_tile(int layer, int x, int y) { return (layer in this.map && x in this.map[layer] && y in this.map[layer][x]); } SpriteArray get_sprites() { SpriteArray sprites; Sprite sprite; int layer_id=0; Vector2i pos; foreach(int[int][int] layer; map) { for(int x=0; x<width; x++) { if(x in layer) { for(int y=0; y<height; y++) { if(y in layer[x]) { if (layer[x][y] != 0) { // is no transparent tile? sprite = new Sprite; pos = tile_to_real(layer_id, x, y); sprite.setX(pos.x); sprite.setY(pos.y); sprite.setImage(this.tileset.tiles[layer[x][y]]); sprites ~= sprite; } } } } } layer_id++; } return sprites; } }