logic
Class GameEngine

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by logic.GameEngine
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class GameEngine
extends javax.swing.JPanel

Author:
See Also:
Serialized Form

Nested Class Summary
private  class GameEngine.KeyInputHandler
          This class manages our key input processes.
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  Sprite background
          Our game screen background.
private  int balloonCount
          This integer holds the number of the valus on the screen to determine if if the level was cleared or not.
private static java.lang.String balloonPath
           
private  javax.swing.JPanel cardPanel
           
private static boolean debug
          This boolean is for testing the GameEngine.
private  java.util.ArrayList entities
          This arraylist holds all the entities on the screen to check collisions and make drawing works easier, withouth checking too many types.
private  boolean gameRunning
          True if the game is currently "running", i.e. the game loop is looping
private  boolean isMainMenu
          This variable is for determining if the game is on main menu Because game will be paused and it will be prevented from rendering again and again.
private  java.awt.CardLayout layout
          We need an instance of the layout when we lost our focus on the game screen
private  int level
          This integer holds the level number.
private  MainPanel mainPanel
           
private  java.lang.String message
          The message to display which waiting for a key press
private  java.lang.String message2
          The message to display which waiting for a key press
private  boolean multiplayer
          This boolean let us know if the game is initialized as multiplayer.
private static java.lang.String obstaclePath
           
private  boolean pause
          True if the game is currently "paused"
private  Player playerOne
          This is one of the mosp important game objects: Player One.
private static java.lang.String playerPath
          And finally to prevent to write paths for images every time we declare them here to make the future changes and coding below easier.
private static java.lang.String playerPath2
           
private  Player playerTwo
          Player Two is created on only multiplayer games.
private  java.util.ArrayList removeList
          This arraylist holds the objects to be removed at the end of each game loop
private  int rounded
          For the widh of the rounded rectangle to be drawn before the screen text
private  SaveManager save
          Our Save manager
private  HighScoreManager score
          Our HighScore manager
private  ScreenManager sm
          Our Screen manager.
private  SoundManager sound
          Our Sound manager
private  boolean timeStop
           
private  long timeStopTime
           
private  boolean waitingForKeyPressed
          On rare occasions, our game will stop for user to press any key on keyboard to continue to prevent any unexpected occurrences.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GameEngine(ScreenManager sm, SaveManager save, SoundManager sound, HighScoreManager score, javax.swing.JPanel cardPanel, java.awt.CardLayout layout, MainPanel mainPanel, boolean multiplayer, int level)
          Here our constructor, GameEngine is being initialized.
 
Method Summary
 void addBalloon()
          Since one level's state depends on number of balloons every time we add a new balloon, we need to hold the exact number of balloons in the list.
 void addEntity(GameEntity entity)
          In this method we add new entity (like bonus or sting) to our GameEntity list.
 void checkCollisions()
          This method doesn't actually check collisions, but instead calls entities to check their own collisions.
 void debug()
           
 void deleteBalloon()
          Same thing as addballoon method applies here but in reverse order.
 void draw(java.awt.Graphics2D g)
          It's pretty much obvious, this method draws every object in the list.
 void initialize(int level, boolean multiplayer)
          This method initializes our game for every level.
 void mainMenu(boolean isMainMenu)
          This method switches between main menu and our game.
 void mainMenuDeath()
           
 void moveEntities(long delta)
          This method moves all the entities in the list.
 void notifyDeath()
          This method decides if the game is over or not.
 void notifyLevelEnd()
          When a level finished we need a player input to begin the new level.
 void pause(boolean pause)
          This method pauses and unpauses the game.
 void removeEntity(GameEntity entity)
          This method removes a certain entity from the list.
 void setTimeStop(boolean timeStop)
          This method manages the Freezer bonus as all entities on the screen except player is to be frozen.
 void startGame()
          This method creates a new thread for game to start.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

debug

private static final boolean debug
This boolean is for testing the GameEngine. Do not change this value to true!

See Also:
Constant Field Values

sm

private ScreenManager sm
Our Screen manager. This variable will be delivered by presentation tier by reference


sound

private SoundManager sound
Our Sound manager


score

private HighScoreManager score
Our HighScore manager


save

private SaveManager save
Our Save manager


layout

private java.awt.CardLayout layout
We need an instance of the layout when we lost our focus on the game screen


cardPanel

private javax.swing.JPanel cardPanel

mainPanel

private MainPanel mainPanel

gameRunning

private boolean gameRunning
True if the game is currently "running", i.e. the game loop is looping


pause

private boolean pause
True if the game is currently "paused"


message

private java.lang.String message
The message to display which waiting for a key press


message2

private java.lang.String message2
The message to display which waiting for a key press


level

private int level
This integer holds the level number. Default value is 1


multiplayer

private boolean multiplayer
This boolean let us know if the game is initialized as multiplayer. Default value is false


waitingForKeyPressed

private boolean waitingForKeyPressed
On rare occasions, our game will stop for user to press any key on keyboard to continue to prevent any unexpected occurrences.


isMainMenu

private boolean isMainMenu
This variable is for determining if the game is on main menu Because game will be paused and it will be prevented from rendering again and again.


rounded

private int rounded
For the widh of the rounded rectangle to be drawn before the screen text


balloonCount

private int balloonCount
This integer holds the number of the valus on the screen to determine if if the level was cleared or not.


timeStop

private boolean timeStop

timeStopTime

private long timeStopTime

entities

private java.util.ArrayList entities
This arraylist holds all the entities on the screen to check collisions and make drawing works easier, withouth checking too many types. In the end, everything on the screen is a GameEntity object.


removeList

private java.util.ArrayList removeList
This arraylist holds the objects to be removed at the end of each game loop


playerOne

private Player playerOne
This is one of the mosp important game objects: Player One. It is created for both single and multiplayer games.


playerTwo

private Player playerTwo
Player Two is created on only multiplayer games. Multiplayer game is decided by the boolean variable @multiplayer


background

private Sprite background
Our game screen background. Just to deliver a better experience. It is a Sprite.


playerPath

private static final java.lang.String playerPath
And finally to prevent to write paths for images every time we declare them here to make the future changes and coding below easier.

See Also:
Constant Field Values

playerPath2

private static final java.lang.String playerPath2
See Also:
Constant Field Values

balloonPath

private static final java.lang.String balloonPath
See Also:
Constant Field Values

obstaclePath

private static final java.lang.String obstaclePath
See Also:
Constant Field Values
Constructor Detail

GameEngine

public GameEngine(ScreenManager sm,
                  SaveManager save,
                  SoundManager sound,
                  HighScoreManager score,
                  javax.swing.JPanel cardPanel,
                  java.awt.CardLayout layout,
                  MainPanel mainPanel,
                  boolean multiplayer,
                  int level)
Here our constructor, GameEngine is being initialized. Our GameEngine is a JPanel to be shuffled in CardLayout system.

Parameters:
sm - ScreenManager object
cardPanel - cardPanel to switch between menu and game
layout - layout to choose between menu and game
multiplayer - to set the game to be initialized to be multiplayer or not
mainPanel - to set menu options like continue button
Method Detail

initialize

public void initialize(int level,
                       boolean multiplayer)
This method initializes our game for every level. It takes two parameters, as level and multiplayer.

Parameters:
level - This parameter helps us to initialize a certain level.
multiplayer - This parameter helps us to initialize second character if our game is multiplayer

addEntity

public void addEntity(GameEntity entity)
In this method we add new entity (like bonus or sting) to our GameEntity list.

Parameters:
entity - is the new entity to be added.

removeEntity

public void removeEntity(GameEntity entity)
This method removes a certain entity from the list. Specifically, it destroys entities once their job is done; but also can prevent ceased player to be drawn on the screen.

Parameters:
entity -

notifyDeath

public void notifyDeath()
This method decides if the game is over or not. It is called from Player class so that player object decides game's state.


notifyLevelEnd

public void notifyLevelEnd()
When a level finished we need a player input to begin the new level.


draw

public void draw(java.awt.Graphics2D g)
It's pretty much obvious, this method draws every object in the list. On some special occasions, this method may alter the transparency and it also draws the stats like scores and level.

Parameters:
g - This parameter is coming from ScreenManager as it is Double Buffered and produces a Graphics2D object.

moveEntities

public void moveEntities(long delta)
This method moves all the entities in the list. Of course, it only calls entities' move method.

Parameters:
delta - This parameter is send to entities to determine how much they will move.

checkCollisions

public void checkCollisions()
This method doesn't actually check collisions, but instead calls entities to check their own collisions. Every game entity is responsible from its own collision and after collision message.


startGame

public void startGame()
This method creates a new thread for game to start. This is the entry point of our actual game, and gameloop takes place here. It's basically a new Runnable and when the level is chosen from game panel, it initializes and starts the game.


pause

public void pause(boolean pause)
This method pauses and unpauses the game. Simply, when specific button is pressed, this method is callsed.

Parameters:
pause - Is game paused?

mainMenu

public void mainMenu(boolean isMainMenu)
This method switches between main menu and our game.

Parameters:
isMainMenu - are we in main menu or game.

mainMenuDeath

public void mainMenuDeath()

debug

public void debug()

addBalloon

public void addBalloon()
Since one level's state depends on number of balloons every time we add a new balloon, we need to hold the exact number of balloons in the list.


deleteBalloon

public void deleteBalloon()
Same thing as addballoon method applies here but in reverse order. However, a new level initialization signal takes here as when the number of balloons are 0, level is finished.


setTimeStop

public void setTimeStop(boolean timeStop)
This method manages the Freezer bonus as all entities on the screen except player is to be frozen.

Parameters:
timeStop - if bonus is taken.