Browse Source

character controls

Hudriwudri 5 years ago
parent
commit
73edf90d19

BIN
res/character/characterBack.png


BIN
res/character/characterFront.png


BIN
res/character/characterLeft.png


BIN
res/character/characterRight.png


+ 135 - 0
src/gameplay/Hiker.java

@@ -0,0 +1,135 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package gameplay;
+
+import me.hammerle.snuviengine.api.KeyBinding;
+import me.hammerle.snuviengine.api.KeyHandler;
+import me.hammerle.snuviengine.api.Renderer;
+import me.hammerle.snuviengine.api.Texture;
+import org.lwjgl.glfw.GLFW;
+
+/**
+ *
+ * @author Julia
+ */
+public class Hiker {
+
+    private float oldX = 0;
+    private float oldY = 0;
+    private float x = 0;
+    private float y = 0;
+
+    private static final float speed = 2;
+    private static final float maxWidth = 1024;
+    private static final float maxHeight = 1024;
+
+    private enum moveStates {
+        still, left, right, up, down
+    };
+    private moveStates moveState = moveStates.still;
+
+    private static final Texture characterFront = new Texture("res\\character\\characterFront.png");
+    private static final Texture characterLeft = new Texture("res\\character\\characterLeft.png");
+    private static final Texture characterRight = new Texture("res\\character\\characterRight.png");
+    private static final Texture characterBack = new Texture("res\\character\\characterBack.png");
+
+    private static final KeyBinding UP_KEY = KeyHandler.register(GLFW.GLFW_KEY_W);
+    private static final KeyBinding DOWN_KEY = KeyHandler.register(GLFW.GLFW_KEY_S);
+    private static final KeyBinding LEFT_KEY = KeyHandler.register(GLFW.GLFW_KEY_A);
+    private static final KeyBinding RIGHT_KEY = KeyHandler.register(GLFW.GLFW_KEY_D);
+
+    public void Hiker() {
+    }
+
+    public void tick() {
+        calcMoveState();
+        animate();
+
+        oldX = x;
+        oldY = y;
+
+        if (LEFT_KEY.isDown() && moveState != moveStates.up && moveState != moveStates.down && x > 0) {
+            x -= speed;
+        } else if (RIGHT_KEY.isDown() && moveState != moveStates.up && moveState != moveStates.down && x < maxWidth) {
+            x += speed;
+        } else if (UP_KEY.isDown() && moveState != moveStates.left && moveState != moveStates.right && y > 0) {
+            y -= speed;
+        } else if (DOWN_KEY.isDown() && moveState != moveStates.left && moveState != moveStates.right && y < maxHeight) {
+            y += speed;
+        } else {
+            if (x % 32 != 0 || y % 32 != 0) {
+                if (moveState == moveStates.left) {
+                    x -= speed;
+                } else if (moveState == moveStates.right) {
+                    x += speed;
+                } else if (moveState == moveStates.up) {
+                    y -= speed;
+                } else if (moveState == moveStates.down) {
+                    y += speed;
+                }
+            }
+        }
+
+    }
+
+    public void renderTick(Renderer r, float lag) {
+        r.setMixColorEnabled(false);
+
+        renderTestBackground(r);
+
+        r.setColorEnabled(false);
+        r.setTextureEnabled(true);
+
+        r.translateTo(0.0f, 0.0f);
+        r.updateMatrix();
+
+        float ix = oldX + (x - oldX) * lag;
+        float iy = oldY + (y - oldY) * lag;
+        r.getTextureRenderer().drawRectangle(ix, iy, ix + 32, iy + 32, 0, 0, 1, 1);
+
+    }
+
+    private void calcMoveState() {
+        if (oldX == x && oldY == y) {
+            moveState = moveStates.still;
+        } else if (oldX > x) {
+            moveState = moveStates.left;
+        } else if (oldX < x) {
+            moveState = moveStates.right;
+        } else if (oldY > y) {
+            moveState = moveStates.up;
+        } else if (oldY < y) {
+            moveState = moveStates.down;
+        }
+    }
+
+    private void animate() {
+        if (moveState == moveStates.still || moveState == moveStates.down) {
+            characterFront.bind();
+        } else if (moveState == moveStates.left) {
+
+            characterLeft.bind();
+        } else if (moveState == moveStates.right) {
+            characterRight.bind();
+        } else if (moveState == moveStates.up) {
+            characterBack.bind();
+        }
+    }
+
+    private void renderTestBackground(Renderer r) {
+        r.setColorEnabled(true);
+        r.setTextureEnabled(false);
+        r.getColorRenderer().drawRectangle(0, 0, 32, 32, 0xFF0000FF);
+        r.getColorRenderer().drawRectangle(32, 0, 64, 32, 0xFF00FF00);
+        r.getColorRenderer().drawRectangle(0, 32, 32, 64, 0xFF00FF00);
+        r.getColorRenderer().drawRectangle(32, 32, 64, 64, 0xFF0000FF);
+        r.getColorRenderer().drawRectangle(64, 0, 96, 32, 0xFF0000FF);
+        r.getColorRenderer().drawRectangle(0, 64, 32, 96, 0xFF0000FF);
+        r.getColorRenderer().drawRectangle(64, 32, 96, 64, 0xFF00FF00);
+        r.getColorRenderer().drawRectangle(32, 64, 64, 96, 0xFF00FF00);
+        r.getColorRenderer().drawRectangle(64, 64, 96, 96, 0xFF0000FF);
+    }
+}

