forked from RobertoIA/Invaders
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the Invaders class to keep things simple. The main change …
…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
1 parent
5dc2077
commit 7d3c2cd
Showing
16 changed files
with
396 additions
and
310 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
|
@@ -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() { | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.