Levels.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.battletank.game;

import com.nokia.example.battletank.Main;
import com.nokia.example.battletank.game.entities.Enemy;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Image;

public class Levels {

    private static final String PATH = "/levels/";
    private static final String DRM_PATH = "/drm/data/ResourceId_";
    private static final String[] fileNames = {
        "first.png",
        "classic.png",
        "tight.png",
        "easy.png",
        "mario.png",
        "mega.png",
        "bob.png"
    };

    /**
     * Loads image related to the given level. Throws ProtectedContentException
     * if a full version level is tried to be loaded when in trial mode.
     *
     * @param level Level number
     * @return Image containing level structure
     * @throws ProtectedContentException
     * @throws IOException
     */
    public static Image getImage(int level)
        throws ProtectedContentException, IOException {
        int levelIndex = (level - 1) % fileNames.length;
        String fileName = fileNames[levelIndex];
        if (levelIndex < 2) {
            return Image.createImage(PATH + fileName);
        }
        else if (Main.isTrial()) {
            throw new ProtectedContentException();
        }
        else {
            try {
                InputStream input = Main.getIAPManager().getDRMResourceAsStream(DRM_PATH
                    + Main.PURCHASE_ID + "/" + fileName);
                return Image.createImage(input);
            }
            catch (Exception e) {
                throw new IOException();
            }
        }
    }

    /**
     * Returns number of enemies total in the given level.
     *
     * @param level Sequence number of the level
     * @return Number of enemies total
     */
    public static int getTotalEnemies(int level) {
        return Math.min(2 + level / 2 * 2, 20);
    }

    /**
     * Return number of max enemies concurrently in the given level.
     *
     * @param level Sequence number of the level
     * @return Number of max enemies concurrently
     */
    public static int getConcurrentEnemies(int level) {
        return Math.min(2 + level / 4, 6);
    }

    /**
     * Returns type of enemy to be spawned.
     *
     * @param level Sequence number of the level
     * @param remainingEnemies Enemies remaining in the level
     * @return Type of enemy to be spawned
     */
    public static int getEnemyType(int level, int remainingEnemies) {
        int enemy = getTotalEnemies(level) - remainingEnemies;
        if (enemy < 3) {
            return Enemy.BASIC;
        }
        else if (enemy % 3 == 0) {
            return Enemy.FAST;
        }
        else if (enemy % 3 == 1) {
            return Enemy.BASIC;
        }
        else {
            return Enemy.HEAVY;
        }
    }
}