+ 14 - 27
src/pathgame/PathGame.java

@@ -5,51 +5,38 @@ import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
 import me.hammerle.snuviengine.api.Renderer;
 import org.lwjgl.glfw.GLFW;
+import gameplay.Hiker;
 
 public class PathGame implements IGame
 {
-    private float oldX = 0;
-    private float oldY = 0;
-    private float x = 0;
-    private float y = 0;
     
-    private static final KeyBinding UP_KEY = KeyHandler.register(GLFW.GLFW_KEY_W);
-    private static final KeyBinding DOWN_KEY = KeyHandler.register(GLFW.GLFW_KEY_S);
-    private static final KeyBinding LEFT_KEY = KeyHandler.register(GLFW.GLFW_KEY_A);
-    private static final KeyBinding RIGHT_KEY = KeyHandler.register(GLFW.GLFW_KEY_D);
+//    private float oldX = 0;
+//    private float oldY = 0;
+//    private float x = 0;
+//    private float y = 0;
+//    
+//    private static final KeyBinding UP_KEY = KeyHandler.register(GLFW.GLFW_KEY_W);
+//    private static final KeyBinding DOWN_KEY = KeyHandler.register(GLFW.GLFW_KEY_S);
+//    private static final KeyBinding LEFT_KEY = KeyHandler.register(GLFW.GLFW_KEY_A);
+//    private static final KeyBinding RIGHT_KEY = KeyHandler.register(GLFW.GLFW_KEY_D);
     
+         Hiker player = new Hiker();
     public PathGame()
     {
+        
     }
     
     @Override
     public void tick()
     {
-        oldX = x;
-        oldY = y;
+        player.tick();
         
-        if(LEFT_KEY.isDown())
-        {
-            x -= 10; 
-        }
-        if(RIGHT_KEY.isDown())
-        {
-            x += 10; 
-        }
     }
     
     @Override
     public void renderTick(Renderer r, float lag)
     {
-        r.setMixColorEnabled(false);
-     
-        r.translateTo(0.0f, 0.0f);
-        r.updateMatrix();
-        
-        float ix = oldX + (x - oldX) * lag;
-        float iy = oldY + (y - oldY) * lag;
-        
-        r.getColorRenderer().drawRectangle(ix, iy, ix + 20, iy + 20, 0xFF0000FF);
+        player.renderTick(r, lag);
     }
 
     @Override