package uk.co.flyingsquirrels.models;
import static uk.co.flyingsquirrels.aero.WingFactory.Type.NACA_63018;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;
import uk.co.flyingsquirrels.AdAstra;
import uk.co.flyingsquirrels.SelfRenderable;
import uk.co.flyingsquirrels.aero.Wing;
import uk.co.flyingsquirrels.aero.WingFactory;
import uk.co.flyingsquirrels.rendering.ShapeFactory;
import uk.co.flyingsquirrels.utils.MathUtils;
public class Glider extends Aircraft implements SelfRenderable {
public static Vec2 INITIAL_POSITION = new Vec2(0, 10);
public static Vec2 INITIAL_VELOCITY = new Vec2(30, 0);
private Shape shape;
private Control throttle;
private Control elevator;
public Glider(World world) {
shape = ShapeFactory.loadFromSvg("graphics/asw20.svg", 7, 30, -20);
BodyDef bd = new BodyDef();
Body body = world.createBody(bd);
PolygonDef polygonDef = new PolygonDef();
polygonDef.addVertex(new Vec2(3.5f, 0f));
polygonDef.addVertex(new Vec2(2, 0.5f));
polygonDef.addVertex(new Vec2(-0.5f, 0f));
polygonDef.addVertex(new Vec2(-2f, 0f));
polygonDef.addVertex(new Vec2(-2.5f, 1f));
polygonDef.addVertex(new Vec2(-3.5f, 1f));
polygonDef.addVertex(new Vec2(-3.5f, -0.25f));
polygonDef.addVertex(new Vec2(2f, -0.5f));
polygonDef.density = 75.0f;
polygonDef.restitution = 0.2f;
body.createShape(polygonDef);
body.setMassFromShapes();
setBody(body);
Wing wing = WingFactory.createWing(NACA_63018, 10.5f, MathUtils
.toRadians(3), new Vec2(-0.125f, 0), this);
Wing stab = WingFactory.createWing(NACA_63018, 3.5f, 0, new Vec2(-3.5f,
0), this, new Control(MathUtils.toRadians(-15), MathUtils
.toRadians(15)));
setWing(wing);
setStab(stab);
elevator = stab.getControl();
Engine engine = new Engine(1250, new Vec2(1, 0), new Vec2(0, 0));
addEngine(engine);
throttle = engine.getControl();
}
public Vec2 getInitialPosition() {
return INITIAL_POSITION;
}
public Vec2 getInitialVelocity() {
return INITIAL_VELOCITY;
}
public void renderToWorld(Graphics g) {
Vec2 position = getPosition();
Transform translateTransform = Transform.createTranslateTransform(
position.x, position.y);
Transform rotationTransform = Transform
.createRotateTransform(getPitch());
Transform transform = translateTransform.concatenate(rotationTransform);
g.setColor(Color.white);
g.draw(shape.transform(transform));
}
public void renderToScreen(int width, int height, Graphics g) {
String[] status = new String[5];
status[0] = String.format("Speed: %.0fkts", getVelocity().length() * 1.94384449);
status[1] = String.format("Altitude: %.0fft", getAltitude() * 3.2808399);
status[2] = String.format("AoA: %.1f", MathUtils
.toDegrees(getAlpha()));
status[3] = String.format("Elev: %.0f", MathUtils
.toDegrees(getStab().getControl().getValue()));
status[4] = String.format("Thr: %.1f", getEngine().getControl().getValue());
int x = width - 100;
int y = 0;
for (String line: status) {
g.drawString(line, x, y);
y += 12;
}
}
public void handleInput(Input input) {
elevator.setValue(MathUtils.map(input.getMouseY(), 1, AdAstra.DISPLAY_HEIGHT,
MathUtils.toRadians(15), MathUtils.toRadians(-15)));
// TODO: register for mouse wheel events
if (input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {
throttle.increment(0.1f);
}
if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
throttle.increment(-0.1f);
}
}
}