Game Loop Pattern (#1083)
* Add game loop module * Add game loop module * Fix merge issue * Implement game loop module * Implement game loop module * Implement time based game loop * implement VariableStepGameLoop * Implement FixedStepGameLoop * Add UT * Add Unit tests * Fix checkstyle issues * Add README.md * Fix code review issues * Fix code review issues * update README.md
This commit is contained in:
76
game-loop/src/main/java/com/iluwatar/gameloop/App.java
Normal file
76
game-loop/src/main/java/com/iluwatar/gameloop/App.java
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
try {
|
||||
LOGGER.info("Start frame-based game loop:");
|
||||
var frameBasedGameLoop = new FrameBasedGameLoop();
|
||||
frameBasedGameLoop.run();
|
||||
Thread.sleep(GAME_LOOP_DURATION_TIME);
|
||||
frameBasedGameLoop.stop();
|
||||
LOGGER.info("Stop frame-based game loop.");
|
||||
|
||||
LOGGER.info("Start variable-step game loop:");
|
||||
var variableStepGameLoop = new VariableStepGameLoop();
|
||||
variableStepGameLoop.run();
|
||||
Thread.sleep(GAME_LOOP_DURATION_TIME);
|
||||
variableStepGameLoop.stop();
|
||||
LOGGER.info("Stop variable-step game loop.");
|
||||
|
||||
LOGGER.info("Start fixed-step game loop:");
|
||||
var fixedStepGameLoop = new FixedStepGameLoop();
|
||||
fixedStepGameLoop.run();
|
||||
Thread.sleep(GAME_LOOP_DURATION_TIME);
|
||||
fixedStepGameLoop.stop();
|
||||
LOGGER.info("Stop variable-step game loop.");
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
44
game-loop/src/main/java/com/iluwatar/gameloop/Bullet.java
Normal file
44
game-loop/src/main/java/com/iluwatar/gameloop/Bullet.java
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* Bullet object class.
|
||||
*/
|
||||
public class Bullet {
|
||||
|
||||
private float position;
|
||||
|
||||
public Bullet() {
|
||||
position = 0.0f;
|
||||
}
|
||||
|
||||
public float getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(float position) {
|
||||
this.position = position;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
public class FixedStepGameLoop extends GameLoop {
|
||||
|
||||
/**
|
||||
* 20 ms per frame = 50 FPS.
|
||||
*/
|
||||
private static final long MS_PER_FRAME = 20;
|
||||
|
||||
@Override
|
||||
protected void processGameLoop() {
|
||||
var previousTime = System.currentTimeMillis();
|
||||
var lag = 0L;
|
||||
while (isGameRunning()) {
|
||||
var currentTime = System.currentTimeMillis();
|
||||
var elapsedTime = currentTime - previousTime;
|
||||
previousTime = currentTime;
|
||||
lag += elapsedTime;
|
||||
|
||||
processInput();
|
||||
|
||||
while (lag >= MS_PER_FRAME) {
|
||||
update();
|
||||
lag -= MS_PER_FRAME;
|
||||
}
|
||||
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
protected void update() {
|
||||
controller.moveBullet(0.5f * MS_PER_FRAME / 1000);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
public class FrameBasedGameLoop extends GameLoop {
|
||||
|
||||
@Override
|
||||
protected void processGameLoop() {
|
||||
while (isGameRunning()) {
|
||||
processInput();
|
||||
update();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
public class GameController {
|
||||
|
||||
protected final Bullet bullet;
|
||||
|
||||
/**
|
||||
* Initialize Bullet instance.
|
||||
*/
|
||||
public GameController() {
|
||||
bullet = new Bullet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move bullet position by the provided offset.
|
||||
*
|
||||
* @param offset moving offset
|
||||
*/
|
||||
public void moveBullet(float offset) {
|
||||
var currentPosition = bullet.getPosition();
|
||||
bullet.setPosition(currentPosition + offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current position of the bullet.
|
||||
*
|
||||
* @return position of bullet
|
||||
*/
|
||||
public float getBulletPosition() {
|
||||
return bullet.getPosition();
|
||||
}
|
||||
|
||||
}
|
||||
|
104
game-loop/src/main/java/com/iluwatar/gameloop/GameLoop.java
Normal file
104
game-loop/src/main/java/com/iluwatar/gameloop/GameLoop.java
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Abstract class for GameLoop implementation class.
|
||||
*/
|
||||
public abstract class GameLoop {
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected volatile GameStatus status;
|
||||
|
||||
protected GameController controller;
|
||||
|
||||
private Thread gameThread;
|
||||
|
||||
/**
|
||||
* Initialize game status to be stopped.
|
||||
*/
|
||||
public GameLoop() {
|
||||
controller = new GameController();
|
||||
status = GameStatus.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run game loop.
|
||||
*/
|
||||
public void run() {
|
||||
status = GameStatus.RUNNING;
|
||||
gameThread = new Thread(() -> processGameLoop());
|
||||
gameThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop game loop.
|
||||
*/
|
||||
public void stop() {
|
||||
status = GameStatus.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if game is running or not.
|
||||
*
|
||||
* @return {@code true} if the game is running.
|
||||
*/
|
||||
public boolean isGameRunning() {
|
||||
return status == GameStatus.RUNNING ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
int lag = new Random().nextInt(200) + 50;
|
||||
Thread.sleep(lag);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
protected abstract void processGameLoop();
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
/**
|
||||
* Enum class for game status.
|
||||
*/
|
||||
public enum GameStatus {
|
||||
|
||||
RUNNING, STOPPED
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
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.
|
||||
*/
|
||||
public class VariableStepGameLoop extends GameLoop {
|
||||
|
||||
@Override
|
||||
protected void processGameLoop() {
|
||||
var lastFrameTime = System.currentTimeMillis();
|
||||
while (isGameRunning()) {
|
||||
processInput();
|
||||
var currentFrameTime = System.currentTimeMillis();
|
||||
var elapsedTime = currentFrameTime - lastFrameTime;
|
||||
update(elapsedTime);
|
||||
lastFrameTime = currentFrameTime;
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
protected void update(Long elapsedTime) {
|
||||
controller.moveBullet(0.5f * elapsedTime / 1000);
|
||||
}
|
||||
|
||||
}
|
39
game-loop/src/test/java/com/iluwatar/gameloop/AppTest.java
Normal file
39
game-loop/src/test/java/com/iluwatar/gameloop/AppTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* App unit test class.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
String[] args = {};
|
||||
new App().main(args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* FixedStepGameLoop unit test class.
|
||||
*/
|
||||
public class FixedStepGameLoopTest {
|
||||
|
||||
private FixedStepGameLoop gameLoop;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
gameLoop = new FixedStepGameLoop();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
gameLoop = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
gameLoop.update();
|
||||
Assert.assertEquals(0.01f, gameLoop.controller.getBulletPosition(), 0);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* FrameBasedGameLoop unit test class.
|
||||
*/
|
||||
public class FrameBasedGameLoopTest {
|
||||
|
||||
private FrameBasedGameLoop gameLoop;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
gameLoop = new FrameBasedGameLoop();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
gameLoop = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
gameLoop.update();
|
||||
Assert.assertEquals(0.5f, gameLoop.controller.getBulletPosition(), 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GameControllerTest {
|
||||
|
||||
private GameController controller;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
controller = new GameController();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
controller = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveBullet() {
|
||||
controller.moveBullet(1.5f);
|
||||
Assert.assertEquals(1.5f, controller.bullet.getPosition(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBulletPosition() {
|
||||
Assert.assertEquals(controller.bullet.getPosition(), controller.getBulletPosition(), 0);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* GameLoop unit test class.
|
||||
*/
|
||||
public class GameLoopTest {
|
||||
|
||||
private GameLoop gameLoop;
|
||||
|
||||
/**
|
||||
* Create mock implementation of GameLoop.
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
gameLoop = new GameLoop() {
|
||||
@Override
|
||||
protected void processGameLoop() {}
|
||||
};
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
gameLoop = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRun() {
|
||||
gameLoop.run();
|
||||
Assert.assertEquals(GameStatus.RUNNING, gameLoop.status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStop() {
|
||||
gameLoop.stop();
|
||||
Assert.assertEquals(GameStatus.STOPPED, gameLoop.status);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsGameRunning() {
|
||||
Assert.assertEquals(false, gameLoop.isGameRunning());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* VariableStepGameLoop unit test class.
|
||||
*/
|
||||
public class VariableStepGameLoopTest {
|
||||
|
||||
private VariableStepGameLoop gameLoop;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
gameLoop = new VariableStepGameLoop();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
gameLoop = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
gameLoop.update(20L);
|
||||
Assert.assertEquals(0.01f, gameLoop.controller.getBulletPosition(), 0);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user