From 4527f219d8b5a91b0b6d1f1503616825f65f49d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Hor=C3=A1=C4=8Dek?= <horaceklubos@gmail.com>
Date: Mon, 13 Jul 2015 15:38:19 +0200
Subject: [PATCH] #31 Shooting and hit sound

---
 .../common/game/kidnapping/{mfx => sfx}/error.mp3 | Bin
 .../tablexia/game/common/media/SfxLibrary.java    |  10 ++++++----
 .../games/shooting_range/ShootingRangeGame.java   |  13 ++++++++++++-
 .../game/games/shooting_range/actors/Flower.java  |   5 +++--
 .../game/games/shooting_range/actors/Scene.java   |   2 ++
 .../game/games/shooting_range/actors/Target.java  |   6 ++++--
 .../shooting_range/tools/TargetGenerator.java     |  14 +++++++++-----
 7 files changed, 36 insertions(+), 14 deletions(-)
 rename core/assets/common/game/kidnapping/{mfx => sfx}/error.mp3 (100%)

diff --git a/core/assets/common/game/kidnapping/mfx/error.mp3 b/core/assets/common/game/kidnapping/sfx/error.mp3
similarity index 100%
rename from core/assets/common/game/kidnapping/mfx/error.mp3
rename to core/assets/common/game/kidnapping/sfx/error.mp3
diff --git a/core/src/cz/nic/tablexia/game/common/media/SfxLibrary.java b/core/src/cz/nic/tablexia/game/common/media/SfxLibrary.java
index fcdf33cfe..a6afdaded 100644
--- a/core/src/cz/nic/tablexia/game/common/media/SfxLibrary.java
+++ b/core/src/cz/nic/tablexia/game/common/media/SfxLibrary.java
@@ -27,6 +27,7 @@ import com.badlogic.gdx.audio.Sound;
 import java.util.HashMap;
 
 import cz.nic.tablexia.game.AbstractTablexiaGame;
+import cz.nic.tablexia.util.Log;
 
 
 /**
@@ -34,16 +35,17 @@ import cz.nic.tablexia.game.AbstractTablexiaGame;
  */
 public class SfxLibrary extends HashMap<AssetDescription, String> {
 
-    private static final String SOUNDS_DIR       = "mfx/";
+    private static final String SOUNDS_DIR       = "sfx/";
     public static final  String SOUNDS_EXTENSION = ".mp3";
 
     private AbstractTablexiaGame abstractTablexiaGame;
 
     public SfxLibrary(AbstractTablexiaGame abstractTablexiaGame, AssetDescription[] assetDescriptions) {
         this.abstractTablexiaGame = abstractTablexiaGame;
-        for (AssetDescription sound : assetDescriptions) {
-            String path = SOUNDS_DIR + sound.getResource() + SOUNDS_EXTENSION;
-            put(sound, path);
+        for (AssetDescription assetDescription : assetDescriptions) {
+            String path = SOUNDS_DIR + assetDescription.getResource() + SOUNDS_EXTENSION;
+            Log.info(getClass(), "Adding sound " + path);
+            put(assetDescription, path);
         }
     }
 
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 95361b0f2..a4a04a9f5 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
@@ -1,7 +1,10 @@
 package cz.nic.tablexia.game.games.shooting_range;
 
+import com.badlogic.gdx.scenes.scene2d.Actor;
+import com.badlogic.gdx.scenes.scene2d.InputEvent;
 import com.badlogic.gdx.scenes.scene2d.actions.Actions;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 
 import java.util.List;
 import java.util.Map;
@@ -36,6 +39,14 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
     private List<Target>[]           waves;
     private TargetGenerator          targetGenerator;
     private TargetPositionController targetPositionController;
+    private ClickListener clickListener = new ClickListener() {
+        @Override
+        public void clicked(InputEvent event, float x, float y) {
+            super.clicked(event, x, y);
+            Actor target = event.getTarget();
+            sfxLibrary.getSound(SoundType.HIT_1).play();
+        }
+    };
 
 
     @Override
@@ -56,7 +67,7 @@ public class ShootingRangeGame extends AbstractTablexiaGame<GameState> {
         super.gameLoaded(gameState);
         initScene();
 
-        targetGenerator = new TargetGenerator(getGameDifficulty(), getRandom(), gfxLibrary);
+        targetGenerator = new TargetGenerator(getGameDifficulty(), getRandom(), gfxLibrary, clickListener);
         targetPositionController = new TargetPositionController(scene, targetGenerator, getRandom(), getStage().getWidth());
         for (Row row : scene.getRows()) {
             row.setWave(Wave.getWave(getGameDifficulty(), row.getRowIndex()));
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Flower.java b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Flower.java
index 73df67885..a0a1cf3c0 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Flower.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Flower.java
@@ -1,6 +1,7 @@
 package cz.nic.tablexia.game.games.shooting_range.actors;
 
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 
 import cz.nic.tablexia.game.games.shooting_range.media.Textures;
 
@@ -8,7 +9,7 @@ import cz.nic.tablexia.game.games.shooting_range.media.Textures;
  * Created by lhoracek on 6/26/15.
  */
 public class Flower extends Target {
-    public Flower(TextureRegion texture, Float startTime, Row row, Textures textureType) {
-        super(texture, startTime, row, textureType);
+    public Flower(TextureRegion texture, Float startTime, Row row, Textures textureType, ClickListener listener) {
+        super(texture, startTime, row, textureType, listener);
     }
 }
diff --git a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Scene.java b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Scene.java
index 93cd4d3d6..16fe946e8 100644
--- a/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Scene.java
+++ b/core/src/cz/nic/tablexia/game/games/shooting_range/actors/Scene.java
@@ -1,6 +1,7 @@
 package cz.nic.tablexia.game.games.shooting_range.actors;
 
 import com.badlogic.gdx.scenes.scene2d.Group;
+import com.badlogic.gdx.scenes.scene2d.Touchable;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
 
 import java.util.ArrayList;
@@ -28,6 +29,7 @@ public class Scene extends Group {
         rows = new Row[]{row1, row2, row3};
 
         addActor(frame = new Image(gfxLibrary.getTextureRegion(Textures.FRAME)));
+        frame.setTouchable(Touchable.disabled);
     }
 
     @Override
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 b6c775552..7a40e7805 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
@@ -3,6 +3,7 @@ package cz.nic.tablexia.game.games.shooting_range.actors;
 import com.badlogic.gdx.graphics.g2d.Sprite;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 import com.badlogic.gdx.scenes.scene2d.ui.Image;
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 
 import cz.nic.tablexia.game.games.shooting_range.media.Textures;
 
@@ -28,11 +29,12 @@ public class Target extends Image {
         };
     }
 
-    public Target(TextureRegion texture, Float startTime, Row row, Textures textureType) {
+    public Target(TextureRegion texture, Float startTime, Row row, Textures textureType, ClickListener listener) {
         super(texture);
         this.startTime = startTime;
         this.textureType = textureType;
         this.row = row;
+        addListener(listener);
     }
 
     public float getStartTime() {
@@ -107,7 +109,7 @@ public class Target extends Image {
     public String toString() {
         return "Target{" +
                 "textureType=" + textureType +
-                ", row=" + row +
+                //", row=" + row +
                 ", shot=" + shot +
                 '}';
     }
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 6b4ddae91..7fd50d7ae 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
@@ -1,5 +1,7 @@
 package cz.nic.tablexia.game.games.shooting_range.tools;
 
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -27,15 +29,17 @@ public class TargetGenerator {
     public static final float MEDIUM_GOOD_PROBABILITY = 0.5f;
     public static final float HARD_GOOD_PROBABILITY   = 0.3f;
 
-    private GameDifficulty gameDifficulty;
-    private Random         random;
-    private GfxLibrary     gfxLibrary;
+    private final GameDifficulty gameDifficulty;
+    private final Random         random;
+    private final GfxLibrary     gfxLibrary;
+    private final ClickListener  clickListener;
 
-    public TargetGenerator(GameDifficulty gameDifficulty, Random random, GfxLibrary gfxLibrary) {
+    public TargetGenerator(GameDifficulty gameDifficulty, Random random, GfxLibrary gfxLibrary, ClickListener clickListener) {
         super();
         this.random = random;
         this.gameDifficulty = gameDifficulty;
         this.gfxLibrary = gfxLibrary;
+        this.clickListener = clickListener;
 
     }
 
@@ -111,7 +115,7 @@ public class TargetGenerator {
             int random = (int) (Math.random() * textureTypeBag.size());
             textureType = textureTypeBag.get(random);
         }
-        return new Target(gfxLibrary.getTextureRegion(textureType), startTime, row, textureType);
+        return new Target(gfxLibrary.getTextureRegion(textureType), startTime, row, textureType, clickListener);
     }
 
     private float getBoxProbability() {
-- 
GitLab