view 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
line wrap: on
line source

/*
    myrrdin, a 2d tile engine
    Copyright (c) 2008 Friedrich Weber

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the Software), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
 */

module myrrdin.tilemap;

import tango.math.Math;

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

import myrrdin.director;
import myrrdin.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;
	this.view = Director.renderer.app.getView();
    }

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