Skip to content

Commit e7eb358

Browse files
committed
refactor DiggableSystem
1 parent 91aa8fe commit e7eb358

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

examples/platformer/src/main/java/io/github/srcimon/screwbox/platformer/systems/FollowPlayerSystem.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import io.github.srcimon.screwbox.platformer.components.FollowPlayerComponent;
1212
import io.github.srcimon.screwbox.platformer.components.PlayerMarkerComponent;
1313

14-
import java.util.Optional;
15-
1614
@Order(Order.SystemOrder.SIMULATION_EARLY)
1715
public class FollowPlayerSystem implements EntitySystem {
1816

@@ -21,22 +19,18 @@ public class FollowPlayerSystem implements EntitySystem {
2119

2220
@Override
2321
public void update(Engine engine) {
24-
Optional<Entity> playerEntity = engine.environment().tryFetchSingleton(PLAYER);
25-
if (playerEntity.isEmpty()) {
26-
return;
27-
}
28-
Entity player = playerEntity.get();
29-
var playerPosition = player.position();
30-
31-
for (Entity followEntity : engine.environment().fetchAll(FOLLOWING)) {
32-
var followComponent = followEntity.get(FollowPlayerComponent.class);
33-
Line lineBetweenFollowerAndPlayer = Line.between(followEntity.position(), playerPosition);
34-
double x = Math.clamp(lineBetweenFollowerAndPlayer.to().x() - lineBetweenFollowerAndPlayer.from().x(), followComponent.speed * -1, followComponent.speed);
35-
36-
double y = Math.clamp(lineBetweenFollowerAndPlayer.to().y() - lineBetweenFollowerAndPlayer.from().y(), followComponent.speed * -1, followComponent.speed);
37-
38-
Vector movement = Vector.of(x, y).multiply(engine.loop().delta());
39-
followEntity.moveBy(movement);
40-
}
22+
engine.environment().tryFetchSingleton(PLAYER).ifPresent(player -> {
23+
var playerPosition = player.position();
24+
25+
for (Entity followEntity : engine.environment().fetchAll(FOLLOWING)) {
26+
var followComponent = followEntity.get(FollowPlayerComponent.class);
27+
Line lineBetweenFollowerAndPlayer = Line.between(followEntity.position(), playerPosition);
28+
double x = Math.clamp(lineBetweenFollowerAndPlayer.to().x() - lineBetweenFollowerAndPlayer.from().x(), followComponent.speed * -1, followComponent.speed);
29+
double y = Math.clamp(lineBetweenFollowerAndPlayer.to().y() - lineBetweenFollowerAndPlayer.from().y(), followComponent.speed * -1, followComponent.speed);
30+
31+
Vector movement = Vector.of(x, y).multiply(engine.loop().delta());
32+
followEntity.moveBy(movement);
33+
}
34+
});
4135
}
4236
}

0 commit comments

Comments
 (0)