From 520b0225e19a4bcae03fedfa13d2102bc4fac4f0 Mon Sep 17 00:00:00 2001 From: Alexander Vardanyan Date: Mon, 23 Oct 2023 16:23:35 +0200 Subject: [PATCH] Fixed vibration --- src/main/java/doodle_jump/Game.java | 30 ++------ src/main/java/game_engine/BaseElement.java | 89 +--------------------- src/main/java/game_engine/GameObject.java | 34 ++++----- 3 files changed, 24 insertions(+), 129 deletions(-) diff --git a/src/main/java/doodle_jump/Game.java b/src/main/java/doodle_jump/Game.java index 49d3c59..5be8fc7 100644 --- a/src/main/java/doodle_jump/Game.java +++ b/src/main/java/doodle_jump/Game.java @@ -1,6 +1,6 @@ package doodle_jump; -import game_engine.BaseElement; +import game_engine.BaseGame; import game_engine.Window; import java.awt.Image; import java.awt.event.KeyAdapter; @@ -8,13 +8,10 @@ import physics.Vector; import utils.ImageUploader; - /** * Class represents game. */ -public class Game extends BaseElement { - private Window window; - +public class Game extends BaseGame { private MainCharacter doodle; private PlatformCollection platforms; @@ -33,12 +30,8 @@ public class Game extends BaseElement { * Constructor. */ public Game() { - super(BG_IMAGE); - this.window = new Window(WIDTH, HEIGHT); - this.window.add(this); + super(WIDTH, HEIGHT, BG_IMAGE); this.addKeyListener(new MovingDoodleKeyListener()); - this.setFocusable(true); // Make sure the panel is focusable - this.requestFocusInWindow(); // Request focus on the panel } /** @@ -71,19 +64,9 @@ protected void actionBegin() { if (this.doodle.getY() > this.getHeight()) { endGame(); } else { - Vector doodleSpeedVector = this.doodle.getSpeedVector(); - if (Math.abs(doodleSpeedVector.x) > 0.1) { - double resistance = Math.abs(doodleSpeedVector.x) * 0.02; - doodleSpeedVector.x += Math.signum(doodleSpeedVector.x) * resistance; - } else { - doodleSpeedVector.x = 0; - } - - this.doodle.setSpeedVector(doodleSpeedVector); - for (Platform p : this.platforms.list) { - if (doodle.getSpeedVector().y > 0 - && doodle.getRectangle().intersects(p.getRectangle())) { + if (doodle.getSpeedVector().y > 0 + && doodle.getRectangle().intersects(p.getRectangle())) { // doodle.setCoordinateY((int) p.getBounds().getY() - doodle.getHeight()); doodle.jump(); } @@ -99,9 +82,8 @@ protected void actionBegin() { * Render game. */ protected void actionEnd() { - this.window.validate(); } - + /** * Init game. * Init all objects on panel and add them on panel. diff --git a/src/main/java/game_engine/BaseElement.java b/src/main/java/game_engine/BaseElement.java index 00dd4be..247fb9c 100644 --- a/src/main/java/game_engine/BaseElement.java +++ b/src/main/java/game_engine/BaseElement.java @@ -10,16 +10,7 @@ public abstract class BaseElement extends JPanel { protected Image image; protected Point2D.Double coordinates; protected ArrayList animatedElements; - - protected Thread thread; - private boolean runAnimation; - - private static final int NANOSECONDS_PER_SECOND = 1000000000; - private static final int MILLISECONDS_PER_SECOND = 1000000; - - private static final int FPS = 60; - private static final int TARGET_TIME = NANOSECONDS_PER_SECOND / FPS; - + /** * Constructor. * @param x x-coordinate of top-left corner. @@ -146,82 +137,4 @@ protected void paintComponent(Graphics g) { this); } } - - /** - * Get if animation is still running. - * - * @return boolean (true - animation is running, falce - not). - */ - protected boolean isRunAnimation() { - return this.runAnimation; - } - - /** - * Interupt animation. - */ - protected void stopAnimation() { - this.runAnimation = false; - - for (BaseElement element: animatedElements) { - element.stopAnimation(); - } - } - - /** - * Run animation by creating animation thread and starting it. - * Subclasses can change plot of animation by defining abstract - * methods preAction and postAction. - */ - protected void runAnimation() { - this.runAnimation = true; - - for (BaseElement element: animatedElements) { - element.runAnimation(); - } - - this.thread = new Thread(new Runnable() { - @Override - public void run() { - while (runAnimation) { - actionBegin(); - - long startTime = System.nanoTime(); - long time = System.nanoTime() - startTime; - - if (time < TARGET_TIME) { - long sleepTime = (TARGET_TIME - time) / MILLISECONDS_PER_SECOND; - sleep(sleepTime); - } - - actionEnd(); - } - } - }); - - this.thread.start(); - } - - /** - * Actions which are performed in the start of animation cycle. - */ - protected abstract void actionBegin(); - - /** - * Actions which are performed in the end of animation cycle. - */ - protected abstract void actionEnd(); - - /** - * Stop animation for specific time in order to work with defined FPS. - * - * @param time time which animation is stopped. - */ - private void sleep(long time) { - try { - Thread.sleep(time); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } - } - } \ No newline at end of file diff --git a/src/main/java/game_engine/GameObject.java b/src/main/java/game_engine/GameObject.java index 8bead53..0cbf7a8 100644 --- a/src/main/java/game_engine/GameObject.java +++ b/src/main/java/game_engine/GameObject.java @@ -1,6 +1,5 @@ package game_engine; - import java.awt.Image; import java.awt.Rectangle; import physics.Vector; @@ -11,19 +10,20 @@ public abstract class GameObject extends BaseElement { protected Vector speedVector; protected Vector boostVector; - /** * Constructor. - * @param x x-coordinate of top-left corner. - * @param y y-coordinate of top-left cordner. - * @param image object image. - * @param teleporting boolean (true - object teleports throught walls, false - not) - * @param xLeftLimit the left limit for x-coordinate. + * + * @param x x-coordinate of top-left corner. + * @param y y-coordinate of top-left cordner. + * @param image object image. + * @param teleporting boolean (true - object teleports throught walls, false - + * not) + * @param xLeftLimit the left limit for x-coordinate. * @param xRightLimit the right limit for x-coordinate. */ public GameObject( - int x, int y, Image image, boolean teleporting, double xLeftLimit, double xRightLimit) { + int x, int y, Image image, boolean teleporting, double xLeftLimit, double xRightLimit) { super(x, y, image); this.speedVector = new Vector(); this.boostVector = new Vector(); @@ -39,6 +39,7 @@ public GameObject(Image image) { /** * Set speed-vector. + * * @param vector speed-vector. */ public void setSpeedVector(Vector vector) { @@ -51,6 +52,7 @@ public void setSpeedVector(Vector vector) { /** * Get speed vector. + * * @return speed vector. */ public Vector getSpeedVector() { @@ -59,6 +61,7 @@ public Vector getSpeedVector() { /** * Set boost vector + * * @param vector boost vector. */ public void setBoostVector(Vector vector) { @@ -68,9 +71,10 @@ public void setBoostVector(Vector vector) { this.boostVector.setLocation(vector); } - + /** * Get boost vector. + * * @return boost vector. */ public Vector getBoostVector() { @@ -92,6 +96,7 @@ public void multiplyBoostVector(double value) { /** * Add to speed-vector. * Method recieves vector. + * * @param addingVector adding vector. */ public void addToSpeedVector(Vector addingVector) { @@ -101,6 +106,7 @@ public void addToSpeedVector(Vector addingVector) { /** * Subtract from speed-vector. * Method receives vector. + * * @param substractingVector subtracting vector. */ public void subtractFromSpeedVector(Vector substractingVector) { @@ -109,6 +115,7 @@ public void subtractFromSpeedVector(Vector substractingVector) { /** * Multiply speed-vector by number. + * * @param numb number. */ public void multiplySpeedVector(double numb) { @@ -124,17 +131,10 @@ public void move() { this.setCoordinates(x, y); } - @Override - protected void actionBegin() { - move(); - } - - @Override - protected void actionEnd() {} - /** * Get rectangle which cover the whole object. * Rectangle is required to calculate collision. + * * @return rectangle which is used to calculate collision. */ public Rectangle getRectangle() {