stuff mnanananana

screensaver
Marcus Gosselin 2016-02-13 15:05:14 -05:00
parent cdc6a2d51f
commit 14f800bc0c
9 changed files with 94 additions and 147 deletions

View File

@ -26,10 +26,13 @@ import MAndEngine.ImageCreator;
* are laying around. * are laying around.
* *
* this is somewhat old architecture and some half finished new architecture can be found * 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 * in the screen saver 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 * a player AI. as well, some of these concepts will be ported over to mandengine
* once they are abstracted a little better. * once they are abstracted a little better.
* *
* screen saver branch has been remerged and deleted.
* screen saver will still be made, but i think the base engine needs more polish first
*
* @author mgosselin * @author mgosselin
* *
*/ */
@ -54,10 +57,17 @@ public class SpaceWars implements BasicApp {
private static int HEIGHT = ORIGINAL_HEIGHT; private static int HEIGHT = ORIGINAL_HEIGHT;
public static int scale; public static int scale;
@Override @Override
public void tick() { public void tick() {
// ticks enemies //add entities
for(Entity e : toAdd)
entities.add(e);
toAdd.clear();
// ticks entities
for (int i = 0; i < entities.size(); i++) for (int i = 0; i < entities.size(); i++)
entities.get(i).tick(); entities.get(i).tick();
@ -84,8 +94,8 @@ public class SpaceWars implements BasicApp {
((e2.getY() > e1.getY()) & (e2.getY() < e1.getY() + e1.getHeight()))) ((e2.getY() > e1.getY()) & (e2.getY() < e1.getY() + e1.getHeight())))
) { ) {
e1.collidedWith(e2); e1.collidedWith(e2);
e2.collidedWith(e1); e2.collidedWith(e1);
} }
} }
} }
@ -103,17 +113,19 @@ public class SpaceWars implements BasicApp {
i++; i++;
} }
} }
public static ArrayList<Entity> getEnemies() { private static ArrayList<Entity> toAdd;
return entities;
public static void addEntity(Entity e) {
toAdd.add(e);
} }
@Override @Override
public void render(Graphics2D g) { public void render(Graphics2D g) {
try {
g.drawImage(background, 0, 0, null);
try {
g.drawImage(background, 0, 0, null);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
@ -123,11 +135,9 @@ public class SpaceWars implements BasicApp {
for (int i = 0; i < entities.size(); i++) for (int i = 0; i < entities.size(); i++)
entities.get(i).render(g); entities.get(i).render(g);
// render level and xp bar.
g.setFont(levelFont);
g.setColor(Color.WHITE);
g.setFont(defaultFont); g.setFont(defaultFont);
g.setColor(Color.WHITE);
g.drawString("" + entities.size(), 20, 32);
if (paused) { if (paused) {
g.setFont(pausedFont); g.setFont(pausedFont);
@ -167,9 +177,10 @@ public class SpaceWars implements BasicApp {
@Override @Override
public void initialize() { public void initialize() {
try { try {
toAdd = new ArrayList<Entity>();
player = new Player(); player = new Player();
entities.add(player); entities.add(player);
for(int i = 0; i < 100; i ++) for(int i = 0; i < 10; i ++)
entities.add(new NormalEnemy()); entities.add(new NormalEnemy());
Engine.timeScale = 60d / (1000d * 1000d); Engine.timeScale = 60d / (1000d * 1000d);
@ -182,8 +193,7 @@ public class SpaceWars implements BasicApp {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
for (int i = 0; i < entities.size(); i++) player.keyPressed(e);
entities.get(i).keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE) { if (e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE) {
paused = !paused; paused = !paused;
} else if (e.getKeyCode() == KeyEvent.VK_Q) { } else if (e.getKeyCode() == KeyEvent.VK_Q) {

View File

@ -4,6 +4,7 @@ import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.entity.bullet.BasicPlayerBullet; import MAndApps.apps.spacewars.entity.bullet.BasicPlayerBullet;
import MAndApps.apps.spacewars.entity.bullet.PlayerImpactBullet; import MAndApps.apps.spacewars.entity.bullet.PlayerImpactBullet;
import MAndApps.apps.spacewars.entity.bullet.PlayerPiercingBullet; import MAndApps.apps.spacewars.entity.bullet.PlayerPiercingBullet;
import MAndApps.apps.spacewars.tools.Direction;
public abstract class Bullet extends Entity { public abstract class Bullet extends Entity {
@ -36,45 +37,8 @@ public abstract class Bullet extends Entity {
super.y = y; super.y = y;
} }
/**
* bullet class now references here to get the cool down for the current bullet
* updates upon ammo change or initialization of a gun type.
* @param bulletType
* @return
*/
public static int getCooldown(int bulletType){
switch (bulletType){
case BASIC:
return 25;
case PLAYER_PIERCE_ONE:
return 20;
case PLAYER_PIERCE_TWO:
return 18;
case PLAYER_PIERCE_THREE:
return 16;
case PLAYER_PIERCE_FOUR:
return 14;
case PLAYER_PIERCE_FIVE:
return 12;
case PLAYER_PIERCE_SIX:
return 10;
case PLAYER_PIERCE_SEVEN:
return 8;
case PLAYER_PIERCE_EIGHT:
return 6;
case PLAYER_PIERCE_NINE:
return 4;
case PLAYER_PIERCE_TEN:
return 2;
case PLAYER_GODMODE:
return 0;
default:
return 25;
}
}
public static Bullet getNewBullet(int bulletType, int x, int y, int direction){ public static Bullet getNewBullet(int bulletType, int x, int y, Direction direction){
switch(bulletType){ switch(bulletType){
//this is the literal bit that goes through and indexes what to do //this is the literal bit that goes through and indexes what to do
//with certain IDs that you defined above.. //with certain IDs that you defined above..
@ -90,27 +54,7 @@ public abstract class Bullet extends Entity {
//inside the parenthesis, in the returns, are called //inside the parenthesis, in the returns, are called
//"arguments" or "parameters". //"arguments" or "parameters".
case BASIC: case BASIC:
return new BasicPlayerBullet(direction, x, y); return new BasicPlayerBullet(direction, x, y);
case PLAYER_PIERCE_ONE:
return new PlayerPiercingBullet(direction, x, y, 2);
case PLAYER_PIERCE_TWO:
return new PlayerPiercingBullet(direction, x, y, 3);
case PLAYER_PIERCE_THREE:
return new PlayerPiercingBullet(direction, x, y, 4);
case PLAYER_PIERCE_FOUR:
return new PlayerPiercingBullet(direction, x, y, 5);
case PLAYER_PIERCE_FIVE:
return new PlayerPiercingBullet(direction, x, y, 6);
case PLAYER_PIERCE_SIX:
return new PlayerPiercingBullet(direction, x, y, 7);
case PLAYER_PIERCE_SEVEN:
return new PlayerPiercingBullet(direction, x, y, 8);
case PLAYER_PIERCE_EIGHT:
return new PlayerPiercingBullet(direction, x, y, 9);
case PLAYER_PIERCE_NINE:
return new PlayerPiercingBullet(direction, x, y, 10);
case PLAYER_PIERCE_TEN:
return new PlayerPiercingBullet(direction, x, y, 15);
case PLAYER_IMPACT_ONE: case PLAYER_IMPACT_ONE:
return new PlayerImpactBullet(direction, x, y, 1); return new PlayerImpactBullet(direction, x, y, 1);
case PLAYER_IMPACT_TWO: case PLAYER_IMPACT_TWO:

View File

@ -3,6 +3,7 @@ package MAndApps.apps.spacewars.entity;
import java.awt.Color; import java.awt.Color;
import MAndApps.apps.spacewars.Entity; import MAndApps.apps.spacewars.Entity;
import MAndApps.apps.spacewars.SpaceWars;
public abstract class Enemy extends Entity { public abstract class Enemy extends Entity {
public abstract boolean getAlive(); public abstract boolean getAlive();
@ -13,10 +14,11 @@ public abstract class Enemy extends Entity {
public abstract boolean isCollidable(); public abstract boolean isCollidable();
@Override @Override
public void collidedWith(Entity e) { public final void collidedWith(Entity e) {
if(e instanceof Bullet) { if(e instanceof Bullet) {
Bullet b = (Bullet)e; Bullet b = (Bullet)e;
damage(b.getDamage()); damage(b.getDamage());
SpaceWars.BOOM(1.0, 1.0, 255, 255, 255, 1, (int)x, (int)y, 100, true, false, 4);
} }
} }
} }

View File

@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
import MAndApps.apps.spacewars.SpaceWars; import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.gun.Gun; import MAndApps.apps.spacewars.gun.Gun;
import MAndApps.apps.spacewars.tools.Direction;
import MAndApps.apps.spacewars.Entity; import MAndApps.apps.spacewars.Entity;
import static MAndEngine.Utils.rand; import static MAndEngine.Utils.rand;
import MAndEngine.Engine; import MAndEngine.Engine;
@ -13,8 +14,8 @@ import MAndEngine.Engine;
public class Player extends Entity { public class Player extends Entity {
private static final double ACC = 0.5, MAXSPEED = 5; private static final double ACC = 0.5, MAXSPEED = 5;
private boolean alive = false; private boolean alive = false;
private Gun gun = new Gun(Bullet.BASIC, 25, (int) x, (int) y); private Gun gun = new Gun(Bullet.BASIC);
/** /**
* go boom is a callback when it dies because it dies on tick i assume? TODO * go boom is a callback when it dies because it dies on tick i assume? TODO
* fix that so no die on tick. * fix that so no die on tick.
@ -47,6 +48,15 @@ public class Player extends Entity {
} }
gun.tick(); gun.tick();
if (alive) { if (alive) {
//hold up, before we dive into physics...
if(Engine.keys[KeyEvent.VK_I]) gun.shoot(Direction.UP);
else if(Engine.keys[KeyEvent.VK_J]) gun.shoot(Direction.LEFT);
else if(Engine.keys[KeyEvent.VK_K]) gun.shoot(Direction.DOWN);
else if(Engine.keys[KeyEvent.VK_L]) gun.shoot(Direction.RIGHT);
if (time != 1) if (time != 1)
time -= 0.05d * Engine.deltaTime; time -= 0.05d * Engine.deltaTime;
if (time < 1) if (time < 1)
@ -122,7 +132,6 @@ public class Player extends Entity {
if (temp == 1) if (temp == 1)
g.fillRect((int) x, (int) y, (int) width, (int) height); g.fillRect((int) x, (int) y, (int) width, (int) height);
} }
gun.render(g);
} }
private double time = 0; private double time = 0;
@ -158,4 +167,12 @@ public class Player extends Entity {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return true; return true;
} }
public int getCenterY() {
return (int)(y + height / 2d);
}
public int getCenterX() {
return (int)(x + width / 2d);
}
} }

View File

@ -9,13 +9,15 @@ import MAndApps.apps.spacewars.SpaceWars;
import MAndApps.apps.spacewars.entity.Bullet; import MAndApps.apps.spacewars.entity.Bullet;
import MAndApps.apps.spacewars.tools.Direction; import MAndApps.apps.spacewars.tools.Direction;
import MAndEngine.Engine;;
public class BasicPlayerBullet extends Bullet { public class BasicPlayerBullet extends Bullet {
private final int direction; private final Direction direction;
private final static int SPEED = 10; private final static int SPEED = 10;
private boolean alive = true; private boolean alive = true;
public BasicPlayerBullet(int direction, int x, int y) { public BasicPlayerBullet(Direction direction, int x, int y) {
super(x, y, 1, 1); super(x, y, 1, 1);
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -31,20 +33,18 @@ public class BasicPlayerBullet extends Bullet {
public int tick() { public int tick() {
if (alive) { if (alive) {
if (direction == Direction.UP) {
y -= SPEED;
} else if (direction == Direction.DOWN) {
y += SPEED;
} else if (direction == Direction.LEFT) {
x -= SPEED;
} else if (direction == Direction.RIGHT) {
x += SPEED;
}
y += direction.getY() * SPEED * Engine.deltaTime;
x += direction.getX() * SPEED * Engine.deltaTime;
if(y + height < 0) alive = false;
if(y > SpaceWars.getHEIGHT()) alive = false;
if(x + width < 0) alive = false;
if(x > SpaceWars.getWIDTH()) alive = false;
} }
return 0; return 0;
} }
public void render(Graphics g) { public void render(Graphics g) {
@ -77,4 +77,16 @@ public class BasicPlayerBullet extends Bullet {
} }
@Override
public int getWIDTH() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getHEIGHT() {
// TODO Auto-generated method stub
return 0;
}
} }

View File

@ -10,7 +10,7 @@ import MAndApps.apps.spacewars.entity.Bullet;
import MAndApps.apps.spacewars.tools.Direction; import MAndApps.apps.spacewars.tools.Direction;
public class PlayerImpactBullet extends Bullet { public class PlayerImpactBullet extends Bullet {
private final int direction; private final Direction direction;
private final int WIDTH, HEIGHT; private final int WIDTH, HEIGHT;
private final static int SPEED = 10; private final static int SPEED = 10;
private double x, y; private double x, y;
@ -25,12 +25,12 @@ public class PlayerImpactBullet extends Bullet {
return HEIGHT; return HEIGHT;
} }
public PlayerImpactBullet(int direction, int x, int y, int level) { public PlayerImpactBullet(Direction direction2, int x, int y, int level) {
super(x, y, 1, 1); super(x, y, 1, 1);
this.x = x; this.x = x;
this.y = y; this.y = y;
this.direction = direction; this.direction = direction2;
if (direction == Direction.UP || direction == Direction.DOWN) { if (direction2 == Direction.UP || direction2 == Direction.DOWN) {
WIDTH = 3; WIDTH = 3;
HEIGHT = 8; HEIGHT = 8;
} else { } else {

View File

@ -152,7 +152,7 @@ public class NormalEnemy extends Enemy {
if (SpaceWars.debug) g.drawLine((int)(x + width / 2), (int)(y + height / 2), (int)(desiredX + width / 2), (int)(desiredY + height / 2)); 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 += (((((double)health/(double)MAX_HEALTH)*(width + 1)) - healthBar)/6) * Engine.deltaTime;
//healthbar //healthbar
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.fillRect((int) x, (int) y - 5, (int)width - 1, 3); g.fillRect((int) x, (int) y - 5, (int)width - 1, 3);
@ -186,10 +186,4 @@ public class NormalEnemy extends Enemy {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public void collidedWith(Entity e) {
// TODO Auto-generated method stub
}
} }

View File

@ -1,58 +1,25 @@
package MAndApps.apps.spacewars.gun; package MAndApps.apps.spacewars.gun;
import java.awt.Graphics; import MAndApps.apps.spacewars.SpaceWars;
import java.util.Stack;
import MAndApps.apps.spacewars.entity.Bullet; import MAndApps.apps.spacewars.entity.Bullet;
import MAndApps.apps.spacewars.tools.Direction;
public class Gun { public class Gun {
private Stack<Bullet> bullets = new Stack<Bullet>();
private int bulletType;
private int MAX_COOLDOWN; private int MAX_COOLDOWN;
private int cooldown = 0, x, y; private double cooldown = 0;
public Gun(int bulletType, int cooldown, int x, int y){ public Gun(int bulletType){
MAX_COOLDOWN = cooldown; MAX_COOLDOWN = 5;
this.bulletType = bulletType;
this.x = x;
this.y = y;
}
public void updatePosition(int x, int y){
this.x = x;
this.y = y;
}
public void render(Graphics g){
for(int i = 0; i < bullets.size(); i ++)
bullets.elementAt(i).render(g);
} }
public void tick(){ public void tick(){
cooldown ++; cooldown += MAndEngine.Engine.deltaTime;
//tick bullets
for(int i = 0; i < bullets.size(); i++)
bullets.elementAt(i).tick();
for(int i = 0; i < bullets.size(); i++){
if(!bullets.elementAt(i).getAlive())
bullets.remove(i);
else
i++;
}
} }
public void shoot(int direction){ public void shoot(Direction direction){
if(cooldown >= MAX_COOLDOWN){ if(cooldown >= MAX_COOLDOWN){
cooldown = 0; cooldown = 0;
bullets.push(Bullet.getNewBullet(bulletType, x-(Bullet.getNewBullet(bulletType, x, y, direction).getWIDTH()/2), y-(Bullet.getNewBullet(bulletType, x, y, direction).getHEIGHT()/2), direction)); SpaceWars.addEntity(Bullet.getNewBullet(Bullet.BASIC, SpaceWars.getPlayer().getCenterX(), SpaceWars.getPlayer().getCenterY(), direction));
} }
} }
public void switchAmmo(int bulletType){
this.bulletType = bulletType;
MAX_COOLDOWN = Bullet.getCooldown(bulletType);
}
public void setCooldown(int i) {
MAX_COOLDOWN = i;
}
} }

View File

@ -8,7 +8,8 @@ import MAndEngine.Engine;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false, true); Engine engine = new Engine(new String[] {"MAndApps.apps.spacewars.SpaceWars"}, false, false);
engine.debug = 2;
engine.run(); engine.run();
} }