Commits

Anonymous committed eedce74

Added basic Tower placement

Comments (0)

Files changed (9)

defender/src/main/java/com/fxexperience/games/defender/games/simple/Game.java

     public final DoubleProperty money = new SimpleDoubleProperty(this, "money", 100);
     public final IntegerProperty lives = new SimpleIntegerProperty(this, "lives", 10);
 
+    private final ObjectProperty<LevelPanel> currentLevel = new SimpleObjectProperty<>(this, "currentLevel");
+    public ObjectProperty<LevelPanel> currentLevelProperty() { return currentLevel; }
+    public LevelPanel getCurrentLevel() { return this.currentLevel.get(); }
+    public void setCurrentLevel(LevelPanel level) { this.currentLevel.set(level); }
+
     private AnimationTimer animationTimer;
     private List<GamePulseListener> gamePulseListeners;
 
             }
         };
 
+        currentLevel.addListener(new ChangeListener<LevelPanel>() {
+            @Override
+            public void changed(ObservableValue<? extends LevelPanel> source, LevelPanel oldLevel, LevelPanel newLevel) {
+                if (oldLevel != null) {
+                    gamePulseListeners.remove(oldLevel);
+                }
+                if (newLevel != null) {
+                    gamePulseListeners.add(newLevel);
+                }
+            }
+        });
+
         paused.addListener(new ChangeListener<Boolean>() {
             @Override
             public void changed(ObservableValue<? extends Boolean> source, Boolean wasPaused, Boolean isPaused) {

defender/src/main/java/com/fxexperience/games/defender/games/simple/GameControlPanel.java

 package com.fxexperience.games.defender.games.simple;
 
+import com.fxexperience.games.defender.games.simple.towers.BasicTowerFactory;
 import javafx.event.ActionEvent;
 import javafx.event.EventHandler;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
 import javafx.scene.control.Button;
+import javafx.scene.control.ToggleButton;
+import javafx.scene.control.ToggleGroup;
+import javafx.scene.input.MouseEvent;
+import javafx.scene.layout.GridPane;
 import javafx.scene.layout.StackPane;
+import javafx.util.Duration;
 
 /**
  */
 public class GameControlPanel extends StackPane {
+
     public GameControlPanel(final Game game) {
         getStyleClass().add("game-control-panel");
 
                     playButton.setText(game.paused.get() ? "Send The Enemies!" : "Pause");
                 }
             });
+
+            final GridPane towerGrid = (GridPane)content.lookup("#tower-grid");
+
+            ToggleGroup toggleGroup = new ToggleGroup();
+
+            TowerFactory pelletGunFactory = new BasicTowerFactory("Pellet Gun", "pellet-gun", Duration.millis(1000), 100, 5);
+            towerGrid.getChildren().add(createTowerButton(toggleGroup, game, pelletGunFactory, 0, 0));
+
+            TowerFactory rapidFireGunFactory = new BasicTowerFactory("Rapid Fire Gun", "rapid-fire-gun", Duration.millis(200), 100, 5);
+            towerGrid.getChildren().add(createTowerButton(toggleGroup, game, rapidFireGunFactory, 1, 0));
+
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
+
+    protected ToggleButton createTowerButton(ToggleGroup toggleGroup, final Game game, final TowerFactory towerFactory, int column, int row) {
+        final ToggleButton towerButton = new ToggleButton(towerFactory.getTowerName());
+        toggleGroup.getToggles().add(towerButton);
+        GridPane.setConstraints(towerButton, column, row);
+        towerButton.setOnAction(new EventHandler<ActionEvent>() {
+            @Override
+            public void handle(ActionEvent actionEvent) {
+                final LevelPanel level = game.getCurrentLevel();
+                if (level != null) {
+                    final Tower tower = towerFactory.createTower();
+                    tower.setTranslateX(-100);
+                    tower.setTranslateY(-100);
+                    level.setOnMouseReleased(new EventHandler<MouseEvent>() {
+                        @Override
+                        public void handle(MouseEvent event) {
+                            tower.setTranslateX(event.getX());
+                            tower.setTranslateY(event.getY());
+                            level.addTower(tower);
+                            towerButton.setSelected(false);
+                            level.setOnMouseReleased(null);
+                        }
+                    });
+                }
+            }
+        });
+        return towerButton;
+    }
 }
+

defender/src/main/java/com/fxexperience/games/defender/games/simple/SimpleGameScene.java

 package com.fxexperience.games.defender.games.simple;
 
 import com.fxexperience.games.defender.games.simple.levels.FirstLevel;
-import com.fxexperience.games.defender.games.simple.towers.BasicTower;
 import javafx.scene.Scene;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.StackPane;
-import javafx.util.Duration;
 
 /**
  * The Scene which will be used for the simple version of the game. This is
         BorderPane root = (BorderPane) getRoot();
         StackPane p = new StackPane();
         FirstLevel level = new FirstLevel();
-
-        // hard code in a tower for now
-        Tower tower = new BasicTower(Duration.seconds(1), 150);
-        tower.setLayoutY(200);
-        tower.setLayoutX(300);
-        level.addTower(tower);
-
         p.getChildren().add(level);
-        game.addGamePulseListener(level);
+        game.setCurrentLevel(level);
         root.setCenter(p);
         root.setRight(new GameControlPanel(game));
     }

defender/src/main/java/com/fxexperience/games/defender/games/simple/Tower.java

  */
 public class Tower extends Parent implements GamePulseListener {
 
+    protected String name;
     protected LevelPanel level;
 
+    public Tower(String name) {
+        this.name = name;
+    }
+
     // Want to create an event that happens when the tower bullet hits an enemy, such that
     // different types of towers can do different things depending on what kind of enemy
     // is hit. For example, if the blue tower hits a blue enemy, it has 2x damage, but
     // if the tower hits a green enemy, it has .5x damage.
 
-    // Need an event for when a shot is fired off, so the UI can create a projectile and fly it to the target
-
-    // Need a method which is called whenever the tower should shoot, which will give it the list
-    // of possible targets so it can choose which target to fire at?
-
-
     public void setLevel(LevelPanel level) {
         this.level = level;
     }

defender/src/main/java/com/fxexperience/games/defender/games/simple/TowerFactory.java

+/*
+* Copyright (c) 2012, FX Experience. All rights  reserved.
+*
+* Redistribution and use in source and binary forms, with or without modification, are
+* permitted provided that the following conditions are met:
+*
+* Redistributions of source code must retain the above copyright notice, this list of
+* conditions and the following disclaimer. 
+*
+* Redistributions in binary form must reproduce the above copyright notice, this list of 
+* conditions and the following disclaimer in the documentation and/or other materials provided 
+* with the distribution.    
+*
+* Neither the name of the author nor the names of its contributors may be used to endorse or
+* promote products derived from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
+* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+package com.fxexperience.games.defender.games.simple;
+
+public interface TowerFactory {
+
+    String getTowerName();
+
+    Tower createTower();
+}

defender/src/main/java/com/fxexperience/games/defender/games/simple/towers/BasicTower.java

     private Duration shotDelay;
     private long lastShot;
     private int range;
+    private int damage;
 
-    public BasicTower(Duration shotDelay, int range) {
+    public BasicTower(String name, String bodyStyle, Duration shotDelay, int range, int damage) {
+        super(name);
         this.shotDelay = shotDelay;
         this.range = range;
+        this.damage = damage;
 
         Region body = new Region();
-        body.getStyleClass().add("tower");
+        body.getStyleClass().addAll("tower", bodyStyle) ;
         getChildren().add(body);
     }
 

defender/src/main/java/com/fxexperience/games/defender/games/simple/towers/BasicTowerFactory.java

+/*
+ * Copyright (c) 2012, FX Experience. All rights  reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * Neither the name of the author nor the names of its contributors may be used to endorse or
+ * promote products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.fxexperience.games.defender.games.simple.towers;
+
+import com.fxexperience.games.defender.games.simple.Tower;
+import com.fxexperience.games.defender.games.simple.TowerFactory;
+import javafx.util.Duration;
+
+public class BasicTowerFactory implements TowerFactory {
+
+    private String name;
+    private String bodyStyle;
+    private Duration shotDelay;
+    private int range;
+    private int damage;
+
+    public BasicTowerFactory(String name, String bodyStyle, Duration shotDelay, int range, int damage) {
+        this.name = name;
+        this.bodyStyle = bodyStyle;
+        this.shotDelay = shotDelay;
+        this.range = range;
+        this.damage = damage;
+    }
+
+    @Override
+    public String getTowerName() {
+        return name;
+    }
+
+    @Override
+    public Tower createTower() {
+        return new BasicTower(name, bodyStyle, shotDelay, range, damage);
+    }
+}

defender/src/main/resources/styles/games/simple/GameControlPanel.fxml

             minWidth="-Infinity" prefHeight="598.0" prefWidth="262.0">
   <children>
     <Label id="title" alignment="CENTER" contentDisplay="LEFT" prefHeight="-1.0" prefWidth="-1.0" text="JavaFX Defender" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0" />
+
+    <GridPane id="tower-grid" layoutY="139.0" prefHeight="342.0" prefWidth="206.0" AnchorPane.leftAnchor="28.0" AnchorPane.rightAnchor="28.0">
+      <columnConstraints>
+          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
+          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
+          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
+      </columnConstraints>
+    </GridPane>
+
     <Button id="new-game-button" layoutY="528.0" mnemonicParsing="false" prefWidth="206.0" text="New Game" AnchorPane.leftAnchor="28.0" AnchorPane.rightAnchor="28.0" />
     <Label id="towers-label" layoutX="28.0" layoutY="116.0" text="Towers" />
     <Label id="money-label" layoutX="28.0" layoutY="62.0" text="Money" />

defender/src/main/resources/styles/games/simple/styles.css

     -fx-background-color: blue;
 }
 
-.tower {
+.pellet-gun {
+    -fx-padding: 20;
     -fx-background-color: orange;
-    -fx-padding: 30 20 30 20;
+    -fx-background-radius: 6;
+    -fx-border-color: gray;
+    -fx-border-radius: 6;
+}
+
+.rapid-fire-gun {
+    -fx-padding: 25;
+    -fx-background-color: #aaaaff;
+    -fx-background-radius: 10;
+    -fx-border-color: #000055;
+    -fx-border-radius: 6;
 }
 
 .bullet {