diff --git a/res/images/testbackground.png b/res/images/testbackground.png new file mode 100644 index 0000000..39dd34d Binary files /dev/null and b/res/images/testbackground.png differ diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 3106292..9af2a6a 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -4,6 +4,7 @@ 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.GameThread; @@ -24,6 +25,7 @@ public class Main { //Whatever. new StarBackground(); new Player(); + new TestEntity(); //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index ac031ab..1914ecc 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -5,6 +5,7 @@ import java.util.ArrayList; public abstract class Entity implements Updateable, Paintable { public static ArrayList entities = null; + private static boolean isEnemy = false; /** * We need to initialize the ArrayList, so the EntityUpdateThread won't beat us. @@ -14,6 +15,14 @@ public abstract class Entity implements Updateable, Paintable { } + public static boolean isEnemy() { + return isEnemy; + } + + protected void setEnemy(boolean isEnemy) { + this.isEnemy = isEnemy; + } + /** * Constructor. * All entities are within a static array list for our convenience. diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index 4f76819..1f37a4a 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -32,20 +32,20 @@ public class Player extends Entity { } public void update(long millisecondsSinceLastCall) { - int offset = (int) ((3.0F/16.0F) * millisecondsSinceLastCall); - if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { - this.y -= offset; + int off = 3; + if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) { + this.y -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { - this.y += offset; + if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (600 - Player.img.getHeight())) { + this.y += off; } - if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { - this.x -= offset; + if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) { + this.x -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { - this.x += offset; + if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (800 - Player.img.getWidth())) { + this.x += off; } - + } public void paint(Graphics g) { diff --git a/src/de/teamteamteam/spacescooter/entities/TestEntity.java b/src/de/teamteamteam/spacescooter/entities/TestEntity.java index a432d2c..e12a384 100644 --- a/src/de/teamteamteam/spacescooter/entities/TestEntity.java +++ b/src/de/teamteamteam/spacescooter/entities/TestEntity.java @@ -3,31 +3,28 @@ package de.teamteamteam.spacescooter.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; -import java.util.Random; import de.teamteamteam.spacescooter.controls.Keyboard; public class TestEntity extends Entity { private Color color; - private Random random; public TestEntity() { super(); + this.setEnemy(true); this.color = new Color(255, 0, 0); - this.random = new Random(); } public void paint(Graphics g) { g.setColor(this.color); - g.drawRect(100, 200, 300, 300); + g.fillRect(300, 300, 100, 100); } public void update(long millisecondsSinceLastCall) { if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) { System.out.println("Hallo Welt!"); } - this.color = new Color(this.random.nextInt(255),this.random.nextInt(255),this.random.nextInt(255)); } } diff --git a/src/de/teamteamteam/spacescooter/threads/BasicTimer.java b/src/de/teamteamteam/spacescooter/threads/BasicTimer.java new file mode 100644 index 0000000..a47ae4e --- /dev/null +++ b/src/de/teamteamteam/spacescooter/threads/BasicTimer.java @@ -0,0 +1,48 @@ +package de.teamteamteam.spacescooter.threads; + +public class BasicTimer { + private int fps; + private long timeThen; + boolean newVersion = true; + public BasicTimer(int frameRate) { + if (System.getProperty("java.version").startsWith("1.4")) + newVersion = false; + if (newVersion) { + fps = frameRate; + timeThen = System.nanoTime(); + } + else { + fps = frameRate; + System.out.println("Old Version Detected: Running Old Java Timer Version"); + timeThen = System.currentTimeMillis(); + } + } + public void changeFPS(int frameRate) { + fps = frameRate; + } + public void sync() { + if (newVersion) { + long gapTo = 1000000000L / fps + timeThen; + long timeNow = System.nanoTime(); + + while (gapTo > timeNow) { + try { Thread.sleep(1); + } catch (InterruptedException e) {} + timeNow = System.nanoTime(); + } + + timeThen = timeNow; + } else { + long gapTo = 1000 / fps + timeThen; + long timeNow = System.currentTimeMillis(); + + while (gapTo > timeNow) { + try { Thread.sleep(1); + } catch (InterruptedException e) {} + timeNow = System.currentTimeMillis(); + } + + timeThen = timeNow; + } + } +} \ No newline at end of file diff --git a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java deleted file mode 100644 index 1cfe4bc..0000000 --- a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java +++ /dev/null @@ -1,48 +0,0 @@ -package de.teamteamteam.spacescooter.threads; - -import java.util.Iterator; - -import de.teamteamteam.spacescooter.background.Background; -import de.teamteamteam.spacescooter.entities.Entity; -import de.teamteamteam.spacescooter.gui.GameFrame; - -/** - * This thread is responsible for updating all the entities. - * It runs about 60 times per second. - */ -public class EntityUpdateThread extends Thread { - - private GameFrame gf; - - public EntityUpdateThread(GameFrame gf) { - this.gf = gf; - } - - public void run() { - while (true) { - try { - Thread.sleep(16); - } catch (InterruptedException e) { - System.err.println(e); - } - this.updateBackgrounds(); - this.updateEntities(); - } - } - - private void updateBackgrounds() { - Iterator i = Background.backgrounds.iterator(); - while(i.hasNext()) { - Background b = i.next(); - b.update(); - } - } - - private void updateEntities() { - Iterator i = Entity.entities.iterator(); - while(i.hasNext()) { - Entity e = i.next(); - e.update(); - } - } -} diff --git a/src/de/teamteamteam/spacescooter/threads/PaintThread.java b/src/de/teamteamteam/spacescooter/threads/PaintThread.java index 0bf7741..e01b3e5 100644 --- a/src/de/teamteamteam/spacescooter/threads/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/threads/PaintThread.java @@ -10,6 +10,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame; public class PaintThread extends Thread { private GameFrame gf; + BasicTimer timer = new BasicTimer(120); public PaintThread(GameFrame gf) { this.gf = gf; @@ -18,11 +19,7 @@ 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); - } + timer.sync(); //Trigger redrawing the things. Important: AWT-Context needed here! EventQueue.invokeLater(new Runnable() { public void run() {