Skip to content

Commit 83a18a6

Browse files
committed
Add README.md
1 parent 342a403 commit 83a18a6

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

update-method/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
layout: pattern
3+
title: Update Method
4+
folder: update-method
5+
permalink: /patterns/update-method/
6+
categories: Other
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time.
14+
15+
## Applicability
16+
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.
17+
18+
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.
19+
20+
Update methods work well when:
21+
22+
- Your game has a number of objects or systems that need to run simultaneously.
23+
24+
- Each object’s behavior is mostly independent of the others.
25+
26+
- The objects need to be simulated over time.
27+
28+
## Explanation
29+
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.
30+
31+
To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern.
32+
33+
## Credits
34+
35+
* [Game Programming Patterns - Update Method](http://gameprogrammingpatterns.com/update-method.html)

update-method/src/main/java/com/iluwatar/updatemethod/App.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class App {
3636

3737
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
3838

39+
private static final int GAME_RUNNING_TIME = 2000;
40+
3941
/**
4042
* Program entry point.
4143
* @param args runtime arguments
@@ -50,7 +52,7 @@ public static void main(String[] args) {
5052
world.addEntity(skeleton2);
5153
world.addEntity(statue);
5254
world.run();
53-
Thread.sleep(200);
55+
Thread.sleep(GAME_RUNNING_TIME);
5456
world.stop();
5557
} catch (InterruptedException e) {
5658
LOGGER.error(e.getMessage());

update-method/src/main/java/com/iluwatar/updatemethod/World.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public class World {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(World.class);
3939

40-
private List<Entity> entities;
40+
protected List<Entity> entities;
4141

42-
private volatile boolean isRunning;
42+
protected volatile boolean isRunning;
4343

4444
public World() {
4545
entities = new ArrayList<>();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.updatemethod;
25+
26+
import org.junit.After;
27+
import org.junit.Assert;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
public class WorldTest {
32+
33+
private World world;
34+
35+
@Before
36+
public void setup() {
37+
world = new World();
38+
}
39+
40+
@After
41+
public void tearDown() {
42+
world = null;
43+
}
44+
45+
@Test
46+
public void testRun() {
47+
world.run();
48+
Assert.assertEquals(true, world.isRunning);
49+
}
50+
51+
@Test
52+
public void testStop() {
53+
world.stop();
54+
Assert.assertEquals(false, world.isRunning);
55+
}
56+
57+
@Test
58+
public void testAddEntity() {
59+
var entity = new Skeleton(1);
60+
world.addEntity(entity);
61+
Assert.assertEquals(entity, world.entities.get(0));
62+
}
63+
}

0 commit comments

Comments
 (0)