package com.gebauz.pingk.game;
import java.util.Vector;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.app.GameObject;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.pingk.effects.FlowerEffect;
import com.gebauz.pingk.effects.OffscreenEffect;
import com.gebauz.pingk.entities.Background;
import com.gebauz.pingk.entities.Ball;
import com.gebauz.pingk.entities.BallTrail;
import com.gebauz.pingk.entities.BallWarp;
import com.gebauz.pingk.entities.Barriers;
import com.gebauz.pingk.entities.CrystalHitCounter;
import com.gebauz.pingk.entities.Crystals;
import com.gebauz.pingk.entities.Entity;
import com.gebauz.pingk.entities.IReflectable;
import com.gebauz.pingk.entities.MenuBar;
import com.gebauz.pingk.entities.Paddle;
import com.gebauz.pingk.entities.PaddleImpact;
import com.gebauz.pingk.entities.PauseScreen;
import com.gebauz.pingk.entities.PlayField;
import com.gebauz.pingk.entities.PowerUpLogic;
import com.gebauz.pingk.entities.PowerUpSystem;
import com.gebauz.pingk.entities.ScoreDisplay;
public class GameLogic
extends GameObject
{
public static final int PHASE_INITIAL_PAUSE =
0;
public static final int PHASE_GAMEPLAY =
1;
public static final int PHASE_SCORE =
2;
private int mPlayPhase = PHASE_INITIAL_PAUSE
;
private boolean mPaused =
false;
private float mInitialPauseCountdown = 2.0f
;
private int mScores
[] =
new int[2];
/* Game entities */
private Background mBackground =
null;
private PlayField mPlayField =
null;
private MenuBar mMenuBar =
null;
private Paddle mPaddles
[] =
new Paddle
[GameConsts.
NUM_PADDLES];
private Ball mBall =
null;
/* Powerup entities. */
private PowerUpSystem mPowerUpSystem =
null;
private Crystals mCrystals =
null;
private CrystalHitCounter mCrystalHitCounter =
null;
private Barriers mBarriers =
null;
/* Effect entities. */
private PaddleImpact mPaddleImpact =
null;
private BallWarp mBallWarp =
null;
private BallTrail mBallTrail =
null;
private OffscreenEffect mOffscreenEffect =
null;
private FlowerEffect mFlowerEffect =
null;
/* Misc entities. */
private ScoreDisplay mScoreDisplay =
null;
private PauseScreen mPauseScreen =
null;
private Vector<Entity> mEntities =
new Vector<Entity>();
private Vector<IReflectable
> mReflectables =
new Vector<IReflectable
>();
private Sound mNewBall =
null;
public GameLogic
(Game game
)
{
super(game
);
}
public void init
()
{
mBackground =
(Background
)addEntity
(new Background
(this));
mPlayField =
(PlayField
)addEntity
(new PlayField
(this));
mMenuBar =
(MenuBar)addEntity
(new MenuBar(this));
mPaddles
[Paddle.
PLAYER_1] =
(Paddle
)addEntity
(new Paddle
(this, Paddle.
PLAYER_1));
mPaddles
[Paddle.
PLAYER_2]=
(Paddle
)addEntity
(new Paddle
(this, Paddle.
PLAYER_2));
mBall =
(Ball
)addEntity
(new Ball
(this));
mPowerUpSystem =
(PowerUpSystem
)addEntity
(new PowerUpSystem
(this));
mCrystals =
(Crystals
)addEntity
(new Crystals
(this));
mCrystalHitCounter =
(CrystalHitCounter
)addEntity
(new CrystalHitCounter
(this));
mBarriers =
(Barriers
)addEntity
(new Barriers
(this));
mPaddleImpact =
(PaddleImpact
)addEntity
(new PaddleImpact
(this));
mBallWarp =
(BallWarp
)addEntity
(new BallWarp
(this));
mBallTrail =
(BallTrail
)addEntity
(new BallTrail
(this, mBall
));
mOffscreenEffect =
(OffscreenEffect
)addEntity
(new OffscreenEffect
(this));
mFlowerEffect =
(FlowerEffect
)addEntity
(new FlowerEffect
(this));
mScoreDisplay =
(ScoreDisplay
)addEntity
(new ScoreDisplay
(this));
mPauseScreen =
(PauseScreen
)addEntity
(new PauseScreen
(this));
for (int i =
0; i
< mEntities.
size(); i++
)
{
mEntities.
get(i
).
init();
}
mScores
[0] =
0;
mScores
[1] =
0;
mInitialPauseCountdown = 2.0f
;
mNewBall = getGame
().
getAudio().
newManagedSound("data/sounds/newball.wav");
}
public void exit
()
{
mReflectables.
clear();
for (int i = mEntities.
size() -
1; i
>=
0; i--
)
{
mEntities.
get(i
).
exit();
}
mEntities.
clear();
mBackground =
null;
mPlayField =
null;
mMenuBar =
null;
mPaddles
[Paddle.
PLAYER_1] =
null;
mPaddles
[Paddle.
PLAYER_2]=
null;
mBall =
null;
mPowerUpSystem =
null;
mCrystals =
null;
mCrystalHitCounter =
null;
mBarriers =
null;
mPaddleImpact =
null;
mBallWarp =
null;
mBallTrail =
null;
mOffscreenEffect =
null;
mFlowerEffect =
null;
mScoreDisplay =
null;
mPauseScreen =
null;
getGame
().
getAudio().
removeManagedSound(mNewBall
);
mNewBall =
null;
}
public void update
(float deltaTime
)
{
mBackground.
update(deltaTime
);
mMenuBar.
update(deltaTime
);
mPauseScreen.
update(deltaTime
);
if (isPaused
())
return;
mPlayField.
update(deltaTime
);
updateStates
(deltaTime
);
mScoreDisplay.
update(deltaTime
);
}
public void updateStates
(float deltaTime
)
{
float factor = 1.0f
;
if (getPowerUpLogic
().
isSlowMoActive())
factor = GameConsts.
POWERUP_SLOWMO_FACTOR;
float dt = deltaTime
* factor
;
if (mPlayPhase == PHASE_SCORE
)
{
mPaddles
[Paddle.
PLAYER_1].
update(dt
);
mPaddles
[Paddle.
PLAYER_2].
update(dt
);
mPaddleImpact.
update(dt
);
mBallWarp.
update(dt
);
mBallTrail.
updateFade(deltaTime
);
mPowerUpSystem.
update(dt
);
mOffscreenEffect.
update(dt
);
mCrystals.
update(dt
);
mCrystalHitCounter.
update(dt
);
mBarriers.
update(dt
);
mFlowerEffect.
update(dt
);
if (mScoreDisplay.
isDone())
{
startGameRound
();
}
}
else if (mPlayPhase == PHASE_GAMEPLAY
)
{
mPaddleImpact.
update(dt
);
mPaddles
[Paddle.
PLAYER_1].
update(dt
);
mPaddles
[Paddle.
PLAYER_2].
update(dt
);
mBall.
update(dt
);
mBallWarp.
update(dt
);
mBallTrail.
update(deltaTime
);
mPowerUpSystem.
update(dt
);
mOffscreenEffect.
update(dt
);
mCrystals.
update(dt
);
mCrystalHitCounter.
update(dt
);
mBarriers.
update(dt
);
mFlowerEffect.
update(dt
);
}
else if (mPlayPhase == PHASE_INITIAL_PAUSE
)
{
mPaddleImpact.
update(dt
);
mPaddles
[Paddle.
PLAYER_1].
update(dt
);
mPaddles
[Paddle.
PLAYER_2].
update(dt
);
mBallWarp.
update(dt
);
mInitialPauseCountdown -= dt
;
if (mInitialPauseCountdown
< 0.0f
)
{
startGameRound
();
}
}
}
public void render
()
{
// render regular game screen to offscreen texture
mOffscreenEffect.
beginRenderToTexture();
renderGame
();
mOffscreenEffect.
endRenderToTexture();
// render with offscreen effects
mOffscreenEffect.
render();
mMenuBar.
render();
mPauseScreen.
render();
}
public void renderGame
()
{
mBackground.
render();
mPaddles
[Paddle.
PLAYER_1].
render();
mPaddles
[Paddle.
PLAYER_2].
render();
mPaddleImpact.
render();
if (mPlayPhase == PHASE_SCORE
)
{
mBallTrail.
render();
}
else if (mPlayPhase == PHASE_GAMEPLAY
)
{
mBallTrail.
render();
mBall.
render();
mBallWarp.
render();
}
mPowerUpSystem.
render();
mCrystals.
render();
mFlowerEffect.
render();
mCrystalHitCounter.
render();
mBarriers.
render();
mPlayField.
render();
mScoreDisplay.
render();
}
public void startGameRound
()
{
mPlayPhase = PHASE_GAMEPLAY
;
mBall.
reset((mScoreDisplay.
getLastPlayerScored() ==
0) ? 1 :
0);
mBallWarp.
reset();
mOffscreenEffect.
startFlash(GameConsts.
SCREENFLASH_BALLSPAWN_TIME);
getGame
().
getAudio().
playSound(mNewBall
);
}
/** Player gains a point, game resets ball. */
public void playerGainsScore
(int playerIndex,
int lastPaddleTouched
)
{
mPlayPhase = PHASE_SCORE
;
int scoreGained =
1;
if (mCrystalHitCounter.
getCurrentHits() > 2)
{
// X points
scoreGained = mCrystalHitCounter.
getCurrentHits();
}
mScores
[playerIndex
] += scoreGained
;
if ((lastPaddleTouched == -
1) ||
(lastPaddleTouched == playerIndex
))
{
// score normal
mScoreDisplay.
startDisplay(playerIndex, ScoreDisplay.
MODE_SCORE, scoreGained
);
}
else
{
// own goal
mScoreDisplay.
startDisplay(playerIndex, ScoreDisplay.
MODE_OWNGOAL, scoreGained
);
}
mCrystalHitCounter.
resetHits();
mOffscreenEffect.
startFlash(GameConsts.
SCREENFLASH_SCORE_TIME);
}
public void pause
(boolean doPause
)
{
mPaused = doPause
;
mPauseScreen.
startShowing();
}
public boolean isPaused
() { return mPaused
; }
/** Adds an entity to the list and returns it. */
public Entity addEntity
(Entity entity
)
{
mEntities.
add(entity
);
return entity
;
}
/** Add an entity to the reflectables list. */
public void addReflectable
(IReflectable reflectable
)
{
mReflectables.
add(reflectable
);
}
/** Convert from dip to virtual coordinates. */
public float dipToVirtualX
(float dip
)
{
return (dip
* getGraphics
().
getDipToPixelScale() + 0.5f
) * (GameConsts.
VIRTUAL_SCREEN_WIDTH /
(float)getGraphics
().
getWidth());
}
/** Convert from dip to virtual coordinates. */
public float dipToVirtualY
(float dip
)
{
return (dip
* getGraphics
().
getDipToPixelScale() + 0.5f
) * (GameConsts.
VIRTUAL_SCREEN_HEIGHT /
(float)getGraphics
().
getHeight());
}
public boolean isOutOfBoard
(float posX,
float posY
)
{
if ((posY
< GameConsts.
PLAYFIELD_BORDER_Y) ||
(posY
> getPlayField
().
getVirtualHeight() - GameConsts.
PLAYFIELD_BORDER_Y))
return true;
if ((posX
< GameConsts.
PLAYFIELD_BORDER_X) ||
(posX
> GameConsts.
VIRTUAL_SCREEN_WIDTH - GameConsts.
PLAYFIELD_BORDER_X))
return true;
return false;
}
public boolean isOutOfBoardInner
(float posX,
float posY
)
{
if ((posY
< GameConsts.
PLAYFIELD_BORDER_Y) ||
(posY
> getPlayField
().
getVirtualHeight() - GameConsts.
PLAYFIELD_BORDER_Y))
return true;
if ((posX
< GameConsts.
PADDLE_DISTANCE_FROM_EDGE_X + GameConsts.
PADDLE_WIDTH) ||
(posX
> GameConsts.
VIRTUAL_SCREEN_WIDTH - GameConsts.
PADDLE_DISTANCE_FROM_EDGE_X - GameConsts.
PADDLE_WIDTH))
return true;
return false;
}
/** Get i-th reflectable. */
public IReflectable getReflectable
(int i
)
{
return mReflectables.
get(i
);
}
/** Get number of reflectables. */
public int getNumReflectables
()
{
return mReflectables.
size();
}
public final Graphics getGraphics
()
{
return getGame
().
getGraphics();
}
public final Background getBackground
() { return mBackground
; }
public final PlayField getPlayField
() { return mPlayField
; }
public final MenuBar getMenuBar
() { return mMenuBar
; }
public final Ball getBall
() { return mBall
; }
public final Paddle getPaddle
(int playerIndex
) { return mPaddles
[playerIndex
]; }
public final PowerUpSystem getPowerUpSystem
() { return mPowerUpSystem
; }
public final PowerUpLogic getPowerUpLogic
() { return mPowerUpSystem.
getPowerUpLogic(); }
public final Crystals getCrystals
() { return mCrystals
; }
public CrystalHitCounter getCrystalHitCounter
() { return mCrystalHitCounter
; }
public Barriers getBarriers
() { return mBarriers
; }
public final BallTrail getBallTrail
() { return mBallTrail
; }
public final PaddleImpact getPaddleImpact
() { return mPaddleImpact
; }
public final int getScore
(int playerIndex
) { return mScores
[playerIndex
]; }
public int getPlayPhase
() { return mPlayPhase
; }
}