2015-04-08 19:15:14 +02:00
|
|
|
package com.saltosion.gladiator.systems;
|
|
|
|
|
2015-04-08 23:44:05 +02:00
|
|
|
import com.badlogic.ashley.core.ComponentMapper;
|
2015-04-08 19:15:14 +02:00
|
|
|
import com.badlogic.ashley.core.Engine;
|
|
|
|
import com.badlogic.ashley.core.Entity;
|
|
|
|
import com.badlogic.ashley.core.EntitySystem;
|
|
|
|
import com.badlogic.ashley.core.Family;
|
|
|
|
import com.badlogic.ashley.utils.ImmutableArray;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
|
|
import com.badlogic.gdx.graphics.GL20;
|
|
|
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
|
|
|
import com.badlogic.gdx.graphics.g2d.Sprite;
|
|
|
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
2015-04-09 00:14:07 +02:00
|
|
|
import com.saltosion.gladiator.components.CPhysics;
|
2015-04-08 19:15:14 +02:00
|
|
|
import com.saltosion.gladiator.components.CRenderedObject;
|
2015-04-10 16:30:30 +02:00
|
|
|
import com.saltosion.gladiator.util.AppUtil;
|
|
|
|
import com.saltosion.gladiator.util.SpriteSequence;
|
2015-04-08 19:15:14 +02:00
|
|
|
|
|
|
|
public class RenderingSystem extends EntitySystem {
|
2015-04-08 23:44:05 +02:00
|
|
|
|
|
|
|
private ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
|
2015-04-09 00:14:07 +02:00
|
|
|
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
2015-04-08 19:15:14 +02:00
|
|
|
private ImmutableArray<Entity> entities;
|
|
|
|
|
|
|
|
private SpriteBatch batch;
|
|
|
|
private OrthographicCamera camera;
|
2015-04-09 20:16:49 +02:00
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
@Override
|
|
|
|
public void addedToEngine(Engine engine) {
|
2015-04-09 20:16:49 +02:00
|
|
|
|
2015-04-08 19:15:14 +02:00
|
|
|
updateEntities(engine);
|
|
|
|
|
|
|
|
batch = new SpriteBatch();
|
|
|
|
camera = new OrthographicCamera();
|
2015-04-09 20:16:49 +02:00
|
|
|
camera.setToOrtho(false, 1, 1);
|
2015-04-08 23:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setViewport(int width, int height) {
|
|
|
|
camera.setToOrtho(false, width, height);
|
2015-04-08 19:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(float deltaTime) {
|
2015-04-10 16:30:30 +02:00
|
|
|
CPhysics phys = pm.get(AppUtil.player);
|
2015-05-08 15:49:42 +02:00
|
|
|
camera.position.set(phys.position.x, phys.position.y, 0);
|
2015-04-10 16:30:30 +02:00
|
|
|
camera.update();
|
2015-04-08 19:15:14 +02:00
|
|
|
|
2015-04-09 20:16:49 +02:00
|
|
|
Gdx.gl.glClearColor(0, 0, 0, 0);
|
2015-04-08 19:15:14 +02:00
|
|
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
|
|
|
batch.setProjectionMatrix(camera.combined);
|
|
|
|
batch.begin();
|
|
|
|
for (int i=0; i<entities.size(); i++) {
|
2015-04-08 23:44:05 +02:00
|
|
|
CRenderedObject renderedObject = rom.get(entities.get(i));
|
2015-04-08 19:50:13 +02:00
|
|
|
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
|
|
|
|
int currFrame = (int) Math.floor(renderedObject.getCurrentFrame());
|
2015-04-08 19:15:14 +02:00
|
|
|
Sprite currSprite = currSequence.getSprite(currFrame);
|
2015-04-08 19:50:13 +02:00
|
|
|
|
2015-04-09 00:14:07 +02:00
|
|
|
CPhysics physics = pm.get(entities.get(i));
|
2015-04-08 19:50:13 +02:00
|
|
|
|
2015-04-09 20:16:49 +02:00
|
|
|
int spriteHeight = currSprite.getRegionHeight();
|
|
|
|
int spriteWidth = currSprite.getRegionWidth();
|
|
|
|
|
2015-05-08 15:49:42 +02:00
|
|
|
currSprite.setPosition(physics.position.x-spriteWidth/2,
|
|
|
|
physics.position.y-spriteHeight/2);
|
2015-04-09 20:16:49 +02:00
|
|
|
currSprite.draw(batch);
|
2015-04-08 19:50:13 +02:00
|
|
|
|
|
|
|
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();
|
|
|
|
renderedObject.setCurrentFrame(nextFrame%currSequence.frameCount());
|
2015-04-09 20:16:49 +02:00
|
|
|
}
|
2015-04-08 19:15:14 +02:00
|
|
|
batch.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateEntities(Engine engine) {
|
2015-04-09 00:14:07 +02:00
|
|
|
entities = engine.getEntitiesFor(Family.getFor(CRenderedObject.class, CPhysics.class));
|
2015-04-08 19:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|