Implemented ParticleSystem.

This commit is contained in:
Jeasonfire 2015-05-17 03:47:45 +03:00
parent 96f2c2ac7a
commit 7a7adda2c5
7 changed files with 195 additions and 9 deletions

View File

@ -14,6 +14,7 @@ import com.saltosion.gladiator.state.MainMenuState;
import com.saltosion.gladiator.systems.AISystem; import com.saltosion.gladiator.systems.AISystem;
import com.saltosion.gladiator.systems.CombatSystem; import com.saltosion.gladiator.systems.CombatSystem;
import com.saltosion.gladiator.systems.MiscManagerSystem; import com.saltosion.gladiator.systems.MiscManagerSystem;
import com.saltosion.gladiator.systems.ParticleSystem;
import com.saltosion.gladiator.systems.PhysicsSystem; import com.saltosion.gladiator.systems.PhysicsSystem;
import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
@ -67,6 +68,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
engine.addSystem(new CombatSystem()); engine.addSystem(new CombatSystem());
engine.addSystem(new MiscManagerSystem()); engine.addSystem(new MiscManagerSystem());
engine.addSystem(new AISystem()); engine.addSystem(new AISystem());
engine.addSystem(new ParticleSystem());
engine.addEntityListener(new EntityListener() { engine.addEntityListener(new EntityListener() {
@Override @Override
public void entityAdded(Entity entity) { public void entityAdded(Entity entity) {

View File

@ -0,0 +1,79 @@
package com.saltosion.gladiator.components;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
public class CParticle extends Component {
private final Vector2 position = new Vector2();
private final Vector2 velocity = new Vector2();
private final Vector2 gravity = new Vector2();
private final Vector2 size = new Vector2(0.1f, 0.1f);
private final Color color = new Color();
private final float creationTime;
private float decayTime = 1;
public CParticle() {
this.creationTime = System.currentTimeMillis();
}
public CParticle setPosition(float x, float y) {
this.position.set(x, y);
return this;
}
public CParticle setVelocity(float x, float y) {
this.velocity.set(x, y);
return this;
}
public CParticle setGravity(float x, float y) {
this.gravity.set(x, y);
return this;
}
public CParticle setSize(float x, float y) {
this.size.set(x, y);
return this;
}
public CParticle setColor(float r, float g, float b, float a) {
this.color.set(r, g, b, a);
return this;
}
public CParticle setDecayTime(float decayTime) {
this.decayTime = decayTime;
return this;
}
public Vector2 getPosition() {
return this.position;
}
public Vector2 getVelocity() {
return this.velocity;
}
public Vector2 getGravity() {
return this.gravity;
}
public Vector2 getSize() {
return this.size;
}
public Color getColor() {
return this.color;
}
public float getCreationTime() {
return this.creationTime;
}
public float getDecayTime() {
return this.decayTime;
}
}

View File

@ -1,17 +1,46 @@
package com.saltosion.gladiator.listeners; package com.saltosion.gladiator.listeners;
import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Entity;
import com.saltosion.gladiator.components.CParticle;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Global;
public class BasicDeathListener implements CombatListener { public class BasicDeathListener implements CombatListener {
private static final float FX_FORCE = 16f, FX_GRAV = -40f;
private static final int FX_HIT_AMT = 4, FX_DEAD_AMT = 96;
@Override @Override
public void died(Entity source, Entity target, int damageTaken) { public void died(Entity source, Entity target, int damageTaken) {
target.flags &= ~Global.FLAG_ALIVE; target.flags &= ~Global.FLAG_ALIVE;
CPhysics cp = target.getComponent(CPhysics.class);
for (int i = 0; i < FX_DEAD_AMT; i++) {
Entity fx = new Entity();
fx.add(new CParticle().setColor(1, 0, 0, 1).setDecayTime(2).setGravity(0, FX_GRAV)
.setVelocity((float) Math.cos(Math.toRadians(Math.random() * 360)) * FX_FORCE,
(float) Math.sin(Math.toRadians(Math.random() * 360)) * FX_FORCE)
.setPosition(cp.getPosition().x + (float) Math.random() - 0.5f,
cp.getPosition().y + (float) Math.random() - 0.5f)
.setSize(0.2f, 0.2f));
AppUtil.engine.addEntity(fx);
}
} }
@Override @Override
public void damageTaken(Entity source, Entity target, int damageTaken) { public void damageTaken(Entity source, Entity target, int damageTaken) {
CPhysics cp = target.getComponent(CPhysics.class);
for (int i = 0; i < FX_HIT_AMT; i++) {
Entity fx = new Entity();
fx.add(new CParticle().setColor(1, 0, 0, 1).setDecayTime(2).setGravity(0, FX_GRAV)
.setVelocity((float) Math.cos(Math.toRadians(Math.random() * 360)) * FX_FORCE,
(float) Math.sin(Math.toRadians(Math.random() * 360)) * FX_FORCE)
.setPosition(cp.getPosition().x + (float) Math.random() - 0.5f,
cp.getPosition().y + (float) Math.random() - 0.5f)
.setSize(0.2f, 0.2f));
AppUtil.engine.addEntity(fx);
}
} }
} }

View File

@ -38,16 +38,14 @@ public class SwingHitboxListener implements CollisionListener {
CPhysics otherPhysics = pm.get(other); CPhysics otherPhysics = pm.get(other);
if (otherPhysics != null && otherPhysics.getCollisionListener() != null if (otherPhysics != null && otherPhysics.getCollisionListener() != null
&& otherPhysics.getCollisionListener() instanceof SwingHitboxListener) { && otherPhysics.getCollisionListener() instanceof SwingHitboxListener) {
float x = 0, y = 0; float x = 0;
if (direction == Direction.LEFT) { if (direction == Direction.LEFT) {
x = 1; x = 1;
} else if (direction == Direction.RIGHT) { } else if (direction == Direction.RIGHT) {
x = -1; x = -1;
} else if (direction == Direction.DOWN) {
y = 1;
} }
float force = cm.get(source).getSwingForce(); float force = cm.get(source).getSwingForce();
pm.get(source).setSimVelocity(x * force, y * force / 8f); pm.get(source).setSimVelocity(x * force, 0);
} }
} }

View File

@ -55,7 +55,7 @@ public class InGameState extends BaseState {
} }
if (level.levelFailed()) { if (level.levelFailed()) {
setState(new GameOverState()); //setState(new GameOverState());
} }
} }

View File

@ -0,0 +1,38 @@
package com.saltosion.gladiator.systems;
import com.badlogic.ashley.core.ComponentMapper;
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.saltosion.gladiator.components.CParticle;
import com.saltosion.gladiator.util.AppUtil;
public class ParticleSystem extends EntitySystem {
private static final ComponentMapper<CParticle> pm = ComponentMapper.getFor(CParticle.class);
private ImmutableArray<Entity> entities;
@Override
public void addedToEngine(Engine engine) {
updateEntities(engine);
}
@Override
public void update(float deltaTime) {
for (int i = 0; i < entities.size(); i++) {
CParticle particle = pm.get(entities.get(i));
particle.getPosition().add(particle.getVelocity().cpy().scl(deltaTime));
particle.getVelocity().add(particle.getGravity().cpy().scl(deltaTime));
if (System.currentTimeMillis() - particle.getCreationTime() > particle.getDecayTime()) {
AppUtil.engine.removeEntity(entities.get(i));
}
}
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CParticle.class));
}
}

View File

@ -1,6 +1,7 @@
package com.saltosion.gladiator.systems; package com.saltosion.gladiator.systems;
import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.ComponentType;
import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.EntitySystem;
@ -18,6 +19,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CCombat;
import com.saltosion.gladiator.components.CParticle;
import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject; import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.gui.nodes.GUINode; import com.saltosion.gladiator.gui.nodes.GUINode;
@ -26,6 +28,7 @@ import com.saltosion.gladiator.gui.nodes.TextNode;
import com.saltosion.gladiator.gui.properties.TextProperty; import com.saltosion.gladiator.gui.properties.TextProperty;
import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.AppUtil;
import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Global;
import com.saltosion.gladiator.util.Log;
import com.saltosion.gladiator.util.SpriteLoader; import com.saltosion.gladiator.util.SpriteLoader;
import com.saltosion.gladiator.util.SpriteSequence; import com.saltosion.gladiator.util.SpriteSequence;
import java.util.ArrayList; import java.util.ArrayList;
@ -36,11 +39,12 @@ public class RenderingSystem extends EntitySystem {
private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class); private final ComponentMapper<CRenderedObject> rom = ComponentMapper.getFor(CRenderedObject.class);
private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class); private final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class); private final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
private final ComponentMapper<CParticle> pam = ComponentMapper.getFor(CParticle.class);
private ImmutableArray<Entity> entities; private ImmutableArray<Entity> entities;
private SpriteBatch batch; private SpriteBatch batch;
private BitmapFont font; private BitmapFont font;
private ShapeRenderer debugRenderer; private ShapeRenderer debugRenderer, particleRenderer;
private OrthographicCamera camera, fontCamera; private OrthographicCamera camera, fontCamera;
public float aspectratio; public float aspectratio;
@ -69,6 +73,7 @@ public class RenderingSystem extends EntitySystem {
font.setUseIntegerPositions(false); font.setUseIntegerPositions(false);
debugRenderer = new ShapeRenderer(); debugRenderer = new ShapeRenderer();
particleRenderer = new ShapeRenderer();
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera.setToOrtho(false, 1, 1); camera.setToOrtho(false, 1, 1);
@ -99,8 +104,9 @@ public class RenderingSystem extends EntitySystem {
updateEntityAnimations(); updateEntityAnimations();
renderEntities(deltaTime); renderEntities(deltaTime);
renderParticles();
renderDebug();
renderGUI(new Vector2(0, 0)); renderGUI(new Vector2(0, 0));
renderDebug(camera);
if (debug) { if (debug) {
drawString("FPS: " + Gdx.graphics.getFramesPerSecond(), new Vector2(camera.position.x - 12, camera.position.y + 8)); drawString("FPS: " + Gdx.graphics.getFramesPerSecond(), new Vector2(camera.position.x - 12, camera.position.y + 8));
@ -254,7 +260,28 @@ public class RenderingSystem extends EntitySystem {
} }
} }
private void renderDebug(Camera camera) { private void renderParticles() {
if (AppUtil.player == null) {
return;
}
CPhysics playerPhys = pm.get(AppUtil.player);
particleRenderer.setProjectionMatrix(camera.combined);
particleRenderer.begin(ShapeType.Filled);
for (int i = 0; i < entities.size(); i++) {
CParticle particle = pam.get(entities.get(i));
if (particle == null) {
continue;
}
particleRenderer.setColor(particle.getColor());
particleRenderer.rect(particle.getPosition().x - particle.getSize().x / 2 + getCameraOffset(playerPhys).x,
particle.getPosition().y - particle.getSize().y / 2 + getCameraOffset(playerPhys).y,
particle.getSize().x, particle.getSize().y);
}
particleRenderer.end();
}
private void renderDebug() {
if (debug) { if (debug) {
if (AppUtil.player == null) { if (AppUtil.player == null) {
return; return;
@ -264,6 +291,10 @@ public class RenderingSystem extends EntitySystem {
debugRenderer.begin(ShapeType.Line); debugRenderer.begin(ShapeType.Line);
for (int i = 0; i < entities.size(); i++) { for (int i = 0; i < entities.size(); i++) {
CPhysics physics = pm.get(entities.get(i)); CPhysics physics = pm.get(entities.get(i));
if (physics == null) {
continue;
}
float x0 = physics.getPosition().x - physics.getSize().x / 2 + getCameraOffset(playerPhys, physics).x; float x0 = physics.getPosition().x - physics.getSize().x / 2 + getCameraOffset(playerPhys, physics).x;
float x1 = physics.getPosition().x + physics.getSize().x / 2 + getCameraOffset(playerPhys, physics).x; float x1 = physics.getPosition().x + physics.getSize().x / 2 + getCameraOffset(playerPhys, physics).x;
float y0 = physics.getPosition().y - physics.getSize().y / 2 + getCameraOffset(playerPhys, physics).y; float y0 = physics.getPosition().y - physics.getSize().y / 2 + getCameraOffset(playerPhys, physics).y;
@ -301,7 +332,8 @@ public class RenderingSystem extends EntitySystem {
} }
public void updateEntities(Engine engine) { public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class)); entities = engine.getEntitiesFor(Family.getFor(ComponentType.getBitsFor(),
ComponentType.getBitsFor(CPhysics.class, CParticle.class), ComponentType.getBitsFor()));
} }
public boolean getDebug() { public boolean getDebug() {
@ -316,6 +348,13 @@ public class RenderingSystem extends EntitySystem {
return new Vector2(this.camera.position.x, this.camera.position.y); return new Vector2(this.camera.position.x, this.camera.position.y);
} }
private Vector2 getCameraOffset(CPhysics playerPhys) {
Vector2 offset = new Vector2(Math.max(xMin + camera.viewportWidth / 2, Math.min(xMax - camera.viewportWidth / 2,
-playerPhys.getPosition().x)) + camera.viewportWidth / 2,
-playerPhys.getPosition().y + camera.viewportHeight / 3);
return offset;
}
private Vector2 getCameraOffset(CPhysics playerPhys, CPhysics currPhys) { private Vector2 getCameraOffset(CPhysics playerPhys, CPhysics currPhys) {
Vector2 offset = new Vector2(Math.max(xMin + camera.viewportWidth / 2, Math.min(xMax - camera.viewportWidth / 2, Vector2 offset = new Vector2(Math.max(xMin + camera.viewportWidth / 2, Math.min(xMax - camera.viewportWidth / 2,
-playerPhys.getPosition().x)) / currPhys.getZParallax() + camera.viewportWidth / 2, -playerPhys.getPosition().x)) / currPhys.getZParallax() + camera.viewportWidth / 2,
@ -344,6 +383,7 @@ public class RenderingSystem extends EntitySystem {
public void dispose() { public void dispose() {
batch.dispose(); batch.dispose();
debugRenderer.dispose(); debugRenderer.dispose();
particleRenderer.dispose();
font.dispose(); font.dispose();
SpriteLoader.dispose(); SpriteLoader.dispose();
} }