Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dongbat.invasion.util; import com.artemis.Entity; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.dongbat.invasion.component.DisplayPosition; import com.dongbat.invasion.component.SpriteAnimation; /** * * @author password */ public class EffectUtil { private static final float SINGLE_EXPLOSION_RADIUS = 20; // pixel /** * Create explosions * * @param physPos physical position, center point for explosions * @param physRadius the radius where explosion can spawn * @param duration the duration to randomize the spawning time of explosion */ public static void explode(Vector2 physPos, float physRadius, int duration) { Vector2 position = RenderCameraUtil.physicsToRenderCoords(physPos); float radius = physRadius * PhysicsCameraUtil.getRatio(); int count; if (radius <= SINGLE_EXPLOSION_RADIUS) { count = 1; } else if (radius > SINGLE_EXPLOSION_RADIUS && radius <= SINGLE_EXPLOSION_RADIUS * 2) { count = 4; } else if (radius > SINGLE_EXPLOSION_RADIUS * 2 && radius <= SINGLE_EXPLOSION_RADIUS * 4) { count = 8; } else { count = 12; } if (count == 1) { createSingleExplosion(position, 0); return; } for (int i = 0; i < count; i++) { Vector2 pos = new Vector2(position); pos.x += MathUtils.random(-radius, radius); pos.y += MathUtils.random(-radius, radius); int delay = MathUtils.random(duration); createSingleExplosion(pos, delay); } } private static void createSingleExplosion(Vector2 position, int delay) { EntityUtil.addCreator(TimeUtil.getGameTime() + delay, new ExplosionCreator(position)); } private static class ExplosionCreator extends EntityUtil.EntityCreator { private final Vector2 position; public ExplosionCreator(Vector2 position) { this.position = position; } @Override public void create() { String name = "effects/explosion"; SpriteAnimation spriteAnimation = new SpriteAnimation(); spriteAnimation.addAnimation(name, AnimationUtil.get(name, 0.1f)); spriteAnimation.setCurrent(name, false); spriteAnimation.setCallback(new DeleteAfterFinished()); DisplayPosition displayPosition = new DisplayPosition(position); Entity explosion = ECSUtil.getWorld().createEntity(); explosion.edit().add(displayPosition).add(spriteAnimation); } } private static class DeleteAfterFinished implements SpriteAnimation.AnimationFinishedCallback { @Override public void finished(Entity entity) { entity.deleteFromWorld(); } } }