core stuff almost done, now refactoring.

screensaver
marcus13345 2015-03-05 00:01:57 -05:00
parent 172ffbc4aa
commit 7aeab62b43
15 changed files with 292 additions and 416 deletions

1
.gitignore vendored
View File

@ -43,3 +43,4 @@ Icon
Network Trash Folder
Temporary Items
.apdisk
/bin/

View File

@ -6,7 +6,7 @@ import java.util.Random;
import MAndApps.apps.spacewars.tools.BasicTickAndRender;
public abstract class Entity extends BasicTickAndRender{
public abstract class Entity extends BasicTicAndRender{
private Rectangle boundingBox;
private Random r = new Random();
public Entity(int x, int y, int width, int height){
@ -30,4 +30,6 @@ public abstract class Entity extends BasicTickAndRender{
public Rectangle getBoundingBox(){
return boundingBox;
}
public abstract boolean isCollidable();
}

View File

@ -11,33 +11,26 @@ import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;
import javax.imageio.ImageIO;
import MAndApps.apps.spacewars.entity.Enemy;
import MAndApps.apps.spacewars.entity.Player;
import MAndApps.apps.spacewars.shop.Shop;
import MAndApps.apps.spacewars.tools.Explosion;
import MAndEngine.BasicApp;
import MAndEngine.Engine;
public class SpaceWars implements BasicApp {
//
private static boolean paused = false, debug = false;
private static int redPoints = 0, bluePoints = 0, greenPoints = 0, time = 0;
private static boolean debug = false;
private static final int WIDTH = 1024, HEIGHT = 600;
private static Player player = new Player();
private static Image background;
private static Random r = new Random();
private static Stack<Enemy> enemies = new Stack<Enemy>();
private static Stack<Explosion> explosions = new Stack<Explosion>();
private static Stack<String> logs = new Stack<String>();
private static int lvl = 1, xpToNextLVL = getMaxXPForLvl(lvl), xp = 0, expBar = 424;
private static boolean shopping = false;
private Shop shop = new Shop();
private static ArrayList<Enemy> entities = new ArrayList<Enemy>();
private static Player player = new Player();
private static ArrayList<Explosion> explosions = new ArrayList<Explosion>();
//
public static final Font defaultFont = new Font("Ubuntu", Font.BOLD, 10);
@ -47,70 +40,50 @@ public class SpaceWars implements BasicApp {
@Override
public void tick() {
addEXP(1);
if (!paused && !shopping) {
time++;
// ticks enemy stack
for (int i = 0; i < enemies.size(); i++)
enemies.elementAt(i).tick();
// tick explosions
for (int i = 0; i < explosions.size(); i++)
explosions.elementAt(i).tick();
// tick player class
player.tick();
Rectangle playerRect = player.getBoundingBox();
if (player.getAlive())
for (int i = 0; i < enemies.size(); i++)
if (enemies.elementAt(i).getBoundingBox().intersects(playerRect))
player.collideWithEnemy(enemies.elementAt(i).getX(), enemies.elementAt(i).getY());
int i = 0;
while (i < enemies.size()) {
if (!enemies.elementAt(i).getAlive()) {
/*
* BOOM( 75, 1.2,
* enemies.elementAt(i).getColor().getRed()-50,
* enemies.elementAt(i).getColor().getGreen()-50,
* enemies.elementAt(i).getColor().getBlue()-50, 50,
* (int)enemies.elementAt(i).getX(),
* (int)enemies.elementAt(i).getY(), 550, true, true, 10 );
*/
// ticks enemies
for (int i = 0; i < entities.size(); i++)
entities.get(i).tick();
// tick explosions
for (int i = 0; i < explosions.size(); i++)
explosions.get(i).tick();
// tick player object
Rectangle playerRect = player.getBoundingBox();
if (player.getAlive())
for (int i = 0; i < entities.size(); i++)
if (entities.get(i).isCollidable() && entities.get(i).getBoundingBox().intersects(playerRect))
player.collideWithEnemy(entities.get(i).getX(), entities.get(i).getY());
int i = 0;
while (i < entities.size()) {
if (!entities.get(i).getAlive()) {
BOOM( 75, 1.2, entities.get(i).getColor().getRed()-50,
entities.get(i).getColor().getGreen()-50,
entities.get(i).getColor().getBlue()-50, 50,
(int)entities.get(i).getX(),
(int)entities.get(i).getY(), 550, true, true, 10 );
entities.remove(i);
addRedPoints(enemies.elementAt(i).getColor().getRed());
addGreenPoints(enemies.elementAt(i).getColor().getGreen());
addBluePoints(enemies.elementAt(i).getColor().getBlue());
log("You gained " + enemies.elementAt(i).getWorth() + " exp.");
addEXP(enemies.elementAt(i).getWorth());
// enemies.remove(i);
} else
i++;
}
i = 0;
while (i < explosions.size()) {
if (!explosions.elementAt(i).getAlive()) {
explosions.remove(i);
} else {
i++;
}
}
expBar += ((int) (((double) xp / (double) xpToNextLVL) * 424) - expBar) / 10;
} else
i++;
}
i = 0;
while (i < explosions.size()) {
if (!explosions.get(i).getAlive()) {
explosions.remove(i);
} else {
i++;
}
}
shop.tick();
// check if there are any new items to log
int cap = logs.size();
for (int i = 0; i < cap; i++)
Engine.log(logs.pop());
}
public static void log(String s) {
logs.push(s);
}
public static Stack<Enemy> getEnemies() {
return enemies;
public static ArrayList<Enemy> getEnemies() {
return entities;
}
@Override
@ -118,44 +91,19 @@ public class SpaceWars implements BasicApp {
try {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setFont(defaultFont);
g.drawImage(background, 0, 0, WIDTH, HEIGHT, null);
for (int i = 0; i < explosions.size(); i++)
explosions.elementAt(i).render(g, i);
explosions.get(i).render(g, i);
player.render(g);
for (int i = 0; i < enemies.size(); i++)
enemies.elementAt(i).render(g);
for (int i = 0; i < entities.size(); i++)
entities.get(i).render(g);
// render points
g.setFont(moneyFont);
g.setColor(Color.RED);
g.drawString(redPointsToString(), 10, HEIGHT - 10 - (20 * 3));
g.setColor(Color.GREEN);
g.drawString(greenPointsToString(), 10, HEIGHT - 10 - (20 * 2));
g.setColor(Color.BLUE);
g.drawString(bluePointsToString(), 10, HEIGHT - 10 - (20 * 1));
g.setFont(defaultFont);
// render level and xp bar.
g.setFont(levelFont);
g.setColor(Color.WHITE);
g.drawString("" + lvl, 300 - ((("" + lvl).length() - 1) * 20), 40);
g.drawRect(330, 18, 424, 15);
g.fillRect(330, 18, expBar, 15);
g.setFont(defaultFont);
if (paused) {
g.setFont(pausedFont);
g.setColor(Color.WHITE);
g.drawString("Paused", 410, 280);
g.setFont(defaultFont);
}
shop.render(g, WIDTH);
} catch (Exception e) {
e.printStackTrace();
}
@ -187,7 +135,7 @@ public class SpaceWars implements BasicApp {
@Override
public void initialize() {
try {
background = ImageIO.read(new URL("http://wallpapersus.com/wallpapers/2012/10/Cool-Wave-600x1024.jpg"));
background = ImageIO.read(new URL("http://wallpaperswiki.org/wallpapers/2012/11/Wallpaper-Abstract-Wallpaper-Background-Texture-Texture-Yellow-Pictures-600x1024.jpg"));
} catch (Exception e) {
background = (Image) new BufferedImage(1024, 600, BufferedImage.TRANSLUCENT);
Graphics g = background.getGraphics();
@ -198,27 +146,14 @@ public class SpaceWars implements BasicApp {
@Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
for (int i = 0; i < enemies.size(); i++)
enemies.elementAt(i).keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE) {
paused = !paused;
} else if (e.getKeyCode() == KeyEvent.VK_E) {
xp = xpToNextLVL - 1;
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
debug = !debug;
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
shopping = !shopping;
shop.toggleState();
}
}
@Override
public void keyReleased(KeyEvent e) {
player.keyReleased(e);
for (int i = 0; i < enemies.size(); i++)
enemies.elementAt(i).keyReleased(e);
shop.keyReleased(e);
for (int i = 0; i < entities.size(); i++)
entities.get(i).keyReleased(e);
}
@Override
@ -242,83 +177,16 @@ public class SpaceWars implements BasicApp {
}
public static void BOOM(double speed, double decay, int r, int g, int b, int variant, int x, int y, int size, boolean singleVariant, boolean bubble, int sizeOfParticles) {
explosions.push(new Explosion(speed, decay, r, g, b, variant, singleVariant));
explosions.peek().goBoom(x, y, size, bubble, sizeOfParticles);
Explosion explosion = new Explosion(speed, decay, r, g, b, variant, singleVariant);
explosions.add(explosion);
explosion.goBoom(x, y, size, bubble, sizeOfParticles);
}
public void addRedPoints(int d) {
redPoints += d;
}
public void addGreenPoints(int d) {
greenPoints += d;
}
public void addBluePoints(int d) {
bluePoints += d;
}
private String redPointsToString() {
String _return = "$" + redPoints / 100d;
if (_return.length() == ("" + redPoints).length() + 1 || _return.length() == ("" + redPoints).length() + 3)
_return += "0";
return _return;
}
private String greenPointsToString() {
String _return = "$" + greenPoints / 100d;
if (_return.length() == ("" + greenPoints).length() + 1 || _return.length() == ("" + greenPoints).length() + 3)
_return += "0";
return _return;
}
private String bluePointsToString() {
String _return = "$" + bluePoints / 100d;
if (_return.length() == ("" + bluePoints).length() + 1 || _return.length() == ("" + bluePoints).length() + 3)
_return += "0";
return _return;
}
private static int getMaxXPForLvl(int lvl) {
return (int) (Math.pow(lvl, 1.618));
}
public void addEXP(int i) {
xp += i;
while (xp >= xpToNextLVL) {
xp -= xpToNextLVL;
lvl++;
xpToNextLVL = getMaxXPForLvl(lvl);
for (int j = 0; j < 424; j += 100) {
BOOM(75, 1.618, 255, 255, 255, 0, 300 + (j), 20, 200, true, false, 2);
}
}
}
// i got bored and made logic methods... so?
public boolean or(boolean a, boolean b) {
return a || b;
}
public boolean not(boolean a) {
return !a;
}
public boolean and(boolean a, boolean b) {
return not(or(not(a), not(b)));
}
public boolean xor(boolean a, boolean b) {
return not(or(not(or(not(a), not(b))), not(or(a, b))));
// return !((!((!a)||(!b)))||(!(a||b)));
// return or(not(or(not(a),b)),not(or(not(b),a)));
// return ((!(a||!(b)))||(!(b||!(a))));
}
@Override
public boolean visibleInMenu() {
// TODO Auto-generated method stub
return true;
}
@ -332,15 +200,11 @@ public class SpaceWars implements BasicApp {
@Override
public void resized(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void click() {
// TODO Auto-generated method stub
BOOM(75, 1.2, 100, 100, 100, 50, Engine.mouseX, Engine.mouseY, 550, true, true, 10);
}
}

View File

@ -55,4 +55,6 @@ public abstract class Enemy extends Entity {
}
public abstract void damage(int damage);
public abstract boolean isCollidable();
}

View File

@ -45,8 +45,8 @@ public class BasicPlayerBullet extends Bullet {
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
Rectangle r = getBoundingBox();
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
if (r.intersects(SpaceWars.getEnemies().elementAt(i).getBoundingBox())) {
SpaceWars.getEnemies().elementAt(i).damage(getDamage());
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
SpaceWars.getEnemies().get(i).damage(getDamage());
alive = false;
i = SpaceWars.getEnemies().size();
}
@ -81,5 +81,9 @@ public class BasicPlayerBullet extends Bullet {
public int getHEIGHT(){
return HEIGHT;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -60,8 +60,8 @@ public class PlayerExplodeBullet extends Bullet {
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
Rectangle r = getBoundingBox();
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
if (r.intersects(SpaceWars.getEnemies().elementAt(i).getBoundingBox())) {
SpaceWars.getEnemies().elementAt(i).damage(getDamage());
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
SpaceWars.getEnemies().get(i).damage(getDamage());
bulletExplosion.goBoom((int)x, (int)y, size * 10, false, size+2);
alive = false;
i = SpaceWars.getEnemies().size();
@ -91,4 +91,8 @@ public class PlayerExplodeBullet extends Bullet {
public int getDamage() {
return 1;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -55,8 +55,8 @@ public class PlayerImpactBullet extends Bullet {
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
Rectangle r = getBoundingBox();
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
if (r.intersects(SpaceWars.getEnemies().elementAt(i).getBoundingBox())) {
SpaceWars.getEnemies().elementAt(i).damage(getDamage());
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
SpaceWars.getEnemies().get(i).damage(getDamage());
alive = false;
i = SpaceWars.getEnemies().size();
}
@ -83,5 +83,8 @@ public class PlayerImpactBullet extends Bullet {
public int getDamage() {
return 1;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -62,9 +62,9 @@ public class PlayerPiercingBullet extends Bullet {
}
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
for (int i = 0; i < SpaceWars.getEnemies().size() && hits < MAX_HITS; i++) {
if (SpaceWars.getEnemies().elementAt(i).getBoundingBox().intersectsLine((int)x, (int)y, (int)oldX, (int)oldY)) {
if (SpaceWars.getEnemies().get(i).getBoundingBox().intersectsLine((int)x, (int)y, (int)oldX, (int)oldY)) {
hits++;
SpaceWars.getEnemies().elementAt(i).damage(getDamage());
SpaceWars.getEnemies().get(i).damage(getDamage());
if(hits >= MAX_HITS){
alive = false;
i = SpaceWars.getEnemies().size();
@ -94,4 +94,8 @@ public class PlayerPiercingBullet extends Bullet {
public int getDamage(){
return 2;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -174,4 +174,8 @@ public class BlueEnemy extends Enemy {
public int getWorth(){
return r.nextInt(3)+1;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -8,176 +8,181 @@ import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Enemy;
public class GreenEnemy extends Enemy {
private int health = 2;
private final int MAX_HEALTH = health;
private static final int WIDTH = 16, HEIGHT = 16, PROXIMITY = 200;
private double x, y, time = 0, desiredX, desiredY, Xmod, Ymod, dx = 0,
dy = 0;
private static final double ACC = 0.005, MAXSPEED = 1, DEAD_ACC = 0.5d,
DEAD_MAXSPEED = 5;
private Color color;
private boolean debug = false, alive = true;
private double healthBar = 1;
private int health = 2;
private final int MAX_HEALTH = health;
private static final int WIDTH = 16, HEIGHT = 16, PROXIMITY = 200;
private double x, y, time = 0, desiredX, desiredY, Xmod, Ymod, dx = 0, dy = 0;
private static final double ACC = 0.005, MAXSPEED = 1, DEAD_ACC = 0.5d, DEAD_MAXSPEED = 5;
private Color color;
private boolean debug = false, alive = true;
private double healthBar = 1;
public GreenEnemy(int x, int y, int i) {
super(x, y, WIDTH, HEIGHT);
this.x = x;
this.y = y;
this.color = new Color(rand(50, 75), 255, rand(50, 75));
public GreenEnemy(int x, int y, int i) {
super(x, y, WIDTH, HEIGHT);
this.x = x;
this.y = y;
this.color = new Color(rand(50, 75), 255, rand(50, 75));
}
}
@Override
public int tick() {
// epic AI
if((int)healthBar <= 0){
alive = false;
}
if (alive) {
//UIFM
if (SpaceWars.getPlayer().getAlive()) {
if (time > 0.4d) {
time = 0;
Xmod = rand(-PROXIMITY, PROXIMITY);
Ymod = rand(-PROXIMITY, PROXIMITY);
}
@Override
public int tick() {
// epic AI
if ((int) healthBar <= 0) {
alive = false;
}
if (alive) {
// UIFM
if (SpaceWars.getPlayer().getAlive()) {
if (time > 0.4d) {
time = 0;
Xmod = rand(-PROXIMITY, PROXIMITY);
Ymod = rand(-PROXIMITY, PROXIMITY);
}
desiredX = SpaceWars.getPlayer().getX() + Xmod;
desiredY = SpaceWars.getPlayer().getY() + Ymod;
desiredX = SpaceWars.getPlayer().getX() + Xmod;
desiredY = SpaceWars.getPlayer().getY() + Ymod;
//UIFM
if ((int) desiredX > (int) x)
dx += ACC;
else if ((int) desiredX < (int) x) {
dx -= ACC;
}
if ((int) desiredY > (int) y)
dy += ACC;
else if ((int) desiredY < (int) y) {
dy -= ACC;
}
// UIFM
if ((int) desiredX > (int) x)
dx += ACC;
else if ((int) desiredX < (int) x) {
dx -= ACC;
}
if ((int) desiredY > (int) y)
dy += ACC;
else if ((int) desiredY < (int) y) {
dy -= ACC;
}
//UIFM
if (dx > MAXSPEED)
while (dx > MAXSPEED)
dx -= ACC;
if (dx < 0 - MAXSPEED)
while (dx < 0 - MAXSPEED)
dx += ACC;
if (dy > MAXSPEED)
while (dy > MAXSPEED)
dy -= ACC;
if (dy < 0 - MAXSPEED)
while (dy < 0 - MAXSPEED)
dy += ACC;
// UIFM
if (dx > MAXSPEED)
while (dx > MAXSPEED)
dx -= ACC;
if (dx < 0 - MAXSPEED)
while (dx < 0 - MAXSPEED)
dx += ACC;
if (dy > MAXSPEED)
while (dy > MAXSPEED)
dy -= ACC;
if (dy < 0 - MAXSPEED)
while (dy < 0 - MAXSPEED)
dy += ACC;
} else {
//UIFM
if (time > 0.4d) {
time = 0;
Xmod = rand(0, 1024);
Ymod = rand(0, 200);
}
} else {
// UIFM
if (time > 0.4d) {
time = 0;
Xmod = rand(0, 1024);
Ymod = rand(0, 200);
}
desiredX = Xmod;
desiredY = Ymod;
desiredX = Xmod;
desiredY = Ymod;
if ((int) desiredX > (int) x)
dx += DEAD_ACC;
else if ((int) desiredX < (int) x) {
dx -= DEAD_ACC;
}
if ((int) desiredY > (int) y)
dy += DEAD_ACC;
else if ((int) desiredY < (int) y) {
dy -= DEAD_ACC;
}
if ((int) desiredX > (int) x)
dx += DEAD_ACC;
else if ((int) desiredX < (int) x) {
dx -= DEAD_ACC;
}
if ((int) desiredY > (int) y)
dy += DEAD_ACC;
else if ((int) desiredY < (int) y) {
dy -= DEAD_ACC;
}
if (dx > DEAD_MAXSPEED)
while (dx > DEAD_MAXSPEED)
dx -= DEAD_ACC;
if (dx < 0 - DEAD_MAXSPEED)
while (dx < 0 - DEAD_MAXSPEED)
dx += DEAD_ACC;
if (dy > DEAD_MAXSPEED)
while (dy > DEAD_MAXSPEED)
dy -= DEAD_ACC;
if (dy < 0 - DEAD_MAXSPEED)
while (dy < 0 - DEAD_MAXSPEED)
dy += DEAD_ACC;
if (dx > DEAD_MAXSPEED)
while (dx > DEAD_MAXSPEED)
dx -= DEAD_ACC;
if (dx < 0 - DEAD_MAXSPEED)
while (dx < 0 - DEAD_MAXSPEED)
dx += DEAD_ACC;
if (dy > DEAD_MAXSPEED)
while (dy > DEAD_MAXSPEED)
dy -= DEAD_ACC;
if (dy < 0 - DEAD_MAXSPEED)
while (dy < 0 - DEAD_MAXSPEED)
dy += DEAD_ACC;
//UIFMMMMMMMMM
}
// UIFMMMMMMMMM
}
//UIFMMMMM
x += dx;
y += dy;
// UIFMMMMM
x += dx;
y += dy;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
x--;
if (x < 0)
while (x < 0)
x++;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
x--;
if (x < 0)
while (x < 0)
x++;
//UIFM UNIDENTIFIED FLYING MATH
if (y > SpaceWars.getHEIGHT() - HEIGHT)
while (y > SpaceWars.getHEIGHT() - HEIGHT)
y--;
if (y < 0)
while (y < 0)
y++;
}
time += 0.01;
absoluteTime++;
updateBoundingBox((int) x, (int) y, 16, 16);
return 0;
}
// UIFM UNIDENTIFIED FLYING MATH
if (y > SpaceWars.getHEIGHT() - HEIGHT)
while (y > SpaceWars.getHEIGHT() - HEIGHT)
y--;
if (y < 0)
while (y < 0)
y++;
}
time += 0.01;
absoluteTime++;
updateBoundingBox((int) x, (int) y, 16, 16);
return 0;
}
private Random r = new Random();
private int absoluteTime = 0;
private Random r = new Random();
private int absoluteTime = 0;
@Override
public void render(Graphics g) {
g.setColor(color);
int temp;
try{
temp = r.nextInt((int)(0-((double)absoluteTime/20d))+5);
}catch(Exception e){
temp = 0;
}
@Override
public void render(Graphics g) {
g.setColor(color);
int temp;
try {
temp = r.nextInt((int) (0 - ((double) absoluteTime / 20d)) + 5);
} catch (Exception e) {
temp = 0;
}
//uifm
if (temp == 0) if (alive) g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
if (debug) g.drawLine((int) x, (int) y, (int) desiredX, (int) desiredY);
// uifm
if (temp == 0)
if (alive)
g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
if (debug)
g.drawLine((int) x, (int) y, (int) desiredX, (int) desiredY);
healthBar += ((((double)health/(double)MAX_HEALTH)*16) - healthBar)/6;
//healthbar
g.setColor(Color.RED);
g.fillRect((int)x, (int)y, WIDTH - 1, 3);
g.setColor(Color.GREEN);
g.fillRect((int)x, (int)y, (int)healthBar, 3);
g.setColor(Color.BLACK);
g.drawRect((int)x, (int)y, WIDTH - 1, 3);
}
healthBar += ((((double) health / (double) MAX_HEALTH) * 16) - healthBar) / 6;
// healthbar
g.setColor(Color.RED);
g.fillRect((int) x, (int) y, WIDTH - 1, 3);
g.setColor(Color.GREEN);
g.fillRect((int) x, (int) y, (int) healthBar, 3);
g.setColor(Color.BLACK);
g.drawRect((int) x, (int) y, WIDTH - 1, 3);
}
@Override
public void damage(int i) {
health -= i;
}
@Override
public void damage(int i) {
health -= i;
}
@Override
public boolean getAlive() {
return alive;
}
@Override
public boolean getAlive() {
return alive;
}
@Override
public Color getColor() {
return color;
}
@Override
public Color getColor() {
return color;
}
@Override
public int getWorth(){
return r.nextInt(3)+1;
}
@Override
public int getWorth() {
return r.nextInt(3) + 1;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -11,10 +11,8 @@ public class NormalEnemy extends Enemy {
private int health = 2;
private final int MAX_HEALTH = health;
private static final int WIDTH = 16, HEIGHT = 16, PROXIMITY = 200;
private double x, y, time = 0, desiredX, desiredY, Xmod, Ymod, dx = 0,
dy = 0;
private static final double ACC = 0.005, MAXSPEED = 1, DEAD_ACC = 0.5d,
DEAD_MAXSPEED = 5;
private double x, y, time = 0, desiredX, desiredY, Xmod, Ymod, dx = 0, dy = 0;
private static final double ACC = 0.005, MAXSPEED = 1, DEAD_ACC = 0.5d, DEAD_MAXSPEED = 5;
private Color color;
private boolean debug = false, alive = true;
private double healthBar = 1;
@ -25,13 +23,13 @@ public class NormalEnemy extends Enemy {
this.y = y;
final int LOW = 200, HIGH = 256, color = rand(LOW, HIGH);
this.color = new Color(color, color, color);
}
@Override
@Override
public int tick() {
// epic AI
if((int)healthBar <= 0){
if ((int) healthBar <= 0) {
alive = false;
}
if (alive) {
@ -131,28 +129,31 @@ public class NormalEnemy extends Enemy {
private Random r = new Random();
private int absoluteTime = 0;
@Override
public void render(Graphics g) {
g.setColor(color);
int temp;
try{
temp = r.nextInt((int)(0-((double)absoluteTime/20d))+5);
}catch(Exception e){
try {
temp = r.nextInt((int) (0 - ((double) absoluteTime / 20d)) + 5);
} catch (Exception e) {
temp = 0;
}
if (temp == 0) if (alive) g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
if (debug) g.drawLine((int) x, (int) y, (int) desiredX, (int) desiredY);
healthBar += ((((double)health/(double)MAX_HEALTH)*16) - healthBar)/6;
//healthbar
if (temp == 0)
if (alive)
g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
if (debug)
g.drawLine((int) x, (int) y, (int) desiredX, (int) desiredY);
healthBar += ((((double) health / (double) MAX_HEALTH) * 16) - healthBar) / 6;
// healthbar
g.setColor(Color.RED);
g.fillRect((int)x, (int)y, WIDTH - 1, 3);
g.fillRect((int) x, (int) y, WIDTH - 1, 3);
g.setColor(Color.GREEN);
g.fillRect((int)x, (int)y, (int)healthBar, 3);
g.fillRect((int) x, (int) y, (int) healthBar, 3);
g.setColor(Color.BLACK);
g.drawRect((int)x, (int)y, WIDTH - 1, 3);
g.drawRect((int) x, (int) y, WIDTH - 1, 3);
}
@Override
@ -169,9 +170,13 @@ public class NormalEnemy extends Enemy {
public Color getColor() {
return color;
}
@Override
public int getWorth(){
return r.nextInt(3)+1;
public int getWorth() {
return r.nextInt(3) + 1;
}
public boolean isCollidable() {
return true;
}
}

View File

@ -171,4 +171,9 @@ public class RedEnemy extends Enemy {
public int getWorth(){
return (int)(Math.random() * 3)+1;
}
@Override
public boolean isCollidable() {
return true;
}
}

View File

@ -89,8 +89,8 @@ public class CollisionParticle extends Entity {
if(active){
Rectangle r = getBoundingBox();
for (int i = 0; i < SpaceWars.getEnemies().size(); i++) {
if (r.intersects(SpaceWars.getEnemies().elementAt(i).getBoundingBox())) {
SpaceWars.getEnemies().elementAt(i).damage(damage);
if (r.intersects(SpaceWars.getEnemies().get(i).getBoundingBox())) {
SpaceWars.getEnemies().get(i).damage(damage);
//active = false;
i = SpaceWars.getEnemies().size();
}

View File

@ -1,32 +0,0 @@
package MAndApps.apps.spacewars.tools;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
public abstract class Entity extends BasicTickAndRender{
private Rectangle boundingBox;
private Random r = new Random();
public Entity(int x, int y, int width, int height){
boundingBox = new Rectangle(x, y, width, height);
}
public void updateBoundingBox(int x, int y, int width, int height){
boundingBox.setBounds(x, y, width, height);
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public final double getX(){
return boundingBox.getX();
}
public final double getY(){
return boundingBox.getY();
}
public int rand(int i, int j){
return r.nextInt(j-i) + i;
}
public Rectangle getBoundingBox(){
return boundingBox;
}
}

View File

@ -98,4 +98,9 @@ public class Particle extends Entity{
public boolean getAlive() {
return alive;
}
@Override
public boolean isCollidable() {
return false;
}
}