using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BurutaruEditor.file.elements;
using BurutaruEditor.file.enemies;
using BauzoidNET.parser;
namespace BurutaruEditor
.file
{
public class LevelUtil
{
private LevelUtil
() { }
public static bool LoadLevel
(String filename, Document doc
)
{
string text
= Preprocessor
.stripComments(File
.ReadAllText(filename
));
Tokenizer t
= new Tokenizer
(text
);
t
.setStringDelimiter(new char[] { '\'',
'"' });
while (!t
.checkNoMoreTokens())
{
String identifier
= t
.readIdentifier();
// name
if (doc
.ReadParameter(t, identifier
))
{
continue;
}
// enemy spawnification
else if (identifier
.Equals("spawn", StringComparison
.OrdinalIgnoreCase))
{
parseSpawnEnemy
(doc, t
);
}
else if (identifier
.Equals("CheckPoint", StringComparison
.OrdinalIgnoreCase))
{
// check checkpoint objects
parsecheckPoint
(doc, t
);
}
else
{
// check condition objects
/*if (parseConditionObject(levelData, t, identifier))
continue;
// check command objects
if (parseLevelObject(levelData, t, identifier, BaseCommand.class))
continue;
// check element objects
if (parseLevelObject(levelData, tokenizer, identifier, BaseLevelElement.class))
continue;*/
if (parseLevelElement
(doc, t, identifier
))
continue;
//Gdx.app.log(LOG_TAG, identifier + " not found as applicable Level Object!");
t
.skipUntilAfterToken("}");
}
}
return true;
}
private static bool parsecheckPoint
(Document doc, Tokenizer tokenizer
)
{
string checkPointName
= tokenizer
.readString();
CheckPoint checkPoint
= new CheckPoint
(doc, checkPointName
);
tokenizer
.readToken("{");
while (!tokenizer
.checkToken("}"))
{
String nextIdentifier
= tokenizer
.readIdentifier();
checkPoint
.ReadParameter(tokenizer, nextIdentifier
);
}
tokenizer
.readToken("}");
doc
.AddCheckPoint(checkPoint
);
return false;
}
private static void parseSpawnEnemy
(Document doc, Tokenizer tokenizer
)
{
string enemyType
= tokenizer
.readIdentifier();
string enemyName
= tokenizer
.readString();
// TODO: parse enemy spawn
EnemySpawner enemy
= EnemySpawner
.createSpawner(doc, enemyType, enemyName
);
tokenizer
.readToken("{");
while (!tokenizer
.checkToken("}"))
{
String nextIdentifier
= tokenizer
.readIdentifier();
enemy
.ReadParameter(tokenizer, nextIdentifier
);
}
tokenizer
.readToken("}");
doc
.AddEnemySpawner(enemy
);
}
private static bool parseLevelElement
(Document doc, Tokenizer t,
string identifier
)
{
if (!t
.checkString())
return false;
int position
= t
.getPosition();
string elementName
= t
.readString();
LevelElement e
= LevelElement
.CreateElement(doc, identifier, elementName
);
if (e
== null)
{
// not a level element
t
.setPosition(position
);
return false;
}
t
.readToken("{");
while (!t
.checkToken("}"))
{
String nextIdentifier
= t
.readIdentifier();
e
.ReadParameter(t, nextIdentifier
);
}
t
.readToken("}");
e
.Init();
doc
.AddLevelElement(e
);
return true;
}
/*
private static boolean parsecheckPoint(LevelData levelData, Tokenizer tokenizer) throws ScanException
{
String checkPointName = tokenizer.readString();
CheckPoint checkPoint = new CheckPoint(levelData.getLevel(), checkPointName);
tokenizer.readToken("{");
while (!tokenizer.checkToken("}"))
{
String nextIdentifier = tokenizer.readIdentifier();
checkPoint.readParameter(tokenizer, nextIdentifier);
}
tokenizer.readToken("}");
levelData.addCheckPoint(checkPoint);
return false;
}
private static boolean parseConditionObject(LevelData levelData, Tokenizer tokenizer, String className) throws ScanException
{
Class<? extends BaseCondition> conditionClass = null;
try
{
conditionClass = Class.forName(BaseCondition.class.getPackage().getName() + "." + className).asSubclass(BaseCondition.class);
}
catch (ClassNotFoundException ex)
{
// not a BaseCondition
return false;
}
String objectName = tokenizer.readString();
BaseCondition instance = null;
try
{
instance = conditionClass.getDeclaredConstructor(Level.class, String.class).newInstance(levelData.getLevel(), objectName);
}
catch (Exception ex)
{
throw new ScanException(Consts.LOG_TAG, ex.getLocalizedMessage() + " while instantiating " + className);
}
tokenizer.readToken("{");
while (!tokenizer.checkToken("}"))
{
String nextIdentifier = tokenizer.readIdentifier();
instance.readParameter(tokenizer, nextIdentifier);
}
tokenizer.readToken("}");
levelData.addCondition(instance);
return true;
}
private static <T> boolean parseLevelObject(LevelData levelData, Tokenizer tokenizer, String className, Class<T> baseClass) throws ScanException
{
// check if element type exists as LevelObject-derived class
Class<? extends T> levelObjectClass = null;
try
{
//mNextStateClass = Class.forName(nextState);
levelObjectClass = Class.forName(baseClass.getPackage().getName() + "." + className).asSubclass(baseClass);
}
catch (ClassNotFoundException ex)
{
// not a LevelObject
return false;
}
String objectName = tokenizer.readString();
// Create instance of class
LevelObject obj = null;
try
{
obj = (LevelObject)levelObjectClass.getDeclaredConstructor(Level.class, String.class).newInstance(levelData.getLevel(), objectName);
}
catch (Exception ex)
{
throw new ScanException(Consts.LOG_TAG, ex.getLocalizedMessage() + " while instantiating " + className);
}
tokenizer.readToken("{");
while (!tokenizer.checkToken("}"))
{
String nextIdentifier = tokenizer.readIdentifier();
obj.readParameter(tokenizer, nextIdentifier);
}
tokenizer.readToken("}");
if (obj instanceof BaseCommand)
{
Gdx.app.log(LOG_TAG, "Created Command " + obj.getClass().getSimpleName() + " instance '" + objectName + "'");
levelData.addCommand((BaseCommand)obj);
}
else if (obj instanceof BaseLevelElement)
{
Gdx.app.log(LOG_TAG, "Created Level Element " + obj.getClass().getSimpleName() + " instance '" + objectName + "'");
levelData.addLevelObject((BaseLevelElement)obj);
}
return true;
}
*/
public static bool SaveLevel
(String filename, Document doc
)
{
return true;
}
}
}