package com.gebauz.burutaru.game.entities.level;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.burutaru.game.entities.Entity;
import com.gebauz.burutaru.game.entities.level.conditions.BaseCondition;
import com.gebauz.burutaru.game.GameLogic;
public class Level extends Entity
{
// Constants========================================================================================
/** Farthest background, behind star field. */
public static final int BACKGROUND_LAYER1 =
0;
/** Background before star field. */
public static final int BACKGROUND_LAYER2 =
1;
public static final int MAIN_LAYER =
2;
public static final int FOREGROUND_LAYER =
3;
public static final int NUM_LAYERS =
4;
public static final int DEFAULT_LAYER = MAIN_LAYER
;
// Embedded Types===================================================================================
// Fields===========================================================================================
private LevelData mLevelData =
null;
private String mLevelFile =
null;
private Vector2 mCurrentPosition =
new Vector2
();
private Vector2 mCurrentScrollVelocity =
new Vector2
();
private int mNextCheckPoint = -
1;
private boolean mHasLevelEnded =
false;
private float mCurrentScrollSpeed = 0.0f
;
private float mTargetScrollSpeed = 0.0f
;
private float mCurrentTime = 0.0f
;
// Methods==========================================================================================
public Level(GameLogic gameLogic
)
{
super(gameLogic
);
mLevelFile = gameLogic.
getCurrentLevelFile();
}
@
Override
public void initAsync
()
{
getAssetManager
().
load(mLevelFile, LevelData.
class,
new LevelDataAsyncLoader.
LevelParameter(this));
}
@
Override
public void init
()
{
mLevelData = getAssetManager
().
get(mLevelFile, LevelData.
class);
mLevelData.
init();
}
@
Override
public void exit
()
{
if (mLevelData
!=
null)
{
mLevelData.
exit();
mLevelData =
null;
}
}
@
Override
public void update
(float deltaTime
)
{
if (hasLevelEnded
())
return;
if ((mNextCheckPoint == -
1) ||
(mNextCheckPoint
>= mLevelData.
getNumCheckPoints()))
{
// done
BaseCondition condition = mLevelData.
getLevelEndedCondition();
if ((condition ==
null) ||
((condition
!=
null) && condition.
isMet()))
{
end
();
}
return;
}
mLevelData.
update(deltaTime
);
updateCheckPoint
(deltaTime
);
mCurrentTime += deltaTime
;
//getGame().log("Current Position: " + mCurrentPosition.x + ", " + mCurrentPosition.y);
}
public void updateCheckPoint
(float deltaTime
)
{
// TODO: ease-in/ease-out parameters!
CheckPoint nextCheckPoint = mLevelData.
getCheckPoint(mNextCheckPoint
);
float dx = nextCheckPoint.
getPosition().
x - mCurrentPosition.
x;
float dy = nextCheckPoint.
getPosition().
y - mCurrentPosition.
y;
final float EASE_DISTANCE = nextCheckPoint.
getScrollSpeed()/
5;
final float l =
(float)Math.
sqrt(dx
*dx + dy
*dy
);
if ((l
< 0.0001f
) && (nextCheckPoint.
isReadyForNextCheckPoint()))
{
getGameLogic
().
getStarField().
switchStatus(nextCheckPoint.
getStarFieldStatus(), nextCheckPoint.
getStarFieldStatusSwitchTime());
// switch checkpoint
mNextCheckPoint++
;
if (mNextCheckPoint
>= mLevelData.
getNumCheckPoints())
{
BaseCondition condition = mLevelData.
getLevelEndedCondition();
if ((condition ==
null) ||
((condition
!=
null) && condition.
isMet()))
{
end
();
}
return;
}
nextCheckPoint = mLevelData.
getCheckPoint(mNextCheckPoint
);
dx = nextCheckPoint.
getPosition().
x - mCurrentPosition.
x;
dy = nextCheckPoint.
getPosition().
y - mCurrentPosition.
y;
//getGame().log("Switch to next Checkpoint: " + nextCheckPoint.getName());
mTargetScrollSpeed = nextCheckPoint.
getScrollSpeed();
}
else if (l
< EASE_DISTANCE
)
{
float overNextSpeed =
0;
if ((nextCheckPoint.
isReadyForNextCheckPoint()) && (mNextCheckPoint
< (mLevelData.
getNumCheckPoints()-
1)))
{
float currScrollSpeed = nextCheckPoint.
getScrollSpeed();
CheckPoint cp = mLevelData.
getCheckPoint(mNextCheckPoint+
1);
float nextScrollSpeed = cp.
getScrollSpeed();
//overNextSpeed = currScrollSpeed + (1-MathUtil.easeInOut(l / EASE_DISTANCE)) * (nextScrollSpeed-currScrollSpeed);
overNextSpeed = currScrollSpeed +
(1-l / EASE_DISTANCE
) * (nextScrollSpeed-currScrollSpeed
);
}
else
{
float currScrollSpeed = nextCheckPoint.
getScrollSpeed();
float t = l / EASE_DISTANCE
;
// ease to zero if not ready for next check point or next checkpoint is last
overNextSpeed = currScrollSpeed +
(1-t
*t
) * (0.1f-currScrollSpeed
);
}
mTargetScrollSpeed = overNextSpeed
;
}
//float nextCheckPointScrollSpeed = nextCheckPoint.getScrollSpeed();
float diff = mTargetScrollSpeed - mCurrentScrollSpeed
;
mCurrentScrollSpeed += diff
* deltaTime
* 15;
mCurrentScrollVelocity.
set(dx, dy
);
mCurrentScrollVelocity.
setLength(Math.
min(mCurrentScrollSpeed
* deltaTime, l
));
mCurrentPosition.
addVector(mCurrentScrollVelocity
);
}
@
Override
public void render
()
{
//getGraphics().getFont().drawText("Sp: " + mCurrentScrollSpeed, 10, 100);
//getGraphics().getFont().drawText("Tg: " + mTargetScrollSpeed, 10, 130);
}
public void render
(int layer
)
{
mLevelData.
render(layer
);
}
/** Called when the level starts/restarts. */
public void start
()
{
mHasLevelEnded =
false;
mLevelData.
start();
mNextCheckPoint =
0;
mCurrentPosition.
setFrom(mLevelData.
getStartPosition());
mTargetScrollSpeed = mLevelData.
getCheckPoint(mNextCheckPoint
).
getScrollSpeed();
mCurrentScrollSpeed = mTargetScrollSpeed
;
mCurrentTime = 0.0f
;
getGameLogic
().
getIntroOutroTextDisplay().
startIntroText();
if (mLevelData.
doWarpIn())
getGameLogic
().
getShip().
startWarpIn();
getGameLogic
().
getStarField().
setStatusImmediately(mLevelData.
getStarFieldStatus());
if ((mLevelData.
getMusic() !=
null) && (!mLevelData.
getMusic().
isEmpty()))
getAudio
().
playMusic("data/music/" + mLevelData.
getMusic(),
true);
}
/** Called when the level ends. */
public void end
()
{
getAudio
().
stopMusic();
getGame
().
log("Level ENDED");
mHasLevelEnded =
true;
mCurrentScrollVelocity.
set(0,
0);
}
// Getters/Setters==================================================================================
public final LevelData getLevelData
() { return mLevelData
; }
public final boolean hasLevelEnded
() { return mHasLevelEnded
; }
public final Vector2 getCurrentPosition
() { return mCurrentPosition
; }
public final Vector2 getCurrentScrollVelocity
() { return mCurrentScrollVelocity
; }
public final float getCurrentTime
() { return mCurrentTime
; }
}