added explosion fx (sound and particles)

This commit is contained in:
Harrison Deng 2017-07-22 09:44:36 -05:00
parent de3a64551d
commit a54a06c799
5 changed files with 71 additions and 6 deletions

View File

@ -21,11 +21,11 @@ void main() {
sum += texture2D(u_texture, texcoord + vec2(j, i)*0.004) * 0.25; sum += texture2D(u_texture, texcoord + vec2(j, i)*0.004) * 0.25;
} }
} }
if (texture2D(u_texture, texcoord).r < 0.3) { if (texture2D(u_texture, texcoord).r * vColor.r < 0.3) {
gl_FragColor = vec4(sum * sum * 0.012 + texture2D(u_texture, texcoord)) gl_FragColor = vec4(sum * sum * 0.012 + texture2D(u_texture, texcoord))
* vColor; * vColor;
} else { } else {
if (texture2D(u_texture, texcoord).r < 0.5) { if (texture2D(u_texture, texcoord).r * vColor.r < 0.5) {
gl_FragColor = vec4( gl_FragColor = vec4(
sum * sum * 0.009 + texture2D(u_texture, texcoord)) sum * sum * 0.009 + texture2D(u_texture, texcoord))
* vColor; * vColor;

View File

@ -4,10 +4,14 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.ParticleEffect; import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool; import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool.PooledEffect;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import zero1hd.polyjet.Polyjet;
public class CollisionDetector { public class CollisionDetector {
Array<Entity> firstGroup; Array<Entity> firstGroup;
Array<Entity> secondGroup; Array<Entity> secondGroup;
@ -16,6 +20,8 @@ public class CollisionDetector {
Preferences prefs; Preferences prefs;
//Particle pools; //Particle pools;
ParticleEffectPool explosionEffectPool; ParticleEffectPool explosionEffectPool;
Array<PooledEffect> effects = new Array<>();
Sound explosionSFX; Sound explosionSFX;
public CollisionDetector(Array<Entity> firstGroup, Array<Entity> secondGroup, AssetManager assetManager, Preferences prefs) { public CollisionDetector(Array<Entity> firstGroup, Array<Entity> secondGroup, AssetManager assetManager, Preferences prefs) {
this.firstGroup = firstGroup; this.firstGroup = firstGroup;
@ -37,6 +43,17 @@ public class CollisionDetector {
if (fe.getHitZone().overlaps(se.getHitZone())) { if (fe.getHitZone().overlaps(se.getHitZone())) {
Gdx.app.debug("Collision Detector", "Collision between entities: " + fe.getEntityType() + " and " + se.getEntityType()); Gdx.app.debug("Collision Detector", "Collision between entities: " + fe.getEntityType() + " and " + se.getEntityType());
//Play FX;
if (fe.playCollideSFX()) {
explosionSFX.play(prefs.getFloat("fx vol"), 1f, (fe.getX()/Polyjet.GAME_AREA_WIDTH)-0.55f);
}
if (fe.playCollidePFX()) {
PooledEffect currentPFX;
currentPFX = explosionEffectPool.obtain();
currentPFX.setPosition(fe.getX() + fe.getWidth()/2f, fe.getY() + fe.getHeight()/2f);
effects.add(currentPFX);
}
fe.collided(se); fe.collided(se);
se.collided(fe); se.collided(fe);
break; break;
@ -45,4 +62,27 @@ public class CollisionDetector {
} }
} }
} }
/**
* draws the explosion particles where necessecary.
* @param batch the batch used to draw the said particles
* @param cleanBatch whether this method should call begin and end on the batch.
*/
public void renderParticles(Batch batch, float delta, boolean cleanBatch) {
if (cleanBatch) {
batch.begin();
}
for (int i = 0; i < effects.size; i++) {
effects.get(i).draw(batch, delta);
if (effects.get(i).isComplete()) {
effects.get(i).free();
effects.removeIndex(i);
}
}
if (cleanBatch) {
batch.end();
}
}
} }

View File

@ -4,6 +4,8 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class Entity extends Actor { public abstract class Entity extends Actor {
/** /**
* Called whenever a collision is detected * Called whenever a collision is detected
* @param entity is the entity that hit this one. * @param entity is the entity that hit this one.
@ -28,4 +30,11 @@ public abstract class Entity extends Actor {
*/ */
public abstract boolean isDead(); public abstract boolean isDead();
public boolean playCollidePFX() {
return true;
}
public boolean playCollideSFX() {
return true;
}
} }

View File

@ -132,4 +132,14 @@ public class VoidCircle extends Entity implements Poolable {
public boolean isDead() { public boolean isDead() {
return done; return done;
} }
@Override
public boolean playCollidePFX() {
return false;
}
@Override
public boolean playCollideSFX() {
return false;
}
} }

View File

@ -77,11 +77,16 @@ public class GamePlayArea extends Stage {
if (glowShader.getLog().length()!=0) { if (glowShader.getLog().length()!=0) {
System.out.println(glowShader.getLog()); System.out.println(glowShader.getLog());
} }
// blurTarget = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getHeight()/Polyjet.GAME_AREA_HEIGHT, Polyjet.GAME_AREA_WIDTH*Gdx.graphics.getHeight()/Polyjet.GAME_AREA_HEIGHT, false);
// blurTarget = new FrameBuffer(Format.RGBA8888, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT, false); if (Gdx.graphics.getWidth() < 1024) {
fboSize = 1024;
} else if (Gdx.graphics.getWidth() < 2048) {
fboSize = 2048;
} else {
fboSize = 4096; fboSize = 4096;
}
blurTarget = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); blurTarget = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
// blurTarget = new FrameBuffer(Format.RGBA8888, (int) getCamera().viewportWidth, (int) getCamera().viewportHeight, false);
fboRegion = new TextureRegion(blurTarget.getColorBufferTexture()); fboRegion = new TextureRegion(blurTarget.getColorBufferTexture());
fboRegion.flip(false, true); fboRegion.flip(false, true);
@ -131,6 +136,7 @@ public class GamePlayArea extends Stage {
getBatch().setBlendFunction(-1, -1); getBatch().setBlendFunction(-1, -1);
Gdx.gl20.glBlendFuncSeparate(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA,GL20.GL_ONE, GL20.GL_DST_ALPHA); Gdx.gl20.glBlendFuncSeparate(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA,GL20.GL_ONE, GL20.GL_DST_ALPHA);
super.draw(); super.draw();
collisionDetector.renderParticles(getBatch(), Gdx.graphics.getDeltaTime(), true);
blurTarget.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight()); blurTarget.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
getBatch().setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); getBatch().setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);