Skip to content

Commit

Permalink
Fixed vibration
Browse files Browse the repository at this point in the history
  • Loading branch information
alex8399 committed Oct 23, 2023
1 parent 95e5a8f commit 520b022
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 129 deletions.
30 changes: 6 additions & 24 deletions src/main/java/doodle_jump/Game.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package doodle_jump;

import game_engine.BaseElement;
import game_engine.BaseGame;
import game_engine.Window;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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;

Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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();
}
Expand All @@ -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.
Expand Down
89 changes: 1 addition & 88 deletions src/main/java/game_engine/BaseElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ public abstract class BaseElement extends JPanel {
protected Image image;
protected Point2D.Double coordinates;
protected ArrayList<BaseElement> 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.
Expand Down Expand Up @@ -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);
}
}

}
34 changes: 17 additions & 17 deletions src/main/java/game_engine/GameObject.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package game_engine;


import java.awt.Image;
import java.awt.Rectangle;
import physics.Vector;
Expand All @@ -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();
Expand All @@ -39,6 +39,7 @@ public GameObject(Image image) {

/**
* Set speed-vector.
*
* @param vector speed-vector.
*/
public void setSpeedVector(Vector vector) {
Expand All @@ -51,6 +52,7 @@ public void setSpeedVector(Vector vector) {

/**
* Get speed vector.
*
* @return speed vector.
*/
public Vector getSpeedVector() {
Expand All @@ -59,6 +61,7 @@ public Vector getSpeedVector() {

/**
* Set boost vector
*
* @param vector boost vector.
*/
public void setBoostVector(Vector vector) {
Expand All @@ -68,9 +71,10 @@ public void setBoostVector(Vector vector) {

this.boostVector.setLocation(vector);
}

/**
* Get boost vector.
*
* @return boost vector.
*/
public Vector getBoostVector() {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -109,6 +115,7 @@ public void subtractFromSpeedVector(Vector substractingVector) {

/**
* Multiply speed-vector by number.
*
* @param numb number.
*/
public void multiplySpeedVector(double numb) {
Expand All @@ -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() {
Expand Down

0 comments on commit 520b022

Please sign in to comment.