SparkManager.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.explonoid.effects;

import com.nokia.example.explonoid.game.Resources;
import java.util.Random;
import javax.microedition.lcdui.game.LayerManager;

public class SparkManager {

    private Spark[] sparks;
    private int nextIndex = 0;

    public SparkManager(int capacity, Resources r, LayerManager lm) {
        sparks = new Spark[capacity];
        for (int i = 0; i < capacity; i++) {
            sparks[i] = new Spark(r.spark);
            lm.append(sparks[i]);
        }
    }

    public void burst(int x, int y) {
        Random rnd = new Random();
        for (int i = 0; i < 12; i++) {
            int dirX = (int) ((100.0 * rnd.nextDouble() % 12 - 6.0));
            int dirY = (int) ((100.0 * rnd.nextDouble() % 12 - 6.0));
            Spark spark = sparks[nextIndex];
            spark.shoot(x, y, dirX, dirY);
            if (i > 2) {
                spark.setFrame(Math.abs(rnd.nextInt(7)));
            }
            nextIndex = (nextIndex + 1) % sparks.length;
        }
    }
}