Package bomb

Source Code of bomb.Bomb

package bomb;

import java.util.Random;
import javafx.geometry.Point3D;
import javafx.scene.effect.BoxBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;
import javafx.scene.transform.Rotate;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Point2d;
import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;
import mainPac.BWNode;
import mainPac.World;
import plane.PlaneState;

public class Bomb extends BWNode {
   
    private static final Image image = new Image(Bomb.class.getResourceAsStream("bomb.png"));
    private static final Image explosion = new Image(Bomb.class.getResourceAsStream("explosion.gif"));
    ImageView imageView = new ImageView(image);
    ImageView explotionView = new ImageView(explosion);
    private final AudioClip bombSound = new AudioClip(Bomb.class.getResource("bomb.mp3").toString());
    private final AudioClip launchSound = new AudioClip(Bomb.class.getResource("launch.mp3").toString());
    Circle circle = new Circle(50, Color.web("white", 0.05));
    private BombPhysicalObject bombPhysicalObject= new BombPhysicalObject();
    private Rotate rotate = new Rotate(0, Rotate.X_AXIS);
    private BombState state= BombState.READY;
    private long stateSwitchTime= System.currentTimeMillis();
    private Random rand = new Random();

    public Bomb(World world) {
        super(world);
        setTranslateZ(1000);
        setTranslateY(1000);
        circle = new Circle(50, Color.web("white", 0.05));
        circle.setStrokeType(StrokeType.OUTSIDE);
        circle.setStroke(Color.web("white", 0.16));
        circle.setStrokeWidth(4);
  
        imageView.setTranslateX(-32);
        imageView.setTranslateY(-32);
        explotionView.setTranslateX(-71);
        explotionView.setTranslateY(-100);
        getChildren().addAll(circle, imageView);
        getTransforms().add(rotate);
    }

    @Override
    public void stepSimulation() {
        if(state==BombState.ACTIVE){
            bombPhysicalObject.stepSimulation();
            Quat4d q = bombPhysicalObject.getOrientation();
            rotate.setAxis(new Point3D(q.x, q.y, q.z));
            double radians = Math.acos(q.w) * 2;
            rotate.setAngle(Math.toDegrees(radians));

            Vector3d place = bombPhysicalObject.getPlace();
            setTranslateX(place.getX());
            setTranslateY(place.getY());

            Point2d planePoint= new Point2d(world.getPlane().getTranslateX(), world.getPlane().getTranslateY());
            if(isTouching(planePoint)){
                switchToExploding();
            }
            if(getTranslateY()>1000){
                switchToReady();
            }
        }
        else if(state==BombState.EXPLODING){
            long delta= System.currentTimeMillis()-stateSwitchTime;
            if(delta>1000){
                switchToReady();
            }
        }  
        else if(state==BombState.READY && world.getPlane().getState()==PlaneState.FLYING){
            long delta= System.currentTimeMillis()-stateSwitchTime;
            if(delta>2000){
                switchToActive();
            }
        }  
    }
   
    private void switchToActive(){
        state= BombState.ACTIVE;
        launchSound.play();
        double planeX= world.getPlane().getTranslateX();
        double deltaY= 1000- world.getPlane().getTranslateY();
        double planeSpeedX= world.getPlane().getPhysicalObject().getSpeed().x;
        double launchX= planeX+(deltaY*planeSpeedX*0.15);
        bombPhysicalObject.setPlace(new Vector3d(launchX, 1000, 1000));
        bombPhysicalObject.launch();
    }
   
    private void switchToExploding(){   
        state= BombState.EXPLODING;
        bombSound.play();
        stateSwitchTime= System.currentTimeMillis();
        getChildren().clear();
        getChildren().add(explotionView);               
        world.getPlane().kill();
    }
   
    private void switchToReady(){
        state= BombState.READY;
        stateSwitchTime= System.currentTimeMillis();       
        setTranslateY(1000);
        getChildren().clear();
        getChildren().addAll(circle, imageView);
    }
   
    private boolean isTouching(Point2d p){
        Point2d center= new Point2d(getTranslateX(), getTranslateY());      
        double dx= p.x-center.x;
        double dy= p.y-center.y;
        double distance= Math.sqrt(dx*dx + dy*dy);
        return distance<50;
    }
   
}
TOP

Related Classes of bomb.Bomb

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.