Skip to content

Instantly share code, notes, and snippets.

@nanonull
Created January 1, 2016 18:39
Show Gist options
  • Save nanonull/85a4de0c27572e5c1a66 to your computer and use it in GitHub Desktop.
Save nanonull/85a4de0c27572e5c1a66 to your computer and use it in GitHub Desktop.
public class ShipAiManager extends Manager {
public static ComponentMapper<ShipAiComponent> components;
public static final TestObstacles TEST_OBSTACLES = new TestObstacles();
private static Vector2 moveDirectionWip = new Vector2();
private static Vector2 start = new Vector2();
private static Vector2 finish = new Vector2();
private static float TEST_DISTANCE = SpasholeUtils.sceneToWorld(242f);
private static float MOVE_STEP_DISTANCE = SpasholeUtils.sceneToWorld(42f);
public static void process(int entityId) {
Box2dBodyComponent bodyComponent = Box2dBodySystem.components.get(entityId);
float bodyAngle = bodyComponent.body.getAngle();
MathUtilsExt.angleRadiansToVector(bodyAngle, moveDirectionWip);
start.set(bodyComponent.body.getPosition());
finish.set(bodyComponent.body.getPosition())
.add(moveDirectionWip.x * TEST_DISTANCE, moveDirectionWip.y * TEST_DISTANCE);
PointEffectHelper.createPoint(finish, Color.WHITE);
TEST_OBSTACLES.reset();
SpasholeApp.box2dWorld.rayCast(TEST_OBSTACLES, start, finish);
if (TEST_OBSTACLES.hasObstacles) {
SpasholeUtils.LOG.info("TEST_OBSTACLES.hasObstacles");
Box2dBodySystem.applyTorqueToRotateBy(
MathUtilsExt.toRadians(45),
bodyComponent.body);
} else {
Box2dBodySystem.applyTorqueToRotateBy(
MathUtilsExt.toRadians(3),
bodyComponent.body);
Box2dBodySystem.applyForceToMoveBy(
moveDirectionWip.x * MOVE_STEP_DISTANCE
, moveDirectionWip.y * MOVE_STEP_DISTANCE
, bodyComponent.body);
}
}
public static void createFor(int id) {
ShipAiComponent shipAiComponent = components.create(id);
IntervalExecutorSystem.schedule(1042, () -> {
// add ship ai
ShipAiManager.process(id);
}, id);
}
static class TestObstacles implements RayCastCallback {
public boolean hasObstacles;
@Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
hasObstacles = true;
return 0;
}
public void reset() {
hasObstacles = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment