-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement birdhouse bird spawning [OLB-51] (#6)
* Implement birdhouse bird spawning - Birdhouse spawns birds every 50 ticks - Implement ticker to manage server ticks * Spawn bird at random offset from birdhouses * Refactor seed consumption to use the Ticker class * Fix seed consumption behaviour * Set bird spawn y-offset to 10 units above a birdhouse
- Loading branch information
Showing
9 changed files
with
157 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ runClient.launch | |
runData.launch | ||
runServer.launch | ||
|
||
src/generated | ||
src/generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/ocelotslovebirds/birdhaus/ticker/FixedIntervalTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ocelotslovebirds.birdhaus.ticker; | ||
|
||
public class FixedIntervalTicker implements Ticker { | ||
private final int interval; | ||
private int counter; | ||
|
||
/** | ||
* @param interval How often the ticker should tick | ||
*/ | ||
public FixedIntervalTicker(int interval) { | ||
this.interval = interval; | ||
this.counter = 0; | ||
} | ||
|
||
public boolean tick() { | ||
this.counter++; | ||
|
||
if (this.counter == this.interval) { | ||
this.counter = 0; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ocelotslovebirds/birdhaus/ticker/Ticker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ocelotslovebirds.birdhaus.ticker; | ||
|
||
// A ticker class helps keep track of when events should happen | ||
public interface Ticker { | ||
/* | ||
This function should be called for every tick that passes. It returns | ||
true when a particular tick matches the ticker's pattern. Patterns can | ||
be things like every 10 ticks or every other tick. | ||
*/ | ||
public boolean tick(); | ||
} |
13 changes: 0 additions & 13 deletions
13
src/test/java/com/ocelotslovebirds/birdhaus/HelloWorldTest.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/test/java/com/ocelotslovebirds/birdhaus/ticker/FixedIntervalTickerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.ocelotslovebirds.birdhaus.ticker; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
public class FixedIntervalTickerTest { | ||
@Test | ||
public void alwaysTicks() { | ||
Ticker ticker = new FixedIntervalTicker(1); | ||
|
||
// We assume that if it works 10 times in a row, | ||
// it will continue to always work | ||
for (int i = 0; i < 10; i++) { | ||
assertTrue(ticker.tick()); | ||
} | ||
} | ||
|
||
@Test | ||
public void ticksEveryOtherTime() { | ||
Ticker ticker = new FixedIntervalTicker(2); | ||
|
||
// We assume that if it works 5 times in a row, | ||
// it will continue to always work | ||
for (int i = 0; i < 5; i++) { | ||
assertFalse(ticker.tick()); | ||
assertTrue(ticker.tick()); | ||
} | ||
} | ||
|
||
@Test | ||
public void ticksEveryTenTicks() { | ||
Ticker ticker = new FixedIntervalTicker(10); | ||
|
||
// We assume that if it works 5 times in a row, | ||
// it will continue to always work | ||
for (int i = 0; i < 5; i++) { | ||
// Returns false 9 times | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
assertFalse(ticker.tick()); | ||
|
||
// Returns true once | ||
assertTrue(ticker.tick()); | ||
} | ||
} | ||
} |