-
Notifications
You must be signed in to change notification settings - Fork 126
Working ECS-based graphics #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
engine/src/main/java/org/destinationsol/rendering/RenderableElement.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2020 The Terasology Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.destinationsol.rendering; | ||
|
|
||
| import com.badlogic.gdx.graphics.Color; | ||
| import com.badlogic.gdx.graphics.g2d.TextureAtlas; | ||
| import com.badlogic.gdx.math.Vector2; | ||
| import org.destinationsol.game.drawables.DrawableLevel; | ||
|
|
||
| /** | ||
| * Contains a {@link TextureAtlas.AtlasRegion} (an image), along with information about how it should be drawn. | ||
| */ | ||
| public class RenderableElement { | ||
|
|
||
| /** Represents the image of this element. */ | ||
| public TextureAtlas.AtlasRegion texture; | ||
|
|
||
| /** The width of the texture when drawn. */ | ||
| public float width; | ||
|
|
||
| /** The height of the texture when drawn. */ | ||
| public float height; | ||
|
|
||
| /** Represents the depth at which this element renders, as well as its logical grouping. */ | ||
| public DrawableLevel drawableLevel; | ||
|
|
||
| /** Represents the spatial offset of this element from the entity that it is associated with. */ | ||
| public Vector2 relativePosition; | ||
|
|
||
| /** Represents the rotational offset of this element from the entity that it is associated with. */ | ||
| public float relativeAngle; | ||
|
|
||
| /** The tint that the texture should be given. */ | ||
| public Color tint; | ||
|
|
||
| public void copy(RenderableElement other) { | ||
| this.texture = new TextureAtlas.AtlasRegion(other.texture); | ||
| this.drawableLevel = other.drawableLevel; | ||
| this.relativePosition = other.relativePosition.cpy(); | ||
| this.relativeAngle = other.relativeAngle; | ||
| this.width = other.width; | ||
| this.height = other.height; | ||
| this.tint = other.tint.cpy(); | ||
| } | ||
| } | ||
|
|
29 changes: 29 additions & 0 deletions
29
engine/src/main/java/org/destinationsol/rendering/components/Invisible.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright 2020 The Terasology Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.destinationsol.rendering.components; | ||
|
|
||
| import org.destinationsol.rendering.systems.RenderingSystem; | ||
| import org.terasology.gestalt.entitysystem.component.Component; | ||
|
|
||
| /** | ||
| * Denotes that the object should not be drawn by the {@link RenderingSystem}. | ||
| */ | ||
| public class Invisible implements Component<Invisible> { | ||
| @Override | ||
| public void copy(Invisible other) { | ||
|
|
||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
engine/src/main/java/org/destinationsol/rendering/components/Renderable.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2020 The Terasology Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.destinationsol.rendering.components; | ||
|
|
||
| import org.destinationsol.rendering.RenderableElement; | ||
| import org.terasology.gestalt.entitysystem.component.Component; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| /** | ||
| * Contains a list of the different elements that combine to visually represent an entity. | ||
| */ | ||
| public final class Renderable implements Component<Renderable> { | ||
|
|
||
| public ArrayList<RenderableElement> elements = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void copy(Renderable other) { | ||
| ArrayList<RenderableElement> newElements = new ArrayList<>(); | ||
| for (int index = 0; index < other.elements.size(); index++) { | ||
| RenderableElement data = new RenderableElement(); | ||
| data.copy(other.elements.get(index)); | ||
| newElements.add(data); | ||
| } | ||
|
|
||
| this.elements = newElements; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
engine/src/main/java/org/destinationsol/rendering/events/RenderEvent.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2020 The Terasology Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.destinationsol.rendering.events; | ||
|
|
||
| import org.destinationsol.rendering.components.Renderable; | ||
| import org.destinationsol.rendering.systems.RenderingSystem; | ||
| import org.terasology.gestalt.entitysystem.event.Event; | ||
|
|
||
| /** | ||
| * Event that tells the {@link RenderingSystem} to update the drawings of each entity with a {@link Renderable}. | ||
| */ | ||
| public class RenderEvent implements Event { | ||
| } |
68 changes: 68 additions & 0 deletions
68
engine/src/main/java/org/destinationsol/rendering/systems/RenderingSystem.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 2020 The Terasology Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.destinationsol.rendering.systems; | ||
|
|
||
| import com.badlogic.gdx.math.Vector2; | ||
| import org.destinationsol.common.In; | ||
| import org.destinationsol.rendering.RenderableElement; | ||
| import org.destinationsol.rendering.components.Renderable; | ||
| import org.destinationsol.rendering.components.Invisible; | ||
| import org.destinationsol.rendering.events.RenderEvent; | ||
| import org.destinationsol.entitysystem.EntitySystemManager; | ||
| import org.destinationsol.entitysystem.EventReceiver; | ||
| import org.destinationsol.game.GameDrawer; | ||
| import org.destinationsol.location.components.Angle; | ||
| import org.destinationsol.location.components.Position; | ||
| import org.terasology.gestalt.entitysystem.entity.EntityRef; | ||
| import org.terasology.gestalt.entitysystem.event.EventResult; | ||
| import org.terasology.gestalt.entitysystem.event.ReceiveEvent; | ||
|
|
||
| /** | ||
| * This handles the drawing of each entity with a {@link Renderable} component. | ||
| */ | ||
| public class RenderingSystem implements EventReceiver { | ||
|
|
||
| @In | ||
| private EntitySystemManager entitySystemManager; | ||
|
|
||
| @In | ||
| private GameDrawer drawer; | ||
|
|
||
| @ReceiveEvent(components = {Renderable.class, Position.class}) | ||
| public EventResult onRender(RenderEvent event, EntityRef entity) { | ||
|
|
||
| if (!entity.hasComponent(Invisible.class)) { | ||
|
|
||
| Renderable renderable = entity.getComponent(Renderable.class).get(); | ||
| Vector2 basePosition = entity.getComponent(Position.class).get().position; | ||
|
|
||
| float baseAngle = 0; | ||
| if (entity.hasComponent(Position.class)) { | ||
| baseAngle = entity.getComponent(Angle.class).get().getAngle(); | ||
| } | ||
|
|
||
| for (RenderableElement renderableElement : renderable.elements) { | ||
| float angle = renderableElement.relativeAngle + baseAngle; | ||
|
|
||
| drawer.draw(renderableElement.texture, renderableElement.width, | ||
| renderableElement.height, renderableElement.width / 2, renderableElement.height / 2, | ||
| basePosition.x, basePosition.y, angle, renderableElement.tint); | ||
| } | ||
| } | ||
|
|
||
| return EventResult.CONTINUE; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.