Now with working Healthbar

Shoot of Player inside own Thread
This commit is contained in:
JJTCM 2014-11-04 14:15:24 +01:00
commit 306150d159
25 changed files with 190 additions and 31 deletions

BIN
res/images/Item.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 B

BIN
res/images/Item2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 B

BIN
res/images/Item3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 B

BIN
res/images/Item4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 B

BIN
res/images/shot02.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 B

BIN
res/images/shot03.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

BIN
res/images/shot04.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 B

BIN
res/images/shot05.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 B

View File

@ -23,6 +23,7 @@ public class EnemyFour extends Enemy{
this.points = points;
this.x = x;
this.y = y;
this.setCollisionDamage(this.getHealthPoints());
getNextPoint();
}
@ -52,9 +53,7 @@ public class EnemyFour extends Enemy{
nextPoint = points.get(index);
index++;
neuerVektor();
System.out.println("neuer point");
}catch(IndexOutOfBoundsException e){
System.out.println("ich bin dann mal weg!!");
this.remove();
}
}

View File

@ -9,7 +9,7 @@ public class EnemyOne extends Enemy {
this.setShootDelay(42);
this.setShootSpawn(-8, 10);
this.setHealthPoints(5);
this.setCollisionDamage(100);
this.setCollisionDamage(this.getHealthPoints());
}
@Override

View File

@ -11,15 +11,17 @@ public class EnemyThree extends Enemy{
private double newY;
private double ySpeed = 0.4;
private Random random;
public EnemyThree(int x, int y) {
super(x, y);
Random random = new Random();
random = new Random();
this.setImage("images/nyancat.png");
this.setShootSpeed(4);
this.setShootDelay(42);
this.setShootSpawn(-10, 10);
this.setHealthPoints(5);
this.setHealthPoints(15);
this.setCollisionDamage(this.getHealthPoints());
this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight()));
this.newY = this.getY();
}
@ -33,6 +35,7 @@ public class EnemyThree extends Enemy{
Screen.currentScreen.addEntity(new EnemyThree(0, 0));
}
if(!this.isAlive()){
if(random.nextInt(10) < 5) Items.create(getX(), getY());
Screen.currentScreen.addEntity(new EnemyThree(0, 0));
}
LinkedList<Entity> list = Screen.currentScreen.getEntities();

View File

@ -15,6 +15,7 @@ public class EnemyTwo extends Enemy{
this.setShootDelay(42);
this.setShootSpawn(-10, 10);
this.setHealthPoints(5);
this.setCollisionDamage(this.getHealthPoints());
this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight()));
}

View File

@ -2,6 +2,9 @@ package de.teamteamteam.spacescooter.entity;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.LinkedList;
import de.teamteamteam.spacescooter.screen.Screen;
public class HealthBar extends Entity {
@ -15,13 +18,19 @@ public class HealthBar extends Entity {
}
public void paint(Graphics2D g) {
this.g = g;
LinkedList<Entity> entities = Screen.currentScreen.getEntities();
for (Entity e : entities) {
if(e instanceof Player){
this.width = ((Player) e).getHealthPoints();
}
}
Graphics2D grpahic = (Graphics2D) g;
this.g = grpahic;
this.g.setColor(new Color(0,255,0));
this.g.fillRect(this.x, this.y, this.width, this.height);
}
public void update() {
//this.g.setColor(new Color(0,0,255));
}
public int getWidth() {

View File

@ -0,0 +1,35 @@
package de.teamteamteam.spacescooter.entity;
import java.util.Random;
public class ItemChance {
private static int summe = 0;
private static int[] items;
public ItemChance() {
ItemChance.items = new int[4];
items[0] = 4;
items[1] = 3;
items[2] = 2;
items[3] = 1;
for(int i=0; i<ItemChance.items.length; i++) {
ItemChance.summe += ItemChance.items[i];
}
}
public static int choose() {
//dauerhaft
Random random = new Random();
int r = random.nextInt(ItemChance.summe - 1) + 1;
for(int i=0; i<ItemChance.items.length; i++) {
r -= ItemChance.items[i];
if(r <= 0) {
return i;
}
}
return -1;
}
}

View File

@ -0,0 +1,47 @@
package de.teamteamteam.spacescooter.entity;
import java.util.LinkedList;
import de.teamteamteam.spacescooter.screen.Screen;
public abstract class Items extends LivingEntity{
public Items(int x, int y) {
super(x, y);
this.setHealthPoints(1);
}
public void update(){
this.setPosition(getX()-1, getY());
if(this.getX() < 0-getWidth()){
this.remove();
};
if(!this.isAlive()){
LinkedList<Entity> entities = Screen.currentScreen.getEntities();
for (Entity e : entities) {
if(e instanceof Player){
itemCollected((Player) e);
}
}
}
}
public abstract void itemCollected(Player player);
public static void create(int x, int y){
int auswahl = ItemChance.choose();
switch (auswahl) {
case 0:
Screen.currentScreen.addEntity(new TestItem1(x, y));
break;
case 1:
Screen.currentScreen.addEntity(new TestItem2(x, y));
break;
case 2:
Screen.currentScreen.addEntity(new TestItem3(x, y));
break;
case 3:
Screen.currentScreen.addEntity(new TestItem4(x, y));;
break;
}
}
}

View File

@ -55,6 +55,11 @@ public abstract class LivingEntity extends Entity implements Collidable {
enemy.takeDamage(this.getCollisionDamage());
this.takeDamage(enemy.getCollisionDamage());
}
if(this instanceof Player && entity instanceof Items){
Items item = (Items) entity;
item.setHealthPoints(0);
item.remove();
}
}
public void setCollisionDamage(int d) {

View File

@ -9,14 +9,15 @@ public class Player extends ShootingEntity {
private boolean shoot = false;
private boolean canMove = true;
public Player(int x, int y) {
super(x, y);
this.setImage("images/ship.png");
this.setShootDelay(5);
this.setShootSpawn(50, 16);
this.setShootDirection(Shot.RIGHT);
this.setShootSpeed(4);
this.setHealthPoints(10000);
this.setShootSpeed(10);
this.setHealthPoints(100);
inputThread.start();
}
@ -38,6 +39,7 @@ public class Player extends ShootingEntity {
}
if(shoot == true) {
this.shoot();
setShootDelay(20);
}
}
@ -53,22 +55,11 @@ public class Player extends ShootingEntity {
Thread inputThread = new Thread(new Runnable() {
public void run() {
int down = 0;
while (true) {
setShootDelay(5);
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
do {
if (Keyboard.isKeyUp(KeyEvent.VK_SPACE))
System.out.print("lol");
setShoot(false);
while(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
setShoot(true);
setShootDelay(20);
} while(Keyboard.isKeyDown(KeyEvent.VK_SPACE));
} else {
setShootDelay(5);
setShoot(false);
}
}
}

View File

@ -9,6 +9,7 @@ public abstract class ShootingEntity extends LivingEntity {
private int shootSpawnX;
private int shootSpawnY;
private int shootDirection;
private int damageValue = 5;
private int shootSpeed;
public ShootingEntity(int x, int y) {
@ -23,7 +24,7 @@ public abstract class ShootingEntity extends LivingEntity {
protected void shoot() {
if(this.currentShootDelay == 0) {
Screen.currentScreen.addEntity(new SingleShot(this.x + this.shootSpawnX, this.y + this.shootSpawnY, this.shootDirection, this.shootSpeed));
Screen.currentScreen.addEntity(new SingleShot(this.x + this.shootSpawnX, this.y + this.shootSpawnY, this.shootDirection, this.shootSpeed, this.damageValue));
this.currentShootDelay = this.shootDelay;
}
}
@ -44,4 +45,12 @@ public abstract class ShootingEntity extends LivingEntity {
this.shootSpawnX = x;
this.shootSpawnY = y;
}
public void setDamageValue(int damageValue){
this.damageValue = damageValue;
}
public int getDamageValue(){
return this.damageValue;
}
}

View File

@ -13,11 +13,12 @@ public abstract class Shot extends LivingEntity {
private int speed;
private int direction;
public Shot(int x, int y, int shootDirection, int shootSpeed) {
public Shot(int x, int y, int shootDirection, int shootSpeed, int damageValue) {
super(x, y);
this.direction = shootDirection;
this.speed = shootSpeed;
this.collisionCount = 1;
this.damageValue = damageValue;
}
public void setImage(String filename) {

View File

@ -2,10 +2,8 @@ package de.teamteamteam.spacescooter.entity;
public class SingleShot extends Shot {
public SingleShot(int x, int y, int shootDirection, int shootSpeed) {
super(x, y, shootDirection, shootSpeed);
public SingleShot(int x, int y, int shootDirection, int shootSpeed, int damageValue) {
super(x, y, shootDirection, shootSpeed, damageValue);
this.setImage("images/shot.png");
this.setDamageValue(5);
}
}

View File

@ -0,0 +1,14 @@
package de.teamteamteam.spacescooter.entity;
public class TestItem1 extends Items{
public TestItem1(int x, int y) {
super(x, y);
this.setImage("images/Item.png");
}
@Override
public void itemCollected(Player player) {
player.setDamageValue(player.getDamageValue()+5);
}
}

View File

@ -0,0 +1,16 @@
package de.teamteamteam.spacescooter.entity;
public class TestItem2 extends Items{
public static int chance = 2;
public TestItem2(int x, int y) {
super(x, y);
this.setImage("images/Item2.png");
}
@Override
public void itemCollected(Player player) {
player.setDamageValue(player.getDamageValue()+5);
}
}

View File

@ -0,0 +1,16 @@
package de.teamteamteam.spacescooter.entity;
public class TestItem3 extends Items{
public static int chance = 3;
public TestItem3(int x, int y) {
super(x, y);
this.setImage("images/Item3.png");
}
@Override
public void itemCollected(Player player) {
player.setDamageValue(player.getDamageValue()+5);
}
}

View File

@ -0,0 +1,16 @@
package de.teamteamteam.spacescooter.entity;
public class TestItem4 extends Items{
public static int chance = 4;
public TestItem4(int x, int y) {
super(x, y);
this.setImage("images/Item4.png");
}
@Override
public void itemCollected(Player player) {
player.setDamageValue(player.getDamageValue()+5);
}
}

View File

@ -11,9 +11,9 @@ import de.teamteamteam.spacescooter.background.StarBackground;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.entity.EnemyFour;
import de.teamteamteam.spacescooter.entity.EnemyThree;
import de.teamteamteam.spacescooter.entity.EnemyTwo;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.HealthBar;
import de.teamteamteam.spacescooter.entity.ItemChance;
import de.teamteamteam.spacescooter.entity.Player;
public class GameScreen extends Screen {
@ -22,6 +22,7 @@ public class GameScreen extends Screen {
public GameScreen(Screen parent) {
super(parent);
new ItemChance();
points.add(new Point(300,300));
points.add(new Point(600,100));
points.add(new Point(0,500));
@ -31,8 +32,6 @@ public class GameScreen extends Screen {
this.entities.add(new EnemyFour(800, 400, points));
this.entities.add(new EnemyThree(650, 300));
this.entities.add(new EnemyThree(450, 100));
this.entities.add(new EnemyTwo(750, 550));
this.entities.add(new EnemyTwo(150, 250));
}
@Override