Skip to content

Commit 0e52ec5

Browse files
author
Julien Poulton
committed
Merge branch 'sdk-guice' into 'master'
doc(sdk): include an example on how to deal with guice See merge request codingame/game-engine!201
2 parents 288fc44 + 8b50697 commit 0e52ec5

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

playground/core-concepts/core-3-game-manager.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,53 @@ public class Referee extends AbstractReferee {
7373

7474
The Game Manager's API will thus work with your `Player` class, which you may modify at leisure.
7575

76+
## Working with Guice
77+
78+
Using Guice in your own code is totally optional but since the SDK uses injections to handle the instantiation of its different components, it is important to note a few things:
79+
- An injected field will be instantiated by Guice, those fields with `@Inject` will always be `null` if you instantiate the class yourself using the `new` operator.
80+
- Any simple class can be injected into your `Referee` and you can inject the `Referee` or `GraphicEntityModule` into any simple class.
81+
- Writing a lot of Guice code may cause the game to slow down.
82+
83+
### Example
84+
85+
`Referee.java`
86+
```java
87+
public class Referee extends AbstractReferee {
88+
@Inject MultiplayerGameManager<Player> gameManager;
89+
@Inject MyGridMaker gridMaker;
90+
91+
@Override
92+
public void init() {
93+
// Map size can be 5,6,7 or 8
94+
int mapSize = 5 + new Random(gameManager.getSeed()).nextInt(4)
95+
gridMaker.init(mapSize);
96+
}
97+
}
98+
```
99+
100+
`MyGridMaker.java`
101+
```java
102+
public class MyGridMaker {
103+
@Inject GraphicEntityModule entityModule;
104+
105+
public static final int cell_size = 20;
106+
107+
@Override
108+
public void init(int size) {
109+
// Use the injected GraphicEntityModule to create a grid
110+
for (int y = 0; y < size; ++y) {
111+
for (int x = 0; x < size; ++x) {
112+
entityModule.createRectangle()
113+
.setX(x * cell_size)
114+
.setY(y * cell_size)
115+
.setWidth(cell_size)
116+
.setHeight(cell_size);
117+
}
118+
}
119+
}
120+
}
121+
```
122+
76123
# Features
77124

78125
## General Features

0 commit comments

Comments
 (0)