conflicts?

screensaver
Marcus Gosselin 2015-04-21 10:56:03 -04:00
parent 083bfa11ce
commit cdc6a2d51f
18 changed files with 451 additions and 848 deletions

BIN
res/background.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 MiB

View File

@ -4,7 +4,7 @@ import java.awt.event.KeyEvent;
public abstract class Entity extends BasicTickAndRender{
protected double x, y, dx, dy;
protected double x, y, dx, dy, width, height;
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
@ -14,6 +14,12 @@ public abstract class Entity extends BasicTickAndRender{
public final double getY(){
return y;
}
public final double getWidth() {
return width;
}
public final double getHeight() {
return height;
}
public abstract boolean isCollidable();

View File

@ -6,38 +6,53 @@ import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
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 javax.imageio.ImageIO;
import MAndApps.apps.spacewars.entity.Enemy;
import MAndApps.apps.spacewars.entity.Player;
import MAndApps.apps.spacewars.entity.enemy.NormalEnemy;
import MAndApps.apps.spacewars.entity.enemy.RedEnemy;
import MAndApps.apps.spacewars.tools.Explosion;
import MAndEngine.BasicApp;
import MAndEngine.Engine;
import MAndEngine.ImageCreator;
/**
* main basicapp class that takes care of managing the abstract concepts of the game.
* like the shop, the player's level and experience, what enemies and explosion particles
* are laying around.
*
* this is somewhat old architecture and some half finished new architecture can be found
* in the screensaver branch as i plan to make this both a game and a screen saver with
* a player AI. as well, some of these concepts will be ported over to mand engine
* once they are abstracted a little better.
*
* @author mgosselin
*
*/
public class SpaceWars implements BasicApp {
//
private static boolean debug = false;
private static final int WIDTH = 1024, HEIGHT = 600;
public static boolean debug = false;
private static final int ORIGINAL_WIDTH = 1024, ORIGINAL_HEIGHT = 600;
private static Image background;
private static ArrayList<Entity> entities = new ArrayList<Entity>();
private static Player player = new Player();
private static Player player;
//
public static final Font defaultFont = new Font("Ubuntu", Font.BOLD, 10);
public static final Font moneyFont = new Font("Ubuntu", Font.BOLD, 20);
public static final Font levelFont = new Font("Ubuntu", Font.BOLD, 40);
public static final Font pausedFont = new Font("Ubuntu", Font.BOLD, 60);
private boolean paused;
private static int WIDTH = ORIGINAL_WIDTH;
private static int HEIGHT = ORIGINAL_HEIGHT;
public static int scale;
@Override
public void tick() {
@ -45,7 +60,7 @@ public class SpaceWars implements BasicApp {
// ticks enemies
for (int i = 0; i < entities.size(); i++)
entities.get(i).tick();
// check dem collisions yo
for (int i = 0; i < entities.size(); i++) {
Entity e1 = entities.get(i);
@ -55,9 +70,23 @@ public class SpaceWars implements BasicApp {
if (e2.isCollidable()) {
// because efficiency.
if(
//x width2 collide
(((e1.getX() > e2.getX()) & (e1.getX() < e2.getX() + e2.getWidth())) ||
//x2 width collide
((e2.getX() > e1.getX()) & (e2.getX() < e1.getX() + e1.getWidth()))) &&
//y height2 collide
(((e1.getY() > e2.getY()) & (e1.getY() < e2.getY() + e2.getHeight())) ||
//y2 height collide
((e2.getY() > e1.getY()) & (e2.getY() < e1.getY() + e1.getHeight())))
) {
e1.collidedWith(e2);
e2.collidedWith(e1);
}
}
}
}
@ -81,18 +110,32 @@ public class SpaceWars implements BasicApp {
@Override
public void render(Graphics2D g) {
try {
g.drawImage(background, 0, 0, null);
try {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(defaultFont);
g.drawImage(background, 0, 0, WIDTH, HEIGHT, null);
for (int i = 0; i < entities.size(); i++)
entities.get(i).render(g);
// render level and xp bar.
g.setFont(levelFont);
g.setColor(Color.WHITE);
g.setFont(defaultFont);
if (paused) {
g.setFont(pausedFont);
g.setColor(Color.WHITE);
g.drawString("Paused", 410, 280);
g.setFont(defaultFont);
}
} catch (Exception e) {
e.printStackTrace();
}
@ -118,30 +161,35 @@ public class SpaceWars implements BasicApp {
@Override
public Dimension getResolution() {
return new Dimension(WIDTH, HEIGHT);
return new Dimension(ORIGINAL_WIDTH, ORIGINAL_HEIGHT);
}
@Override
public void initialize() {
try {
//background = ImageIO.read(new URL("http://wallpaperswiki.org/2012/11/Wallpaper-Abstract-Wallpaper-Background-Texture-Texture-Yellow-Pictures-600x1024.jpg"));
background = ImageCreator.colorNoise(Color.WHITE, .4, .6, WIDTH, HEIGHT);
player = new Player();
entities.add(player);
for (int i = 0; i < 100; i++)
entities.add(new NormalEnemy(0, 0));
for(int i = 0; i < 100; i ++)
entities.add(new NormalEnemy());
Engine.timeScale = 60d / (1000d * 1000d);
background = ImageCreator.colorNoise(Color.WHITE, .4, .6, WIDTH, HEIGHT );
} catch (Exception e) {
background = (Image) new BufferedImage(1024, 600, BufferedImage.TRANSLUCENT);
Graphics g = background.getGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, 1024, 600);
}
}
@Override
public void keyPressed(KeyEvent e) {
for (int i = 0; i < entities.size(); i++)
entities.get(i).keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE) {
paused = !paused;
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
System.out.println("YOEIRGSODBH");
debug = !debug;
}
}
@Override
@ -161,11 +209,6 @@ public class SpaceWars implements BasicApp {
return new Color(88, 128, 255);
}
@Override
public int getFramerate() {
return 50;
}
@Override
public boolean getResizable() {
return false;
@ -174,7 +217,6 @@ 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) {
Explosion explosion = new Explosion(speed, decay, r, g, b, variant, singleVariant);
entities.add((Entity) explosion);
explosion.goBoom(x, y, size, bubble, sizeOfParticles);
@ -202,4 +244,11 @@ public class SpaceWars implements BasicApp {
public void click() {
}
@Override
public void updateDimensions(int width, int height) {
scale = ((int)(Math.sqrt(width*width + height*height)))/((int)(Math.sqrt(WIDTH*WIDTH + HEIGHT*HEIGHT)));
WIDTH = width;
HEIGHT = height;
}
}

View File

@ -92,25 +92,25 @@ public abstract class Bullet extends Entity {
case BASIC:
return new BasicPlayerBullet(direction, x, y);
case PLAYER_PIERCE_ONE:
return new PlayerPiercingBullet(direction, x, y, 2, false);
return new PlayerPiercingBullet(direction, x, y, 2);
case PLAYER_PIERCE_TWO:
return new PlayerPiercingBullet(direction, x, y, 3, false);
return new PlayerPiercingBullet(direction, x, y, 3);
case PLAYER_PIERCE_THREE:
return new PlayerPiercingBullet(direction, x, y, 4, false);
return new PlayerPiercingBullet(direction, x, y, 4);
case PLAYER_PIERCE_FOUR:
return new PlayerPiercingBullet(direction, x, y, 5, false);
return new PlayerPiercingBullet(direction, x, y, 5);
case PLAYER_PIERCE_FIVE:
return new PlayerPiercingBullet(direction, x, y, 6, false);
return new PlayerPiercingBullet(direction, x, y, 6);
case PLAYER_PIERCE_SIX:
return new PlayerPiercingBullet(direction, x, y, 7, false);
return new PlayerPiercingBullet(direction, x, y, 7);
case PLAYER_PIERCE_SEVEN:
return new PlayerPiercingBullet(direction, x, y, 8, false);
return new PlayerPiercingBullet(direction, x, y, 8);
case PLAYER_PIERCE_EIGHT:
return new PlayerPiercingBullet(direction, x, y, 9, false);
return new PlayerPiercingBullet(direction, x, y, 9);
case PLAYER_PIERCE_NINE:
return new PlayerPiercingBullet(direction, x, y, 10, false);
return new PlayerPiercingBullet(direction, x, y, 10);
case PLAYER_PIERCE_TEN:
return new PlayerPiercingBullet(direction, x, y, 15, true);
return new PlayerPiercingBullet(direction, x, y, 15);
case PLAYER_IMPACT_ONE:
return new PlayerImpactBullet(direction, x, y, 1);
case PLAYER_IMPACT_TWO:

View File

@ -2,55 +2,9 @@ package MAndApps.apps.spacewars.entity;
import java.awt.Color;
import MAndApps.apps.spacewars.entity.enemy.BlueEnemy;
import MAndApps.apps.spacewars.entity.enemy.GreenEnemy;
import MAndApps.apps.spacewars.entity.enemy.NormalEnemy;
import MAndApps.apps.spacewars.entity.enemy.RedEnemy;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
public abstract class Enemy extends Entity {
public static final int NORMAL = 0;
public static final int
RED_TIER_ONE = 1,
RED_TIER_TWO = 2,
RED_TIER_THREE = 3,
RED_TIER_FOUR = 4,
RED_TIER_FIVE = 5;
public static final int
BLUE_TIER_ONE = 6,
BLUE_TIER_TWO = 7,
BLUE_TIER_THREE = 8,
BLUE_TIER_FOUR = 9,
BLUE_TIER_FIVE = 10;
public static final int
GREEN_TIER_ONE = 11,
GREEN_TIER_TWO = 12,
GREEN_TIER_THREE = 13,
GREEN_TIER_FOUR = 14,
GREEN_TIER_FIVE = 15;
public Enemy(int x, int y, int width, int height) {
super.x = x;
super.y = y;
super.dx = dx;
super.dy = dy;
}
public final static Enemy getNewEnemy(int type, int x, int y){
switch(type){
case NORMAL:
return new NormalEnemy(x, y);
case BLUE_TIER_ONE:
return new BlueEnemy(x, y, 2);
case RED_TIER_ONE:
return new RedEnemy(x, y, 2);
case GREEN_TIER_ONE:
return new GreenEnemy(x, y, 2);
default:
return new NormalEnemy(x, y);
}
}
public abstract boolean getAlive();
public abstract Color getColor();

View File

@ -0,0 +1,121 @@
package MAndApps.apps.spacewars.entity;
import static MAndEngine.Utils.rand;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
import MAndEngine.Engine;
public class Particle extends Entity{
private double x, y, life, speed;
private final double angleDeg, moveLife, renderLife;
private final int size;
private final double DECAY;
private boolean alive = false, bubble;
private Random rand = new Random();
private Color color;
public Particle(int angle, double speed, int movelife, int renderlife,
int x, int y, double speedDecay, int r, int g, int b, int variant,
boolean singleVariant, boolean bubble, int sizeOfParticles) {
renderLife = renderlife;
this.speed = speed;
angleDeg = angle;
this.x = x;
moveLife = movelife;
this.y = y;
alive = true;
size = rand.nextInt(sizeOfParticles);
DECAY = speedDecay;
int i = 0;
this.bubble = bubble;
if (!bubble) {
if (!singleVariant) {
while (i < 10) {
try {
color = new Color(r + rand.nextInt(variant * 2)
- variant, g + rand.nextInt(variant * 2)
- variant, b + rand.nextInt(variant * 2)
- variant);
i = 10;
} catch (IllegalArgumentException e) {
i++;
if (i >= 10) {
color = new Color(r, g, b);
}
}
}
} else {
while (i < 10) {
try {
int tempVar = rand.nextInt(variant * 2) - variant;
color = new Color(r + tempVar, g + tempVar, b + tempVar);
i = 10;
} catch (IllegalArgumentException e) {
i++;
if (i >= 10) {
color = new Color(r, g, b);
}
}
}
}
} else {
color = new Color(rand(0, 255), rand(0, 255), rand(0, 255));
}
}
private static final double PHI = 1.618033988749894848204586;
public int tick() {
if (life < moveLife) {
x += Math.cos(((double) angleDeg * Math.PI) / 180d) * speed;
y += Math.sin(((double) angleDeg * Math.PI) / 180d) * speed;
}
speed /= PHI;
if (life > renderLife)
alive = false;
if (x > SpaceWars.getWIDTH() || x < 0 - size
|| y > SpaceWars.getHEIGHT() || y < 0 - size) {
alive = false;
}
life+= Engine.deltaTime / 2d;
return 0;
}
public void render(Graphics g) {
if (alive) {
g.setColor(color);
if(bubble) g.drawOval((int) x, (int) y, size, size);
else g.fillRect((int) x, (int) y, size, size);
}
}
public boolean getAlive() {
return alive;
}
@Override
public boolean isCollidable() {
// TODO Auto-generated method stub
return false;
}
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}
}

View File

@ -3,49 +3,62 @@ package MAndApps.apps.spacewars.entity;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.Random;
import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.gun.Gun;
import MAndApps.apps.spacewars.tools.Direction;
import MAndApps.apps.spacewars.Entity;
import static MAndEngine.Utils.rand;
import MAndEngine.Engine;
public class Player extends Entity {
private final static int WIDTH = 16, HEIGHT = 16;
private static final double ACC = 0.5, MAXSPEED = 5;
private boolean A = false, S = false, D = false, W = false, alive = true;
private Gun gun = new Gun(Bullet.BASIC, 25, (int)x, (int)y);
private boolean alive = false;
private Gun gun = new Gun(Bullet.BASIC, 25, (int) x, (int) y);
/**
* go boom is a callback when it dies because it dies on tick i assume?
* TODO fix that so no die on tick.
* go boom is a callback when it dies because it dies on tick i assume? TODO
* fix that so no die on tick.
*/
private boolean goBoom = false;
public Player() {
x = 512;
y = 550;
reset();
width = 16 * SpaceWars.scale;
height = 16 * SpaceWars.scale;
}
private void reset() {
x = SpaceWars.getWIDTH() / 2 - width / 2;
y = SpaceWars.getHEIGHT() - (50 * SpaceWars.scale);
dy = -10;
dx = 0;
timer = 0;
time = 5;
}
@Override
public int tick() {
double ACC = Player.ACC * Engine.deltaTime / 2d;
if (goBoom) {
SpaceWars.BOOM(50, 1.2, 50, 50, 50, 30, (int) x, (int) y, 550, true,
false, 3);
SpaceWars.BOOM(50, 1.2, 50, 50, 50, 30, (int)(x + width/2), (int)(y + height/2), 550,
true, false, 3);
goBoom = false;
}
gun.tick();
if (alive) {
if (time != 1)
time -= 0.05d;
time -= 0.05d * Engine.deltaTime;
if (time < 1)
time = 1;
if (D && !A)
if (x > SpaceWars.getWIDTH() - width) {
dx -= ACC;
} else if (x < 0) {
dx += ACC;
else if (A && !D) {
} else if ((Engine.keys[KeyEvent.VK_D] && !Engine.keys[KeyEvent.VK_A]))
dx += ACC;
else if ((Engine.keys[KeyEvent.VK_A] && !Engine.keys[KeyEvent.VK_D])) {
dx -= ACC;
} else {
if ((int) (dx * 100) > 0)
@ -56,9 +69,14 @@ public class Player extends Entity {
dx = 0;
}
if (S && !W)
if (y > SpaceWars.getHEIGHT() - height) {
dy -= ACC;
} else if (y < 0) {
dy += ACC;
else if (W && !S) {
} else if (Engine.keys[KeyEvent.VK_S]
&& !Engine.keys[KeyEvent.VK_W])
dy += ACC;
else if (Engine.keys[KeyEvent.VK_W] && !Engine.keys[KeyEvent.VK_S]) {
dy -= ACC;
} else {
if ((int) (dy * 100) > 0)
@ -70,63 +88,39 @@ public class Player extends Entity {
}
if (dx > MAXSPEED)
while (dx > MAXSPEED)
dx -= ACC;
dx -= ACC;
if (dx < 0 - MAXSPEED)
while (dx < 0 - MAXSPEED)
dx += ACC;
dx += ACC;
if (dy > MAXSPEED)
while (dy > MAXSPEED)
dy -= ACC;
dy -= ACC;
if (dy < 0 - MAXSPEED)
while (dy < 0 - MAXSPEED)
dy += ACC;
dy += ACC;
y += dy;
x += dx;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
x--;
if (x < 0)
while (x < 0)
x++;
if (y > SpaceWars.getHEIGHT() - HEIGHT)
while (y > SpaceWars.getHEIGHT() - HEIGHT)
y--;
if (y < 0)
while (y < 0)
y++;
y += dy * Engine.deltaTime;
x += dx * Engine.deltaTime;
} else {
x = -1;
y = -1;
if (timer > 4 * 50) {
if (timer > 4 * 50)
alive = true;
x = 512;
y = 550;
}
timer++;
else
timer += 0.75d * Engine.deltaTime;
}
return 0;
}
private int timer = 0;
private double timer = 0;
@Override
public void render(Graphics g) {
g.setColor(Color.BLACK);
if (alive) {
int temp;
try {
temp = rand(0, (int)time);
} catch (Exception e) {
temp = 1;
}
if (temp == 0)
g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
int temp = rand(1, (int) time);
if (temp == 1)
g.fillRect((int) x, (int) y, (int) width, (int) height);
}
gun.render(g);
}
@ -134,27 +128,34 @@ public class Player extends Entity {
private double time = 0;
public boolean getAlive() {
return alive;
return true;
}
@Override
public boolean isCollidable() {
return true;
public boolean isRespawning() {
return !alive;
}
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
if(e instanceof Enemy) {
if (alive && e instanceof Enemy) {
alive = false;
goBoom = true;
timer = 0;
alive = false;
time = 5;
reset();
}
}
@Override
public boolean isCollidable() {
// TODO Auto-generated method stub
return true;
}
}

View File

@ -11,9 +11,7 @@ import MAndApps.apps.spacewars.tools.Direction;
public class BasicPlayerBullet extends Bullet {
private final int direction;
private final int WIDTH, HEIGHT;
private final static int SPEED = 10;
private double x, y;
private boolean alive = true;
@ -23,13 +21,12 @@ public class BasicPlayerBullet extends Bullet {
this.y = y;
this.direction = direction;
if (direction == Direction.UP || direction == Direction.DOWN) {
WIDTH = 3;
HEIGHT = 8;
width = 3;
height = 8;
} else {
WIDTH = 8;
HEIGHT = 3;
width = 8;
height = 3;
}
updateBoundingBox((int) this.x, (int) this.y, WIDTH, HEIGHT);
}
public int tick() {
@ -45,13 +42,7 @@ public class BasicPlayerBullet extends Bullet {
}
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
Rectangle r = getBoundingBox();
//if out of bound
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
alive = false;
}
}
return 0;
}
@ -59,7 +50,7 @@ public class BasicPlayerBullet extends Bullet {
public void render(Graphics g) {
g.setColor(Color.BLACK);
if (alive)
g.fillRect((int) x, (int) y, WIDTH, HEIGHT);
g.fillRect((int) x, (int) y, (int)width, (int)height);
}
@Override
@ -71,14 +62,6 @@ public class BasicPlayerBullet extends Bullet {
public int getDamage() {
return 1;
}
public int getWIDTH(){
return WIDTH;
}
public int getHEIGHT(){
return HEIGHT;
}
public boolean isCollidable() {
return true;
@ -86,13 +69,11 @@ public class BasicPlayerBullet extends Bullet {
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}

View File

@ -37,7 +37,6 @@ public class PlayerImpactBullet extends Bullet {
WIDTH = 8;
HEIGHT = 3;
}
updateBoundingBox((int) this.x, (int) this.y, WIDTH, HEIGHT);
this.level = level;
}
@ -52,9 +51,6 @@ public class PlayerImpactBullet extends Bullet {
} else if (direction == Direction.RIGHT) {
x += SPEED;
}
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
Rectangle r = getBoundingBox();
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
alive = false;

View File

@ -8,10 +8,13 @@ import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Bullet;
import MAndApps.apps.spacewars.tools.Direction;
import MAndEngine.Engine;
public class PlayerPiercingBullet extends Bullet {
private final int direction, MAX_HITS;
private final Direction direction;
private final int MAX_HITS;
private final int WIDTH, HEIGHT;
private final static int SPEED = 10;
private final static double SPEED = .01;
private double x, y, oldX, oldY;
private boolean alive = true;
private int hits = 0;
@ -26,7 +29,7 @@ public class PlayerPiercingBullet extends Bullet {
return HEIGHT;
}
public PlayerPiercingBullet(int direction, int x, int y, int pierce, boolean b) {
public PlayerPiercingBullet(Direction direction, int x, int y, int pierce) {
super(x, y, 1, 1);
this.x = x;
this.y = y;
@ -39,29 +42,15 @@ public class PlayerPiercingBullet extends Bullet {
WIDTH = 8;
HEIGHT = 3;
}
updateBoundingBox((int) this.x, (int) this.y, WIDTH, HEIGHT);
INFINISHOT = b;
INFINISHOT = pierce == -1;
}
public int tick() {
oldX = x;
oldY = y;
if (alive) {
switch(direction){
case Direction.UP:
y -= SPEED;
break;
case Direction.DOWN:
y += SPEED;
break;
case Direction.LEFT:
x -= SPEED;
break;
case Direction.RIGHT:
x += SPEED;
break;
}
updateBoundingBox((int) x, (int) y, WIDTH, HEIGHT);
y += SPEED * direction.getY() * Engine.deltaTime;
x += SPEED * direction.getX() * Engine.deltaTime;
if(x > SpaceWars.getWIDTH() || x < 0 - WIDTH || y > SpaceWars.getHEIGHT() || y < 0 - HEIGHT){
alive = false;

View File

@ -1,189 +0,0 @@
package MAndApps.apps.spacewars.entity.enemy;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Enemy;
public class BlueEnemy 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;
public BlueEnemy(int x, int y, int i) {
super(x, y, 16, 16);
this.x = x;
this.y = y;
final int LOW = 200, HIGH = 256, color = rand(LOW, HIGH);
this.color = new Color(color, color, color);
}
@Override
public int tick() {
// epic AI
if((int)healthBar <= 0){
alive = false;
}
if (alive) {
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;
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;
}
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 {
if (time > 0.4d) {
time = 0;
Xmod = rand(0, 1024);
Ymod = rand(0, 200);
}
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 (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;
}
x += dx;
y += dy;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
x--;
if (x < 0)
while (x < 0)
x++;
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;
@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;
}
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);
}
@Override
public void damage(int i) {
health -= i;
}
@Override
public boolean getAlive() {
return alive;
}
@Override
public Color getColor() {
return color;
}
public boolean isCollidable() {
return true;
}
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}
}

View File

@ -1,196 +0,0 @@
package MAndApps.apps.spacewars.entity.enemy;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import MAndApps.apps.spacewars.Entity;
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;
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);
}
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 (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);
}
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 (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
}
// UIFMMMMM
x += dx;
y += dy;
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;
}
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;
}
// 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);
}
@Override
public void damage(int i) {
health -= i;
}
@Override
public boolean getAlive() {
return alive;
}
@Override
public Color getColor() {
return color;
}
public boolean isCollidable() {
return true;
}
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}
}

View File

@ -7,31 +7,35 @@ import java.util.Random;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Enemy;
import MAndEngine.Engine;
import static MAndEngine.Utils.rand;
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 time = 0, desiredX, desiredY, Xmod, Ymod;
private static final int PROXIMITY = 200;
private double time = 10000, desiredX, desiredY, Xmod, Ymod;
private static final double ACC = 0.005, MAXSPEED = 1, DEAD_ACC = 0.5d, DEAD_MAXSPEED = 5;
private Color color;
private boolean debug = true, alive = true;
private boolean alive = true;
private double healthBar = 1;
private final double reEvaluateTime;
public NormalEnemy(int x, int y) {
this(x, y, Math.random());
public NormalEnemy() {
this(Math.random());
}
public NormalEnemy(int x, int y, double hyperness) {
super(x, y, 16, 16);
this.x = x;
this.y = y;
final int LOW = 200, HIGH = 256, color = rand(LOW, HIGH);
public NormalEnemy(double hyperness) {
final int LOW = 200, HIGH = 255, color = rand(LOW, HIGH);
this.color = new Color(color, color, color);
x = SpaceWars.getWIDTH() / 2 - width / 2;
y = 100;
reEvaluateTime = 1 - hyperness;
width = 16 * SpaceWars.scale;
height = 16 * SpaceWars.scale;
}
@Override
@ -41,60 +45,56 @@ public class NormalEnemy extends Enemy {
alive = false;
}
if (alive) {
if (SpaceWars.getPlayer().getAlive()) {
if (!SpaceWars.getPlayer().isRespawning()) {
if (time > reEvaluateTime) {
time = 0;
Xmod = rand(-PROXIMITY, PROXIMITY);
Ymod = rand(-PROXIMITY, PROXIMITY);
Xmod = rand(-PROXIMITY * SpaceWars.scale, PROXIMITY * SpaceWars.scale);
Ymod = rand(-PROXIMITY * SpaceWars.scale, PROXIMITY * SpaceWars.scale);
}
desiredX = SpaceWars.getPlayer().getX() + Xmod;
desiredY = SpaceWars.getPlayer().getY() + Ymod;
if ((int) desiredX > (int) x)
dx += ACC;
dx += ACC * Engine.deltaTime;
else if ((int) desiredX < (int) x) {
dx -= ACC;
dx -= ACC * Engine.deltaTime;
}
if ((int) desiredY > (int) y)
dy += ACC;
dy += ACC * Engine.deltaTime;
else if ((int) desiredY < (int) y) {
dy -= ACC;
dy -= ACC * Engine.deltaTime;
}
if (dx > MAXSPEED)
while (dx > MAXSPEED)
dx -= ACC;
dx = MAXSPEED;
if (dx < 0 - MAXSPEED)
while (dx < 0 - MAXSPEED)
dx += ACC;
dx = 0 - MAXSPEED;
if (dy > MAXSPEED)
while (dy > MAXSPEED)
dy -= ACC;
dy = MAXSPEED;
if (dy < 0 - MAXSPEED)
while (dy < 0 - MAXSPEED)
dy += ACC;
dy = 0 - MAXSPEED;
} else {
if (time > reEvaluateTime) {
time = 0;
Xmod = rand(0, 1024);
Ymod = rand(0, 200);
Xmod = rand(0, SpaceWars.getWIDTH());
Ymod = rand(0, SpaceWars.getHEIGHT() / 3);
}
desiredX = Xmod;
desiredY = Ymod;
if ((int) desiredX > (int) x)
dx += DEAD_ACC;
dx += DEAD_ACC * Engine.deltaTime;
else if ((int) desiredX < (int) x) {
dx -= DEAD_ACC;
dx -= DEAD_ACC * Engine.deltaTime;
}
if ((int) desiredY > (int) y)
dy += DEAD_ACC;
dy += DEAD_ACC * Engine.deltaTime;
else if ((int) desiredY < (int) y) {
dy -= DEAD_ACC;
dy -= DEAD_ACC * Engine.deltaTime;
}
if (dx > DEAD_MAXSPEED)
@ -112,56 +112,54 @@ public class NormalEnemy extends Enemy {
}
x += dx;
y += dy;
x += dx * Engine.deltaTime;
y += dy * Engine.deltaTime;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
if (x > SpaceWars.getWIDTH() - width)
while (x > SpaceWars.getWIDTH() - width)
x--;
if (x < 0)
while (x < 0)
x++;
if (y > SpaceWars.getHEIGHT() - HEIGHT)
while (y > SpaceWars.getHEIGHT() - HEIGHT)
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);
time += 0.01 * Engine.deltaTime;
absoluteTime += 0.01 * Engine.deltaTime;
return 0;
}
private double 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) {
try{
temp = r.nextDouble() > absoluteTime / 2 ? 1 : 0;
}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
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);
if (temp == 0) if (alive) g.fillRect((int) x, (int) y, (int)width, (int)height);
if (SpaceWars.debug) g.drawLine((int)(x + width / 2), (int)(y + height / 2), (int)(desiredX + width / 2), (int)(desiredY + height / 2));
healthBar += ((((double)health/(double)MAX_HEALTH)*(width + 1)) - healthBar)/6;
//healthbar
g.setColor(Color.BLACK);
g.drawRect((int) x, (int) y, WIDTH - 1, 3);
g.fillRect((int) x, (int) y - 5, (int)width - 1, 3);
g.setColor(Color.RED);
g.fillRect((int) x, (int) y - 5, (int)width, 3);
g.setColor(Color.GREEN);
g.fillRect((int) x, (int) y - 5, (int) healthBar, 3);
}
@Override

View File

@ -1,187 +0,0 @@
package MAndApps.apps.spacewars.entity.enemy;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Enemy;
public class RedEnemy 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;
public RedEnemy(int x, int y, int i) {
super(x, y, WIDTH, HEIGHT);
this.x = x;
this.y = y;
this.color = new Color(255, rand(50, 75), rand(50, 75));
}
public int tick() {
// epic AI
if((int)healthBar <= 0){
alive = false;
}
if (alive) {
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;
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;
}
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 {
if (time > 0.4d) {
time = 0;
Xmod = rand(0, 1024);
Ymod = rand(0, 200);
}
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 (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;
}
x += dx;
y += dy;
if (x > SpaceWars.getWIDTH() - WIDTH)
while (x > SpaceWars.getWIDTH() - WIDTH)
x--;
if (x < 0)
while (x < 0)
x++;
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;
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;
}
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);
}
@Override
public void damage(int i) {
health -= i;
}
@Override
public boolean getAlive() {
return alive;
}
@Override
public Color getColor() {
return color;
}
@Override
public boolean isCollidable() {
return true;
}
@Override
public void die() {
// TODO Auto-generated method stub
}
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}
}

View File

@ -9,6 +9,7 @@ public class Shop {
private final int ON = 17, OFF = 64, BOX_WIDTH = 500, BOX_HEIGHT = 200, BOX_Y_OFFSET = 50, BOX_Y_MULTIPLIER = 250, MAX_ANIMATION_TIME = BOX_WIDTH;
private int state = OFF, selection = 0;
private double animationTime = 0;
public void render(Graphics g, final int WIDTH){
g.setColor(selection == 0 ? Color.CYAN : Color.BLUE);
g.fillRect((int)animationTime-BOX_WIDTH, BOX_Y_OFFSET, BOX_WIDTH, BOX_HEIGHT);

View File

@ -1,12 +1,85 @@
package MAndApps.apps.spacewars.tools;
public class Direction {
private final int direction;
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
public Direction(int i){
direction = i;
}
public int getDirection() {
return direction;
}
public abstract class Direction {
public static Direction UP = new Direction() {
@Override
public int getX() {
return 0;
}
@Override
public Direction getOpposite() {
return DOWN;
}
@Override
public int getY() {
// TODO Auto-generated method stub
return -1;
}
};
public static Direction DOWN = new Direction() {
@Override
public int getX() {
return 0;
}
@Override
public Direction getOpposite() {
return UP;
}
@Override
public int getY() {
// TODO Auto-generated method stub
return 1;
}
};
public static Direction LEFT = new Direction() {
@Override
public int getX() {
return -1;
}
@Override
public Direction getOpposite() {
return RIGHT;
}
@Override
public int getY() {
// TODO Auto-generated method stub
return 0;
}
};
public static Direction RIGHT = new Direction() {
@Override
public int getX() {
return 1;
}
@Override
public Direction getOpposite() {
return LEFT;
}
@Override
public int getY() {
// TODO Auto-generated method stub
return 0;
}
};
public abstract int getX();
public abstract Direction getOpposite();
public abstract int getY();
}

View File

@ -5,10 +5,12 @@ import java.awt.Graphics;
import java.util.Random;
import java.util.Stack;
import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.Particle;
public class Explosion extends Entity{
private Stack<Particle> bits = new Stack<Particle>();
private Random rand = new Random();

View File

@ -1,10 +1,14 @@
import MAndEngine.Engine;
/**
* initializes an engine object that will open up spacewars.
* @author mgosselin
*
*/
public class Main {
public static void main(String[] args) {
Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false);
Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false, true);
engine.run();
}