package net.mgsx.game.plugins.box2d.systems;

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.math.MathUtils;

import net.mgsx.game.core.GamePipeline;
import net.mgsx.game.plugins.box2d.components.Box2DBodyModel;
import net.mgsx.game.plugins.core.components.SlavePhysics;
import net.mgsx.game.plugins.core.components.Transform2DComponent;

public class Box2DSlaveSystem extends IteratingSystem {
	public Box2DSlaveSystem() {
		super(Family.all(Box2DBodyModel.class, Transform2DComponent.class, SlavePhysics.class).get(), GamePipeline.AFTER_LOGIC);
	}

	@Override
	public void processEntity(Entity entity, float deltaTime) {
		Box2DBodyModel physic = entity.getComponent(Box2DBodyModel.class);
		Transform2DComponent t = entity.getComponent(Transform2DComponent.class);
		if(physic.body != null){
			physic.body.setLinearVelocity(t.position.cpy().sub(physic.body.getPosition()).scl(1.f / 30f)); // TODO not good neither ? timestep ?
			if(t.rotation){
				float a1 = t.angle * MathUtils.degreesToRadians;
				float a2 = physic.body.getAngle();
				float delta = a2 - a1; // TODO not good !
				physic.body.setAngularVelocity(delta);
			}
		}
	}
}
@@ -210,16 +210,54 @@
 thUtils;
+%0Aimport com.badlogic.gdx.math.Vector2;
 %0A%0Aimport
@@ -290,16 +290,62 @@
 peline;%0A
+import net.mgsx.game.core.annotations.Inject;%0A
 import n
@@ -581,16 +581,95 @@
 ystem %7B%0A
+%09%0A%09@Inject Box2DWorldSystem world;%0A%09%0A%09private Vector2 delta = new Vector2();%0A%09%0A
 %09public 
@@ -1060,16 +1060,46 @@
  null)%7B%0A
+%09%09%09float ts = world.timeStep;%0A
 %09%09%09physi
@@ -1123,16 +1123,26 @@
 elocity(
+delta.set(
 t.positi
@@ -1147,13 +1147,8 @@
 tion
-.cpy(
 ).su
@@ -1185,57 +1185,17 @@
 cl(1
-.
 f / 
-30f)); // TODO not good neither ? timestep ?
+ts));
 %0A%09%09%09

fix invert physics