Skip to content
Snippets Groups Projects
Commit d3f97461 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

lab06

parent ba7281bc
Branches
No related merge requests found
......@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vsb-cs-java1</groupId>
<artifactId>lab04</artifactId>
<artifactId>lab06</artifactId>
<version>0.0.1-SNAPHOST</version>
<packaging>jar</packaging>
<properties>
......
......@@ -7,15 +7,16 @@ import javafx.scene.image.Image;
public class BulletAnimated implements DrawableSimulable, Collisionable{
private static final double STRENGTH_CANNON_COEFICIENT = 4.;
private Point2D position;
private Point2D start;
private Point2D speed;
private Point2D initialSpeed;
private double size;
private double mass = 2;
private double strenghtOfCannon = 2;
private double cannonLength = 100;
private boolean accelerate = true;
private boolean accelerate = false;
private boolean hitToGround = false;
private double crossSectionalArea;
......@@ -24,7 +25,10 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
private Image image;
private World world;
private Cannon cannon;
private boolean fired;
private HitListener hitListener = new EmptyHitListener();
public BulletAnimated(World world, Cannon cannon) {
this(world, cannon, new Point2D(0, 0), new Point2D(0, 0), 10);
}
......@@ -32,7 +36,7 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) {
this.start = start;
this.position = this.start;
this.initialSpeed = speed;
this.initialSpeed = Point2D.ZERO;
this.speed = speed;
this.size = size;
this.world = world;
......@@ -49,9 +53,13 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
}
public void simulate(double deltaT) {
if (!fired) {
return;
}
double timeStep = deltaT * 1000;
if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle();
double strenghtOfCannon = cannon.getStrength() * STRENGTH_CANNON_COEFICIENT/100.;
speed = speed
.add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon)
.multiply(1 / mass));
......@@ -74,7 +82,6 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
reload();
}
}
public Rectangle2D getBoundingBox() {
......@@ -86,14 +93,25 @@ public class BulletAnimated implements DrawableSimulable, Collisionable{
}
public void hitBy(Collisionable other) {
hitListener.hit();
reload();
}
public void reload() {
position = start;
speed = initialSpeed;
speed = Point2D.ZERO;
hitToGround = false;
accelerate = false;
fired = false;
}
public void fire() {
if (fired) {
return;
}
fired = true;
accelerate = true;
speed = initialSpeed;
}
}
......@@ -8,11 +8,13 @@ import javafx.scene.transform.Affine;
public class Cannon implements DrawableSimulable {
private double angle = 0;
private double strength = 50.;
private Point2D position;
private Point2D size;
private World world;
public Cannon(World world, Point2D position, Point2D size) {
......@@ -50,4 +52,13 @@ public class Cannon implements DrawableSimulable {
public void setAngle(double doubleValue) {
angle = doubleValue;
}
public double getStrength() {
return strength;
}
public void setStrength(double doubleValue) {
this.strength = doubleValue;
}
}
package lab;
public class EmptyGameListener implements GameListener {
@Override
public void stateChanged(int shoots, int hits) {
}
@Override
public void gameOver() {
}
}
package lab;
public class EmptyHitListener implements HitListener {
@Override
public void hit() {
}
}
package lab;
package lab;
import javafx.animation.AnimationTimer;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
public class GameController {
......@@ -13,9 +14,18 @@ public class GameController {
@FXML
private Slider angleSlider;
@FXML
private Slider strengthSlider;
@FXML
private Canvas canvas;
@FXML
private Label shoots;
@FXML
private Label hits;
private AnimationTimer animationTimer;
public GameController() {
......@@ -39,6 +49,9 @@ public class GameController {
};
angleSlider.valueProperty().addListener(this::angleChanged);
world.setCannonAngle(angleSlider.getValue());
strengthSlider.valueProperty().addListener(this::strenghtChanged);
world.setCannonStrength(strengthSlider.getValue());
animationTimer.start();
}
......@@ -54,7 +67,7 @@ public class GameController {
@FXML
private void firePressed() {
System.out.println("Fire");
world.fire();
}
......@@ -63,5 +76,8 @@ public class GameController {
world.setCannonAngle(newValue.doubleValue());
}
private void strenghtChanged(ObservableValue<? extends Number> observable
, Number oldValue, Number newValue) {
world.setCannonStrength(newValue.doubleValue());
}
}
package lab;
public interface GameListener {
void stateChanged(int shoots, int hits);
void gameOver();
}
package lab;
public interface HitListener {
void hit();
}
......@@ -8,10 +8,17 @@ import javafx.scene.canvas.GraphicsContext;
public class World {
private final static int NUMBER_OF_DRAGONS = 5;
private final static int NUMBER_OF_DRAGONS = 2;
private double width;
private double height;
private GameListener gameListener = new EmptyGameListener();
private int hits = 0;
private int shoots = 0;
private DrawableSimulable []entities;
public World(double width, double height) {
......@@ -61,16 +68,6 @@ public class World {
}
}
}
/*bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
for(Dragon dragon: dragons) {
if (bulletAnimatted.overlaps(dragon)) {
dragon.hit();
bulletAnimatted.reload();
}
dragon.simulate(timeDelta);
}*/
}
public double getWidth() {
......@@ -98,5 +95,26 @@ public class World {
}
}
public void setCannonStrength(double doubleValue) {
for (DrawableSimulable d: entities) {
if (d instanceof Cannon) {
Cannon cannon = (Cannon) d;
cannon.setStrength(doubleValue);
}
}
}
public void fire() {
for (DrawableSimulable e: entities) {
if (e instanceof BulletAnimated) {
BulletAnimated ba = (BulletAnimated) e;
ba.fire();
shoots++;
gameListener.stateChanged(shoots, hits);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Group?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.GameController">
<center>
<Canvas fx:id="canvas" height="400.0" width="600.0" BorderPane.alignment="CENTER" />
</center>
<bottom>
<HBox alignment="CENTER" BorderPane.alignment="CENTER_LEFT">
<children>
......@@ -19,8 +21,31 @@
<HBox.margin>
<Insets bottom="11.0" left="14.0" right="11.0" top="12.0" />
</HBox.margin></Button>
<Slider HBox.hgrow="ALWAYS" />
<Slider fx:id="strengthSlider" showTickLabels="true" showTickMarks="true" value="50.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</bottom>
<center>
<Group BorderPane.alignment="CENTER">
<children>
<GridPane layoutX="20.0" layoutY="20.0" prefHeight="61.0" prefWidth="167.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="79.0" minWidth="10.0" prefWidth="51.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="124.0" minWidth="10.0" prefWidth="116.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Shoots:" />
<Label fx:id="shoots" text="0" GridPane.columnIndex="1" />
<Label text="Hits:" GridPane.rowIndex="1" />
<Label fx:id="hits" text="0" GridPane.rowIndex="1" GridPane.columnIndex="1" />
</children>
</GridPane>
<Canvas fx:id="canvas" height="400.0" width="600.0" />
</children>
</Group>
</center>
</BorderPane>
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