Skip to content

Commit

Permalink
Player now uses Gun class for weapon.
Browse files Browse the repository at this point in the history
Player now has three lives. When player loses all health a life will be lost.
Added "graphical" heads-up-display with player lives, health, and current weapon name.
removed debug log from highscoreList class.
refactored gun parameter names.
  • Loading branch information
frekfra committed Aug 1, 2013
1 parent 1b89786 commit 3f331ce
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 37 deletions.
Binary file added GDCalaga/Pics/HealthBar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 1 addition & 4 deletions GDCalaga/highscore.dat
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
1000,Thomas
40000,Eddison
10000,George
5000,Micheal
110,Player1
2 changes: 1 addition & 1 deletion GDCalaga/src/org/gdc/gdcalaga/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void Collide(Entity other)
public void fire()
{
Vector2f bulletPosition = new Vector2f((pos.x - size.x / 2) + 3, pos.y);
shipGun.shoot(bulletPosition, player);
shipGun.shoot(bulletPosition, player.getPosition());
}


Expand Down
39 changes: 25 additions & 14 deletions GDCalaga/src/org/gdc/gdcalaga/Gun.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.gdc.gdcalaga;

import org.gdc.gdcalaga.Entity.Alliance;
import org.newdawn.slick.geom.Vector2f;


