Created
February 27, 2023 19:31
-
-
Save fourlastor/9204ae9a8a18f04f190da4ec324961ae to your computer and use it in GitHub Desktop.
StageSystem
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
package io.github.fourlastor.game.level.component; | |
import com.badlogic.ashley.core.Component; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
public class ActorComponent implements Component { | |
public final Actor actor; | |
public final Enum<?> layer; | |
public ActorComponent(Actor actor, Enum<?> layer) { | |
this.actor = actor; | |
this.layer = layer; | |
} | |
} |
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
package io.github.fourlastor.game.level.system; | |
import com.badlogic.ashley.core.ComponentMapper; | |
import com.badlogic.ashley.core.Engine; | |
import com.badlogic.ashley.core.Entity; | |
import com.badlogic.ashley.core.EntityListener; | |
import com.badlogic.ashley.core.EntitySystem; | |
import com.badlogic.ashley.core.Family; | |
import com.badlogic.gdx.graphics.Camera; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.scenes.scene2d.Actor; | |
import com.badlogic.gdx.scenes.scene2d.Group; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import io.github.fourlastor.game.level.component.ActorComponent; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class StageSystem extends EntitySystem implements EntityListener { | |
private static final Family FAMILY = Family.one(ActorComponent.class).get(); | |
private final Stage stage; | |
private final ComponentMapper<ActorComponent> actors; | |
private final List<Group> layers; | |
public StageSystem(Stage stage, ComponentMapper<ActorComponent> actors, Class<? extends Enum<?>> layersEnum) { | |
this.stage = stage; | |
this.actors = actors; | |
int layersCount = layersEnum.getEnumConstants().length; | |
layers = new ArrayList<>(layersCount); | |
for (int i = 0; i < layersCount; i++) { | |
layers.add(new Group()); | |
} | |
} | |
@Override | |
public void update(float deltaTime) { | |
stage.act(deltaTime); | |
stage.draw(); | |
} | |
@Override | |
public void addedToEngine(Engine engine) { | |
engine.addEntityListener(FAMILY, this); | |
for (Group layer : layers) { | |
stage.addActor(layer); | |
} | |
} | |
@Override | |
public void removedFromEngine(Engine engine) { | |
engine.removeEntityListener(this); | |
for (Group layer : layers) { | |
layer.remove(); | |
} | |
} | |
@Override | |
public void entityAdded(Entity entity) { | |
if (actors.has(entity)) { | |
ActorComponent actorComponent = actors.get(entity); | |
Actor actor = actorComponent.actor; | |
Enum<?> layer = actorComponent.layer; | |
layers.get(layer.ordinal()).addActor(actor); | |
} | |
} | |
@Override | |
public void entityRemoved(Entity entity) { | |
if (actors.has(entity)) { | |
actors.get(entity).actor.remove(); | |
} | |
} | |
} |
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
class Usage { | |
static enum class Layer { | |
BACKGROUND, | |
MIDDLE, | |
FOREGROUND | |
} | |
public static void main(String[] args) { | |
StageSystem system = new StageSystem(new Stage(), Layer.class); | |
Entity background = new Entity(); | |
Actor bg = new Actor(...); | |
background.add(new ActorComponent(bg, Layer.BACKGROUND)); | |
Entity player = new Entity(); | |
Actor playerImage = new Actor(...); | |
player.add(new ActorComponent(playerImage, Layer.FOREGROUND))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment