Merge Threads into GameThread, Introduce interfaces for Entities.
Background now inherits Entity, too. Fixed imports all over the place.
This commit is contained in:
parent
475bdb73f4
commit
a276298174
|
@ -4,10 +4,8 @@ import java.awt.EventQueue;
|
|||
|
||||
import de.teamteamteam.spacescooter.background.StarBackground;
|
||||
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;
|
||||
import de.teamteamteam.spacescooter.threads.GameThread;
|
||||
|
||||
/**
|
||||
* Nothing but a class containing the main method.
|
||||
|
@ -35,12 +33,9 @@ public class Main {
|
|||
}
|
||||
});
|
||||
|
||||
//Initialize PaintThread
|
||||
PaintThread pt = new PaintThread(gf);
|
||||
pt.start();
|
||||
//Initialize GameThread
|
||||
GameThread gameThread = new GameThread(gf);
|
||||
gameThread.start();
|
||||
|
||||
//Initialize EntityUpdateThread
|
||||
EntityUpdateThread ut = new EntityUpdateThread(gf);
|
||||
ut.start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package de.teamteamteam.spacescooter.background;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Background {
|
||||
import de.teamteamteam.spacescooter.entities.Entity;
|
||||
|
||||
public abstract class Background extends Entity {
|
||||
|
||||
public static ArrayList<Background> backgrounds = null;
|
||||
|
||||
|
@ -18,12 +19,4 @@ public abstract class Background {
|
|||
Background.backgrounds.add(this);
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
|
||||
}
|
||||
|
||||
public void update() {
|
||||
//scroll shit
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,16 +23,16 @@ public class StarBackground extends Background {
|
|||
|
||||
private int offset = 0;
|
||||
|
||||
public void update() {
|
||||
public void update(long millisecondsSinceLastCall) {
|
||||
this.offset -= 15;
|
||||
if(Math.abs(this.offset) > this.img.getWidth()) {
|
||||
this.offset += this.img.getWidth();
|
||||
if(Math.abs(this.offset) > StarBackground.img.getWidth()) {
|
||||
this.offset += StarBackground.img.getWidth();
|
||||
}
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
g.drawImage(this.img, 0+this.offset, 0, this.img.getWidth(), this.img.getHeight(), null);
|
||||
g.drawImage(this.img, this.img.getWidth()+this.offset, 0, this.img.getWidth(), this.img.getHeight(), null);
|
||||
g.drawImage(StarBackground.img, 0+this.offset, 0, StarBackground.img.getWidth(), StarBackground.img.getHeight(), null);
|
||||
g.drawImage(StarBackground.img, StarBackground.img.getWidth()+this.offset, 0, StarBackground.img.getWidth(), StarBackground.img.getHeight(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
package de.teamteamteam.spacescooter.entities;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class Entity {
|
||||
public abstract class Entity implements Updateable, Paintable {
|
||||
|
||||
public static ArrayList<Entity> entities = null;
|
||||
|
||||
|
@ -16,10 +13,6 @@ public abstract class Entity {
|
|||
Entity.entities = new ArrayList<Entity>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default hitbox for every entity
|
||||
*/
|
||||
protected Shape HitBox;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -27,16 +20,6 @@ public abstract class Entity {
|
|||
*/
|
||||
public Entity() {
|
||||
Entity.entities.add(this);
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
public void update() {
|
||||
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package de.teamteamteam.spacescooter.entities;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
public interface Paintable {
|
||||
|
||||
public void paint(Graphics g);
|
||||
|
||||
}
|
|
@ -31,25 +31,24 @@ public class Player extends Entity {
|
|||
this.y = 300;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
int off = 3;
|
||||
public void update(long millisecondsSinceLastCall) {
|
||||
System.out.println(millisecondsSinceLastCall / (1000.0/60.0));
|
||||
int offset = (int) ((3.0F/16.0F) * millisecondsSinceLastCall);
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
||||
this.y -= off;
|
||||
this.y -= offset;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
||||
this.y += off;
|
||||
this.y += offset;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
||||
this.x -= off;
|
||||
this.x -= offset;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
||||
this.x += off;
|
||||
this.x += offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
g.drawImage(img, this.x, this.y, null);
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public class TestEntity extends Entity {
|
|||
g.drawRect(100, 200, 300, 300);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
public void update(long millisecondsSinceLastCall) {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
|
||||
System.out.println("Hallo Welt!");
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package de.teamteamteam.spacescooter.entities;
|
||||
|
||||
public interface Updateable {
|
||||
|
||||
public void update(long millisecondsSinceLastCall);
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package de.teamteamteam.spacescooter.threads;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.util.Iterator;
|
||||
|
||||
import de.teamteamteam.spacescooter.background.Background;
|
||||
import de.teamteamteam.spacescooter.entities.Entity;
|
||||
import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||
|
||||
/**
|
||||
* This thread triggers about 60 redraws per second.
|
||||
*/
|
||||
public class GameThread extends Thread {
|
||||
|
||||
private GameFrame gf;
|
||||
|
||||
private long lastFrame;
|
||||
|
||||
public GameThread(GameFrame gf) {
|
||||
this.setName("GameThread");
|
||||
this.gf = gf;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
final GameFrame gf = this.gf; // :'-(
|
||||
this.lastFrame = System.currentTimeMillis();
|
||||
while (true) {
|
||||
//If it is not time yet, sleep and continue the loop.
|
||||
if((System.currentTimeMillis() - this.lastFrame) <= 15) {
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch(InterruptedException e) {
|
||||
System.err.println(e.getStackTrace());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
this.updateBackgrounds();
|
||||
this.updateEntities();
|
||||
|
||||
//Trigger redrawing the things. Important: AWT-Context needed here!
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
gf.draw();
|
||||
}
|
||||
});
|
||||
//Update time for the last frame
|
||||
this.lastFrame = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBackgrounds() {
|
||||
Iterator<Background> i = Background.backgrounds.iterator();
|
||||
while(i.hasNext()) {
|
||||
Background b = i.next();
|
||||
b.update(System.currentTimeMillis() - this.lastFrame);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateEntities() {
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.update(System.currentTimeMillis() - this.lastFrame);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue