Commits

jmartrican  committed d959292

- Added AnimatedImageNode
- Added SpriteEnemy to test out AnimatedImageNode
- Added images of space jet
- Changed PathTransition in LevelPanel to be orthagonal to tangent
- Added some SpriteEnemy's to FirstLevel

  • Participants
  • Parent commits f55796b

Comments (0)

Files changed (5)

File defender/src/main/java/com/fxexperience/games/defender/games/simple/AnimatedImageNode.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;
+
+import javafx.animation.KeyFrame;
+import javafx.animation.Timeline;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.geometry.Rectangle2D;
+import javafx.scene.image.Image;
+import javafx.scene.image.ImageView;
+import javafx.util.Duration;
+
+/**
+ *
+ * @author team
+ */
+public class AnimatedImageNode extends ImageView {
+
+    ///////////////////////////////////////////////////////////////////////////
+    //variables
+    private final int framesPerSec;
+    private final int frameWidth;
+    private final int frameHeight;
+    private final int frameCount;
+    private final int numColumns;
+    private int currentFrame = 0;
+    private Timeline animator = null;
+    private EventHandler<ActionEvent> animate = new EventHandler<ActionEvent>() {
+        @Override
+        public final void handle(ActionEvent arg0) {
+            currentFrame = currentFrame < (frameCount - 1) ? ++currentFrame : 0;
+            setViewport(getSpriteViewport());
+        }
+    };
+
+    ///////////////////////////////////////////////////////////////////////////
+    //constructors
+    public AnimatedImageNode(Image spriteSheet, int frameWidth, int frameHeight, int frameCount, int framesPerSec) {
+        super(spriteSheet);
+        this.frameWidth = frameWidth;
+        this.frameHeight = frameHeight;
+        this.frameCount = frameCount;
+        this.framesPerSec = framesPerSec;
+        this.numColumns = (int) getImage().getWidth() / frameWidth;
+        makeAnimator();
+        validate();
+        initialize();
+    }
+
+    ///////////////////////////////////////////////////////////////////////////
+    //private functions
+    private void validate() {
+        if (numColumns <= 0) {
+            throw new IllegalArgumentException("Sprite sheet width is smaller than frame width.  Sprite sheet width = " + (int) getImage().getWidth() + ".  Frame width = " + frameWidth);
+        }
+
+        int numRows = (int) getImage().getHeight() / frameHeight;
+        if (numRows <= 0) {
+            throw new IllegalArgumentException("Sprite sheet height is smaller than frame height.  Sprite sheet height = " + (int) getImage().getHeight() + ".  Frame height = " + frameHeight);
+        }
+
+        int numPossibleFrames = numRows * numColumns;
+        if (numPossibleFrames < frameCount) {
+            throw new IllegalArgumentException("Number of possible frames is less than frame count.  Number of possible frames = " + numPossibleFrames + ".  Frame count = " + frameCount);
+        }
+    }
+
+    private void initialize() {
+        currentFrame = 0;
+        setViewport(getSpriteViewport());
+    }
+
+    private Rectangle2D getSpriteViewport() {
+        int spriteRow = currentFrame / numColumns;
+        int spriteCol = currentFrame % numColumns;
+        return new Rectangle2D(spriteCol * frameWidth, spriteRow * frameHeight, frameWidth, frameHeight);
+    }
+
+    private void makeAnimator() {
+        final KeyFrame kf = new KeyFrame(Duration.seconds(1).divide(framesPerSec), animate);
+        animator = new Timeline(kf);
+        animator.setCycleCount(Timeline.INDEFINITE);
+    }
+
+    ///////////////////////////////////////////////////////////////////////////
+    //public functions
+    public void play() {
+        animator.play();
+    }
+
+    public void playFromStart() {
+        animator.playFromStart();
+    }
+
+    public void pause() {
+        animator.pause();
+    }
+
+    public void stop() {
+        animator.stop();
+    }
+}

File defender/src/main/java/com/fxexperience/games/defender/games/simple/LevelPanel.java

                 enemyLayer.getChildren().add(e);
                 final PathTransition tx = new PathTransition(Duration.seconds(e.getSpeed()* 3), path, e);
                 tx.setInterpolator(Interpolator.LINEAR);
+                tx.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
                 tx.setOnFinished(new EventHandler<ActionEvent>() {
                     @Override
                     public void handle(ActionEvent actionEvent) {

File defender/src/main/java/com/fxexperience/games/defender/games/simple/enemies/SpriteEnemy.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.enemies;
+
+import com.fxexperience.games.defender.games.simple.AnimatedImageNode;
+import com.fxexperience.games.defender.games.simple.Enemy;
+import javafx.scene.image.Image;
+
+/**
+ * @author team
+ */
+public class SpriteEnemy extends Enemy {
+
+    public SpriteEnemy() {
+        super(300, 10);
+
+        AnimatedImageNode animatedNode = new AnimatedImageNode(new Image("flier.png"), 80, 63, 2, 20);
+        getChildren().add(animatedNode);
+        animatedNode.playFromStart();
+    }
+}

File defender/src/main/java/com/fxexperience/games/defender/games/simple/levels/FirstLevel.java

 import com.fxexperience.games.defender.games.simple.enemies.BlackEnemy;
 import com.fxexperience.games.defender.games.simple.enemies.BlueEnemy;
 import com.fxexperience.games.defender.games.simple.enemies.GreenEnemy;
+import com.fxexperience.games.defender.games.simple.enemies.SpriteEnemy;
 import javafx.scene.shape.LineTo;
 import javafx.scene.shape.MoveTo;
 import javafx.scene.shape.Path;
                 new LineTo(1024, 768)
         ), new Wave(
                 Duration.seconds(1),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy(),
-                new BlueEnemy()
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy(),
+                new SpriteEnemy()
         ), new Wave(
                 Duration.seconds(1),
                 new GreenEnemy(),

File defender/src/main/resources/flier.png

Added
New image