public class Gun {

public static int MAX_GUN_TYPES = 3; //fixme use enum max value correctly.


enum GunType {
StraightShot,
AimedShot,
Expand All @@ -18,6 +18,8 @@ enum GunType {
protected GunType gunType;
protected EntityManager entityManager;
protected Entity.Alliance alliance;
protected int bulletDamage = 1;


public Gun(EntityManager entityManager, float velocity, Entity.Alliance alliance, GunType gunType)
{
Expand All @@ -28,15 +30,15 @@ public Gun(EntityManager entityManager, float velocity, Entity.Alliance alliance

}

public void shoot(Vector2f bulletPosition, Player player)
public void shoot(Vector2f bulletPosition, Vector2f target)
{
switch(gunType)
{
case StraightShot:
straightShot(bulletPosition);
break;
case AimedShot:
aimedShot(bulletPosition, player);
aimedShot(bulletPosition, target);
break;
case TripleShot:
tripleShot(bulletPosition);
Expand All @@ -47,25 +49,30 @@ public void shoot(Vector2f bulletPosition, Player player)
}

private void straightShot(Vector2f bulletPosition) {
Bullet newBullet = new Bullet(entityManager, bulletPosition, 1, alliance);
newBullet.setSpeed(-250, 0);
Bullet newBullet = new Bullet(entityManager, bulletPosition, bulletDamage, alliance);
if(alliance == Alliance.ENEMY)
newBullet.setSpeed(-250, 0);
else
newBullet.setSpeed(250, 0);
}

private void aimedShot(Vector2f bulletPosition, Player player) {
private void aimedShot(Vector2f bulletPosition, Vector2f target) {

Bullet newBullet = new Bullet(entityManager, bulletPosition, 1, alliance);
Bullet newBullet = new Bullet(entityManager, bulletPosition, bulletDamage, alliance);

Vector2f directionOfPlayer = new Vector2f(player.getPosition());
directionOfPlayer.x -= bulletPosition.x;
directionOfPlayer.y += (-bulletPosition.y) + (Math.random() * 20) - 10;
directionOfPlayer.normalise();
// change the x direction to point towards target,
// add random y direction to make the shot seem not 100% accurate.
Vector2f directionOfTarget = new Vector2f(target);
directionOfTarget.x -= bulletPosition.x;
directionOfTarget.y += (-bulletPosition.y) + (Math.random() * 20) - 10;
directionOfTarget.normalise();

newBullet.setSpeed(directionOfPlayer.x * 250,directionOfPlayer.y * 250);
newBullet.setSpeed(directionOfTarget.x * 250, directionOfTarget.y * 250);
}

private void tripleShot(Vector2f bulletPosition){

Bullet diagonalUpBullet = new Bullet(entityManager, bulletPosition, 1, alliance);
Bullet diagonalUpBullet = new Bullet(entityManager, bulletPosition, bulletDamage, alliance);
Vector2f diagonalUp = new Vector2f(-2,1);
diagonalUp.normalise();
diagonalUpBullet.setSpeed(diagonalUp.x * velocity, diagonalUp.y * velocity);
Expand All @@ -74,8 +81,12 @@ private void tripleShot(Vector2f bulletPosition){

Vector2f diagonalDown = new Vector2f(-2,-1);
diagonalDown.normalise();
Bullet diagonalDownBullet = new Bullet(entityManager, bulletPosition, 1, alliance);
Bullet diagonalDownBullet = new Bullet(entityManager, bulletPosition, bulletDamage, alliance);

diagonalDownBullet.setSpeed(diagonalDown.x * velocity, diagonalDown.y * velocity);
}

public String getName() {
return gunType.toString();
}
}
79 changes: 79 additions & 0 deletions GDCalaga/src/org/gdc/gdcalaga/HeadsUpDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.gdc.gdcalaga;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class HeadsUpDisplay {

private Image lifeIcon = null;
private Image healthIcon = null;

private int livesX = 100;
private int livesY = 15;
private int livesIconOffset = 20;

private int healthX = 180;
private int healthY = 15;
private int healthIconOffset = 25;

private int weaponX = 450;
private int weaponY = 15;


private int imageScale = 1;
private Player player;

public HeadsUpDisplay(Player player) {
this.player = player;
}

public void render(Graphics g) {

loadResources();
if(player != null) {
drawHealthBar(g);
drawLives(g);
drawWeapon(g);
}
}

private void loadResources() {
if(lifeIcon == null){
try {
lifeIcon = new Image("Pics/upgrade_invalid.png");
}
catch (SlickException e) {
e.printStackTrace();
}

try {
healthIcon = new Image("Pics/HealthBar.png");
}
catch (SlickException e) {
e.printStackTrace();
}
}
}

private void drawHealthBar(Graphics g)
{
for(int i = 0; i < player.getHealth(); ++i)
{
healthIcon.draw(healthX + (healthIconOffset*i), healthY, imageScale, Color.white);
}
}

private void drawLives(Graphics g) {
for(int i = 0; i < player.getLives(); ++i)
{
lifeIcon.draw(livesX + (livesIconOffset*i), livesY, imageScale, Color.white);
}
}

private void drawWeapon(Graphics g) {

String weaponName = new String("Weapon: " + player.getWeapon().getName());
g.drawString(weaponName, weaponX, weaponY);
}
}
8 changes: 4 additions & 4 deletions GDCalaga/src/org/gdc/gdcalaga/HighscoreList.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private List<Highscore> getHighscoreList() {
if(scores.size() == 0)
{
try {
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
//String current = new java.io.File( "." ).getCanonicalPath();
//System.out.println("Current dir:"+current);

BufferedReader in = new BufferedReader(new FileReader("highscore.dat"));

Expand Down Expand Up @@ -92,8 +92,8 @@ public void addHighscore(int score, String name) {
public void saveHighscore()
{
try {
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
//String current = new java.io.File( "." ).getCanonicalPath();
//System.out.println("Current dir:"+current);

BufferedWriter out = new BufferedWriter(new FileWriter("highscore.dat"));

Expand Down
14 changes: 7 additions & 7 deletions GDCalaga/src/org/gdc/gdcalaga/PlayState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ private enum State {
PostGame,
}

public static final int ID = 0;
public static final int ID = 0;

private Player player;

final int menuKeyDelay = 250;
Expand All @@ -40,7 +41,7 @@ private enum State {
private AudioManager audioManager = AudioManager.getAudioManager();
private HighscoreList highscoreList = new HighscoreList(GDCalaga.SCREEN_SIZE_X,GDCalaga.SCREEN_SIZE_Y);
private Menu mainMenu = new Menu(GDCalaga.SCREEN_SIZE_X,GDCalaga.SCREEN_SIZE_Y);

private HeadsUpDisplay hud;

private List<DisplayObject> disObjs = new LinkedList<DisplayObject>();

Expand All @@ -60,6 +61,7 @@ public void init(GameContainer container, StateBasedGame game) throws SlickExcep
paths.loadFromJson("./data/paths.json");

disObjs.add(new Background(container.getWidth(), container.getHeight()));
hud = new HeadsUpDisplay(player);
}

@Override
Expand All @@ -73,6 +75,7 @@ public void render(GameContainer container, StateBasedGame game, Graphics g) thr
switch(state)
{
case Playing:
hud.render(g);
break;
case Pause:
break;
Expand All @@ -85,9 +88,7 @@ public void render(GameContainer container, StateBasedGame game, Graphics g) thr
case PostGame:
renderPostGame(g);
break;
}


}
renderDebugText(g);
}

Expand Down Expand Up @@ -150,6 +151,7 @@ private void updateGame(int delta) {
}
}



private void renderDebugText(Graphics g) {
/* If we use this same monospace font, each letter takes up 10 pixels
Expand Down Expand Up @@ -200,8 +202,6 @@ private void postGameInput() {
highscoreList.addHighscore(Player.getTotalPoints(), playerName);
changeState(State.MainMenu);
}


}


Expand Down
37 changes: 30 additions & 7 deletions GDCalaga/src/org/gdc/gdcalaga/Player.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.gdc.gdcalaga;
import org.gdc.gdcalaga.Gun.GunType;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
Expand Down Expand Up @@ -29,10 +30,14 @@ public class Player extends Entity

//Upgradable Player attributes
private static float health;
private static final float maxHealth = 10;
private static float shields;
private static float fireRate; //fireRate in bullets per second
private static int numGuns;
private static float damage;
private static int lives;

private Gun gun;
protected Vector2f velocity;

private static boolean shieldActivated;
Expand All @@ -57,6 +62,7 @@ public Player(EntityManager manager, Vector2f position)
shields = 0;
fireRate = 5; //bullets per second
numGuns = 1;
lives = 3;
ticksPerBullet = (int)(1000 / fireRate); //milliseconds in a second / fireRate
//since delta time is in milliseconds
ticksSinceLastBullet = ticksPerBullet; //so we can shoot right off the bat
Expand All @@ -67,6 +73,8 @@ public Player(EntityManager manager, Vector2f position)

gunPositions = new GunPositions(SIZE_WIDTH, SIZE_HEIGHT);

gun = new Gun(manager, 250, Entity.Alliance.FRIENDLY, GunType.StraightShot);

try {
ship= new Image("Pics/Player.png");
} catch (SlickException e) {
Expand Down Expand Up @@ -129,11 +137,8 @@ public boolean fire(float delta)
int max = numGuns < gunPositions.getSize() ? numGuns : gunPositions.getSize();
for (int i = 0; i < max; i++)
{
Vector2f position = new Vector2f(gunPositions.getPosition(i));
position.add(pos);
Bullet newBullet = new Bullet(entities, position, (int)(damage), alliance);
Vector2f direction = gunPositions.getDirection(i);
newBullet.setSpeed(direction.x * 500, direction.y * 500);
Vector2f shotOrigin = new Vector2f(gunPositions.getPosition(i));
gun.shoot(shotOrigin.add(pos), gunPositions.getPosition(i));
}

ticksSinceLastBullet = 0;
Expand Down Expand Up @@ -185,8 +190,16 @@ public void Hurt(float dmg)

if(health <= 0)
{
Destroy();
}
--lives;
if(lives < 0)
{
Destroy();
}
else
{
health = maxHealth;
}
}
}

public float getHealth()
Expand All @@ -209,6 +222,15 @@ public Vector2f getPosition()
return pos;
}

public int getLives()
{
return lives;
}

public Gun getWeapon() {
return gun;
}

public static int getTotalPoints()
{
return totalPoints;
Expand All @@ -218,6 +240,7 @@ public static void increaseTotalPoints(int pointValue)
{
totalPoints += pointValue;
}

//my idea here is that ship upgrades will cost points to buy, like in most games.
public static void decreaseTotalPoints(int pointValue)
{
Expand Down

0 comments on commit 3f331ce

Please sign in to comment.