From 56f05f7e7efb90e907b2d9a4deb42bbb608b58f7 Mon Sep 17 00:00:00 2001 From: toksikk Date: Thu, 6 Nov 2014 20:45:59 +0100 Subject: [PATCH] =?UTF-8?q?Aufger=C3=A4umt.=20Rendering.=20Erste=20Kollisi?= =?UTF-8?q?onserkennung.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HeatUp/src/de/heatup/testing/GameLoopD.java | 26 ++++---- HeatUp/src/de/heatup/testing/Player.java | 67 +++++++-------------- HeatUp/src/de/heatup/ui/KeyBindings.java | 24 +++++--- 3 files changed, 51 insertions(+), 66 deletions(-) diff --git a/HeatUp/src/de/heatup/testing/GameLoopD.java b/HeatUp/src/de/heatup/testing/GameLoopD.java index 4b15752..61f566e 100644 --- a/HeatUp/src/de/heatup/testing/GameLoopD.java +++ b/HeatUp/src/de/heatup/testing/GameLoopD.java @@ -3,6 +3,7 @@ package de.heatup.testing; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; +import java.awt.Point; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -11,6 +12,7 @@ import javax.swing.JFrame; import javax.swing.JPanel; import de.heatup.mapengine.MapEngine; +import de.heatup.mapengine.SpawnAssistant; import de.heatup.objects.DynamicObject; import de.heatup.objects.StaticObject; import de.heatup.objects.logic; @@ -79,6 +81,8 @@ public class GameLoopD extends JFrame implements Runnable { /* * zeug aus dem alten gameloop */ + + DynamicObject dynO1 = new DynamicObject(); gamePanel.add(dynO1); DynamicObject dynO2 = new DynamicObject(); @@ -88,35 +92,31 @@ public class GameLoopD extends JFrame implements Runnable { DynamicObject dynO4 = new DynamicObject(); gamePanel.add(dynO4); + + player01 = new Player(); - Query.setPlayer(player01); +// Query.setPlayer(player01); gamePanel.add(player01); - player01.setBounds(25, 25, 200, 200); + player01.setBounds(25, 25, 200, 200); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!! gamePanel.setFocusable(true); kb = new KeyBindings(player01); gamePanel.addKeyListener(kb); - //dynO3.setRescale(1.5,3.5); - -// Config.setGamePanelWidth(900); MapEngine me = new MapEngine(1); for (StaticObject currentStaticObject : me.getAllMapObjects()) { gamePanel.add(currentStaticObject); } + + Point spawnPoint = SpawnAssistant.getPathLocationOnGrid(); + player01.spawnAt(spawnPoint); gamePanel.setVisible(true); //GameInterface.getGamePanel().repaint(); //GameInterface.getGamePanel().setComponentZOrder(dynO1, 1); //GameInterface.getGamePanel().add(dynO1); - //logic.startMovement(dynO1); - //logic.startMovement(dynO2); - - logic.startMovement(dynO3); - - //logic.startMovement(dynO4); /* @@ -158,7 +158,7 @@ public class GameLoopD extends JFrame implements Runnable { public void run() { long lastTime = System.nanoTime(); - final double fps = 30.0; + final double fps = 60.0; double ns = 1000000000 / fps; double delta = 0; int updates = 0; @@ -195,7 +195,7 @@ public class GameLoopD extends JFrame implements Runnable { } // alles was animiert wird hier rein (paints) private void render() { - + player01.repaint(); } } diff --git a/HeatUp/src/de/heatup/testing/Player.java b/HeatUp/src/de/heatup/testing/Player.java index 56e3241..95f5c00 100644 --- a/HeatUp/src/de/heatup/testing/Player.java +++ b/HeatUp/src/de/heatup/testing/Player.java @@ -3,6 +3,7 @@ package de.heatup.testing; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Point; import java.awt.RenderingHints; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; @@ -39,7 +40,16 @@ public class Player extends JComponent { int startY; int currentCountX = 25; - int currentCountY = 0; + + private Point currentGridPosition; + + public void setGridLocation(int x, int y) { + currentGridPosition.setLocation(x, y); + } + + public Point getGridLocation() { + return currentGridPosition; + } public void paintComponent(Graphics g) { g2d = (Graphics2D) g; @@ -55,7 +65,12 @@ public class Player extends JComponent { g2d.setStroke(new BasicStroke(8.0f)); g2d.finalize(); } - + + public void spawnAt(Point spawnPoint) { + this.currentGridPosition = spawnPoint; + this.setAutoBounds(spawnPoint.x*25, spawnPoint.y*25); + this.repaint(); + } private void loadBufferedImage() { try { this.bf1 = ImageIO @@ -86,7 +101,7 @@ public class Player extends JComponent { int newx = this.getX(); int newy = this.getY(); - for (int s = 0; s<4; s++) { // speed multiplikator + for (int s = 0; s<2; s++) { // speed multiplikator if (d == 'x' && i == -1) { @@ -101,7 +116,9 @@ public class Player extends JComponent { this.setAutoBounds(newx, newy); - System.out.println("X: " + newx + " Y: " + newy); + //debug + System.out.println("on grid - x: "+getGridLocation().x + " y: "+getGridLocation().y); + currentCountX--; if ( currentCountX <= 0 ) { @@ -114,46 +131,6 @@ public class Player extends JComponent { } } - -// if (foo) { -// -// int newx = this.getX(); -// int newy = this.getY(); -// -// int countx = 0; -// int county = 0; -// -// if (currentCountX == 0 && currentCountY == 0) { -// currentCountX = stepx; -// currentCountY = stepy; -// } else { -// countx = currentCountX; -// county = currentCountY; -// } -// -// // while (true) { -// if (countx != 0) { -// if (d == 'x' && i == -1 && countx != 0) { -// countx = countx - 1; -// newx = newx - 1; -// } else if (d == 'x' && i == 1 && countx != 0) { -// countx = countx - 1; -// newx = newx + 1; -// } else if (d == 'y' && i == -1 && county != 0) { -// county = county - 1; -// newy = newy - 1; -// } else if (d == 'y' && i == 1 && county != 0) { -// county = county - 1; -// newy = newy + 1; -// } -// this.setAutoBounds(newx, newy); -// } -// -// currentCountX = countx; -// currentCountY = county; -// -// // } - startMove = false; -// } + startMove = false; } } diff --git a/HeatUp/src/de/heatup/ui/KeyBindings.java b/HeatUp/src/de/heatup/ui/KeyBindings.java index a06d804..36de76e 100644 --- a/HeatUp/src/de/heatup/ui/KeyBindings.java +++ b/HeatUp/src/de/heatup/ui/KeyBindings.java @@ -3,6 +3,7 @@ package de.heatup.ui; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import de.heatup.mapengine.MapGrid; import de.heatup.testing.Player; public class KeyBindings extends KeyAdapter { @@ -57,29 +58,36 @@ public class KeyBindings extends KeyAdapter { public void foo() { - if (isLeft()) { - if (!player.animate) { + /* + * rudimentäre kollisionserkennung, exportiere ich gerade in eigenes modul :) + */ + if (isLeft() && MapGrid.getGridCell(player.getGridLocation().x-1, player.getGridLocation().y).isPath()) { + if (!player.animate ) { + player.setGridLocation(player.getGridLocation().x-1, player.getGridLocation().y); player.step('x', -1); player.startMove = true; } player.animate = true; } - if (isRight()) { - if (!player.animate) { + if (isRight() && MapGrid.getGridCell(player.getGridLocation().x+1, player.getGridLocation().y).isPath()) { + if (!player.animate ) { + player.setGridLocation(player.getGridLocation().x+1, player.getGridLocation().y); player.step('x', 1); player.startMove = true; } player.animate = true; } - if (isUp()) { - if (!player.animate) { + if (isUp() && MapGrid.getGridCell(player.getGridLocation().x, player.getGridLocation().y-1).isPath()) { + if (!player.animate ) { + player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y-1); player.step('y', -1); player.startMove = true; } player.animate = true; } - if (isDown()) { - if (!player.animate) { + if (isDown() && MapGrid.getGridCell(player.getGridLocation().x, player.getGridLocation().y+1).isPath()) { + if (!player.animate ) { + player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y+1); player.step('y', 1); player.startMove = true; }