* Player bleibt nun im Spelfeld
* Anfang der Gegner Kollision * Experimenteller Timmer
This commit is contained in:
parent
475bdb73f4
commit
5181be3619
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
Binary file not shown.
|
@ -6,6 +6,7 @@ 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.BasicTimer;
|
||||
import de.teamteamteam.spacescooter.threads.PaintThread;
|
||||
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
||||
|
||||
|
@ -26,6 +27,7 @@ public class Main {
|
|||
//Whatever.
|
||||
new StarBackground();
|
||||
new Player();
|
||||
new TestEntity();
|
||||
|
||||
//Initialize the GameFrame properly within the AWT EventQueue
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
|
@ -42,5 +44,6 @@ public class Main {
|
|||
//Initialize EntityUpdateThread
|
||||
EntityUpdateThread ut = new EntityUpdateThread(gf);
|
||||
ut.start();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public class StarBackground extends Background {
|
|||
private static BufferedImage img;
|
||||
static {
|
||||
try {
|
||||
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/starbackground.png"));
|
||||
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/testbackground.png"));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
@ -24,7 +24,7 @@ public class StarBackground extends Background {
|
|||
private int offset = 0;
|
||||
|
||||
public void update() {
|
||||
this.offset -= 15;
|
||||
this.offset -= 5;
|
||||
if(Math.abs(this.offset) > this.img.getWidth()) {
|
||||
this.offset += this.img.getWidth();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.ArrayList;
|
|||
public abstract class Entity {
|
||||
|
||||
public static ArrayList<Entity> entities = null;
|
||||
private static boolean isEnemy = false;
|
||||
|
||||
/**
|
||||
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
|
||||
|
@ -21,6 +22,14 @@ public abstract class Entity {
|
|||
*/
|
||||
protected Shape HitBox;
|
||||
|
||||
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.
|
||||
|
|
|
@ -34,18 +34,22 @@ public class Player extends Entity {
|
|||
@Override
|
||||
public void update() {
|
||||
int off = 3;
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) {
|
||||
this.y -= off;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (600 - this.img.getHeight())) {
|
||||
this.y += off;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) {
|
||||
this.x -= off;
|
||||
}
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (800 - this.img.getWidth())) {
|
||||
this.x += off;
|
||||
}
|
||||
|
||||
if (Entity.isEnemy()) {
|
||||
System.out.print("hit");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame;
|
|||
public class EntityUpdateThread extends Thread {
|
||||
|
||||
private GameFrame gf;
|
||||
BasicTimer timer = new BasicTimer(60);
|
||||
|
||||
public EntityUpdateThread(GameFrame gf) {
|
||||
this.gf = gf;
|
||||
|
@ -20,11 +21,7 @@ public class EntityUpdateThread extends Thread {
|
|||
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(16);
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
timer.sync();
|
||||
this.updateBackgrounds();
|
||||
this.updateEntities();
|
||||
}
|
||||
|
|
|
@ -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