package org.academiadecodigo.bootcamp11.sniperelite;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Game {
    private GameObject[] gameObjects;
    private SniperRifle sniper;

    public Game(int objects) {
        this.sniper = new SniperRifle();
        this.gameObjects = new GameObject[objects];
    }

    public void start() {
        createObjects();

        for (GameObject object : gameObjects) {
            if (Enemy.isEnemy(object)) {
                sniper.shoot((Enemy) object);
            }
        }

        System.out.println(sniper);
    }

    private void createObjects() {
        System.out.println("======     GAME START    ======");
        System.out.printf("===  <Spawning %-2s objects>  ===\n",
            gameObjects.length);
        for (int i = 0; i < gameObjects.length; i++) {
            double roll = Math.random();

            if (roll < ObjectType.TREE.getSpawnChance()) {
                gameObjects[i] = new Tree();
            } else if (roll < ObjectType.ARMORED.getSpawnChance()) {
                gameObjects[i] = new ArmoredEnemy();
            } else {
                gameObjects[i] = new SoldierEnemy();
            }
            System.out.println(gameObjects[i].getMessage());
        }
        System.out.println("======   <end spawning>  ======\n");
    }
}
