Skip to content

Commit 008d74c

Browse files
committed
fix: add ECS EventReceiver injection and fix associated tests
1 parent ecc3f19 commit 008d74c

File tree

20 files changed

+146
-57
lines changed

20 files changed

+146
-57
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2022 The Terasology Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.destinationsol;
18+
19+
import org.destinationsol.entitysystem.EventReceiver;
20+
import org.terasology.context.Lifetime;
21+
import org.terasology.gestalt.di.ServiceRegistry;
22+
import org.terasology.gestalt.module.ModuleEnvironment;
23+
24+
public class EventReceiverServiceRegistry extends ServiceRegistry {
25+
public EventReceiverServiceRegistry(ModuleEnvironment environment) {
26+
for (Class<? extends EventReceiver> receiver : environment.getSubtypesOf(EventReceiver.class)) {
27+
this.with(receiver).lifetime(Lifetime.Singleton);
28+
}
29+
}
30+
}

engine/src/main/java/org/destinationsol/SolApplication.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ private void draw() {
332332
}
333333

334334
public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig worldConfig) {
335-
335+
ModuleManager moduleManager = appContext.getBean(ModuleManager.class);
336336
gameContext = appContext.getNestedContainer(
337337
new GameConfigurationServiceRegistry(worldConfig),
338+
new EventReceiverServiceRegistry(moduleManager.getEnvironment()),
338339
new SolGameServiceRegistry(tut),
339340
new ContextWrapperService());
340-
ModuleManager moduleManager = appContext.getBean(ModuleManager.class);
341341
solGame = gameContext.getBean(SolGame.class);
342342

343343
//TODO: rework how system will trigger preBegin
@@ -347,6 +347,7 @@ public void play(boolean tut, String shipName, boolean isNewGame, WorldConfig wo
347347
systems.forEach(ComponentSystem::preBegin);
348348

349349
entitySystemManager = gameContext.getBean(EntitySystemManager.class);
350+
entitySystemManager.initialise();
350351

351352
solGame.createUpdateSystems();
352353
solGame.startGame(shipName, isNewGame, worldConfig, entitySystemManager);

engine/src/main/java/org/destinationsol/asteroids/systems/AsteroidBodyCreationSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import com.badlogic.gdx.physics.box2d.Body;
2121
import com.badlogic.gdx.physics.box2d.BodyDef;
2222
import com.badlogic.gdx.physics.box2d.FixtureDef;
23-
import com.badlogic.gdx.physics.box2d.World;
2423
import org.destinationsol.Const;
2524
import org.destinationsol.asteroids.components.AsteroidMesh;
2625
import org.destinationsol.body.events.BodyCreatedEvent;
2726
import org.destinationsol.body.events.GenerateBodyEvent;
2827
import org.destinationsol.entitysystem.EntitySystemManager;
2928
import org.destinationsol.entitysystem.EventReceiver;
3029
import org.destinationsol.game.CollisionMeshLoader;
30+
import org.destinationsol.game.ObjectManager;
3131
import org.destinationsol.game.UpdateAwareSystem;
3232
import org.destinationsol.location.components.Angle;
3333
import org.destinationsol.location.components.Position;
@@ -59,7 +59,7 @@ public class AsteroidBodyCreationSystem implements EventReceiver {
5959
EntitySystemManager entitySystemManager;
6060

6161
@Inject
62-
World world;
62+
ObjectManager objectManager;
6363

6464
private final CollisionMeshLoader collisionMeshLoader = new CollisionMeshLoader("engine:asteroids");
6565

@@ -83,7 +83,7 @@ public EventResult onGenerateBody(GenerateBodyEvent event, EntityRef entity) {
8383
bd.angularDamping = 0;
8484
bd.position.set(position);
8585
bd.linearDamping = 0;
86-
Body body = world.createBody(bd);
86+
Body body = objectManager.getWorld().createBody(bd);
8787

8888
//This sets a reference to an entity in the Body, so that the entity can be retrieved from the body during collision handling.
8989
body.setUserData(entity);

engine/src/main/java/org/destinationsol/entitysystem/EntitySystemManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.common.collect.Lists;
1919
import org.destinationsol.modules.ModuleManager;
20+
import org.terasology.gestalt.di.BeanContext;
2021
import org.terasology.gestalt.entitysystem.component.Component;
2122
import org.terasology.gestalt.entitysystem.component.management.ComponentManager;
2223
import org.terasology.gestalt.entitysystem.component.store.ArrayComponentStore;
@@ -40,10 +41,11 @@ public class EntitySystemManager {
4041

4142
private static EntityManager entityManager;
4243
private final EventSystem eventSystem = new EventSystemImpl();
44+
private final BeanContext context;
4345
private static final EventReceiverMethodSupport eventReceiverMethodSupport = new EventReceiverMethodSupport();
4446

4547
@Inject
46-
public EntitySystemManager(ModuleManager moduleManager, ComponentManager componentManager, List<EventReceiver> eventReceivers) {
48+
public EntitySystemManager(ModuleManager moduleManager, ComponentManager componentManager, BeanContext context) {
4749
List<ComponentStore<?>> stores = Lists.newArrayList();
4850
for (Class<? extends Component> componentType : moduleManager.getEnvironment().getSubtypesOf(Component.class)) {
4951
//This filters out abstract components, which would create exceptions
@@ -54,7 +56,11 @@ public EntitySystemManager(ModuleManager moduleManager, ComponentManager compone
5456
}
5557
entityManager = new CoreEntityManager(stores);
5658

57-
for (EventReceiver eventReceiver : eventReceivers) {
59+
this.context = context;
60+
}
61+
62+
public void initialise() {
63+
for (EventReceiver eventReceiver : context.getBeans(EventReceiver.class)) {
5864
eventReceiverMethodSupport.register(eventReceiver, eventSystem);
5965
}
6066
}

engine/src/main/java/org/destinationsol/entitysystem/EventReceiver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
*/
1616
package org.destinationsol.entitysystem;
1717

18+
import org.terasology.context.annotation.IndexInherited;
19+
20+
@IndexInherited
1821
public interface EventReceiver {
1922
}

engine/src/main/java/org/destinationsol/game/SolGame.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.destinationsol.ui.Waypoint;
6868
import org.destinationsol.ui.nui.screens.MainGameScreen;
6969
import org.destinationsol.world.GalaxyBuilder;
70+
import org.terasology.context.exception.BeanNotFoundException;
7071
import org.terasology.gestalt.assets.ResourceUrn;
7172
import org.terasology.gestalt.di.BeanContext;
7273
import org.terasology.gestalt.entitysystem.entity.EntityRef;
@@ -206,7 +207,12 @@ public void createUpdateSystems() {
206207
}
207208
RegisterUpdateSystem registerAnnotation = updateSystemClass.getDeclaredAnnotation(RegisterUpdateSystem.class);
208209
UpdateAwareSystem system = (UpdateAwareSystem) updateSystemClass.newInstance();
209-
beanContext.inject(system);
210+
try {
211+
beanContext.inject(system);
212+
} catch (BeanNotFoundException e) {
213+
e.printStackTrace();
214+
continue;
215+
}
210216
if (!registerAnnotation.paused()) {
211217
if (!updateSystems.containsKey(registerAnnotation.priority())) {
212218
ArrayList<UpdateAwareSystem> systems = new ArrayList<UpdateAwareSystem>();

engine/src/main/java/org/destinationsol/game/screens/ConsoleScreen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public class ConsoleScreen implements SolUiScreen, ConsoleSubscriber {
105105
private StringBuilder inputLine;
106106

107107
@Inject
108-
public ConsoleScreen(Context context) {
108+
public ConsoleScreen(Console console) {
109109
font = Assets.getFont("engine:main").getBitmapFont();
110110

111-
this.console = new ConsoleImpl( context);
111+
this.console = console;
112112

113113
exitControl = new SolUiControl(null, true, Input.Keys.ESCAPE);
114114
commandHistoryUpControl = new SolUiControl(null, true, Input.Keys.UP);

engine/src/main/java/org/destinationsol/game/screens/GameScreens.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.destinationsol.game.screens;
1717

1818
import org.destinationsol.SolApplication;
19+
import org.destinationsol.game.console.Console;
1920
import org.destinationsol.game.context.Context;
2021
import org.destinationsol.ui.SolLayouts;
2122

@@ -40,7 +41,7 @@ public GameScreens(SolApplication cmp, Context context) {
4041
inventoryScreen = new InventoryScreen(cmp.getOptions());
4142
talkScreen = new TalkScreen(layouts.menuLayout, cmp.getOptions());
4243
waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen);
43-
consoleScreen = new ConsoleScreen(context);
44+
consoleScreen = new ConsoleScreen(context.get(Console.class));
4445
}
4546

4647
// This was added for PlayerCreatorTest.java (used in PlayerCreator)

engine/src/main/java/org/destinationsol/removal/systems/DestroyOnZeroHealthSystem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.destinationsol.removal.events.ShouldBeDestroyedEvent;
2222
import org.destinationsol.removal.events.ZeroHealthEvent;
2323
import org.terasology.gestalt.entitysystem.entity.EntityRef;
24+
import org.terasology.gestalt.entitysystem.event.EventResult;
2425
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
2526

2627
import javax.inject.Inject;
@@ -38,7 +39,8 @@ public DestroyOnZeroHealthSystem() {
3839
}
3940

4041
@ReceiveEvent
41-
public void onZeroHealth(ZeroHealthEvent event, EntityRef entity) {
42+
public EventResult onZeroHealth(ZeroHealthEvent event, EntityRef entity) {
4243
entitySystemManager.sendEvent(new ShouldBeDestroyedEvent(), entity);
44+
return EventResult.COMPLETE;
4345
}
4446
}

engine/src/main/java/org/destinationsol/rubble/systems/RubbleBodyCreationSystem.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.destinationsol.entitysystem.EntitySystemManager;
2828
import org.destinationsol.entitysystem.EventReceiver;
2929
import org.destinationsol.game.CollisionMeshLoader;
30+
import org.destinationsol.game.ObjectManager;
3031
import org.destinationsol.location.components.Angle;
3132
import org.destinationsol.location.components.Position;
3233
import org.destinationsol.rendering.RenderableElement;
@@ -51,7 +52,7 @@ public class RubbleBodyCreationSystem implements EventReceiver {
5152
protected EntitySystemManager entitySystemManager;
5253

5354
@Inject
54-
protected World world;
55+
protected ObjectManager objectManager;
5556

5657
private final CollisionMeshLoader collisionMeshLoader = new CollisionMeshLoader("engine:miscCollisionMeshes");
5758

@@ -75,7 +76,7 @@ public EventResult onGenerateBody(GenerateBodyEvent event, EntityRef entity) {
7576
bd.angularDamping = 0;
7677
bd.position.set(position);
7778
bd.linearDamping = 0;
78-
Body body = world.createBody(bd);
79+
Body body = objectManager.getWorld().createBody(bd);
7980

8081
//This sets a reference to an entity in the Body, so that the entity can be retrieved from the body during collision handling.
8182
body.setUserData(entity);

0 commit comments

Comments
 (0)