From 4ffc0055ba92bdecf62d0cc99d2f2684b4734684 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Hor=C3=A1=C4=8Dek?= <horaceklubos@gmail.com>
Date: Fri, 10 Jul 2015 14:03:00 +0200
Subject: [PATCH] #31 Targets movement and adding new ones to rows

---
 .../shooting_range/ShootingRangeGame.java     | 10 +---
 .../game/games/shooting_range/actors/Row.java | 27 +++++++--
 .../games/shooting_range/actors/Target.java   | 19 +++---
 .../game/games/shooting_range/model/Wave.java |  2 +-
 .../shooting_range/tools/TargetGenerator.java | 53 +++++++----------
 .../tools/TargetPositionController.java       | 59 ++++++++++---------
 6 files changed, 89 insertions(+), 81 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 e589cb97c..4fc47381c 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
@@ -11,6 +11,7 @@ import cz.nic.tablexia.game.AbstractTablexiaGame;
 import cz.nic.tablexia.game.common.media.GfxLibrary;
 import cz.nic.tablexia.game.common.media.SfxLibrary;
 import cz.nic.tablexia.game.games.shooting_range.actors.Carousel;
+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;
 import cz.nic.tablexia.game.games.shooting_range.actors.Watch;
@@ -55,14 +56,9 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
         initScene();
 
         targetGenerator = new TargetGenerator(getGameDifficulty(), getRandom(), gfxLibrary);
-        waves = targetGenerator.initWaves();
         targetPositionController = new TargetPositionController(scene, targetGenerator, getRandom(), getStage().getWidth());
-
-        for (int i = 0; i < waves.length; i++) {
-            List<Target> wave = waves[i];
-            for (Target target : wave) {
-                scene.getRows()[i].addTarget(target);
-            }
+        for (Row row : scene.getRows()) {
+            row.addTargets(targetGenerator.generateTargetRow(row));
         }
         scene.addAction(Actions.sequence(Actions.delay(3), Actions.run(new Runnable() {
             // TODO animate flowers in
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Row.java b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Row.java
index 9d0c3d67d..f494f8e02 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Row.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Row.java
@@ -3,23 +3,38 @@ package cz.nic.tablexia.game.games.shooting_range.actors;
 import com.badlogic.gdx.scenes.scene2d.Group;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
 
-import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import cz.nic.tablexia.game.common.media.GfxLibrary;
 import cz.nic.tablexia.game.games.shooting_range.media.Textures;
+import cz.nic.tablexia.game.games.shooting_range.model.Wave;
 
 /**
  * Created by lhoracek on 6/23/15.
  */
 public class Row extends Group {
-    private Image wave;
+    private Image waveImage;
     private Group targetGroup;
-    private List<Target> targets = new ArrayList<Target>();
+    private List<Target> targets = new CopyOnWriteArrayList<Target>();
+    private Wave wave;
 
-    public Row(GfxLibrary gfxLibrary, Textures texture) {
+    public Row(GfxLibrary gfxLibrary, Wave wave, Textures texture) {
         addActor(targetGroup = new Group());
-        addActor(wave = new Image(gfxLibrary.getTextureRegion(texture)));
+        addActor(waveImage = new Image(gfxLibrary.getTextureRegion(texture)));
+        this.wave = wave;
+    }
+
+    public Wave getWave() {
+        return wave;
+    }
+
+    public void addTargets(Collection<Target> targets) {
+        for (Target target : targets) {
+            targetGroup.addActor(target);
+            targets.add(target);
+        }
     }
 
     public void addTarget(Target target) {
@@ -39,7 +54,7 @@ public class Row extends Group {
     @Override
     protected void sizeChanged() {
         super.sizeChanged();
-        wave.setSize(getWidth(), getHeight());
+        waveImage.setSize(getWidth(), getHeight());
         targetGroup.setSize(getWidth(), getHeight());
     }
 }
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Target.java b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Target.java
index 133c31c68..b6c775552 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Target.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Target.java
@@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
 
 import cz.nic.tablexia.game.games.shooting_range.media.Textures;
-import cz.nic.tablexia.game.games.shooting_range.model.Wave;
 
 /**
  * Created by lhoracek on 6/22/15.
@@ -13,9 +12,10 @@ import cz.nic.tablexia.game.games.shooting_range.model.Wave;
 public class Target extends Image {
     private final float    startTime;
     private       Textures textureType;
-    private final Wave     wave;
     private boolean shot = false;
     private Sprite effect;
+    private Row    row;
+
 
     public static interface PositionProvider {
         public float getY(float elapseTime, float maxHeight);
@@ -28,11 +28,11 @@ public class Target extends Image {
         };
     }
 
-    public Target(TextureRegion texture, Float startTime, Wave wave, Textures textureType) {
+    public Target(TextureRegion texture, Float startTime, Row row, Textures textureType) {
         super(texture);
         this.startTime = startTime;
         this.textureType = textureType;
-        this.wave = wave;
+        this.row = row;
     }
 
     public float getStartTime() {
@@ -47,17 +47,18 @@ public class Target extends Image {
         return textureType;
     }
 
-    public Wave getWave() {
-        return wave;
+    public Row getRow() {
+        return row;
     }
 
+
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = (prime * result) + Float.floatToIntBits(startTime);
         result = (prime * result) + ((textureType == null) ? 0 : textureType.hashCode());
-        result = (prime * result) + ((wave == null) ? 0 : wave.hashCode());
+        result = (prime * result) + ((row == null) ? 0 : row.hashCode());
         return result;
     }
 
@@ -79,7 +80,7 @@ public class Target extends Image {
         if (textureType != other.textureType) {
             return false;
         }
-        if (wave != other.wave) {
+        if (row != other.row) {
             return false;
         }
         return true;
@@ -106,7 +107,7 @@ public class Target extends Image {
     public String toString() {
         return "Target{" +
                 "textureType=" + textureType +
-                ", wave=" + wave +
+                ", row=" + row +
                 ", shot=" + shot +
                 '}';
     }
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/model/Wave.java b/core/src/cz/nic/tablexia/game/games/shooting_range/model/Wave.java
index ff1031bdc..23e2a157c 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/model/Wave.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/model/Wave.java
@@ -132,7 +132,7 @@ public enum Wave {
         }
     }
 
-    public int getFlowerOnScreen() {
+    public int getFlowersOnScreen() {
         return flowerOnScreen;
     }
 
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 6319b69ae..6b4ddae91 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
@@ -9,9 +9,9 @@ import java.util.Random;
 
 import cz.nic.tablexia.game.common.media.GfxLibrary;
 import cz.nic.tablexia.game.difficulty.GameDifficulty;
-import cz.nic.tablexia.game.games.shooting_range.media.Textures;
+import cz.nic.tablexia.game.games.shooting_range.actors.Row;
 import cz.nic.tablexia.game.games.shooting_range.actors.Target;
-import cz.nic.tablexia.game.games.shooting_range.model.Wave;
+import cz.nic.tablexia.game.games.shooting_range.media.Textures;
 
 /**
  * Created by lhoracek on 6/22/15.
@@ -29,7 +29,7 @@ public class TargetGenerator {
 
     private GameDifficulty gameDifficulty;
     private Random         random;
-    private GfxLibrary gfxLibrary;
+    private GfxLibrary     gfxLibrary;
 
     public TargetGenerator(GameDifficulty gameDifficulty, Random random, GfxLibrary gfxLibrary) {
         super();
@@ -45,27 +45,27 @@ public class TargetGenerator {
         return textureTypeBag.get(random);
     }
 
-    private Map<Wave, Target> lastTargetsForWave = new HashMap<Wave, Target>();
+    private Map<Row, Target> lastTargetsForRow = new HashMap<Row, Target>();
 
-    public Target getLastTargetInWave(Wave wave) {
-        return lastTargetsForWave.get(wave);
+    public Target getLastTargetInRow(Row row) {
+        return lastTargetsForRow.get(row);
     }
 
-    public Target addNewTargetToWave(Wave wave, Textures requiredTextureType) {
-        Target prevLastTarget = getLastTargetInWave(wave);
-        float targetPeriod = wave.getRunningTime() / wave.getFlowerOnScreen();
-        return addNewTargetToWave(wave, requiredTextureType, prevLastTarget.getStartTime() + targetPeriod);
+    public Target addNewTargetToRow(Row row, Textures requiredTextureType) {
+        Target prevLastTarget = getLastTargetInRow(row);
+        float targetPeriod = row.getWave().getRunningTime() / row.getWave().getFlowersOnScreen();
+        return addNewTargetToRow(row, requiredTextureType, prevLastTarget.getStartTime() + targetPeriod);
     }
 
-    public Target addNewTargetToWave(Wave wave, Textures requiredTextureType, float startTime) {
-        return addNewTargetToWave(wave, requiredTextureType, startTime, true);
+    public Target addNewTargetToRow(Row row, Textures requiredTextureType, float startTime) {
+        return addNewTargetToRow(row, requiredTextureType, startTime, true);
     }
 
-    public Target addNewTargetToWave(Wave wave, Textures requiredTextureType, float startTime, boolean asLast) {
+    public Target addNewTargetToRow(Row row, Textures requiredTextureType, float startTime, boolean asLast) {
         List<Textures> textureTypeBag = new ArrayList<Textures>(requiredTextureType == null ? getTextureTypeBag() : Arrays.asList((new Textures[]{requiredTextureType})));
-        Target target = getRandomTarget(textureTypeBag, startTime, wave);
+        Target target = getRandomTarget(textureTypeBag, startTime, row);
         if (asLast) {
-            lastTargetsForWave.put(wave, target);
+            lastTargetsForRow.put(row, target);
         }
         return target;
     }
@@ -73,19 +73,19 @@ public class TargetGenerator {
     /*
      * Init row on start
      */
-    public List<Target> generateFlowerRow(Wave wave) {
+    public List<Target> generateTargetRow(Row row) {
         List<Target> flowers = new ArrayList<Target>();
         List<Textures> textureTypeBag = new ArrayList<Textures>(getTextureTypeBag());
 
-        int flowerNumber = wave.getFlowerOnScreen();
-        float targetPeriod = wave.getRunningTime() / wave.getFlowerOnScreen();
+        int flowerNumber = row.getWave().getFlowersOnScreen();
+        float targetPeriod = row.getWave().getRunningTime() / row.getWave().getFlowersOnScreen();
 
         // přidáme jeden terč za okraj na prvn obrazovce
         for (int i = -flowerNumber; i <= 0; i++) {
-            Target target = getRandomTarget(textureTypeBag, targetPeriod * i, wave);
+            Target target = getRandomTarget(textureTypeBag, targetPeriod * i, row);
             flowers.add(target);
             if (i == 0) {
-                lastTargetsForWave.put(wave, target);
+                lastTargetsForRow.put(row, target);
             }
         }
         return flowers;
@@ -104,15 +104,14 @@ public class TargetGenerator {
         return Textures.BOX_GOOD;
     }
 
-    private Target getRandomTarget(List<Textures> textureTypeBag, float startTime, Wave wave) {
+    private Target getRandomTarget(List<Textures> textureTypeBag, float startTime, Row row) {
         float boxIndex = random.nextFloat();
         Textures textureType = getRandomBoxType();
         if (boxIndex > getBoxProbability()) {
             int random = (int) (Math.random() * textureTypeBag.size());
             textureType = textureTypeBag.get(random);
         }
-
-        return new Target(gfxLibrary.getTextureRegion(textureType), startTime, wave, textureType);
+        return new Target(gfxLibrary.getTextureRegion(textureType), startTime, row, textureType);
     }
 
     private float getBoxProbability() {
@@ -140,12 +139,4 @@ public class TargetGenerator {
                 throw new IllegalStateException("Unknown difficulty");
         }
     }
-
-    public List<Target>[] initWaves() {
-        List<Target>[] targets = new List[Wave.WAVE_ROWS];
-        for (int i = 0; i < Wave.WAVE_ROWS; i++) {
-            targets[i] = generateFlowerRow(Wave.getWave(gameDifficulty, i));
-        }
-        return targets;
-    }
 }
\ No newline at end of file
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 fdd4725f4..0f3c828f4 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
@@ -1,6 +1,8 @@
 package cz.nic.tablexia.game.games.shooting_range.tools;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
@@ -26,6 +28,7 @@ public class TargetPositionController {
     private final Random          random;
     private final float           width;
     private final Scene           scene;
+    private       Textures        targetType;
 
 
     public TargetPositionController(Scene scene, TargetGenerator targetGenerator, Random random, float width) {
@@ -51,33 +54,37 @@ public class TargetPositionController {
     public void onUpdate(float pSecondsElapsed) {
         elapsedTime += (pSecondsElapsed * gameSpeed);
         Set<Textures> availableTypes = new HashSet<Textures>();
+        List<Target> invisibles = new ArrayList<Target>();
 
         for (Row row : scene.getRows()) {
             for (Target target : row.getTargets()) {
-                float x = target.getWave().getX(elapsedTime - target.getStartTime(), width);
+                float x = row.getWave().getX(elapsedTime - target.getStartTime(), width);
                 // is target in scene?
                 if (((x + target.getWidth()) > 0) && (x < width)) {
-                    if (target.getParent() == null) {
-                        if (targetGenerator.getLastTargetInWave(target.getWave()).equals(target)) {
-                            // TODO last invisible target is on scene - add new one
-                            //toAdd.add(targetGenerator.addNewTargetToWave(target.getWave(), parent.getVertexBufferObjectManager(), null));
-                        }
+                    // 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, Textures.BOX_SMOKE_1));
+                        //Log.info(getClass(), "Adding " + target);
                     }
                     target.setX(x);
-                    target.setY(row.getHeight() - target.getWave().getY(elapsedTime - target.getStartTime(), row.getHeight()));
+                    target.setY(row.getHeight() - row.getWave().getY(elapsedTime - target.getStartTime(), row.getHeight()));
 
-                    // koukame pouze na prvnĂ­ polovinu obrazovky a ukladama si dostupne kytky
-                    if ((x > (Direction.RIGHT == target.getWave().getDirection() ? 0 : width / 2)) && (x < (Direction.RIGHT == target.getWave().getDirection() ? width / 2 : width))) {
+                    // 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é
                         if (!target.isShot()) {
                             availableTypes.add(target.getTextureType());
                         }
                     }
+                    // target is not on scene
                 } else {
                     if (target.getParent() != null) {
-                        if (((target.getWave().getDirection() == Direction.LEFT) && (x < 0)) || ((target.getWave().getDirection() == Direction.RIGHT) && (x > width))) {
+                        if (((row.getWave().getDirection() == Direction.LEFT) && (x < 0)) || ((row.getWave().getDirection() == Direction.RIGHT) && (x > width))) {
                             // uĹľ je mimo obraz
-                            target.remove();
+                            // 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
@@ -87,22 +94,20 @@ public class TargetPositionController {
             }
         }
 
-
-        //targets.removeAll(toRemove);
-        //targets.addAll(toAdd);
-        //invisibles.addAll(toAdd);
-
-        //if ((parent.getCurrentTarget() != null) && !availableTypes.contains(parent.getCurrentTarget().first)) {
-//            if (invisibles.size() > 0) {
-//                int index = random.nextInt(invisibles.size());
-//                Target swap = invisibles.get(index);
-//                Target swapTo = targetGenerator.addNewTargetToWave(swap.getWave(), parent.getVertexBufferObjectManager(), parent.getCurrentTarget().first, swap.getStartTime(), targetGenerator.getLastTargetInWave(swap.getWave()).equals(swap));
-//                targets.remove(swap);
-//                targets.add(swapTo);
-//            } else {
-//                targets.add(targetGenerator.addNewTargetToWave(waves.toArray(new Wave[0])[random.nextInt(waves.size())], parent.getVertexBufferObjectManager(), parent.getCurrentTarget().first));
-//            }
-//        }
+        if ((targetType != null) && !availableTypes.contains(targetType)) {
+            if (invisibles.size() > 0) {
+                int index = random.nextInt(invisibles.size());
+                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 {
+                Row row = scene.getRows()[random.nextInt(scene.getRows().length)];
+                row.addTarget(targetGenerator.addNewTargetToRow(row, targetType));
+            }
+        }
 
     }
 }
-- 
GitLab