package com.gebauz.burutaru.game.entities.level;
import java.io.IOException;
import java.util.Vector;
import com.badlogic.gdx.Gdx;
import com.gebauz.bauzoid.app.Consts;
import com.gebauz.bauzoid.file.File;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.parser.ParseUtil;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;
import com.gebauz.burutaru.game.entities.StarField;
import com.gebauz.burutaru.game.entities.level.commands.BaseCommand;
import com.gebauz.burutaru.game.entities.level.conditions.BaseCondition;
import com.gebauz.burutaru.game.entities.level.elements.BaseLevelElement;
public class LevelData
{
// Constants========================================================================================
// Embedded Types===================================================================================
// Fields===========================================================================================
protected Level mLevel =
null;
private Vector<BaseCommand
> mCommandList =
new Vector<BaseCommand
>();
private Vector<BaseLevelElement
> mLevelElements =
new Vector<BaseLevelElement
>();
private Vector<CheckPoint
> mCheckPoints =
new Vector<CheckPoint
>();
private Vector<BaseCondition
> mConditions =
new Vector<BaseCondition
>();
private String mLevelName =
null;
private String mStageTitle =
null;
private Vector2 mStartPosition =
new Vector2
();
private boolean mDoWarpIn =
true;
private StarField.
Status mStarFieldStatus = StarField.
Status.
WARP;
private String mLevelEndConditionName =
null;
private BaseCondition mLevelEndCondition =
null;
private String mLevelMusic =
null;
// Methods==========================================================================================
public LevelData
(Level level
)
{
mLevel = level
;
}
public void initAsync
()
{
// kick off async loading - this is most likely called from the LevelDataAsyncLoader
for (int i =
0; i
< mLevelElements.
size(); i++
)
{
mLevelElements.
get(i
).
initAsync();
}
for (int i =
0; i
< mCommandList.
size(); i++
)
{
mCommandList.
get(i
).
initAsync();
}
for (int i =
0; i
< mCheckPoints.
size(); i++
)
{
mCheckPoints.
get(i
).
initAsync();
}
for (int i =
0; i
< mConditions.
size(); i++
)
{
mConditions.
get(i
).
initAsync();
}
}
public void init
()
{
// async loading is done, so tie everything together
Gdx.
app.
log(Consts.
LOG_TAG,
"Level Name: " + mLevelName
);
for (int i =
0; i
< mLevelElements.
size(); i++
)
{
mLevelElements.
get(i
).
init();
}
for (int i =
0; i
< mCommandList.
size(); i++
)
{
mCommandList.
get(i
).
init();
}
for (int i =
0; i
< mCheckPoints.
size(); i++
)
{
mCheckPoints.
get(i
).
init();
}
for (int i =
0; i
< mConditions.
size(); i++
)
{
mConditions.
get(i
).
init();
}
if (mLevelEndConditionName
!=
null)
mLevelEndCondition = getLevel
().
getLevelData().
findCondition(mLevelEndConditionName
);
}
public void start
()
{
}
public void exit
()
{
for (int i =
0; i
< mLevelElements.
size(); i++
)
{
mLevelElements.
get(i
).
exit();
}
for (int i =
0; i
< mCommandList.
size(); i++
)
{
mCommandList.
get(i
).
exit();
}
for (int i =
0; i
< mCheckPoints.
size(); i++
)
{
mCheckPoints.
get(i
).
exit();
}
for (int i =
0; i
< mConditions.
size(); i++
)
{
mConditions.
get(i
).
exit();
}
}
public void update
(float deltaTime
)
{
for (int i =
0; i
< mLevelElements.
size(); i++
)
{
mLevelElements.
get(i
).
update(deltaTime
);
}
for (int i =
0; i
< mCommandList.
size(); i++
)
{
mCommandList.
get(i
).
update(deltaTime
);
}
for (int i =
0; i
< mCheckPoints.
size(); i++
)
{
mCheckPoints.
get(i
).
update(deltaTime
);
}
for (int i =
0; i
< mConditions.
size(); i++
)
{
mConditions.
get(i
).
update(deltaTime
);
}
}
public void render
(int layer
)
{
for (int i =
0; i
< mLevelElements.
size(); i++
)
{
BaseLevelElement obj = mLevelElements.
get(i
);
if (obj.
layer != layer
)
continue;
// do not render level objects before they actually spawn
if (obj.
hasSpawned())
{
obj.
render();
}
}
}
public void addLevelObject
(BaseLevelElement object
)
{
mLevelElements.
add(object
);
}
public void addCommand
(BaseCommand object
)
{
mCommandList.
add(object
);
}
public void addCheckPoint
(CheckPoint object
)
{
mCheckPoints.
add(object
);
}
public void addCondition
(BaseCondition object
)
{
mConditions.
add(object
);
}
public BaseCondition findCondition
(String conditionName
)
{
for (int i =
0; i
< mConditions.
size(); i++
)
{
BaseCondition condition = mConditions.
get(i
);
if (condition.
getName().
equalsIgnoreCase(conditionName
))
return condition
;
}
return null;
}
public void readBinary
(File file
) throws IOException
{
mLevelName = file.
readString();
mStageTitle = file.
readString();
mStartPosition = file.
readVector2();
mDoWarpIn = file.
readBool32();
int status = file.
readInt();
StarField.
Status[] values = StarField.
Status.
values();
mStarFieldStatus = values
[MathUtil.
clamp(status,
0, values.
length-
1)];
mLevelEndConditionName = file.
readString();
mLevelMusic = file.
readString();
}
public boolean readParameter
(Tokenizer tokenizer,
String identifier
) throws ScanException
{
if (identifier.
equalsIgnoreCase("name"))
{
mLevelName = tokenizer.
readString();
}
else if (identifier.
equalsIgnoreCase("stage"))
{
mStageTitle = tokenizer.
readString();
}
else if (identifier.
equalsIgnoreCase("startPosition"))
{
mStartPosition = ParseUtil.
readVector2(tokenizer
);
}
else if (identifier.
equalsIgnoreCase("warpIn"))
{
mDoWarpIn =
(tokenizer.
readIdentifier().
equalsIgnoreCase("true"));
}
else if (identifier.
equalsIgnoreCase("starFieldStatus"))
{
int status =
(int)tokenizer.
readNumber();
StarField.
Status[] values = StarField.
Status.
values();
mStarFieldStatus = values
[MathUtil.
clamp(status,
0, values.
length-
1)];
}
else if (identifier.
equalsIgnoreCase("levelEndCondition"))
{
mLevelEndConditionName = tokenizer.
readString();
}
else if (identifier.
equalsIgnoreCase("levelMusic"))
{
mLevelMusic = tokenizer.
readString();
}
else
{
return false;
}
tokenizer.
readToken(";");
return true;
}
public BaseLevelElement findLevelElement
(String name
)
{
for (int i =
0; i
< getNumLevelElements
(); i++
)
{
BaseLevelElement e = getLevelElement
(i
);
if (e.
getName().
equalsIgnoreCase(name
))
return e
;
}
return null;
}
// Getters/Setters==================================================================================
public final Level getLevel
() { return mLevel
; }
public final void setName
(String name
) { mLevelName = name
; }
public final String getName
() { return mLevelName
; }
public final void setstage
(String name
) { mStageTitle = name
; }
public final String getStage
() { return mStageTitle
; }
public final int getNumLevelElements
() { return mLevelElements.
size(); }
public final BaseLevelElement getLevelElement
(int i
) { return mLevelElements.
get(i
); }
public final int getNumCommands
() { return mCommandList.
size(); }
public final BaseCommand getCommand
(int i
) { return mCommandList.
get(i
); }
public final int getNumCheckPoints
() { return mCheckPoints.
size(); }
public final CheckPoint getCheckPoint
(int i
) { return mCheckPoints.
get(i
); }
public final Vector2 getStartPosition
() { return mStartPosition
; }
public final boolean doWarpIn
() { return mDoWarpIn
; }
public final StarField.
Status getStarFieldStatus
() { return mStarFieldStatus
; }
public final BaseCondition getLevelEndedCondition
() { return mLevelEndCondition
; }
public final String getMusic
() { return mLevelMusic
; }
}