mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 10:19:46 +02:00
Opponent von Player extended und drei Gegner hinzugefügt.
This commit is contained in:
@@ -27,11 +27,14 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
private static JPanel statusPanel;
|
||||
private static JPanel gamePanel;
|
||||
private static JPanel actionPanel;
|
||||
|
||||
public static Player player01;
|
||||
public static KeyBindings kb;
|
||||
|
||||
DynamicObject dynO3;
|
||||
public static Player player01;
|
||||
|
||||
public static Opponent opponent01;
|
||||
public static Opponent opponent02;
|
||||
public static Opponent opponent03;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -73,29 +76,26 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
||||
/*
|
||||
* zeug aus dem alten gameloop
|
||||
*/
|
||||
|
||||
|
||||
DynamicObject dynO1 = new DynamicObject();
|
||||
gamePanel.add(dynO1);
|
||||
DynamicObject dynO2 = new DynamicObject();
|
||||
gamePanel.add(dynO2);
|
||||
DynamicObject dynO3 = new DynamicObject();
|
||||
gamePanel.add(dynO3);
|
||||
DynamicObject dynO4 = new DynamicObject();
|
||||
gamePanel.add(dynO4);
|
||||
|
||||
|
||||
|
||||
player01 = new Player();
|
||||
// Query.setPlayer(player01);
|
||||
gamePanel.add(player01);
|
||||
player01.setBounds(25, 25, 200, 200); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!!
|
||||
player01.setBounds(25,25,25, 25); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!!
|
||||
|
||||
// Gegner hinzufügen
|
||||
opponent01 = new Opponent();
|
||||
gamePanel.add(opponent01);
|
||||
opponent01.setBounds(50,50,25,25);
|
||||
|
||||
opponent02 = new Opponent();
|
||||
gamePanel.add(opponent02);
|
||||
opponent02.setBounds(50,50,25,25);
|
||||
|
||||
opponent03 = new Opponent();
|
||||
gamePanel.add(opponent03);
|
||||
opponent03.setBounds(50,50,25,25);
|
||||
|
||||
|
||||
gamePanel.setFocusable(true);
|
||||
|
||||
kb = new KeyBindings(player01);
|
||||
gamePanel.addKeyListener(kb);
|
||||
|
||||
@@ -107,6 +107,13 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
Point spawnPoint = SpawnAssistant.getPathLocationOnGrid();
|
||||
player01.spawnAt(spawnPoint);
|
||||
|
||||
Point spawnPoint2 = SpawnAssistant.getPathLocationOnGrid();
|
||||
opponent01.spawnAt(spawnPoint2);
|
||||
Point spawnPoint3 = SpawnAssistant.getPathLocationOnGrid();
|
||||
opponent02.spawnAt(spawnPoint3);
|
||||
Point spawnPoint4 = SpawnAssistant.getPathLocationOnGrid();
|
||||
opponent03.spawnAt(spawnPoint4);
|
||||
|
||||
gamePanel.setVisible(true);
|
||||
|
||||
//GameInterface.getGamePanel().repaint();
|
||||
@@ -193,6 +200,9 @@ public class GameLoopD extends JFrame implements Runnable {
|
||||
private void tick() {
|
||||
kb.checkKeyActivity();
|
||||
player01.tick();
|
||||
opponent01.tick();
|
||||
opponent02.tick();
|
||||
opponent03.tick();
|
||||
|
||||
}
|
||||
// alles was animiert wird hier rein (paints)
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package de.heatup.testing;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import de.heatup.mapengine.MapGrid;
|
||||
|
||||
public class Opponent extends Player {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private boolean redecide = true; // Nötig sobald der Schritt in die gewollte Richtung erfolgt ist
|
||||
private Random rand = new Random();
|
||||
private int testX;
|
||||
private int testY;
|
||||
// Notwendig um Blockade zu erkennen
|
||||
private int previ;
|
||||
private char prevd;
|
||||
private int prevX;
|
||||
private int prevY;
|
||||
private boolean blocked = false;
|
||||
private int blockedCounter = 0;
|
||||
|
||||
@Override protected void loadBufferedImage() {
|
||||
try {
|
||||
super.bf1 = ImageIO.read(new File("src/de/heatup/resources/images/little_green_guy_verysmall.png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void tick() {
|
||||
|
||||
if ( redecide ) {
|
||||
while ( true ) {
|
||||
|
||||
this.d = (rand.nextInt(10) <= 4) ? 'x' : 'y';
|
||||
this.i = (rand.nextInt(10) <= 4) ? 1 : -1;
|
||||
|
||||
// Sicher stellen das man nicht in die Richtung aus der man gekommen ist.
|
||||
if ( redecide && this.d == this.prevd && this.i != this.previ ) {
|
||||
this.i *= -1;
|
||||
}
|
||||
// Blockade Erkennung
|
||||
if ( this.prevX == this.getGridLocation().x & this.prevY == this.getGridLocation().y ) {
|
||||
this.blockedCounter++;
|
||||
} else {
|
||||
this.blockedCounter = 0;
|
||||
}
|
||||
this.blocked = (this.blockedCounter >= 30) ? true : false;
|
||||
this.prevX = this.getGridLocation().x;
|
||||
this.prevY = this.getGridLocation().y;
|
||||
// Blockade Erkennung
|
||||
|
||||
// Blockade auflösen, gehe doch in die Richtung aus man gekommen ist
|
||||
if ( this.blocked ) {
|
||||
System.out.println("Warning: Block detected!");
|
||||
this.i *= -1;
|
||||
this.blocked = false;
|
||||
this.blockedCounter = 0;
|
||||
}
|
||||
// Blockade auflösen
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (d == 'x') {
|
||||
testX=this.getGridLocation().x + i;
|
||||
testY=this.getGridLocation().y;
|
||||
} else if (d == 'y') {
|
||||
testY=this.getGridLocation().y + i;
|
||||
testX=this.getGridLocation().x;
|
||||
}
|
||||
|
||||
if (MapGrid.getGridCell(testX,testY).isPath()) {
|
||||
this.setGridLocation(testX, testY);
|
||||
this.prevd = d;
|
||||
this.previ = i;
|
||||
redecide = false;
|
||||
animate = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( animate ) {
|
||||
int newx = this.getX();
|
||||
int newy = this.getY();
|
||||
|
||||
for (int s = 0; s<2; s++) { // speed multiplikator
|
||||
if (d == 'x' && i == -1) {
|
||||
newx = newx - 1;
|
||||
} else if (d == 'x' && i == 1) {
|
||||
newx = newx + 1;
|
||||
} else if (d == 'y' && i == -1) {
|
||||
newy = newy - 1;
|
||||
} else if (d == 'y' && i == 1) {
|
||||
newy = newy + 1;
|
||||
}
|
||||
|
||||
this.setAutoBounds(newx, newy);
|
||||
stepCounter--;
|
||||
if ( stepCounter <= 0 ) {
|
||||
animate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stepCounter <= 0) {
|
||||
stepCounter = 25;
|
||||
redecide = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,27 +13,19 @@ import javax.swing.JComponent;
|
||||
|
||||
public class Player extends JComponent {
|
||||
private static final long serialVersionUID = 1L;
|
||||
// private Image image;
|
||||
private BufferedImage bf1;
|
||||
private Graphics2D g2d;
|
||||
|
||||
protected BufferedImage bf1;
|
||||
protected Graphics2D g2d;
|
||||
private double rescaleFactorX = 0.0;
|
||||
private double rescaleFactorY = 0.0;
|
||||
|
||||
public boolean startMove = false;
|
||||
public boolean animate = false;
|
||||
|
||||
private char d;
|
||||
private int i;
|
||||
|
||||
protected char d;
|
||||
protected int i;
|
||||
final int stepx = 25;
|
||||
final int stepy = 25;
|
||||
|
||||
int startX;
|
||||
int startY;
|
||||
|
||||
int currentCountX = 25;
|
||||
|
||||
int stepCounter = 25;
|
||||
private Point currentGridPosition;
|
||||
|
||||
public void setGridLocation(int x, int y) {
|
||||
@@ -46,14 +38,11 @@ public class Player extends JComponent {
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
g2d = (Graphics2D) g;
|
||||
// this.image =
|
||||
// Toolkit.getDefaultToolkit().getImage("src/de/heatup/resources/images/little_green_guy.png");
|
||||
if (bf1 == null) {
|
||||
loadBufferedImage();
|
||||
}
|
||||
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.drawImage(bf1, 0, 0, this);
|
||||
g2d.setStroke(new BasicStroke(8.0f));
|
||||
g2d.finalize();
|
||||
@@ -64,11 +53,10 @@ public class Player extends JComponent {
|
||||
this.setAutoBounds(spawnPoint.x*25, spawnPoint.y*25);
|
||||
this.repaint();
|
||||
}
|
||||
private void loadBufferedImage() {
|
||||
|
||||
protected void loadBufferedImage() {
|
||||
try {
|
||||
this.bf1 = ImageIO
|
||||
.read(new File(
|
||||
"src/de/heatup/resources/images/little_blue_guy_verysmall.png"));
|
||||
this.bf1 = ImageIO.read(new File("src/de/heatup/resources/images/little_blue_guy_verysmall.png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -88,15 +76,10 @@ public class Player extends JComponent {
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
|
||||
if ( startMove || animate ) {
|
||||
|
||||
int newx = this.getX();
|
||||
int newy = this.getY();
|
||||
|
||||
for (int s = 0; s<2; s++) { // speed multiplikator
|
||||
|
||||
|
||||
if (d == 'x' && i == -1) {
|
||||
newx = newx - 1;
|
||||
} else if (d == 'x' && i == 1) {
|
||||
@@ -106,26 +89,18 @@ public class Player extends JComponent {
|
||||
} else if (d == 'y' && i == 1) {
|
||||
newy = newy + 1;
|
||||
}
|
||||
|
||||
this.setAutoBounds(newx, newy);
|
||||
|
||||
|
||||
currentCountX--;
|
||||
|
||||
if ( currentCountX <= 0 ) {
|
||||
stepCounter--;
|
||||
if ( stepCounter <= 0 ) {
|
||||
animate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentCountX <= 0) {
|
||||
currentCountX = 25;
|
||||
|
||||
if (stepCounter <= 0) {
|
||||
stepCounter = 25;
|
||||
//debug
|
||||
System.out.println("on grid - x: "+getGridLocation().x + " y: "+getGridLocation().y);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
startMove = false;
|
||||
}
|
||||
|
||||
@@ -1,129 +1,129 @@
|
||||
package de.heatup.ui;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.DisplayMode;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Point;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.Window;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
import de.heatup.mapengine.MapEngine;
|
||||
import de.heatup.objects.DynamicObject;
|
||||
import de.heatup.objects.StaticObject;
|
||||
import de.heatup.objects.logic;
|
||||
import de.heatup.testing.GameLoop;
|
||||
|
||||
|
||||
public class GameInterface extends JFrame {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static JPanel outerPanel;
|
||||
private static JPanel statusPanel;
|
||||
private static JPanel gamePanel;
|
||||
private static JPanel actionPanel;
|
||||
//private BufferedImage image;
|
||||
|
||||
|
||||
|
||||
public static void createGUI() {
|
||||
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
final JFrame frame = new JFrame();
|
||||
|
||||
// Fullscreen testing: Remember not to use pack() when using fullscreen mode
|
||||
// frame.setUndecorated(true);
|
||||
// frame.setResizable(false);
|
||||
// frame.setExtendedState(Frame.MAXIMIZED_BOTH);
|
||||
// frame.validate();
|
||||
// GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
|
||||
|
||||
frame.setTitle("HeatUP!");
|
||||
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
//frame.setResizable(true);
|
||||
//frame.createBufferStrategy(2);
|
||||
|
||||
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
//frame.setBounds(0,0,screenSize.width, screenSize.height);
|
||||
|
||||
|
||||
outerPanel = new JPanel();
|
||||
outerPanel.setLayout(new BorderLayout());
|
||||
//outerPanel.setBorder(BorderFactory.createTitledBorder("outerPanel"));
|
||||
outerPanel.setVisible(true);
|
||||
|
||||
statusPanel = new JPanel();
|
||||
statusPanel.setLayout(new GridLayout(1,4));
|
||||
statusPanel.setPreferredSize(new Dimension(800,80));
|
||||
//statusPanel.setBorder(BorderFactory.createTitledBorder("statusPanel"));
|
||||
|
||||
gamePanel = new JPanel();
|
||||
gamePanel.setLayout(null);
|
||||
gamePanel.setPreferredSize(new Dimension(800,600));
|
||||
gamePanel.setVisible(true);
|
||||
//gamePanel.setBorder(BorderFactory.createTitledBorder("gamePanel"));
|
||||
|
||||
actionPanel = new JPanel();
|
||||
actionPanel.setLayout(new GridLayout(1,1));
|
||||
//actionPanel.setBorder(BorderFactory.createTitledBorder("actionPanel"));
|
||||
actionPanel.setPreferredSize(new Dimension(800,80));
|
||||
|
||||
|
||||
outerPanel.add(statusPanel,BorderLayout.NORTH);
|
||||
outerPanel.add(gamePanel,BorderLayout.CENTER);
|
||||
outerPanel.add(actionPanel,BorderLayout.SOUTH);
|
||||
|
||||
frame.add(outerPanel);
|
||||
// Comment out for fullscreen
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
/*
|
||||
* We need to use EQ for EDT
|
||||
*
|
||||
* TODO: add fullscreen mode
|
||||
*/
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// GameInterface is static now.
|
||||
//GameInterface gameWindow = new GameInterface();
|
||||
GameInterface.createGUI();
|
||||
|
||||
// Start game loop to add player, opponents, maps and everything you need
|
||||
GameLoop.startGameLoop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static JPanel getGamePanel() {
|
||||
return gamePanel;
|
||||
}
|
||||
public static JPanel getStatusPanel() {
|
||||
return statusPanel;
|
||||
}
|
||||
}
|
||||
//package de.heatup.ui;
|
||||
//import java.awt.BorderLayout;
|
||||
//import java.awt.Dimension;
|
||||
//import java.awt.DisplayMode;
|
||||
//import java.awt.EventQueue;
|
||||
//import java.awt.Frame;
|
||||
//import java.awt.GraphicsDevice;
|
||||
//import java.awt.GraphicsEnvironment;
|
||||
//import java.awt.GridLayout;
|
||||
//import java.awt.Point;
|
||||
//import java.awt.Toolkit;
|
||||
//import java.awt.Window;
|
||||
//import java.io.IOException;
|
||||
//
|
||||
//import javax.swing.BorderFactory;
|
||||
//import javax.swing.JFrame;
|
||||
//import javax.swing.JLabel;
|
||||
//import javax.swing.JPanel;
|
||||
//import javax.swing.UIManager;
|
||||
//import javax.swing.UnsupportedLookAndFeelException;
|
||||
//
|
||||
//import de.heatup.mapengine.MapEngine;
|
||||
//import de.heatup.objects.DynamicObject;
|
||||
//import de.heatup.objects.StaticObject;
|
||||
//import de.heatup.objects.logic;
|
||||
//import de.heatup.testing.GameLoop;
|
||||
//
|
||||
//
|
||||
//public class GameInterface extends JFrame {
|
||||
//
|
||||
// /**
|
||||
// *
|
||||
// */
|
||||
// private static final long serialVersionUID = 1L;
|
||||
// private static JPanel outerPanel;
|
||||
// private static JPanel statusPanel;
|
||||
// private static JPanel gamePanel;
|
||||
// private static JPanel actionPanel;
|
||||
// //private BufferedImage image;
|
||||
//
|
||||
//
|
||||
//
|
||||
// public static void createGUI() {
|
||||
//
|
||||
// try {
|
||||
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
// } catch (Exception e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// final JFrame frame = new JFrame();
|
||||
//
|
||||
//// Fullscreen testing: Remember not to use pack() when using fullscreen mode
|
||||
//// frame.setUndecorated(true);
|
||||
//// frame.setResizable(false);
|
||||
//// frame.setExtendedState(Frame.MAXIMIZED_BOTH);
|
||||
//// frame.validate();
|
||||
//// GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
|
||||
//
|
||||
// frame.setTitle("HeatUP!");
|
||||
// frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
// //frame.setResizable(true);
|
||||
// //frame.createBufferStrategy(2);
|
||||
//
|
||||
// //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
// //frame.setBounds(0,0,screenSize.width, screenSize.height);
|
||||
//
|
||||
//
|
||||
// outerPanel = new JPanel();
|
||||
// outerPanel.setLayout(new BorderLayout());
|
||||
// //outerPanel.setBorder(BorderFactory.createTitledBorder("outerPanel"));
|
||||
// outerPanel.setVisible(true);
|
||||
//
|
||||
// statusPanel = new JPanel();
|
||||
// statusPanel.setLayout(new GridLayout(1,4));
|
||||
// statusPanel.setPreferredSize(new Dimension(800,80));
|
||||
// //statusPanel.setBorder(BorderFactory.createTitledBorder("statusPanel"));
|
||||
//
|
||||
// gamePanel = new JPanel();
|
||||
// gamePanel.setLayout(null);
|
||||
// gamePanel.setPreferredSize(new Dimension(800,600));
|
||||
// gamePanel.setVisible(true);
|
||||
// //gamePanel.setBorder(BorderFactory.createTitledBorder("gamePanel"));
|
||||
//
|
||||
// actionPanel = new JPanel();
|
||||
// actionPanel.setLayout(new GridLayout(1,1));
|
||||
// //actionPanel.setBorder(BorderFactory.createTitledBorder("actionPanel"));
|
||||
// actionPanel.setPreferredSize(new Dimension(800,80));
|
||||
//
|
||||
//
|
||||
// outerPanel.add(statusPanel,BorderLayout.NORTH);
|
||||
// outerPanel.add(gamePanel,BorderLayout.CENTER);
|
||||
// outerPanel.add(actionPanel,BorderLayout.SOUTH);
|
||||
//
|
||||
// frame.add(outerPanel);
|
||||
// // Comment out for fullscreen
|
||||
// frame.pack();
|
||||
// frame.setVisible(true);
|
||||
// frame.repaint();
|
||||
// }
|
||||
//
|
||||
// /*
|
||||
// * We need to use EQ for EDT
|
||||
// *
|
||||
// * TODO: add fullscreen mode
|
||||
// */
|
||||
// public static void main(String[] args) throws IOException, InterruptedException {
|
||||
//
|
||||
// EventQueue.invokeLater(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// // GameInterface is static now.
|
||||
// //GameInterface gameWindow = new GameInterface();
|
||||
// GameInterface.createGUI();
|
||||
//
|
||||
// // Start game loop to add player, opponents, maps and everything you need
|
||||
// GameLoop.startGameLoop();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// public static JPanel getGamePanel() {
|
||||
// return gamePanel;
|
||||
// }
|
||||
// public static JPanel getStatusPanel() {
|
||||
// return statusPanel;
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user