Skip to content

Commit

Permalink
Refactored the Invaders class to keep things simple. The main change …
Browse files Browse the repository at this point in the history
…involved moving the next level logic into the game screen class. This way students can focus on the if statements in the Invaders.run loop and won't have to worry about much else.
  • Loading branch information
CodeRhymesLife committed Jul 28, 2015
1 parent 5dc2077 commit 7d3c2cd
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 310 deletions.
347 changes: 209 additions & 138 deletions .idea/workspace.xml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/engine/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package engine;

/**
* Created by Ryan on 7/28/2015.
*/
public class Constants {
/** Max lives. */
public static final int MAX_LIVES = 3;
}
4 changes: 2 additions & 2 deletions src/engine/DrawManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public enum SpriteType {
* Private constructor.
*/
private DrawManager() {
fileManager = Core.getFileManager();
logger = Core.getLogger();
fileManager = Main.getFileManager();
logger = Main.getLogger();
logger.info("Started loading resources.");

try {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class FileManager {
* private constructor.
*/
private FileManager() {
logger = Core.getLogger();
logger = Main.getLogger();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/engine/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Frame(final int width, final int height) {
this.height = height - insets.top + insets.bottom;
setTitle("Invaders");

addKeyListener(Core.getInputManager());
addKeyListener(Main.getInputManager());
}

/**
Expand All @@ -54,14 +54,14 @@ public Frame(final int width, final int height) {
* @return Return code of the finished screen.
*/
public final ScreenType setScreen(final Screen screen) {
Core.getLogger().info("Starting " + screen.getScreenType() + " " + Core.WIDTH + "x" + Core.HEIGHT +
" at " + Core.FPS + " fps.");
Main.getLogger().info("Starting " + screen.getScreenType() + " " + Main.WIDTH + "x" + Main.HEIGHT +
" at " + Main.FPS + " fps.");

currentScreen = screen;
currentScreen.initialize();
ScreenType nextScreen = currentScreen.run();

Core.getLogger().info("Closing " + screen.getScreenType() + ".");
Main.getLogger().info("Closing " + screen.getScreenType() + ".");

return nextScreen;
}
Expand Down
43 changes: 34 additions & 9 deletions src/engine/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,25 @@ public GameState(final int level, final int score,
/**
* @return the level
*/
public final int getLevel() {
return level;
}
public final int getLevel() {return level; }

/**
* Sets a new level
* @param newLevel
*/
public final void setLevel(int newLevel) { this.level = newLevel; }

/**
* @return the score
*/
public final int getScore() {
return score;
}
public final int getScore() { return score; }

/**
* Sets a new score
* @param newScore
* @return
*/
public final void setScore(int newScore) { this.score = newScore; }

/**
* @return the livesRemaining
Expand All @@ -64,12 +73,22 @@ public final int getLivesRemaining() {
return livesRemaining;
}

/**
* Set new lives remaining
* @param newLivesRemaining
*/
public final void setLivesRemaining(int newLivesRemaining) { this.livesRemaining = newLivesRemaining; }

/**
* @return the bulletsShot
*/
public final int getBulletsShot() {
return bulletsShot;
}
public final int getBulletsShot() { return bulletsShot; }

/**
* Set bullets shot
* @param bulletsShot
*/
public void setBulletsShot(int bulletsShot) { this.bulletsShot = bulletsShot; }

/**
* @return the shipsDestroyed
Expand All @@ -78,4 +97,10 @@ public final int getShipsDestroyed() {
return shipsDestroyed;
}

/**
* Set ships destroyed
* @param shipsDestroyed
*/
public void setShipsDestroyed(int shipsDestroyed) { this.shipsDestroyed = shipsDestroyed; }

}
57 changes: 57 additions & 0 deletions src/engine/Invaders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package engine;

import screen.*;

import java.util.List;

/**
* Created by Ryan on 7/28/2015.
*/
public class Invaders {

/**
* Start running the game
*/
public static void run(Frame frame) {
// Get the size of the frame
int width = frame.getWidth();
int height = frame.getHeight();

// Get a list of levels to play
List<GameSettings> levelSettings = Levels.getLevels();

// Hold on to all of the game's information
GameState gameState;

// The current screen
// Empty by default
Screen currentScreen;

// Start at the title screen
ScreenType nextScreen = ScreenType.TitleScreen;

while (nextScreen != ScreenType.EndGame) {
// Reset the game's state each time the game starts over
gameState = new GameState(1, 0, Constants.MAX_LIVES, 0, 0);

if(nextScreen == ScreenType.TitleScreen) {
// Main menu.
currentScreen = new TitleScreen(width, height, Main.FPS);
nextScreen = frame.setScreen(currentScreen);
}
else if (nextScreen == ScreenType.GameScreen) {
currentScreen = new GameScreen(gameState, levelSettings, width, height, Main.FPS);
frame.setScreen(currentScreen);

currentScreen = new ScoreScreen(width, height, Main.FPS, gameState);
nextScreen = frame.setScreen(currentScreen);
}
else if (nextScreen == ScreenType.HighScroreScreen) {
// High scores.
currentScreen = new HighScoreScreen(width, height, Main.FPS);
nextScreen = frame.setScreen(currentScreen);
}

}
}
}
83 changes: 4 additions & 79 deletions src/engine/Core.java → src/engine/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package engine;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
Expand All @@ -20,30 +19,18 @@
* @author <a href="mailto:[email protected]">Roberto Izquierdo Amo</a>
*
*/
public final class Core {

public final class Main {
/** Width of current screen. */
public static final int WIDTH = 448;
/** Height of current screen. */
public static final int HEIGHT = 520;
/** Max fps of current screen. */
public static final int FPS = 60;

/** Max lives. */
private static final int MAX_LIVES = 3;
/** Levels between extra life. */
private static final int EXTRA_LIFE_FRECUENCY = 3;
/** Total number of levels. */
private static final int NUM_LEVELS = 7;

/** Frame to draw the screen on. */
private static Frame frame;
/** Screen currently shown. */
private static Screen currentScreen;
/** Difficulty settings list. */
private static List<GameSettings> gameSettings;
/** Application logger. */
private static final Logger LOGGER = Logger.getLogger(Core.class
private static final Logger LOGGER = Logger.getLogger(Main.class
.getSimpleName());
/** Logger handler for printing to disk. */
private static Handler fileHandler;
Expand Down Expand Up @@ -78,81 +65,19 @@ public static void main(final String[] args) {

frame = new Frame(WIDTH, HEIGHT);
DrawManager.getInstance().setFrame(frame);
int width = frame.getWidth();
int height = frame.getHeight();

// Run the game
run(width, height);
Invaders.run(frame);

fileHandler.flush();
fileHandler.close();
System.exit(0);
}

/**
* Start running the game
* @param width
* @param height
*/
public static void run(int width, int height) {
// Get a list of levels to play
gameSettings = Levels.getLevels();

// Hold on to all of the game's information
GameState gameState;

// Start at the title screen
ScreenType nextScreen = ScreenType.TitleScreen;

do {
// Reset the game's state each time the game starts over
gameState = new GameState(1, 0, MAX_LIVES, 0, 0);

if(nextScreen == ScreenType.TitleScreen) {
// Main menu.
currentScreen = new TitleScreen(width, height, FPS);
nextScreen = frame.setScreen(currentScreen);
}
else if (nextScreen == ScreenType.GameScreen) {
// Game & score.
do {
// One extra life every few levels.
boolean bonusLife = gameState.getLevel()
% EXTRA_LIFE_FRECUENCY == 0
&& gameState.getLivesRemaining() < MAX_LIVES;

currentScreen = new GameScreen(gameState,
gameSettings.get(gameState.getLevel() - 1),
bonusLife, width, height, FPS);
frame.setScreen(currentScreen);

gameState = ((GameScreen) currentScreen).getGameState();

gameState = new GameState(gameState.getLevel() + 1,
gameState.getScore(),
gameState.getLivesRemaining(),
gameState.getBulletsShot(),
gameState.getShipsDestroyed());

} while (gameState.getLivesRemaining() > 0
&& gameState.getLevel() <= NUM_LEVELS);

currentScreen = new ScoreScreen(width, height, FPS, gameState);
nextScreen = frame.setScreen(currentScreen);
}
else if (nextScreen == ScreenType.HighScroreScreen) {
// High scores.
currentScreen = new HighScoreScreen(width, height, FPS);
nextScreen = frame.setScreen(currentScreen);
}

} while (nextScreen != ScreenType.EndGame);
}

/**
* Constructor, not called.
*/
private Core() {
private Main() {

}

Expand Down
4 changes: 2 additions & 2 deletions src/entity/EnemyShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.awt.Color;

import engine.Cooldown;
import engine.Core;
import engine.Main;
import engine.DrawManager.SpriteType;

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ public EnemyShip(final int positionX, final int positionY,
super(positionX, positionY, 12 * 2, 8 * 2, Color.WHITE);

this.spriteType = spriteType;
this.animationCooldown = Core.getCooldown(500);
this.animationCooldown = Main.getCooldown(500);
this.isDestroyed = false;

switch (this.spriteType) {
Expand Down
8 changes: 4 additions & 4 deletions src/entity/EnemyShipFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import screen.Screen;
import engine.Cooldown;
import engine.Core;
import engine.Main;
import engine.DrawManager;
import engine.DrawManager.SpriteType;
import engine.GameSettings;
Expand Down Expand Up @@ -112,8 +112,8 @@ private enum Direction {
* Current game settings.
*/
public EnemyShipFormation(final GameSettings gameSettings) {
this.drawManager = Core.getDrawManager();
this.logger = Core.getLogger();
this.drawManager = Main.getDrawManager();
this.logger = Main.getLogger();
this.enemyShips = new ArrayList<List<EnemyShip>>();
this.currentDirection = Direction.RIGHT;
this.movementInterval = 0;
Expand Down Expand Up @@ -191,7 +191,7 @@ public final void draw() {
*/
public final void update() {
if(this.shootingCooldown == null) {
this.shootingCooldown = Core.getVariableCooldown(shootingInterval,
this.shootingCooldown = Main.getVariableCooldown(shootingInterval,
shootingVariance);
this.shootingCooldown.reset();
}
Expand Down
6 changes: 3 additions & 3 deletions src/entity/Ship.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Set;

import engine.Cooldown;
import engine.Core;
import engine.Main;
import engine.DrawManager.SpriteType;

/**
Expand Down Expand Up @@ -39,8 +39,8 @@ public Ship(final int positionX, final int positionY) {
super(positionX, positionY, 13 * 2, 8 * 2, Color.GREEN);

this.spriteType = SpriteType.Ship;
this.shootingCooldown = Core.getCooldown(SHOOTING_INTERVAL);
this.destructionCooldown = Core.getCooldown(1000);
this.shootingCooldown = Main.getCooldown(SHOOTING_INTERVAL);
this.destructionCooldown = Main.getCooldown(1000);
}

/**
Expand Down
Loading

0 comments on commit 7d3c2cd

Please sign in to comment.