view src/tilemap.d @ 0:a2d653eb9e99

first working version.
author fred@reichbier.de
date Thu, 17 Jul 2008 18:52:55 +0200
parents
children f193d0c14685
line wrap: on
line source

module tilemap;

import tango.math.Math;

import dsfml.window.all;
import dsfml.system.all;
import dsfml.graphics.all;

import tileset;

typedef Sprite[] SpriteArray;

class Tilemap {
    public int[int][int] map;
    private Tileset tileset;
    public int width, height, tilewidth, tileheight;

    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;
    }

    void real_to_tile(int real_x, int real_y, inout int tile_x, inout int tile_y) {
	tile_x = rndint(floor(real_x / this.tilewidth));
	tile_y = rndint(floor(real_y / this.tileheight));
    }

    void tile_to_real(int tile_x, int tile_y, inout int real_x, inout int real_y) {
	real_x = tile_x*this.tilewidth;
	real_y = tile_y*this.tileheight;
    }

    SpriteArray get_sprites() {
	SpriteArray sprites;
	Sprite sprite;
	int real_x, real_y;
	for(int x=0; x<width; x++) {
	    if(x in this.map) {
		for(int y=0; y<height; y++) {
		    if(y in this.map[x]) {
			sprite = new Sprite;
			tile_to_real(x, y, real_x, real_y);
			sprite.setX(real_x);
			sprite.setY(real_y);
			sprite.setImage(this.tileset.tiles[this.map[x][y]]);
			sprites ~= sprite;
		    }
		}
	    }
	}
	return sprites;	
    }
}