@Override public void gameObjectAdded(GameObject object, GameObject parent) { ParticleController pc = object.getController(ParticleController.class); if (pc != null) { PooledEffect pe = getLayer().getState().getEffect(pc.getEffectName()); effects.put(object, pe); pe.setEmittersCleanUpBlendFunction(false); pos_check.set((getLayer().getX() / cam.getZoom() + object.getAbsoluteX() + pc.getRelativeX()) * cam.getZoom(), (getLayer().getY() / cam.getZoom() + object.getAbsoluteY() + pc.getRelativeY()) * cam.getZoom()); pe.scaleEffect(pc.getXScaleFactor() * cam.getZoom(), pc.getYScaleFactor() * cam.getZoom(), pc.getMotionScaleFactor() * cam.getZoom()); if (pc.isPostDraw()) { post_draws.add(pe); } else { pre_draws.add(pe); } } }
@Override public void preDraw(Batch batch, float parentAlpha) { // boolean last_additive = pre_draws.size > 0?containsAdditive(pre_draws.first()):false; for (int i = 0; i < pre_draws.size; i++) { PooledEffect pe = pre_draws.get(i); // boolean contains_additive = containsAdditive(pe); // if(last_additive&&!contains_additive) { // batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // } // last_additive = contains_additive; pe.draw(batch); } batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); }
@Override public void postDraw(Batch batch, float parentAlpha) { // boolean last_additive = post_draws.size > 0?containsAdditive(post_draws.first()):false; for (int i = 0; i < post_draws.size; i++) { PooledEffect pe = post_draws.get(i); // boolean contains_additive = containsAdditive(pe); // if(last_additive&&!contains_additive) { // batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // } // last_additive = contains_additive; pe.draw(batch); } batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); }
@Override public void update(float delta) { for (int i = 0; i < effects.size; i++) { GameObject o = effects.getKeyAt(i); ParticleController pc = o.getController(ParticleController.class); PooledEffect pe = effects.getValueAt(i); if (pc.isComplete()) pe.allowCompletion(); pos_check.set((getLayer().getX() / cam.getZoom() + o.getAbsoluteX() + pc.getRelativeX()) * cam.getZoom(), (getLayer().getY() / cam.getZoom() + o.getAbsoluteY() + pc.getRelativeY()) * cam.getZoom()); // cam.unproject(pos_check); pe.update(delta); if (cam.getZoom() != last_zoom) { float zoom_dif = last_zoom - cam.getZoom(); pe.scaleEffect(1f - zoom_dif, 1f - zoom_dif, 1f - zoom_dif); last_zoom = cam.getZoom(); } pre_draws.sort(pcomp); post_draws.sort(pcomp); pe.setPosition(pos_check.x, pos_check.y); } }
public void updateAndRender(float delta, Batch batch) { for (Entry<PooledEffect, GameObject> effectEntry : objects.entrySet()) { float x = effectEntry.getValue().getLeft() + offsets.get(effectEntry.getValue()).x; float y = effectEntry.getValue().getTop() + offsets.get(effectEntry.getValue()).y; effectEntry.getKey().setPosition(x, y); effectEntry.getKey().draw(batch, delta); if (effectEntry.getKey().isComplete()) { effectEntry.getKey().reset(); remove(effectEntry.getKey()); break; } } for (PooledEffect single : singleEffects) { single.draw(batch, delta); if (single.isComplete()) { single.free(); singleEffects.remove(single); } } }
@Override public void render(float delta) { // TODO: Make this UDACITY_BLUE instead clearScreen(UDACITY_ORANGE); batch.begin(); batch.draw(logo, (Gdx.graphics.getWidth() - LOGO_WIDTH) / 2, (Gdx.graphics.getHeight() - logoHeight) / 2, LOGO_WIDTH, logoHeight); for (int i = effects.size - 1; i >= 0; i--) { PooledEffect effect = effects.get(i); effect.draw(batch, delta); if (effect.isComplete()) { effect.free(); effects.removeIndex(i); } } batch.end(); }
@Override public void render(float delta) { clearScreen(UDACITY_ORANGE); batch.begin(); // TODO: Investigate the .draw() method on SpriteBatch batch.draw(logo, (Gdx.graphics.getWidth() - LOGO_WIDTH) / 2, (Gdx.graphics.getHeight() - logoHeight) / 2, LOGO_WIDTH, logoHeight); // TODO: Why are we iterating backwards? for (int i = effects.size - 1; i >= 0; i--) { PooledEffect effect = effects.get(i); effect.draw(batch, delta); if (effect.isComplete()) { effect.free(); effects.removeIndex(i); } } batch.end(); }
private void addEffect(String effectName, String imgName) { ParticleEffect reffect = new ParticleEffect(); TextureAtlas atlas = new TextureAtlas(); atlas.addRegion( imgName, new TextureRegion(new Texture(Gdx.files.internal("particles/" + imgName + ".png")))); reffect.load(Gdx.files.internal("particles/" + effectName + ".p"), atlas); EffectPool = new ParticleEffectPool(reffect, 1, 2); // Create effect PooledEffect effect = EffectPool.obtain(); effect.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); effects.add(effect); }
/** * Invoke effect for special effect by its key. Change the current active key by key input parameter * @param x position-x of location where the effect happen. * @param y position-y of location where the effect happen. * @param key key for special effect */ public void invokeEffect(String key, float x, float y) { PooledEffect effect = collection.get(key).obtain(); effect.setPosition(x, y); effects.add(effect); }
@Override public void render(float gameTime) { for (int i = effects.size - 1; i >= 0; i--) { PooledEffect effect = effects.get(i); effect.draw(getGameService().getSpriteBatch(), gameTime); if (effect.isComplete()) { effect.free(); effects.removeIndex(i); } } }
public PooledEffect getEffect(String name) { if (particle_effects.containsKey(name)) { return particle_effects.get(name).obtain(); } error("ParticleEffect -> not found [" + name + "]."); return null; }
@Override public void init(MapProperties properties) { effects = new ArrayMap<GameObject, ParticleEffectPool.PooledEffect>(); pre_draws = new Array<ParticleEffectPool.PooledEffect>(); post_draws = new Array<ParticleEffectPool.PooledEffect>(); pos_check = new Vector2(); cam = getLayer().getCamera(); last_zoom = cam.getZoom(); pcomp = new ParticleComparator(); }
@Override public void gameObjectRemoved(GameObject object, GameObject parent) { if (effects.containsKey(object)) { PooledEffect pe = effects.get(object); effects.removeKey(object); pre_draws.removeValue(pe, true); post_draws.removeValue(pe, true); pe.free(); } }
private static boolean containsAdditive(PooledEffect effect) { boolean additive = false; Array<ParticleEmitter> emmiters = effect.getEmitters(); for (int i = 0; i < emmiters.size; i++) { if (emmiters.get(i).isAdditive()) { additive = true; break; } } return additive; }
@Override public int compare(PooledEffect o1, PooledEffect o2) { boolean o1_add = false; boolean o2_add = false; o1_add = containsAdditive(o1); o2_add = containsAdditive(o2); if (o1_add && !o2_add) return 11; else if (o2_add && !o1_add) return -1; return 0; }
public ParticleRenderer() { singleEffects = new CopyOnWriteArrayList<PooledEffect>(); pools = new HashMap<String, ParticleEffectPool>(); effects = new HashMap<GameObject, PooledEffect>(); mapping = new HashMap<GameObject, String>(); offsets = new HashMap<GameObject, Vector2>(); objects = new HashMap<PooledEffect, GameObject>(); }
public PooledEffect getEffect(GameObject object) { PooledEffect effect = effects.get(object); if (effect != null) { return effect; } else if (mapping.containsKey(object)) { String id = mapping.get(object); ParticleEffectPool pool = pools.get(id); effect = pool.obtain(); effects.put(object, effect); return effect; } else { return null; } }
public void applyParticleEffect(String id, float x, float y) { ensurePool(id); ParticleEffectPool pool = pools.get(id); PooledEffect effect = pool.obtain(); effect.setPosition(x, y); singleEffects.add(effect); }
public void applyParticleEffect(GameObject object, String id, float offsetX, float offsetY) { ensurePool(id); mapping.put(object, id); offsets.put(object, new Vector2(offsetX, offsetY)); PooledEffect effect = getEffect(object); objects.put(effect, object); }
public PooledEffect obtain(String name) { ParticleEffectPool pool = pools.get(name); if (pool == null) { logger.error("pool not found " + name); return null; } return pool.obtain(); }
public void free(PooledEffect effect) { Values<ParticleEffectPool> values = pools.values(); while (values.hasNext) { values.next().free(effect); } }
@Override public void render(float delta) { // model.frame(); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.begin(); // Update and draw effects: PooledEffect effect = effects.get(effectIndex); effect.draw(batch, delta); if (effect.isComplete()) { effect.free(); effects.removeIndex(effectIndex); } batch.end(); }
public void remove(PooledEffect e) { e.free(); objects.remove(e); }
ManagedParticleEffect(PooledEffect effect, String path) { this.effect = effect; this.path = path; }
PooledEffect getEffect() { return effect; }
private void spawnParticleEffect(int x, int y) { PooledEffect effect = touchEffectPool.obtain(); effect.setPosition(x, Gdx.graphics.getHeight() - y); effects.add(effect); }
public EntityBuilder particle (ParticleType type) { PooledEffect effect = engine.getSystem(ParticleSystem.class).createEffect(position, type); entity.add(engine.createComponent(ParticleComponent.class).init(effect)); return this; }
public PooledEffect createEffect (Vector2 position, ParticleType type) { PooledEffect effect = effectPools.get(type.ordinal()).obtain(); effect.setPosition(position.x, position.y); return effect; }
public ParticleComponent init (PooledEffect effect) { this.effect = effect; return this; }
protected PooledEffect newObject () { return new PooledEffect(effect); }
public PooledEffect obtain () { PooledEffect effect = super.obtain(); effect.reset(); return effect; }
PooledEffect (ParticleEffect effect) { super(effect); }