Переглянути джерело

added experimental animations for entities

Kajetan Johannes Hammerle 7 роки тому
батько
коміт
2dd61b32d4

+ 105 - 0
src/me/hammerle/supersnuvi/entity/AnimatedEntity.java

@@ -0,0 +1,105 @@
+package me.hammerle.supersnuvi.entity;
+
+import javafx.scene.image.Image;
+import javafx.scene.paint.Color;
+import me.hammerle.supersnuvi.rendering.WorldRenderer;
+import me.hammerle.supersnuvi.util.Utils;
+
+public class AnimatedEntity extends Entity
+{
+    private int counter;
+    private int frame;
+    
+    private Image leftFace;
+    private Image rightFace;
+    private Image[] left;
+    private Image[] right;
+    private Image[] leftAir;
+    private Image[] rightAir;
+    
+    public AnimatedEntity(WorldRenderer map, double x, double y, double width, double height) 
+    {
+        super(map, x, y, width, height);
+        counter = 0;
+        frame = 0;
+        left = new Image[3];
+        left[0] = Utils.getColoredImage(Color.CYAN, (int) getWidth(), (int) getHeight());
+        left[1] = Utils.getColoredImage(Color.ANTIQUEWHITE, (int) getWidth(), (int) getHeight());
+        left[2] = Utils.getColoredImage(Color.RED, (int) getWidth(), (int) getHeight());
+        right = new Image[3];
+        right[0] = Utils.getColoredImage(Color.CYAN, (int) getWidth(), (int) getHeight());
+        right[1] = Utils.getColoredImage(Color.ANTIQUEWHITE, (int) getWidth(), (int) getHeight());
+        right[2] = Utils.getColoredImage(Color.RED, (int) getWidth(), (int) getHeight());
+        
+        leftAir = new Image[1];
+        leftAir[0] = Utils.getColoredImage(Color.BROWN, (int) getWidth(), (int) getHeight());
+        rightAir = new Image[1];
+        rightAir[0] = Utils.getColoredImage(Color.YELLOW, (int) getWidth(), (int) getHeight());
+        
+        leftFace = Utils.getColoredImage(Color.DARKOLIVEGREEN, (int) getWidth(), (int) getHeight());
+        rightFace = Utils.getColoredImage(Color.DARKSEAGREEN, (int) getWidth(), (int) getHeight());
+    }
+
+    @Override
+    public Image getImage() 
+    {
+        counter++;
+        if(counter >= 15)
+        {
+            counter = 0;
+            frame++;
+        }
+        double motionX = getMotionX();
+        if(isOnGround())
+        {
+            if(motionX < 0)
+            {
+                if(frame >= left.length)
+                {
+                    frame = 0;
+                }
+                setImage(leftFace);
+                return left[frame];
+            }
+            else if(motionX > 0)
+            {
+                if(frame >= right.length)
+                {
+                    frame = 0;
+                }
+                setImage(rightFace);
+                return right[frame];
+            }
+            else
+            {
+                frame = 0;
+            }
+        }
+        else
+        {
+            if(motionX < 0)
+            {
+                if(frame >= leftAir.length)
+                {
+                    frame = 0;
+                }
+                setImage(leftFace);
+                return leftAir[frame];
+            }
+            else if(motionX > 0)
+            {
+                if(frame >= rightAir.length)
+                {
+                    frame = 0;
+                }
+                setImage(rightFace);
+                return rightAir[frame];
+            }
+            else
+            {
+                frame = 0;
+            }
+        }
+        return super.getImage();
+    }
+}

+ 17 - 18
src/me/hammerle/supersnuvi/entity/Entity.java

@@ -1,10 +1,7 @@
 package me.hammerle.supersnuvi.entity;
 
-import java.util.Collections;
 import java.util.LinkedList;
 import javafx.scene.image.Image;
-import javafx.scene.image.PixelWriter;
-import javafx.scene.image.WritableImage;
 import javafx.scene.paint.Color;
 import me.hammerle.supersnuvi.input.IKeyHandler;
 import me.hammerle.supersnuvi.rendering.GameRenderer;
@@ -56,20 +53,7 @@ public class Entity
         this.width = width;
         this.height = height;
         
-        image = new WritableImage((int) width, (int) height);
-        PixelWriter writer = ((WritableImage) image).getPixelWriter();
-        Color black = Color.BLACK;
-        for(int fx = 0; fx < width; fx++)
-        {
-            for(int fy = 1; fy < height; fy++)
-            {
-                writer.setColor(fx, fy, black);
-            }
-        }
-        for(int fx = 0; fx < width; fx++)
-        {
-            writer.setColor(fx, 0, Color.WHITE);
-        }
+        image = Utils.getColoredImage(Color.BLACK, (int) width, (int) height);
     }
 
     //--------------------------------------------------------------------------
@@ -546,6 +530,16 @@ public class Entity
     // rendering stuff
     //--------------------------------------------------------------------------
     
+    public final double getWidth()
+    {
+        return width;
+    }
+    
+    public final double getHeight()
+    {
+        return height;
+    }
+    
     public final double getRenderX()
     {
         return posX;
@@ -556,7 +550,12 @@ public class Entity
         return posY + height;
     }
     
-    public final Image getImage()
+    public void setImage(Image image)
+    {
+        this.image = image;
+    }
+    
+    public Image getImage()
     {
         return image;
     }

+ 1 - 1
src/me/hammerle/supersnuvi/entity/Hero.java

@@ -4,7 +4,7 @@ import javafx.scene.input.KeyCode;
 import me.hammerle.supersnuvi.input.IKeyHandler;
 import me.hammerle.supersnuvi.rendering.WorldRenderer;
 
-public class Hero extends Entity
+public class Hero extends AnimatedEntity
 {
     public Hero(WorldRenderer map, double x, double y, double width, double height) 
     {

+ 1 - 1
src/me/hammerle/supersnuvi/rendering/WorldRenderer.java

@@ -52,7 +52,7 @@ public class WorldRenderer
         this.entities = new HashMap<>();
         
         Entity ent = new Entity(this, 80, 340, 32, 64);
-        Hero hero = new Hero(this, 32, 100, 32, 64);
+        Hero hero = new Hero(this, 32, 100, 32, 48);
         
         entities.put(1, ent);
         entities.put(2, hero);

+ 18 - 0
src/me/hammerle/supersnuvi/util/Utils.java

@@ -7,6 +7,10 @@ import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
+import javafx.scene.image.Image;
+import javafx.scene.image.PixelWriter;
+import javafx.scene.image.WritableImage;
+import javafx.scene.paint.Color;
 
 public class Utils 
 {
@@ -98,4 +102,18 @@ public class Utils
             return new int[3][0][0];
         } 
     }
+    
+    public static Image getColoredImage(Color c, int width, int height)
+    {
+        WritableImage image = new WritableImage(width, height);
+        PixelWriter writer = ((WritableImage) image).getPixelWriter();
+        for(int fx = 0; fx < width; fx++)
+        {
+            for(int fy = 0; fy < height; fy++)
+            {
+                writer.setColor(fx, fy, c);
+            }
+        }
+        return image;
+    }
 }

+ 27 - 27
test.map

@@ -1,29 +1,29 @@
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
-1,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+2, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+2, 1, 1, 2, 2, 3, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1
+2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1
+2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1
+2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1
+2, 2, 2, 5, 5, 5, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1
 #
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
--1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1