'use strict';

import Sprite from './sprite';

export default class TiledMap {
	constructor(graphics, content) {
		this.graphics = graphics;
		this.content = content;
		this.sprite = new Sprite(0, 0, null);
		this.textures = {};
	}
	update(deltaTime) {
		// TODO update the animations
	}
	draw() {
		for (let layer of this.content.layers) {
			if (layer.type === 'tilelayer') {
				this.drawTileLayer(layer);
			}
		}
	}
	drawTileLayer(layer) {
		this.sprite.setColor(1.0, 1.0, 1.0, layer.opacity);
		for (let x = 0; x < layer.width; ++x) {
			for (let y = 0; y < layer.height; ++y) {
				let gid = layer.data[y * layer.width + x];
				this.drawTile(gid, x, y);
			}
		}
	}
	drawTile(gid, tileX, tileY) {
		if (gid === 0) return;
		
		for (let tileset of this.content.tilesets) {
			if (gid >= tileset.firstgid && gid <= tileset.firstgid + tileset.tilecount) {
				this.sprite.texture = this.textures[tileset.image];
				this.sprite.width = tileset.tilewidth;
				this.sprite.height = tileset.tileheight;
				this.sprite.setPosition(
					this.content.tilewidth * tileX,
					this.content.tileheight * tileY
				);
				
				let tilesetIndex = gid - tileset.firstgid;
				let tilesetX = tilesetIndex % tileset.columns;
				let tilesetY = Math.floor(tilesetIndex / tileset.columns);
				
				let textureX = tilesetX * tileset.tilewidth;
				let textureY = tilesetY * tileset.tileheight;
				this.sprite.setTextureCoordinates(
					textureX / tileset.imagewidth,
					textureY / tileset.imageheight,
					(textureX + tileset.tilewidth) / tileset.imagewidth,
					(textureY + tileset.tileheight) / tileset.imageheight
				);
				
				this.graphics.draw(this.sprite);
			}
		}
	}
	registerTexture(name, texture) {
		this.textures[name] = texture;
	}
}

@@ -237,64 +237,101 @@
 ;%0D%0A%09
-%7D%0D%0A%09update(deltaTime) %7B%0D%0A%09%09// TODO update the animations
+%09this.animationTime = 0;%0D%0A%09%7D%0D%0A%09update(deltaTime) %7B%0D%0A%09%09this.animationTime += deltaTime * 1000;
 %0D%0A%09%7D
@@ -792,18 +792,16 @@
 eturn;%0D%0A
-%09%09
 %0D%0A%09%09for 
@@ -1181,28 +1181,24 @@
 eY%0D%0A%09%09%09%09);%0D%0A
-%09%09%09%09
 %0D%0A%09%09%09%09let ti
@@ -1203,41 +1203,50 @@
 tile
-setIndex = gid - 
+Id = this.getEffectiveTileId(
 tileset
-.first
+, 
 gid
+)
 ;%0D%0A%09
@@ -1267,24 +1267,18 @@
 X = tile
-setIndex
+Id
  %25 tiles
@@ -1328,16 +1328,10 @@
 tile
-setIndex
+Id
  / t
@@ -1344,28 +1344,24 @@
 .columns);%0D%0A
-%09%09%09%09
 %0D%0A%09%09%09%09let te
@@ -1693,20 +1693,16 @@
 %09%09%09%09);%0D%0A
-%09%09%09%09
 %0D%0A%09%09%09%09th
@@ -1748,16 +1748,635 @@
 %09%7D%0D%0A%09%7D%0D%0A
+%09getEffectiveTileId(tileset, gid) %7B%0D%0A%09%09let tileId = gid - tileset.firstgid;%0D%0A%09%09if (tileset.tiles) %7B%0D%0A%09%09%09let tiledata = tileset.tiles%5BtileId%5D;%0D%0A%09%09%09if (tiledata) %7B%0D%0A%09%09%09%09let animation = tiledata.animation;%0D%0A%09%09%09%09if (animation) %7B%0D%0A%09%09%09%09%09let totalTime = 0;%0D%0A%09%09%09%09%09for (let frame of animation) %7B%0D%0A%09%09%09%09%09%09totalTime += frame.duration;%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%09let currentTime = Math.floor(this.animationTime) %25 totalTime;%0D%0A%09%09%09%09%09totalTime = 0;%0D%0A%09%09%09%09%09for (let frame of animation) %7B%0D%0A%09%09%09%09%09%09totalTime += frame.duration;%0D%0A%09%09%09%09%09%09if (totalTime %3E= currentTime) %7B%0D%0A%09%09%09%09%09%09%09return frame.tileid;%0D%0A%09%09%09%09%09%09%7D%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return tileId;%0D%0A%09%7D%0D%0A
 %09registe

Add animated tiles support to TiledMap