Entities verwalten sich jetzt ein Stück weit selbst.
This commit is contained in:
parent
2be8fa2bec
commit
061ab3c9c1
|
@ -2,6 +2,8 @@ package de.teamteamteam.spacescooter;
|
|||
|
||||
import java.awt.EventQueue;
|
||||
|
||||
import de.teamteamteam.spacescooter.entities.Player;
|
||||
import de.teamteamteam.spacescooter.entities.TestEntity;
|
||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||
import de.teamteamteam.spacescooter.threads.PaintThread;
|
||||
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
||||
|
@ -27,6 +29,10 @@ public class Main {
|
|||
}
|
||||
});
|
||||
|
||||
//Whatever.
|
||||
new TestEntity();
|
||||
new Player();
|
||||
|
||||
//Initialize PaintThread
|
||||
PaintThread pt = new PaintThread(gf);
|
||||
pt.start();
|
||||
|
|
|
@ -1,9 +1,27 @@
|
|||
package de.teamteamteam.spacescooter.entities;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Entity {
|
||||
|
||||
|
||||
public static ArrayList<Entity> entities = null;
|
||||
|
||||
/**
|
||||
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
|
||||
*/
|
||||
static {
|
||||
Entity.entities = new ArrayList<Entity>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* All entities are within a static array list for our convenience.
|
||||
*/
|
||||
public Entity() {
|
||||
Entity.entities.add(this);
|
||||
}
|
||||
|
||||
public abstract void update();
|
||||
|
||||
public abstract void paint(Graphics g);
|
||||
|
|
|
@ -4,7 +4,6 @@ import java.awt.Graphics;
|
|||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
|
@ -19,11 +18,7 @@ public class Player extends Entity {
|
|||
|
||||
static {
|
||||
try {
|
||||
InputStream res = Player.class.getClassLoader().getResourceAsStream("images/nyancat.png");
|
||||
if(res == null) {
|
||||
System.out.println("Kein res :/");
|
||||
}
|
||||
img = ImageIO.read(res);
|
||||
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/nyancat.png"));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
@ -31,6 +26,7 @@ public class Player extends Entity {
|
|||
}
|
||||
|
||||
public Player() {
|
||||
super();
|
||||
this.x = 200;
|
||||
this.y = 300;
|
||||
}
|
||||
|
@ -38,16 +34,16 @@ public class Player extends Entity {
|
|||
@Override
|
||||
public void update() {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
||||
this.y--;
|
||||
this.y -= 3;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
||||
this.y++;
|
||||
this.y += 3;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
||||
this.x--;
|
||||
this.x -= 3;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
||||
this.x++;
|
||||
this.x += 3;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,36 +1,25 @@
|
|||
package de.teamteamteam.spacescooter.gui;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.util.ArrayList;
|
||||
import java.awt.image.BufferStrategy;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import de.teamteamteam.spacescooter.controls.Keyboard;
|
||||
import de.teamteamteam.spacescooter.entities.Entity;
|
||||
import de.teamteamteam.spacescooter.entities.Player;
|
||||
import de.teamteamteam.spacescooter.entities.TestEntity;
|
||||
|
||||
/**
|
||||
* The game will take place in this beautiful window.
|
||||
*/
|
||||
public class GameFrame extends JFrame {
|
||||
|
||||
private ArrayList<Entity> entities;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public GameFrame() {
|
||||
super();
|
||||
this.entities = new ArrayList<Entity>();
|
||||
|
||||
//TODO: Remove this!
|
||||
this.entities.add(new TestEntity());
|
||||
this.entities.add(new Player());
|
||||
}
|
||||
|
||||
public ArrayList<Entity> getEntityList() {
|
||||
return this.entities;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set up the GameFrame before showing it to the world.
|
||||
*/
|
||||
|
@ -39,25 +28,40 @@ public class GameFrame extends JFrame {
|
|||
this.setSize(800, 600);
|
||||
this.setUndecorated(false);
|
||||
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
//Make sure we get the keyboard events. Use Keyboard.isKeyDown() to ask about keys status.
|
||||
|
||||
// Make sure we get the keyboard events. Use Keyboard.isKeyDown() to ask
|
||||
// about keys status.
|
||||
this.addKeyListener(new Keyboard());
|
||||
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Have the GameFrame repaint all the visible entities.
|
||||
* The pain, do not underestimate it!
|
||||
* @see http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering for details.
|
||||
*/
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
Iterator<Entity> i = this.entities.iterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.paint(g);
|
||||
public void drawEntities() {
|
||||
this.createBufferStrategy(2);
|
||||
Graphics bufferedGraphics = null;
|
||||
BufferStrategy bufferStrategy = this.getBufferStrategy();
|
||||
if (bufferStrategy == null) {
|
||||
System.out.println("Mist");
|
||||
}
|
||||
try {
|
||||
bufferedGraphics = bufferStrategy.getDrawGraphics();
|
||||
|
||||
//Now we can use bufferedGraphics to actually draw stuff ...
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while (i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.paint(bufferedGraphics);
|
||||
}
|
||||
} finally {
|
||||
//We are done, dispose the pen and celebrate the result!
|
||||
if (bufferedGraphics != null) bufferedGraphics.dispose();
|
||||
}
|
||||
|
||||
bufferStrategy.show();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package de.teamteamteam.spacescooter.threads;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
import de.teamteamteam.spacescooter.entities.Entity;
|
||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||
|
||||
|
@ -31,8 +29,7 @@ public class EntityUpdateThread extends Thread {
|
|||
}
|
||||
|
||||
private void updateEntities() {
|
||||
ArrayList<Entity> entityList = this.gf.getEntityList();
|
||||
Iterator<Entity> i = entityList.iterator();
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.update();
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
package de.teamteamteam.spacescooter.threads;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
|
||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||
|
||||
/**
|
||||
* This thread triggers about 60 redraws per second.
|
||||
*/
|
||||
public class PaintThread extends Thread {
|
||||
|
||||
private GameFrame gf;
|
||||
|
@ -11,13 +16,19 @@ public class PaintThread extends Thread {
|
|||
}
|
||||
|
||||
public void run() {
|
||||
final GameFrame gf = this.gf; // :'-(
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(16);
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
this.gf.repaint();
|
||||
//Trigger redrawing the things. Important: AWT-Context needed here!
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
gf.drawEntities();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue