package de.fe1k.game9.components;

import de.fe1k.game9.entities.Entity;
import de.fe1k.game9.events.*;
import de.fe1k.game9.utils.Direction;
import de.nerogar.noise.input.InputHandler;
import de.nerogar.noise.input.KeyboardKeyEvent;
import de.nerogar.noise.util.Logger;
import de.nerogar.noise.util.Vector2f;
import org.lwjgl.glfw.GLFW;

@Depends(components = { ComponentMoving.class })
public class ComponentControllable extends Component {

	private InputHandler inputs;
	private float   jumpPower  = 0;
	private boolean wasKeyDown = false;

	private int moveDirection = 1;

	private EventListener<EventUpdate>    eventUpdate;
	private EventListener<EventCollision> eventCollision;

	public ComponentControllable(InputHandler inputHandler) {
		this.inputs = inputHandler;
		eventUpdate = this::update;
		eventCollision = this::collision;
		Event.register(EventUpdate.class, eventUpdate);
		Event.register(EventCollision.class, eventCollision);
	}

	private void update(EventUpdate event) {
		ComponentMoving moving = getOwner().getComponent(ComponentMoving.class);
		boolean isKeyDown = inputs.isKeyDown(' ');
		boolean onGround = moving.touching[Direction.DOWN.val];

		if (onGround) {
			jumpPower = 1;
		}

		if (isKeyDown && !(onGround && wasKeyDown)) {
			moving.velocity.add(moving.gravity.multiplied(-0.09f * jumpPower));
			jumpPower *= 1 - (10 * event.deltaTime);
		}

		moving.velocity.setX(10f * moveDirection);
		wasKeyDown = isKeyDown;

		for (KeyboardKeyEvent keyboardKeyEvent : inputs.getKeyboardKeyEvents()) {
			if (keyboardKeyEvent.action == GLFW.GLFW_PRESS && keyboardKeyEvent.key == GLFW.GLFW_KEY_ENTER) {
				moveDirection *= -1;

				Vector2f scale = getOwner().getScale().setX(moveDirection);
			}
		}

	}

	private void collision(EventCollision event) {
		if (!event.movingComponent.getOwner().equals(getOwner())) {
			return;
		}
		if (event.collisionDirection.isHorizontal()) {
			// death
			Event.trigger(new EventEntityDestroyed(getOwner()));
			resetPosition();
		}
	}

	public void resetPosition() {
		ComponentStartMarker start = Entity.getFirstComponent(ComponentStartMarker.class);
		if (start == null) {
			Logger.getErrorStream().printf("No start marker found, can't teleport to start!");
			return;
		}
		getOwner().teleport(start.getOwner().getPosition());
	}

	@Override
	public void destroy() {
		Event.unregister(EventUpdate.class, eventUpdate);
		Event.unregister(EventCollision.class, eventCollision);
	}

}

@@ -268,47 +268,8 @@
 er;%0A
-import de.nerogar.noise.util.Vector2f;%0A
 impo
@@ -512,16 +512,18 @@
 ate int 
+  
 moveDire
@@ -528,19 +528,77 @@
 rection 
-= 1
+      = 1;%0A%09private float targetMoveDirection = moveDirection
 ;%0A%0A%09priv
@@ -1672,30 +1672,195 @@
 -1;%0A
-%0A%09%09%09%09Vector2f scale = 
+%09%09%09%7D%0A%09%09%7D%0A%09%09if (targetMoveDirection %3C moveDirection) targetMoveDirection += 10 * event.deltaTime;%0A%09%09if (targetMoveDirection %3E moveDirection) targetMoveDirection -= 10 * event.deltaTime;%0A%09%09
 getO
@@ -1882,17 +1882,23 @@
 ().setX(
-m
+targetM
 oveDirec
@@ -1903,25 +1903,16 @@
 ection);
-%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%7D%0A%0A%09p

add short turning animation for player