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 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.gui.GameFrame;
|
||||||
import de.teamteamteam.spacescooter.threads.PaintThread;
|
import de.teamteamteam.spacescooter.threads.PaintThread;
|
||||||
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
||||||
|
@ -27,6 +29,10 @@ public class Main {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Whatever.
|
||||||
|
new TestEntity();
|
||||||
|
new Player();
|
||||||
|
|
||||||
//Initialize PaintThread
|
//Initialize PaintThread
|
||||||
PaintThread pt = new PaintThread(gf);
|
PaintThread pt = new PaintThread(gf);
|
||||||
pt.start();
|
pt.start();
|
||||||
|
|
|
@ -1,9 +1,27 @@
|
||||||
package de.teamteamteam.spacescooter.entities;
|
package de.teamteamteam.spacescooter.entities;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public abstract class Entity {
|
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 update();
|
||||||
|
|
||||||
public abstract void paint(Graphics g);
|
public abstract void paint(Graphics g);
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.awt.Graphics;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
@ -19,11 +18,7 @@ public class Player extends Entity {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
InputStream res = Player.class.getClassLoader().getResourceAsStream("images/nyancat.png");
|
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/nyancat.png"));
|
||||||
if(res == null) {
|
|
||||||
System.out.println("Kein res :/");
|
|
||||||
}
|
|
||||||
img = ImageIO.read(res);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -31,6 +26,7 @@ public class Player extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player() {
|
public Player() {
|
||||||
|
super();
|
||||||
this.x = 200;
|
this.x = 200;
|
||||||
this.y = 300;
|
this.y = 300;
|
||||||
}
|
}
|
||||||
|
@ -38,16 +34,16 @@ public class Player extends Entity {
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
||||||
this.y--;
|
this.y -= 3;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
||||||
this.y++;
|
this.y += 3;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
||||||
this.x--;
|
this.x -= 3;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
||||||
this.x++;
|
this.x += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,25 @@
|
||||||
package de.teamteamteam.spacescooter.gui;
|
package de.teamteamteam.spacescooter.gui;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.util.ArrayList;
|
import java.awt.image.BufferStrategy;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
import de.teamteamteam.spacescooter.controls.Keyboard;
|
import de.teamteamteam.spacescooter.controls.Keyboard;
|
||||||
import de.teamteamteam.spacescooter.entities.Entity;
|
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.
|
* The game will take place in this beautiful window.
|
||||||
*/
|
*/
|
||||||
public class GameFrame extends JFrame {
|
public class GameFrame extends JFrame {
|
||||||
|
|
||||||
private ArrayList<Entity> entities;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public GameFrame() {
|
public GameFrame() {
|
||||||
super();
|
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.
|
* Set up the GameFrame before showing it to the world.
|
||||||
*/
|
*/
|
||||||
|
@ -39,25 +28,40 @@ public class GameFrame extends JFrame {
|
||||||
this.setSize(800, 600);
|
this.setSize(800, 600);
|
||||||
this.setUndecorated(false);
|
this.setUndecorated(false);
|
||||||
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
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.addKeyListener(new Keyboard());
|
||||||
|
|
||||||
this.setVisible(true);
|
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 drawEntities() {
|
||||||
public void paint(Graphics g) {
|
this.createBufferStrategy(2);
|
||||||
Iterator<Entity> i = this.entities.iterator();
|
Graphics bufferedGraphics = null;
|
||||||
while(i.hasNext()) {
|
BufferStrategy bufferStrategy = this.getBufferStrategy();
|
||||||
Entity e = i.next();
|
if (bufferStrategy == null) {
|
||||||
e.paint(g);
|
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;
|
package de.teamteamteam.spacescooter.threads;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
|
||||||
import de.teamteamteam.spacescooter.entities.Entity;
|
import de.teamteamteam.spacescooter.entities.Entity;
|
||||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||||
|
|
||||||
|
@ -31,8 +29,7 @@ public class EntityUpdateThread extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateEntities() {
|
private void updateEntities() {
|
||||||
ArrayList<Entity> entityList = this.gf.getEntityList();
|
Iterator<Entity> i = Entity.entities.iterator();
|
||||||
Iterator<Entity> i = entityList.iterator();
|
|
||||||
while(i.hasNext()) {
|
while(i.hasNext()) {
|
||||||
Entity e = i.next();
|
Entity e = i.next();
|
||||||
e.update();
|
e.update();
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
package de.teamteamteam.spacescooter.threads;
|
package de.teamteamteam.spacescooter.threads;
|
||||||
|
|
||||||
|
import java.awt.EventQueue;
|
||||||
|
|
||||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This thread triggers about 60 redraws per second.
|
||||||
|
*/
|
||||||
public class PaintThread extends Thread {
|
public class PaintThread extends Thread {
|
||||||
|
|
||||||
private GameFrame gf;
|
private GameFrame gf;
|
||||||
|
@ -11,13 +16,19 @@ public class PaintThread extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
final GameFrame gf = this.gf; // :'-(
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(16);
|
Thread.sleep(16);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
System.err.println(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