Skip to content

Commit 10986ef

Browse files
author
Julien
committed
doc(sdk): include an example on how to deal with guice
1 parent d7a6df9 commit 10986ef

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,52 @@ 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+
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 field with `@Inject` will always be `null` if you instantiate the class yourself.
80+
- Any simple class can be injected into yout `Referee` and you can inject the `Referee` or `GraphicEntityModule` into any custom class.
81+
82+
### Example
83+
84+
`Referee.java`
85+
```java
86+
public class Referee extends AbstractReferee {
87+
@Inject MultiplayerGameManager<Player> gameManager;
88+
@Inject MyGridMaker gridMaker;
89+
90+
@Override
91+
public void init() {
92+
// Map size can be 5,6,7 or 8
93+
int mapSize = 5 + new Random(gameManager.getSeed()).nextInt(4)
94+
gridMaker.init(mapSize);
95+
}
96+
}
97+
```
98+
99+
`MyGridMaker.java`
100+
```java
101+
public class MyGridMaker {
102+
@Inject GraphicEntityModule entityModule;
103+
104+
public static final int cell_size = 20;
105+
106+
@Override
107+
public void init(int size) {
108+
// Use the injected GraphicEntityModule to create a grid
109+
for (int y = 0; y < size; ++y) {
110+
for (int x = 0; x < size; ++x) {
111+
entityModule.createRectangle()
112+
.setX(x * cell_size)
113+
.setY(y * cell_size)
114+
.setWidth(cell_size)
115+
.setHeight(cell_size);
116+
}
117+
}
118+
}
119+
}
120+
```
121+
76122
# Features
77123

78124
## General Features

0 commit comments

Comments
 (0)