Java tutorial
/** Copyright 2014 Robin Stumm (serverkorken@gmail.com, http://dermetfan.net) * * 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 net.dermetfan.same.systems.shapes; import com.badlogic.ashley.core.Component; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Family; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Matrix4; import net.dermetfan.same.components.TransformComponent; import net.dermetfan.same.systems.DisposableIteratingSystem; /** @author dermetfan * @since 0.0.1 */ @SuppressWarnings("unchecked") public abstract class ShapeRendererSystem extends DisposableIteratingSystem<ShapeRenderer> { /** ShapeRenderer parameters * @author dermetfan * @since 0.0.1 */ public static class ParametersComponent extends Component { public static final ComponentMapper<ParametersComponent> mapper = ComponentMapper .getFor(ParametersComponent.class); /** @see ShapeRenderer#set(ShapeType) */ public ShapeType shapeType; /** @see ShapeRenderer#setColor(Color) */ public Color color; public ParametersComponent(ShapeType shapeType, Color color) { this.shapeType = shapeType; this.color = color; } } /** The parameters used if an entity holds none. Won't change parameters if this is {@code null} and entity holds no parameters. */ private ParametersComponent defaultParameters = new ParametersComponent(null, Color.WHITE); public ShapeRendererSystem(Family family) { super(family); } public ShapeRendererSystem(Family family, int priority) { super(family, priority, null); } public ShapeRendererSystem(Family family, ShapeRenderer resource) { super(family, resource); } public ShapeRendererSystem(Family family, int priority, ShapeRenderer resource) { super(family, priority, resource); } /** @return a new {@link ShapeRenderer} */ @Override public ShapeRenderer newResource() { return new ShapeRenderer(); } @Override public void update(float deltaTime) { resource.setAutoShapeType(true); resource.begin(); super.update(deltaTime); resource.end(); } @Override public void processEntity(Entity entity, float deltaTime) { boolean transformed = false; if (TransformComponent.mapper.has(entity)) { // set transform matrix TransformComponent transformComponent = TransformComponent.mapper.get(entity); Matrix4 transform = transformComponent.matrix; if (transform != null) { resource.setTransformMatrix(transform); transformed = true; } } // apply parameters apply(ParametersComponent.mapper.has(entity) ? ParametersComponent.mapper.get(entity) : defaultParameters); draw(entity); if (transformed) // reset transform matrix resource.identity(); } /** @param params the parameters to apply */ private void apply(ParametersComponent params) { if (params == null) return; if (params.shapeType != null) resource.set(params.shapeType); if (params.color != null) resource.setColor(params.color); } protected abstract void draw(Entity entity); // getters and setters /** @return the {@link #defaultParameters} */ public ParametersComponent getDefaultParameters() { return defaultParameters; } /** @param defaultParameters the {@link #defaultParameters} to set */ public void setDefaultParameters(ParametersComponent defaultParameters) { this.defaultParameters = defaultParameters; } }