mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 01:26:35 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -27,20 +27,19 @@ package com.iluwatar.gameloop;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A game loop runs continuously during gameplay. Each turn of the loop, it processes
|
||||
* user input without blocking, updates the game state, and renders the game. It tracks
|
||||
* the passage of time to control the rate of gameplay.
|
||||
* A game loop runs continuously during gameplay. Each turn of the loop, it processes user input
|
||||
* without blocking, updates the game state, and renders the game. It tracks the passage of time to
|
||||
* control the rate of gameplay.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Each type of game loop will run for 2 seconds.
|
||||
*/
|
||||
/** Each type of game loop will run for 2 seconds. */
|
||||
private static final int GAME_LOOP_DURATION_TIME = 2000;
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args runtime arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
@@ -71,5 +70,4 @@ public class App {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,14 +27,10 @@ package com.iluwatar.gameloop;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Bullet object class.
|
||||
*/
|
||||
/** Bullet object class. */
|
||||
public class Bullet {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private float position;
|
||||
@Getter @Setter private float position;
|
||||
|
||||
public Bullet() {
|
||||
position = 0.0f;
|
||||
|
||||
@@ -25,15 +25,13 @@
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* For fixed-step game loop, a certain amount of real time has elapsed since the
|
||||
* last turn of the game loop. This is how much game time need to be simulated for
|
||||
* the game’s “now” to catch up with the player’s.
|
||||
* For fixed-step game loop, a certain amount of real time has elapsed since the last turn of the
|
||||
* game loop. This is how much game time need to be simulated for the game’s “now” to catch up with
|
||||
* the player’s.
|
||||
*/
|
||||
public class FixedStepGameLoop extends GameLoop {
|
||||
|
||||
/**
|
||||
* 20 ms per frame = 50 FPS.
|
||||
*/
|
||||
/** 20 ms per frame = 50 FPS. */
|
||||
private static final long MS_PER_FRAME = 20;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,12 +25,11 @@
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* Frame-based game loop is the easiest implementation. The loop always keeps spinning
|
||||
* for the following three processes: processInput, update and render. The problem with
|
||||
* it is you have no control over how fast the game runs. On a fast machine, that loop
|
||||
* will spin so fast users won’t be able to see what’s going on. On a slow machine, the
|
||||
* game will crawl. If you have a part of the game that’s content-heavy or does more AI
|
||||
* or physics, the game will actually play slower there.
|
||||
* Frame-based game loop is the easiest implementation. The loop always keeps spinning for the
|
||||
* following three processes: processInput, update and render. The problem with it is you have no
|
||||
* control over how fast the game runs. On a fast machine, that loop will spin so fast users won’t
|
||||
* be able to see what’s going on. On a slow machine, the game will crawl. If you have a part of the
|
||||
* game that’s content-heavy or does more AI or physics, the game will actually play slower there.
|
||||
*/
|
||||
public class FrameBasedGameLoop extends GameLoop {
|
||||
|
||||
@@ -44,11 +43,10 @@ public class FrameBasedGameLoop extends GameLoop {
|
||||
}
|
||||
|
||||
/**
|
||||
* Each time when update() is invoked, a new frame is created, and the bullet will be
|
||||
* moved 0.5f away from the current position.
|
||||
* Each time when update() is invoked, a new frame is created, and the bullet will be moved 0.5f
|
||||
* away from the current position.
|
||||
*/
|
||||
protected void update() {
|
||||
controller.moveBullet(0.5f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,16 +25,14 @@
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* Update and render objects in the game. Here we add a Bullet object to the
|
||||
* game system to show how the game loop works.
|
||||
* Update and render objects in the game. Here we add a Bullet object to the game system to show how
|
||||
* the game loop works.
|
||||
*/
|
||||
public class GameController {
|
||||
|
||||
protected final Bullet bullet;
|
||||
|
||||
/**
|
||||
* Initialize Bullet instance.
|
||||
*/
|
||||
/** Initialize Bullet instance. */
|
||||
public GameController() {
|
||||
bullet = new Bullet();
|
||||
}
|
||||
@@ -57,6 +55,4 @@ public class GameController {
|
||||
public float getBulletPosition() {
|
||||
return bullet.getPosition();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -28,9 +28,7 @@ import java.security.SecureRandom;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Abstract class for GameLoop implementation class.
|
||||
*/
|
||||
/** Abstract class for GameLoop implementation class. */
|
||||
public abstract class GameLoop {
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
@@ -39,26 +37,20 @@ public abstract class GameLoop {
|
||||
|
||||
protected final GameController controller;
|
||||
|
||||
/**
|
||||
* Initialize game status to be stopped.
|
||||
*/
|
||||
/** Initialize game status to be stopped. */
|
||||
protected GameLoop() {
|
||||
controller = new GameController();
|
||||
status = GameStatus.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run game loop.
|
||||
*/
|
||||
/** Run game loop. */
|
||||
public void run() {
|
||||
status = GameStatus.RUNNING;
|
||||
Thread gameThread = new Thread(this::processGameLoop);
|
||||
gameThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop game loop.
|
||||
*/
|
||||
/** Stop game loop. */
|
||||
public void stop() {
|
||||
status = GameStatus.STOPPED;
|
||||
}
|
||||
@@ -73,9 +65,8 @@ public abstract class GameLoop {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle any user input that has happened since the last call. In order to
|
||||
* simulate the situation in real-life game, here we add a random time lag.
|
||||
* The time lag ranges from 50 ms to 250 ms.
|
||||
* Handle any user input that has happened since the last call. In order to simulate the situation
|
||||
* in real-life game, here we add a random time lag. The time lag ranges from 50 ms to 250 ms.
|
||||
*/
|
||||
protected void processInput() {
|
||||
try {
|
||||
@@ -88,18 +79,12 @@ public abstract class GameLoop {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render game frames to screen. Here we print bullet position to simulate
|
||||
* this process.
|
||||
*/
|
||||
/** Render game frames to screen. Here we print bullet position to simulate this process. */
|
||||
protected void render() {
|
||||
var position = controller.getBulletPosition();
|
||||
logger.info("Current bullet position: {}", position);
|
||||
}
|
||||
|
||||
/**
|
||||
* execute game loop logic.
|
||||
*/
|
||||
/** execute game loop logic. */
|
||||
protected abstract void processGameLoop();
|
||||
|
||||
}
|
||||
|
||||
@@ -24,11 +24,8 @@
|
||||
*/
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* Enum class for game status.
|
||||
*/
|
||||
/** Enum class for game status. */
|
||||
public enum GameStatus {
|
||||
|
||||
RUNNING, STOPPED
|
||||
|
||||
RUNNING,
|
||||
STOPPED
|
||||
}
|
||||
|
||||
@@ -25,10 +25,9 @@
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* The variable-step game loop chooses a time step to advance based on how much
|
||||
* real time passed since the last frame. The longer the frame takes, the bigger
|
||||
* steps the game takes. It always keeps up with real time because it will take
|
||||
* bigger and bigger steps to get there.
|
||||
* The variable-step game loop chooses a time step to advance based on how much real time passed
|
||||
* since the last frame. The longer the frame takes, the bigger steps the game takes. It always
|
||||
* keeps up with real time because it will take bigger and bigger steps to get there.
|
||||
*/
|
||||
public class VariableStepGameLoop extends GameLoop {
|
||||
|
||||
@@ -48,5 +47,4 @@ public class VariableStepGameLoop extends GameLoop {
|
||||
protected void update(Long elapsedTime) {
|
||||
controller.moveBullet(0.5f * elapsedTime / 1000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user