From ae98f17fa57123607c3be628dde497a2ac4c5b7f Mon Sep 17 00:00:00 2001 From: Azureyjt Date: Tue, 19 Nov 2019 16:33:51 +0800 Subject: [PATCH 1/4] Add update method pattern --- pom.xml | 1 + update-method/README.md | 0 update-method/pom.xml | 38 ++++++++++++ .../java/com/iluwatar/updatemethod/App.java | 38 ++++++++++++ .../com/iluwatar/updatemethod/Entity.java | 51 ++++++++++++++++ .../com/iluwatar/updatemethod/Skeleton.java | 61 +++++++++++++++++++ .../com/iluwatar/updatemethod/Statue.java | 55 +++++++++++++++++ .../java/com/iluwatar/updatemethod/World.java | 59 ++++++++++++++++++ 8 files changed, 303 insertions(+) create mode 100644 update-method/README.md create mode 100644 update-method/pom.xml create mode 100644 update-method/src/main/java/com/iluwatar/updatemethod/App.java create mode 100644 update-method/src/main/java/com/iluwatar/updatemethod/Entity.java create mode 100644 update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java create mode 100644 update-method/src/main/java/com/iluwatar/updatemethod/Statue.java create mode 100644 update-method/src/main/java/com/iluwatar/updatemethod/World.java diff --git a/pom.xml b/pom.xml index 39582fd93718..bf4e4da88b7e 100644 --- a/pom.xml +++ b/pom.xml @@ -188,6 +188,7 @@ double-buffer sharding game-loop + update-method diff --git a/update-method/README.md b/update-method/README.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/update-method/pom.xml b/update-method/pom.xml new file mode 100644 index 000000000000..09b9a3b06a2a --- /dev/null +++ b/update-method/pom.xml @@ -0,0 +1,38 @@ + + + + + + java-design-patterns + com.iluwatar + 1.23.0-SNAPSHOT + + 4.0.0 + + update-method + + + \ No newline at end of file diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/App.java b/update-method/src/main/java/com/iluwatar/updatemethod/App.java new file mode 100644 index 000000000000..c6c5cfe3a799 --- /dev/null +++ b/update-method/src/main/java/com/iluwatar/updatemethod/App.java @@ -0,0 +1,38 @@ +/* + * 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.updatemethod; + +public class App { + + public static void main(String[] args) { + var world = new World(); + var skeleton1 = new Skeleton(1, 10); + var skeleton2 = new Skeleton(2, 70); + var statue = new Statue(3, 20); + world.addEntity(skeleton1); + world.addEntity(skeleton2); + world.addEntity(statue); + world.gameLoop(); + } +} diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java b/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java new file mode 100644 index 000000000000..90b84122065c --- /dev/null +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java @@ -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.updatemethod; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class Entity { + + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + protected int id; + + protected int position; + + public Entity(int id) { + this.id = id; + this.position = 0; + } + + public abstract void update(); + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } +} diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java b/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java new file mode 100644 index 000000000000..637c35b92ab2 --- /dev/null +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java @@ -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.updatemethod; + +public class Skeleton extends Entity { + + private static final int PATROLLING_LEFT_BOUNDING = 0; + + private static final int PATROLLING_RIGHT_BOUNDING = 100; + + protected boolean patrollingLeft; + + public Skeleton(int id) { + super(id); + patrollingLeft = false; + } + + public Skeleton(int id, int postition) { + super(id); + this.position = position; + patrollingLeft = false; + } + + @Override + public void update() { + if (patrollingLeft) { + position -= 1; + if (position == PATROLLING_LEFT_BOUNDING) { + patrollingLeft = false; + } + } else { + position += 1; + if (position == PATROLLING_RIGHT_BOUNDING) { + patrollingLeft = true; + } + } + logger.info("Skeleton " + id + " is on position " + position); + } +} + diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java b/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java new file mode 100644 index 000000000000..e8bd03efc3f7 --- /dev/null +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java @@ -0,0 +1,55 @@ +/* + * 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.updatemethod; + +public class Statue extends Entity { + + protected int frames; + + protected int delay; + + public Statue(int id) { + super(id); + this.frames = 0; + this.delay = 0; + } + + public Statue(int id, int delay) { + super(id); + this.frames = 0; + this.delay = delay; + } + + @Override + public void update() { + if (++ frames == delay) { + shootLightning(); + frames = 0; + } + } + + private void shootLightning() { + logger.info("Statue " + id + " is shooting lightning!"); + } +} diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/World.java b/update-method/src/main/java/com/iluwatar/updatemethod/World.java new file mode 100644 index 000000000000..62c115a7e4c9 --- /dev/null +++ b/update-method/src/main/java/com/iluwatar/updatemethod/World.java @@ -0,0 +1,59 @@ +/* + * 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.updatemethod; + +import java.util.ArrayList; +import java.util.List; + +public class World { + + private List entities; + + public World() { + entities = new ArrayList<>(); + } + + public void gameLoop() { + while (true) { + processInput(); + update(); + render(); + } + } + + private void processInput() {} + + private void update() { + for (var entity : entities) { + entity.update(); + } + } + + private void render() {} + + public void addEntity(Entity entity) { + entities.add(entity); + } + +} From 342a403e1645aeae94aa51982a4f77f533b50784 Mon Sep 17 00:00:00 2001 From: Azureyjt Date: Tue, 19 Nov 2019 17:54:24 +0800 Subject: [PATCH 2/4] Add unit tests --- update-method/pom.xml | 6 ++ .../java/com/iluwatar/updatemethod/App.java | 37 +++++++-- .../com/iluwatar/updatemethod/Entity.java | 3 + .../com/iluwatar/updatemethod/Skeleton.java | 19 ++++- .../com/iluwatar/updatemethod/Statue.java | 16 +++- .../java/com/iluwatar/updatemethod/World.java | 61 ++++++++++++++- .../com/iluwatar/updatemethod/AppTest.java | 35 +++++++++ .../iluwatar/updatemethod/SkeletonTest.java | 78 +++++++++++++++++++ .../com/iluwatar/updatemethod/StatueTest.java | 58 ++++++++++++++ 9 files changed, 300 insertions(+), 13 deletions(-) create mode 100644 update-method/src/test/java/com/iluwatar/updatemethod/AppTest.java create mode 100644 update-method/src/test/java/com/iluwatar/updatemethod/SkeletonTest.java create mode 100644 update-method/src/test/java/com/iluwatar/updatemethod/StatueTest.java diff --git a/update-method/pom.xml b/update-method/pom.xml index 09b9a3b06a2a..ede79f8f6c06 100644 --- a/update-method/pom.xml +++ b/update-method/pom.xml @@ -33,6 +33,12 @@ 4.0.0 update-method + + + junit + junit + + \ No newline at end of file diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/App.java b/update-method/src/main/java/com/iluwatar/updatemethod/App.java index c6c5cfe3a799..0f860a73c254 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/App.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/App.java @@ -23,16 +23,37 @@ package com.iluwatar.updatemethod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This pattern simulate a collection of independent objects by telling each to + * process one frame of behavior at a time. The game world maintains a collection + * of objects. Each object implements an update method that simulates one frame of + * the object’s behavior. Each frame, the game updates every object in the collection. + */ public class App { + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); + + /** + * Program entry point. + * @param args runtime arguments + */ public static void main(String[] args) { - var world = new World(); - var skeleton1 = new Skeleton(1, 10); - var skeleton2 = new Skeleton(2, 70); - var statue = new Statue(3, 20); - world.addEntity(skeleton1); - world.addEntity(skeleton2); - world.addEntity(statue); - world.gameLoop(); + try { + var world = new World(); + var skeleton1 = new Skeleton(1, 10); + var skeleton2 = new Skeleton(2, 70); + var statue = new Statue(3, 20); + world.addEntity(skeleton1); + world.addEntity(skeleton2); + world.addEntity(statue); + world.run(); + Thread.sleep(200); + world.stop(); + } catch (InterruptedException e) { + LOGGER.error(e.getMessage()); + } } } diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java b/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java index 90b84122065c..0f10d77758cd 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Entity.java @@ -26,6 +26,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Abstract class for all the entity types. + */ public abstract class Entity { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java b/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java index 637c35b92ab2..e365d6aecfcb 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Skeleton.java @@ -23,6 +23,12 @@ package com.iluwatar.updatemethod; +/** + * Skeletons are always patrolling on the game map. Initially all the skeletons + * patrolling to the right, and after them reach the bounding, it will start + * patrolling to the left. For each frame, one skeleton will move 1 position + * step. + */ public class Skeleton extends Entity { private static final int PATROLLING_LEFT_BOUNDING = 0; @@ -31,11 +37,22 @@ public class Skeleton extends Entity { protected boolean patrollingLeft; + /** + * Constructor of Skeleton. + * + * @param id id of skeleton + */ public Skeleton(int id) { super(id); patrollingLeft = false; } + /** + * Constructor of Skeleton. + * + * @param id id of skeleton + * @param postition position of skeleton + */ public Skeleton(int id, int postition) { super(id); this.position = position; @@ -55,7 +72,7 @@ public void update() { patrollingLeft = true; } } - logger.info("Skeleton " + id + " is on position " + position); + logger.info("Skeleton " + id + " is on position " + position + "."); } } diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java b/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java index e8bd03efc3f7..f1f3e2a87a6f 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/Statue.java @@ -23,18 +23,32 @@ package com.iluwatar.updatemethod; +/** + * Statues shoot lightning at regular intervals. + */ public class Statue extends Entity { protected int frames; protected int delay; + /** + * Constructor of Statue. + * + * @param id id of statue + */ public Statue(int id) { super(id); this.frames = 0; this.delay = 0; } + /** + * Constructor of Statue. + * + * @param id id of statue + * @param delay the number of frames between two lightning + */ public Statue(int id, int delay) { super(id); this.frames = 0; @@ -50,6 +64,6 @@ public void update() { } private void shootLightning() { - logger.info("Statue " + id + " is shooting lightning!"); + logger.info("Statue " + id + " shoots lightning!"); } } diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/World.java b/update-method/src/main/java/com/iluwatar/updatemethod/World.java index 62c115a7e4c9..62bfa93070df 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/World.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/World.java @@ -25,33 +25,88 @@ import java.util.ArrayList; import java.util.List; +import java.util.Random; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The game world class. Maintain all the objects existed in the game frames. + */ public class World { + private static final Logger LOGGER = LoggerFactory.getLogger(World.class); + private List entities; + private volatile boolean isRunning; + public World() { entities = new ArrayList<>(); + isRunning = false; } - public void gameLoop() { - while (true) { + /** + * Main game loop. This loop will always run until the game is over. For + * each loop it will process user input, update internal status, and render + * the next frames. For more detail please refer to the game-loop pattern. + */ + private void gameLoop() { + while (isRunning) { processInput(); update(); render(); } } - private void processInput() {} + /** + * 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. + */ + private void processInput() { + try { + int lag = new Random().nextInt(200) + 50; + Thread.sleep(lag); + } catch (InterruptedException e) { + LOGGER.error(e.getMessage()); + } + } + /** + * Update internal status. The update method pattern invoke udpate method for + * each entity in the game. + */ private void update() { for (var entity : entities) { entity.update(); } } + /** + * Render the next frame. Here we do nothing since it is not related to the + * pattern. + */ private void render() {} + /** + * Run game loop. + */ + public void run() { + LOGGER.info("Start game."); + isRunning = true; + var thread = new Thread(this::gameLoop); + thread.start(); + } + + /** + * Stop game loop. + */ + public void stop() { + LOGGER.info("Stop game."); + isRunning = false; + } + public void addEntity(Entity entity) { entities.add(entity); } diff --git a/update-method/src/test/java/com/iluwatar/updatemethod/AppTest.java b/update-method/src/test/java/com/iluwatar/updatemethod/AppTest.java new file mode 100644 index 000000000000..75c30470d1b7 --- /dev/null +++ b/update-method/src/test/java/com/iluwatar/updatemethod/AppTest.java @@ -0,0 +1,35 @@ +/* + * 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.updatemethod; + +import org.junit.Test; + +public class AppTest { + + @Test + public void testMain() { + String[] args = {}; + App.main(args); + } +} diff --git a/update-method/src/test/java/com/iluwatar/updatemethod/SkeletonTest.java b/update-method/src/test/java/com/iluwatar/updatemethod/SkeletonTest.java new file mode 100644 index 000000000000..73ab9eb1dc13 --- /dev/null +++ b/update-method/src/test/java/com/iluwatar/updatemethod/SkeletonTest.java @@ -0,0 +1,78 @@ +/* + * 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.updatemethod; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class SkeletonTest { + + private Skeleton skeleton; + + @Before + public void setup() { + skeleton = new Skeleton(1); + } + + @After + public void tearDown() { + skeleton = null; + } + + @Test + public void testUpdateForPatrollingLeft() { + skeleton.patrollingLeft = true; + skeleton.setPosition(50); + skeleton.update(); + Assert.assertEquals(49, skeleton.getPosition()); + } + + @Test + public void testUpdateForPatrollingRight() { + skeleton.patrollingLeft = false; + skeleton.setPosition(50); + skeleton.update(); + Assert.assertEquals(51, skeleton.getPosition()); + } + + @Test + public void testUpdateForReverseDirectionFromLeftToRight() { + skeleton.patrollingLeft = true; + skeleton.setPosition(1); + skeleton.update(); + Assert.assertEquals(0, skeleton.getPosition()); + Assert.assertEquals(false, skeleton.patrollingLeft); + } + + @Test + public void testUpdateForReverseDirectionFromRightToLeft() { + skeleton.patrollingLeft = false; + skeleton.setPosition(99); + skeleton.update(); + Assert.assertEquals(100, skeleton.getPosition()); + Assert.assertEquals(true, skeleton.patrollingLeft); + } +} diff --git a/update-method/src/test/java/com/iluwatar/updatemethod/StatueTest.java b/update-method/src/test/java/com/iluwatar/updatemethod/StatueTest.java new file mode 100644 index 000000000000..2d523237cfc3 --- /dev/null +++ b/update-method/src/test/java/com/iluwatar/updatemethod/StatueTest.java @@ -0,0 +1,58 @@ +/* + * 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.updatemethod; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StatueTest { + + private Statue statue; + + @Before + public void setup() { + statue = new Statue(1, 20); + } + + @After + public void tearDown() { + statue = null; + } + + @Test + public void testUpdateForPendingShoot() { + statue.frames = 10; + statue.update(); + Assert.assertEquals(11, statue.frames); + } + + @Test + public void testUpdateForShooting() { + statue.frames = 19; + statue.update(); + Assert.assertEquals(0, statue.frames); + } +} From 83a18a60edb9bf05f39027fa240bd6a944805eac Mon Sep 17 00:00:00 2001 From: Azureyjt Date: Thu, 21 Nov 2019 16:56:54 +0800 Subject: [PATCH 3/4] Add README.md --- update-method/README.md | 35 +++++++++++ .../java/com/iluwatar/updatemethod/App.java | 4 +- .../java/com/iluwatar/updatemethod/World.java | 4 +- .../com/iluwatar/updatemethod/WorldTest.java | 63 +++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 update-method/src/test/java/com/iluwatar/updatemethod/WorldTest.java diff --git a/update-method/README.md b/update-method/README.md index e69de29bb2d1..50cba44f34c5 100644 --- a/update-method/README.md +++ b/update-method/README.md @@ -0,0 +1,35 @@ +--- +layout: pattern +title: Update Method +folder: update-method +permalink: /patterns/update-method/ +categories: Other +tags: + - Java + - Difficulty-Beginner +--- + +## Intent +Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time. + +## Applicability +If the Game Loop pattern is the best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of games featuring live entities that the player interacts with use this pattern in some form or other. If the game has space marines, dragons, Martians, ghosts, or athletes, there’s a good chance it uses this pattern. + +However, if the game is more abstract and the moving pieces are less like living actors and more like pieces on a chessboard, this pattern is often a poor fit. In a game like chess, you don’t need to simulate all of the pieces concurrently, and you probably don’t need to tell the pawns to update themselves every frame. + +Update methods work well when: + +- Your game has a number of objects or systems that need to run simultaneously. + +- Each object’s behavior is mostly independent of the others. + +- The objects need to be simulated over time. + +## Explanation +The game world maintains a collection of objects. Each object implements an update method that simulates one frame of the object’s behavior. Each frame, the game updates every object in the collection. + +To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern. + +## Credits + +* [Game Programming Patterns - Update Method](http://gameprogrammingpatterns.com/update-method.html) \ No newline at end of file diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/App.java b/update-method/src/main/java/com/iluwatar/updatemethod/App.java index 0f860a73c254..45069a3dee21 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/App.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/App.java @@ -36,6 +36,8 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); + private static final int GAME_RUNNING_TIME = 2000; + /** * Program entry point. * @param args runtime arguments @@ -50,7 +52,7 @@ public static void main(String[] args) { world.addEntity(skeleton2); world.addEntity(statue); world.run(); - Thread.sleep(200); + Thread.sleep(GAME_RUNNING_TIME); world.stop(); } catch (InterruptedException e) { LOGGER.error(e.getMessage()); diff --git a/update-method/src/main/java/com/iluwatar/updatemethod/World.java b/update-method/src/main/java/com/iluwatar/updatemethod/World.java index 62bfa93070df..8cabead56f00 100644 --- a/update-method/src/main/java/com/iluwatar/updatemethod/World.java +++ b/update-method/src/main/java/com/iluwatar/updatemethod/World.java @@ -37,9 +37,9 @@ public class World { private static final Logger LOGGER = LoggerFactory.getLogger(World.class); - private List entities; + protected List entities; - private volatile boolean isRunning; + protected volatile boolean isRunning; public World() { entities = new ArrayList<>(); diff --git a/update-method/src/test/java/com/iluwatar/updatemethod/WorldTest.java b/update-method/src/test/java/com/iluwatar/updatemethod/WorldTest.java new file mode 100644 index 000000000000..2a9dad316ba5 --- /dev/null +++ b/update-method/src/test/java/com/iluwatar/updatemethod/WorldTest.java @@ -0,0 +1,63 @@ +/* + * 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.updatemethod; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class WorldTest { + + private World world; + + @Before + public void setup() { + world = new World(); + } + + @After + public void tearDown() { + world = null; + } + + @Test + public void testRun() { + world.run(); + Assert.assertEquals(true, world.isRunning); + } + + @Test + public void testStop() { + world.stop(); + Assert.assertEquals(false, world.isRunning); + } + + @Test + public void testAddEntity() { + var entity = new Skeleton(1); + world.addEntity(entity); + Assert.assertEquals(entity, world.entities.get(0)); + } +} From bc962a0284cd13a394259b9edd019aa05204a378 Mon Sep 17 00:00:00 2001 From: Azureyjt Date: Thu, 21 Nov 2019 17:00:58 +0800 Subject: [PATCH 4/4] Resolve conflict --- pom.xml | 81 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index bf4e4da88b7e..ca14141155cc 100644 --- a/pom.xml +++ b/pom.xml @@ -1,29 +1,21 @@ - - + 4.0.0 com.iluwatar @@ -176,15 +168,15 @@ master-worker-pattern spatial-partition priority-queue - commander - typeobjectpattern + commander + typeobjectpattern bytecode leader-election data-locality subclass-sandbox circuit-breaker role-object - saga + saga double-buffer sharding game-loop @@ -382,6 +374,28 @@ spring-boot-maven-plugin ${spring-boot.version} + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + + jar-with-dependencies + + + ${project.artifactId} + false + + + + @@ -409,9 +423,9 @@ - + org.commonjava.maven.plugins directory-maven-plugin @@ -433,7 +447,6 @@ - com.mycila license-maven-plugin @@ -484,10 +497,8 @@ - - @@ -498,4 +509,4 @@ - + \ No newline at end of file