package net.alcuria.umbracraft.engine.components;

import net.alcuria.umbracraft.Config;
import net.alcuria.umbracraft.Game;
import net.alcuria.umbracraft.engine.entities.Entity;
import net.alcuria.umbracraft.engine.entities.EntityManager.EntityScope;
import net.alcuria.umbracraft.util.StringUtils;

public class PlatformComponent implements Component {

	private void checkCollision(final Entity entity, final Entity otherEntity) {
		MapCollisionComponent otherCollision = otherEntity.getComponent(MapCollisionComponent.class);
		if (otherCollision != null) {
			int x = (int) (entity.position.x / Config.tileWidth);
			int y = (int) (entity.position.y / Config.tileWidth);
			int otherX = (int) (otherEntity.position.x / Config.tileWidth);
			int otherY = (int) (otherEntity.position.y / Config.tileWidth);
			if (x == otherX && y == otherY) {
				otherCollision.setOnPlatform();
				otherEntity.velocity.z = 0;
			}
		}
	}

	@Override
	public void create(Entity entity) {

		int centerX = (int) (entity.position.x) / Config.tileWidth;
		int centerY = (int) (entity.position.y) / Config.tileWidth;
		int altitude = Game.map().getAltitudeAt(centerX, centerY);
		if (entity.getArguments().size > 0) {
			if (StringUtils.isNumber(entity.getArguments().get(0))) {
				altitude += Integer.valueOf(entity.getArguments().get(0));
			}
		}
		entity.position.z = altitude * Config.tileWidth;
		entity.position.x += Config.tileWidth / 2;
		entity.position.y += Config.tileWidth / 2; // TODO: should this be origin?
	}

	@Override
	public void dispose(Entity entity) {

	}

	@Override
	public void render(Entity entity) {

	}

	@Override
	public void update(Entity entity) {
		for (EntityScope scope : EntityScope.values()) {
			for (final Entity otherEntity : Game.entities().getEntities(scope)) {
				checkCollision(entity, otherEntity);
			}
		}

	}

}

@@ -298,61 +298,244 @@
 s;%0A%0A
-public class PlatformComponent implements Component %7B
+import com.badlogic.gdx.graphics.Texture;%0Aimport com.badlogic.gdx.math.Rectangle;%0A%0Apublic class PlatformComponent implements Component %7B%0A%0A%09private final Rectangle bounds = new Rectangle(0, 0, Config.tileWidth + 2, Config.tileWidth + 2);
 %0A%0A%09p
@@ -1794,60 +1794,283 @@
 ) %7B%0A
-%0A%09%7D%0A%0A%09@Override%0A%09public void update(Entity entity) %7B
+%09%09if (Game.isDebug()) %7B%0A%09%09%09Game.batch().draw(Game.assets().get(%22debug.png%22, Texture.class), bounds.x, bounds.y + entity.position.z, bounds.width, bounds.height);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void update(Entity entity) %7B%0A%09%09bounds.setCenter(entity.position.x, entity.position.y);
 %0A%09%09f
@@ -2244,11 +2244,10 @@
 %09%09%7D%0A%0A%09%7D%0A
-%0A
 %7D%0A

Add rectangle to platforms