From 5381162896a9cca9ea7b8fdaf0fda635d9b56b19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Hor=C3=A1=C4=8Dek?= <horaceklubos@gmail.com>
Date: Fri, 17 Jul 2015 18:58:00 +0200
Subject: [PATCH] #31 Generating targets

---
 .../shooting_range/ShootingRangeGame.java     | 15 +++++--
 .../games/shooting_range/model/GameState.java | 13 +++---
 .../shooting_range/tools/TargetGenerator.java |  2 +-
 .../tools/TargetPositionController.java       | 43 ++++++++++---------
 4 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/ShootingRangeGame.java b/core/src/cz/nic/tablexia/game/games/shooting_range/ShootingRangeGame.java
index 6187ecc2b..d08b9b0f0 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/ShootingRangeGame.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/ShootingRangeGame.java
@@ -60,6 +60,9 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
                     target.setShot(true);
                     target.addAction(Actions.scaleTo(1, 0, 0.1f));
                     getData().addHit();
+                    if (getData().getHitLimit() == 0) {
+                        newTarget();
+                    }
                 } else if (target.getTextureType().equals(TextureType.BOX_BAD)) {
                     target.setShot(true);
                     getData().addBoxBad();
@@ -80,7 +83,6 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
                             sfxLibrary.getSound(SoundType.BOX_SMOKE);
                             break;
                         default:
-                            throw new IllegalStateException("Unknown state");
                     }
                 } else if (target.getTextureType().equals(TextureType.BOX_GOOD)) {
                     target.setShot(true);
@@ -148,7 +150,7 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
 
 
         targetGenerator = new TargetGenerator(getGameDifficulty(), getRandom(), gfxLibrary, clickListener, hitEvaluator);
-        targetPositionController = new TargetPositionController(scene, targetGenerator, getRandom(), getStage().getWidth());
+        targetPositionController = new TargetPositionController(scene, targetGenerator, getRandom(), getGameDifficulty(), getStage().getWidth());
         for (Row row : scene.getRows()) {         // init each row with starting flowers
             row.setWave(Wave.getWave(getGameDifficulty(), row.getRowIndex()));
             row.addTargets(targetGenerator.generateTargetRow(row));
@@ -180,17 +182,22 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
      * Generate new target and hit limit a show it in carousel
      */
     private void newTarget() {
-        getData().setCurrentHitLimit(getNewHitLimit());
+        getData().setHitLimit(getNewHitLimit());
         getData().setCurrentTarget(getRandomFlower());
         carousel.showNextFlower(getData().getCurrentTarget());
+        targetPositionController.setTargetType(getData().getCurrentTarget());
     }
 
     /**
-     * Get random flower from target bag. Currently ignoring if target type is on the screen
+     * Get random flower from target bag.
      *
      * @return
      */
     private TextureType getRandomFlower() {
+        if (targetPositionController.getAvailableTypes().size() > 0) {
+            TextureType[] types = targetPositionController.getAvailableTypes().toArray(new TextureType[0]);
+            return types[getRandom().nextInt(types.length)];
+        }
         TextureType flowerType = targetGenerator.getRandomFlowerType(getGameDifficulty());
         return flowerType;
     }
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/model/GameState.java b/core/src/cz/nic/tablexia/game/games/shooting_range/model/GameState.java
index 9ba1fd95a..b8b97e2bc 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/model/GameState.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/model/GameState.java
@@ -19,19 +19,19 @@ public class GameState {
     private int   boxesGoodHit = 0;
     private float time         = 0f;
     private TextureType currentTarget;
-    private int   currentHitLimit = 0;
-    private float gameSpeed       = Properties.GAME_SPEED_NORMAL;
+    private int   hitLimit  = 0;
+    private float gameSpeed = Properties.GAME_SPEED_NORMAL;
 
-    public int getCurrentHitLimit() {
-        return currentHitLimit;
+    public int getHitLimit() {
+        return hitLimit;
     }
 
     public void setGameSpeed(float gameSpeed) {
         this.gameSpeed = gameSpeed;
     }
 
-    public void setCurrentHitLimit(int currentHitLimit) {
-        this.currentHitLimit = currentHitLimit;
+    public void setHitLimit(int limit) {
+        this.hitLimit = limit;
     }
 
     public int getScore() {
@@ -68,6 +68,7 @@ public class GameState {
     public void addHit() {
         this.hits++;
         addScore(2);
+        hitLimit--;
     }
 
     public int getMisses() {
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetGenerator.java b/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetGenerator.java
index d22930898..c769ac2e9 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetGenerator.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetGenerator.java
@@ -84,7 +84,7 @@ public class TargetGenerator {
         int flowerNumber = row.getWave().getFlowersOnScreen();
         float targetPeriod = row.getWave().getRunningTime() / row.getWave().getFlowersOnScreen();
 
-        // přidáme jeden terč za okraj na prvn obrazovce
+        // add one more target off the screen
         for (int i = -flowerNumber; i <= 0; i++) {
             Target target = getRandomTarget(textureTypeBag, targetPeriod * i, row);
             flowers.add(target);
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetPositionController.java b/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetPositionController.java
index 247fe31eb..ed818644c 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetPositionController.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/tools/TargetPositionController.java
@@ -6,6 +6,7 @@ import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
+import cz.nic.tablexia.game.difficulty.GameDifficulty;
 import cz.nic.tablexia.game.games.shooting_range.actors.Row;
 import cz.nic.tablexia.game.games.shooting_range.actors.Scene;
 import cz.nic.tablexia.game.games.shooting_range.actors.Target;
@@ -23,14 +24,16 @@ public class TargetPositionController {
     private final float           width;
     private final Scene           scene;
     private       TextureType     targetType;
+    private       GameDifficulty  gameDifficulty;
+    Set<TextureType> availableTypes = new HashSet<TextureType>();
 
-
-    public TargetPositionController(Scene scene, TargetGenerator targetGenerator, Random random, float width) {
+    public TargetPositionController(Scene scene, TargetGenerator targetGenerator, Random random, GameDifficulty gameDifficulty, float width) {
         super();
         this.scene = scene;
         this.targetGenerator = targetGenerator;
         this.random = random;
         this.width = width;
+        this.gameDifficulty = gameDifficulty;
     }
 
 
@@ -38,10 +41,14 @@ public class TargetPositionController {
         this.targetType = targetType;
     }
 
+    public Set<TextureType> getAvailableTypes() {
+        return availableTypes;
+    }
+
     /*
-         * (non-Javadoc)
-         * @see org.andengine.engine.handler.IUpdateHandler#onUpdate(float)
-         */
+             * (non-Javadoc)
+             * @see org.andengine.engine.handler.IUpdateHandler#onUpdate(float)
+             */
     public void onUpdate(float pSecondsElapsed) {
         elapsedTime += pSecondsElapsed;
         Set<TextureType> availableTypes = new HashSet<TextureType>();
@@ -55,31 +62,28 @@ public class TargetPositionController {
                     // is last target in row
                     if (targetGenerator.getLastTargetInRow(row).equals(target)) {
                         // last invisible target is on scene - add new one
-                        row.addTarget(targetGenerator.addNewTargetToRow(row, TextureType.FLOWER_1));
-                        //Log.info(getClass(), "Adding " + target);
+                        List<TextureType> textures = TextureType.getTargetPack(gameDifficulty);
+                        row.addTarget(targetGenerator.addNewTargetToRow(row, textures.get(random.nextInt(textures.size()))));
                     }
                     target.setX(x);
                     target.setY(row.getHeight() - 50 + row.getWave().getY(elapsedTime - target.getStartTime(), row.getHeight()));
 
                     // is target in first half of its way? to get available target types not immediately leaving screen
                     if ((x > (Direction.RIGHT == row.getWave().getDirection() ? 0 : width / 2)) && (x < (Direction.RIGHT == row.getWave().getDirection() ? width / 2 : width))) {
-                        // pouze nesestřelené
+                        // only not hit
                         if (!target.isShot()) {
                             availableTypes.add(target.getTextureType());
                         }
                     }
                     // target is not on scene
                 } else {
-                    if (target.getParent() != null) {
-                        if (((row.getWave().getDirection() == Direction.LEFT) && (x < 0)) || ((row.getWave().getDirection() == Direction.RIGHT) && (x > width))) {
-                            // uĹľ je mimo obraz
-                            // TODO remove with row.removeTarget method
-                            row.removeTarget(target);
-                            //Log.info(getClass(), "Removing target " + target);
-                        } else {
-                            // teprve přichází do obrazu
-                            availableTypes.add(target.getTextureType()); // TODO zkusime vypnout ocekavane kytky, aby jich rovnou pridaval vic
-                        }
+                    if (((row.getWave().getDirection() == Direction.LEFT) && (x < 0)) || ((row.getWave().getDirection() == Direction.RIGHT) && (x > width))) {
+                        // gone out of screen
+                        row.removeTarget(target);
+                    } else {
+                        // comming to the screen
+                        invisibles.add(target);
+                        availableTypes.add(target.getTextureType()); // TODO count available to make them more available or select tartgetType, tha has multiple occurences
                     }
                 }
             }
@@ -91,7 +95,6 @@ public class TargetPositionController {
                 Target swap = invisibles.get(index);
                 Target swapTo = targetGenerator.addNewTargetToRow(swap.getRow(), targetType, swap.getStartTime(), targetGenerator.getLastTargetInRow(swap.getRow()).equals(swap));
 
-                // TODO
                 swap.getRow().addTarget(swapTo);
                 swap.getRow().removeTarget(swap);
             } else {
@@ -99,6 +102,6 @@ public class TargetPositionController {
                 row.addTarget(targetGenerator.addNewTargetToRow(row, targetType));
             }
         }
-
+        this.availableTypes = availableTypes;
     }
 }
-- 
GitLab