From a276298174ba2431202223426ef27e8ea328d43c Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 24 Oct 2014 10:52:49 +0200 Subject: [PATCH] Merge Threads into GameThread, Introduce interfaces for Entities. Background now inherits Entity, too. Fixed imports all over the place. --- src/de/teamteamteam/spacescooter/Main.java | 13 ++-- .../spacescooter/background/Background.java | 13 +--- .../background/StarBackground.java | 10 +-- .../spacescooter/entities/Entity.java | 19 +----- .../spacescooter/entities/Paintable.java | 9 +++ .../spacescooter/entities/Player.java | 15 ++--- .../spacescooter/entities/TestEntity.java | 2 +- .../spacescooter/entities/Updateable.java | 7 ++ .../spacescooter/threads/GameThread.java | 67 +++++++++++++++++++ 9 files changed, 104 insertions(+), 51 deletions(-) create mode 100644 src/de/teamteamteam/spacescooter/entities/Paintable.java create mode 100644 src/de/teamteamteam/spacescooter/entities/Updateable.java create mode 100644 src/de/teamteamteam/spacescooter/threads/GameThread.java diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 134460f..3106292 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -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(); } } diff --git a/src/de/teamteamteam/spacescooter/background/Background.java b/src/de/teamteamteam/spacescooter/background/Background.java index 1f8beea..3da0420 100644 --- a/src/de/teamteamteam/spacescooter/background/Background.java +++ b/src/de/teamteamteam/spacescooter/background/Background.java @@ -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 backgrounds = null; @@ -18,12 +19,4 @@ public abstract class Background { Background.backgrounds.add(this); } - public void paint(Graphics g) { - - } - - public void update() { - //scroll shit - } - } diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java index ba1dd0d..026a9a9 100644 --- a/src/de/teamteamteam/spacescooter/background/StarBackground.java +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -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); } } diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index e8cf00b..ac031ab 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -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 entities = null; @@ -16,10 +13,6 @@ public abstract class Entity { Entity.entities = new ArrayList(); } - /** - * 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) { - } } diff --git a/src/de/teamteamteam/spacescooter/entities/Paintable.java b/src/de/teamteamteam/spacescooter/entities/Paintable.java new file mode 100644 index 0000000..36b1120 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/entities/Paintable.java @@ -0,0 +1,9 @@ +package de.teamteamteam.spacescooter.entities; + +import java.awt.Graphics; + +public interface Paintable { + + public void paint(Graphics g); + +} diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index 8d85fdc..0380ddd 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -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); } diff --git a/src/de/teamteamteam/spacescooter/entities/TestEntity.java b/src/de/teamteamteam/spacescooter/entities/TestEntity.java index 606eb61..a432d2c 100644 --- a/src/de/teamteamteam/spacescooter/entities/TestEntity.java +++ b/src/de/teamteamteam/spacescooter/entities/TestEntity.java @@ -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!"); } diff --git a/src/de/teamteamteam/spacescooter/entities/Updateable.java b/src/de/teamteamteam/spacescooter/entities/Updateable.java new file mode 100644 index 0000000..d22f8e0 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/entities/Updateable.java @@ -0,0 +1,7 @@ +package de.teamteamteam.spacescooter.entities; + +public interface Updateable { + + public void update(long millisecondsSinceLastCall); + +} diff --git a/src/de/teamteamteam/spacescooter/threads/GameThread.java b/src/de/teamteamteam/spacescooter/threads/GameThread.java new file mode 100644 index 0000000..33eeed5 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/threads/GameThread.java @@ -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 i = Background.backgrounds.iterator(); + while(i.hasNext()) { + Background b = i.next(); + b.update(System.currentTimeMillis() - this.lastFrame); + } + } + + private void updateEntities() { + Iterator i = Entity.entities.iterator(); + while(i.hasNext()) { + Entity e = i.next(); + e.update(System.currentTimeMillis() - this.lastFrame); + } + } +}