Hello and good day.
I work with the Sprite library by Peter Lager.
My current project is a 2D platformer and I got a rly strange problem but I cant't find the exact source.
I restricted the problem of the overlapping Sprites to the WorldManager class in the void registerWorld(int index) function.
I use S4P.updateSprites(sw.getElapsedTime()); S4P.drawSprites();
to display the sprites.
It seems that all 3 worlds get registered by S4P at the same time.
The content of the 3 worldfiles I got here is like that,
OOOOOOOOOO
O________O
OZZZZZZZZO
//GameProject_Disconnect
import sprites.*;
import sprites.maths.*;
import sprites.utils.*;
import java.util.*;
import java.lang.*;
StopWatch sw;
WorldManager wm;
void setup() {
size(500, 500);
sw = new StopWatch();
wm = new WorldManager(this);
}
void draw() {
background(0);
S4P.updateSprites(sw.getElapsedTime());
S4P.drawSprites();
}
//WorldManager
class WorldManager {
FileManager fm;
WorldTranslater wt;
ArrayList<World> worlds;
WorldManager(PApplet _pa) {
fm = new FileManager();
wt = new WorldTranslater(_pa);
initWorlds();
registerWorld(0);
}
void registerWorld(int index) {
Sprite[][] tempW = worlds.get(index).getWorldMap();
for (int i=0; i < 5; i++) {
for (int j=0; j < 10; j++) {
S4P.registerSprite(tempW[i][j]);
}
}
}
private void initWorlds() {
worlds = new ArrayList<World>();
for (int i=0; i < fm.getNumberOfWorlds(); i++) {
World tempWorld = new World(
fm.getFileContent(i),
wt.translateWorld(fm.getFileContent(i)),
fm.getTrimmedFilename(i),
fm.getWorldPath(i));
worlds.add(new World());
worlds.set(i, tempWorld);
}
println("Worlds successful loaded!");
}
}
//FileManager
class FileManager {
private String worldPath; // Path of the world files.
private String[] rawFilenames; // Filenames with prefix and suffix.
private String[] trimmedFilenames; // Filenames without prefix and suffix.
private ArrayList<String[]> fileContent; // Save the file content here.
private int numberOfFiles; // Number of worlds found.
FileManager() {
worldPath = new String(sketchPath() + "/Worlds/");
readFileDirectory();
numberOfFiles = rawFilenames.length;
trimFilenames();
fileContent = new ArrayList<String[]>();
initFileContent();
}
// Give me the content of one file chosen by index.
String[] getFileContent(int index) {
return fileContent.get(index);
}
int getNumberOfWorlds() {
return numberOfFiles;
}
String getWorldPath(int index) {
return new String(worldPath + rawFilenames[index]);
}
String getRawFilename(int index) {
return rawFilenames[index];
}
String getTrimmedFilename(int index) {
return trimmedFilenames[index];
}
private void initFileContent() {
for (int i=0; i < numberOfFiles; i++) {
fileContent.add(new String[0]);
fileContent.set(i, parseFile(rawFilenames[i]));
}
}
// Get all filenames from the world directory.
private void readFileDirectory() {
rawFilenames = getFilenames(worldPath);
}
// Cut the prefix and suffix off.
private void trimFilenames() {
trimmedFilenames = cutFilenames(getFilenames(worldPath));
}
// Reade the content of the file.
private String[] parseFile(String filename) {
BufferedReader reader = createReader(worldPath + filename);
String line = null;
String[] tempWorld = new String[5];
int counter = 0;
try {
while ((line = reader.readLine()) != null) {
tempWorld[counter] = line;
counter++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
return tempWorld;
}
// Return all the raw filenames in a directory as an array of Strings.
private String[] getFilenames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String[] names = file.list();
return names;
} else {
// If it's not a directory.
println("ERROR: There is no worlds directory!");
return null;
}
}
// Delete the prefix and the suffix of the filename.
private String[] cutFilenames(String[] filenames) {
String[] names = filenames;
for (int i=0; i < names.length; i++) {
names[i] = names[i].substring(2, names[i].length()-4); // -4 for the ".txt" suffix.
}
return names;
}
}
//WorldTranslater
class WorldTranslater {
PApplet pa;
// Agreements for the height and width of a world.
final int mapWidth = 10;
final int mapHeight = 5;
private String[] crypticWorld;
private Sprite[][] translatedWorld;
final String wall = "Sprites/Wall.png";
final String background = "Sprites/Background.png";
final String ground = "Sprites/Ground.png";
final String platform = "Sprites/Platform2.png";
final String nothing = "Sprites/Nothing.png";
final int viewDistance = 1; // The higher the number, the closer is the picture in the foreground.
WorldTranslater(PApplet _pa) {
pa = _pa;
crypticWorld = null;
translatedWorld = new Sprite[mapHeight][mapWidth];
}
// Give me the fancy sprite array from this cryptical String array plz.
public Sprite[][] translateWorld(String[] cw) {
crypticWorld = cw;
translateCWorld();
return translatedWorld;
}
// Translate the crypticWorld to a world with fancy sprites.
private void translateCWorld() {
Sprite sp;
for (int i=0; i < mapHeight; i++) {
for (int j=0; j < mapWidth; j++) {
sp = distinguishSprite(crypticWorld[i].charAt(j));
sp.setPos(new Vector2D(sp.getWidth()*j, sp.getHeight()*i));
translatedWorld[i][j] = sp;
}
}
}
// Check which token is which sprite.
private Sprite distinguishSprite(char c) {
if (c == 'O') {
return new Sprite(pa, wall, viewDistance);
} else if (c == 'X') {
return new Sprite(pa, background, viewDistance);
} else if (c == 'Z') {
return new Sprite(pa, ground, viewDistance);
} else if (c == '_') {
return new Sprite(pa, platform, viewDistance);
} else if (c == ' ') {
return new Sprite(pa, nothing, viewDistance);
}
println("ERROR: Invalid token");
return null;
}
};
//WorldObjekt
class World {
private String[] rawWorldMap;
private Sprite[][] worldMap;
private String name;
private String path;
World() {
rawWorldMap = null;
worldMap = null;
name = null;
path = null;
}
World(String[] _rawWorldMap, Sprite[][] _worldMap, String _name, String _path) {
rawWorldMap = _rawWorldMap;
worldMap = _worldMap;
name = _name;
path = _path;
}
public Sprite[][] getWorldMap() {
return worldMap;
}
String[] getRawWorldMap() {
return rawWorldMap;
}
String getName() {
return name;
}
String getPath() {
return path;
}
};