Java tutorial
/* * Copyright (C) 2016 Saltosion * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNUss General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.saltosion.pixelprophecy.systems; import box2dLight.RayHandler; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.systems.IteratingSystem; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.maps.MapProperties; import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.World; import org.saltosion.pixelprophecy.Comp; import org.saltosion.pixelprophecy.util.Globals; import org.saltosion.pixelprophecy.util.Names; import org.saltosion.pixelprophecy.util.SpriteChannel; public class RenderingSystem extends IteratingSystem { private final World world; private final Box2DDebugRenderer b2dDebugRenderer; private final OrthographicCamera camera; private final RayHandler rayHandler; private final SpriteBatch spriteBatch; private Color ambientLight; private Entity followedEntity; private final ComponentMapper<Comp.RenderedObject> rom = ComponentMapper.getFor(Comp.RenderedObject.class); private final ComponentMapper<Comp.Physics> pm = ComponentMapper.getFor(Comp.Physics.class); private OrthogonalTiledMapRenderer mapRenderer; private TiledMap mapToRender; public RenderingSystem(World world) { super(Family.all(Comp.RenderedObject.class, Comp.Physics.class).get()); this.world = world; camera = new OrthographicCamera(); b2dDebugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true); rayHandler = new RayHandler(world); rayHandler.setBlurNum(2); spriteBatch = new SpriteBatch(); } /** * Used to change the map * @param map */ public final void setMap(TiledMap map) { mapToRender = map; MapProperties properties = map.getProperties(); mapRenderer = new OrthogonalTiledMapRenderer(mapToRender, Globals.SPRITE_SCALE); if (properties.containsKey("ambientlight")) { ambientLight = Globals.parseColor(properties.get("ambientlight", String.class)); } else { ambientLight = new Color(0, 0, 0, 1); } rayHandler.setAmbientLight(ambientLight); } @Override public void update(float deltaTime) { Vector2 playerLoc = pm.get(followedEntity).body.getPosition(); camera.position.set(playerLoc.x, playerLoc.y, 0); camera.update(); rayHandler.setCombinedMatrix(camera.combined); spriteBatch.setProjectionMatrix(camera.combined); mapRenderer.setView(camera); mapRenderer.render(Globals.BACKGROUND); spriteBatch.begin(); super.update(deltaTime); spriteBatch.end(); mapRenderer.render(Globals.FOREGROUND); if (Globals.PHYSICS_DEBUG) { b2dDebugRenderer.render(world, camera.combined); } rayHandler.updateAndRender(); } @Override protected void processEntity(Entity entity, float deltaTime) { Comp.Physics physics = pm.get(entity); Comp.RenderedObject ro = rom.get(entity); if (physics.destination.x < 0) { ro.setLookingLeft(true); } else if (physics.destination.x > 0) { ro.setLookingLeft(false); } for (SpriteChannel channel : ro.getChannels()) { // If entity is not moving if (physics.destination.isZero()) { channel.playAnimation(Names.IDLE); // Play idle animation } else { // Otherwise channel.playAnimation(Names.WALK); // Play walk animation } Sprite sprite = channel.getCurrentSprite(); sprite.setFlip(ro.isLookingLeft(), false); Vector2 pos; // If Physics has body, use it. if (physics.body != null) { pos = physics.body.getPosition(); } else { // Otherwise use backup plan pos = physics.location; } pos.x -= sprite.getWidth() / 2; pos.y -= sprite.getHeight() / 2; sprite.setPosition(pos.x, pos.y); sprite.draw(spriteBatch); channel.advance(deltaTime); } } public void resize(int width, int height) { Globals.ASPECT_RATIO = width * 1f / height; camera.setToOrtho(false, Globals.VIEWPORT_HEIGHT * Globals.ASPECT_RATIO, Globals.VIEWPORT_HEIGHT); } public RayHandler getRayHandler() { return rayHandler; } public void setFollowedEntity(Entity followedEntity) { this.followedEntity = followedEntity; } public Entity getFollowedEntity() { return followedEntity; } public OrthographicCamera getCamera() { return camera; } }