Skip to content
Snippets Groups Projects
Commit bdfbfb56 authored by koz01's avatar koz01
Browse files

solution_Wed_9-00

parent ba626e1a
No related merge requests found
Pipeline #70 failed with stages
in 0 seconds
......@@ -5,6 +5,7 @@ import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
......@@ -29,7 +30,7 @@ public class App extends Application {
Group root = new Group();
canvas = new Canvas(800, 400);
root.getChildren().add(canvas);
Scene scene = new Scene(root, 800, 400);
Scene scene = new Scene(root, 800, 400, Color.MAGENTA);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false);
......
package lab;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
......@@ -75,4 +76,12 @@ public class BulletAnimated {
accelerate = true;
}
public Rectangle2D getBB() {
return new Rectangle2D(position.getX(), position.getY(), size, size);
}
public boolean isInCannon() {
return accelerate;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class Dragon {
private static final int SIZE_OF_DRAGON = 50;
private final World world;
private Point2D position;
private Point2D velocity;
private final Point2D dimension;
private final Image image;
public Dragon(World world, Point2D position, Point2D velocity) {
this(world, position, velocity, new Point2D(SIZE_OF_DRAGON, SIZE_OF_DRAGON));
}
public Dragon(World world, Point2D position, Point2D velocity, Point2D dimension) {
this.world = world;
this.position = position;
this.velocity = velocity;
this.dimension = dimension;
this.image = Constants.DRAGON_IMAGE;
}
public void draw(GraphicsContext gc) {
Point2D p = world.getCanvasPoint(position);
gc.drawImage(image, p.getX(), p.getY(), dimension.getX(), dimension.getY());
}
public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT));
position = new Point2D((position.getX() + world.getWidth()) % world.getWidth(),
(position.getY() + world.getHeight()) % world.getHeight());
}
public Rectangle2D getBB() {
return new Rectangle2D(position.getX(), position.getY(), dimension.getX(), dimension.getY());
}
public void hit() {
velocity = velocity.multiply(-1);
}
}
package lab;
public class Proof {
public static void main(String[] args) {
System.out.println(-10.%20.);
}
}
......@@ -6,14 +6,22 @@ import javafx.scene.canvas.GraphicsContext;
public class World {
private double width;
private double height;
private BulletAnimated bulletAnimatted;
private Cannon cannon;
private final BulletAnimated bulletAnimatted;
private final Cannon cannon;
private final Dragon[] dragons;
public World(double width, double height) {
this.width = width;
this.height = height;
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
dragons = new Dragon[] {
new Dragon(this, new Point2D(110, 150), new Point2D(70, 70)),
new Dragon(this, new Point2D(110, 150), new Point2D(-70, 70)),
new Dragon(this, new Point2D(110, 150), new Point2D(70, -40)),
new Dragon(this, new Point2D(110, 150), new Point2D(-20, 70)),
new Dragon(this, new Point2D(110, 150), new Point2D(-70, 100))
};
}
public Point2D getCanvasPoint(Point2D worldPoint) {
......@@ -24,11 +32,27 @@ public class World {
gc.clearRect(0, 0, width, height);
cannon.draw(gc);
bulletAnimatted.draw(gc);
for (Dragon dragon : dragons) {
dragon.draw(gc);
}
}
public void simulate(double timeDelta) {
bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
for (Dragon dragon : dragons) {
dragon.simulate(timeDelta);
}
if (bulletAnimatted.isInCannon()) {
return;
}
for(Dragon dragon: dragons) {
if (dragon.getBB().intersects(bulletAnimatted.getBB())) {
dragon.hit();
bulletAnimatted.reload();
}
}
}
public double getWidth() {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment