package plane;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.transform.Rotate;
import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;
import mainPac.BWNode;
import mainPac.World;
import highscores.SubmitScorePane;
public class Plane extends BWNode {
private PlanePhysicalObject planePhysicalObject;
private Rotate rotate;
private PlaneState state= PlaneState.READY;
private int lives;
public Plane(World world) {
super(world);
setTranslateZ(1000);
Wings upperWings = new Wings(Color.RED.deriveColor(0.0, 1.0, 0.5, 1.0));
upperWings.setRotationAxis(Rotate.X_AXIS);
upperWings.setRotate(-90);
upperWings.setTranslateY(-16);
Wings lowerWings = new Wings(Color.YELLOW);
lowerWings.setRotationAxis(Rotate.X_AXIS);
lowerWings.setRotate(-90);
lowerWings.setTranslateY(-15);
Fuselage body = new Fuselage();
Tail tail = new Tail();
tail.setRotationAxis(Rotate.X_AXIS);
tail.setRotate(90);
tail.setTranslateZ(-43);
tail.setTranslateY(-14);
getChildren().addAll(body, upperWings, lowerWings, tail);
init();
}
public void init() {
lives= 3;
planePhysicalObject = new PlanePhysicalObject();
rotate = new Rotate(0, Rotate.X_AXIS);
getTransforms().clear();
getTransforms().add(rotate);
setPlacement();
}
@Override
public void stepSimulation() {
if (state == PlaneState.FLYING) {
planePhysicalObject.airborneSimulationStep();
} else if (state == PlaneState.FALLING) {
planePhysicalObject.fallingSimulationStep();
if (getTranslateY() > 700) {
switchToGrounded();
}
} else if (state == PlaneState.GROUNDED) {
planePhysicalObject.groundedSimulationStep();
double length = planePhysicalObject.getSpeed().length();
if (length < 0.3) {
switchToDead();
}
} else if (state == PlaneState.DEAD) {
}
planePhysicalObject.stepSimulation();
setPlacement();
}
private void setPlacement() {
Vector3d place = planePhysicalObject.getPlace();
setTranslateX(place.getX());
setTranslateY(place.getY());
Quat4d q = planePhysicalObject.getOrientation();
double radians = Math.acos(q.w) * 2;
if(!Double.isNaN(radians)){
rotate.setAxis(new Point3D(q.x, q.y, q.z));
rotate.setAngle(Math.toDegrees(radians));
}
}
private void switchToGrounded() {
state = PlaneState.GROUNDED;
Vector3d newSpeed = new Vector3d(planePhysicalObject.getSpeed().x, 0, planePhysicalObject.getSpeed().z);
Vector3d newPlace = new Vector3d(planePhysicalObject.getPlace().x, 700, planePhysicalObject.getPlace().z);
planePhysicalObject.setSpeed(newSpeed);
planePhysicalObject.setPlace(newPlace);
}
private void switchToDead() {
state = PlaneState.DEAD;
planePhysicalObject.setSpeed(new Vector3d(0, 0, 0));
world.getHud().getStartButton().setDisable(false);
world.getSubmitScorePane().setTranslateX(getTranslateX()-100);
world.getSubmitScorePane().setVisible(true);
world.getSubmitScorePane().setScore(world.getHud().getTime());
world.getSubmitScorePane().getNameTf().requestFocus();
}
public PlanePhysicalObject getPhysicalObject() {
return planePhysicalObject;
}
public PlaneState getState() {
return state;
}
public void setState(PlaneState newState){
this.state= newState;
}
public void kill() {
lives-= 1;
if(lives==0){
state = PlaneState.FALLING;
}
}
public int getLives(){
return lives;
}
}