Merge mit erster Fensterkollision und Enemy-Implementation.
Conflicts: src/de/teamteamteam/spacescooter/Main.java src/de/teamteamteam/spacescooter/background/StarBackground.java src/de/teamteamteam/spacescooter/entities/Player.java
This commit is contained in:
commit
72b914cd3b
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
|
@ -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() {
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||
public abstract class Entity implements Updateable, Paintable {
|
||||
|
||||
public static ArrayList<Entity> 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.
|
||||
|
|
|
@ -32,18 +32,18 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Background> i = Background.backgrounds.iterator();
|
||||
while(i.hasNext()) {
|
||||
Background b = i.next();
|
||||
b.update();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateEntities() {
|
||||
Iterator<Entity> i = Entity.entities.iterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
e.update();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue