final/src/MapReader.java

146 lines
6.0 KiB
Java

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class MapReader implements Serializable {
public static int x = 0;
public static int y = 0;
public static int TileX = 0;
public static int TileY = 0;
//Input game map
/*
1: Normal Grass
2: Left Grass:
3: Right Grass:
Grass Tiling:
qwe
asd
zxc
!: Slime
v= background
Grass:
*/
//o = steel
// u
// h k Shooting
// m
public static void inputMap(String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
x = 0;
y = 0;
GameFrame.game.enemy.clear();
GameFrame.game.particleTiles.clear();
GameFrame.game.shootingTiles.clear();
GameFrame.game.fireballs.clear();
for(int i=0; i<GameFrame.game.map.length; i++){
Arrays.fill(GameFrame.game.map[i], null);
}
String file = FileManager.readFile(filePath);
for(int i=0; i<file.length(); i++){
if(file.charAt(i)=='\n'){
y+=1;
x=0;
}
TileX = x*Tile.length - (GamePanel.GAME_WIDTH/2);
if(y==0){
TileX += Tile.length;
}
TileY = y*Tile.length;
if(file.charAt(i)=='1'){
newTile("img/tiles/terrain/grass.png");
} else if(file.charAt(i)=='2'){
newTile("img/tiles/terrain/grassLeft.png");
} else if(file.charAt(i)=='3'){
newTile("img/tiles/terrain/grassRight.png");
} else if(file.charAt(i)=='q'){
newTile("img/tiles/terrain/grassTopLeft.png");
} else if(file.charAt(i)=='w'){
newTile("img/tiles/terrain/grassMid.png");
} else if(file.charAt(i)=='e'){
newTile("img/tiles/terrain/grassTopRight.png");
} else if(file.charAt(i)=='a'){
newTile("img/tiles/terrain/grassMiddleLeft.png");
} else if(file.charAt(i)=='s'){
newTile("img/tiles/terrain/grassCenter.png");
} else if(file.charAt(i)=='d'){
newTile("img/tiles/terrain/grassMiddleRight.png");
} else if(file.charAt(i)=='z'){
newTile("img/tiles/terrain/grassBottomLeft.png");
} else if(file.charAt(i)=='x'){
newTile("img/tiles/terrain/grassBottomMiddle.png");
} else if(file.charAt(i)=='c'){
newTile("img/tiles/terrain/grassBottomRight.png");
} else if(file.charAt(i)=='r'){
newTile("img/tiles/terrain/cornerTopLeft.png");
} else if(file.charAt(i)=='t'){
newTile("img/tiles/terrain/cornerTopRight.png");
} else if(file.charAt(i)=='f'){
newTile("img/tiles/terrain/cornerBottomLeft.png");
} else if(file.charAt(i)=='g'){
newTile("img/tiles/terrain/cornerBottomRight.png");
} else if(file.charAt(i)=='b'){
newTile("img/tiles/boxes/box.png");
GameFrame.game.map[x][y].breakable = true;
} else if(file.charAt(i)=='!'){
GameFrame.game.enemy.add(new NonPlayer(TileX, TileY, GameFrame.game.slimeSpriteArray, 50, 28, 100));
} else if(file.charAt(i)=='+') {
newTile("img/tiles/boxes/finish.png");
GameFrame.game.map[x][y].isFinish = true;
GameFrame.game.map[x][y].nonBombCollide = true;
} else if(file.charAt(i)=='v'){
newTile("img/tiles/background/wall.png");
GameFrame.game.map[x][y].collision = false;
GameFrame.game.map[x][y].replaceAble = true;
} else if(file.charAt(i)=='l'){
newTile("img/tiles/terrain/lava.png");
GameFrame.game.map[x][y].nonBombCollide = true;
GameFrame.game.map[x][y].kills = true;
if(y>0&&GameFrame.game.map[x][y-1]==null) {
GameFrame.game.particleTiles.add(GameFrame.game.map[x][y]);
}
} else if(file.charAt(i)=='o'){
newTile("img/tiles/boxes/steel.png");
GameFrame.game.map[x][y].movable = true;
} else if(file.charAt(i)=='h'){
newTile("img/tiles/boxes/boxShootLeft.png");
GameFrame.game.map[x][y].shootingDir = "left";
GameFrame.game.shootingTiles.add(GameFrame.game.map[x][y]);
} else if(file.charAt(i)=='u'){
newTile("img/tiles/boxes/boxShootUp.png");
GameFrame.game.map[x][y].shootingDir = "up";
GameFrame.game.shootingTiles.add(GameFrame.game.map[x][y]);
} else if(file.charAt(i)=='k'){
newTile("img/tiles/boxes/boxShootRight.png");
GameFrame.game.map[x][y].shootingDir = "right";
GameFrame.game.shootingTiles.add(GameFrame.game.map[x][y]);
} else if(file.charAt(i)=='m'){
newTile("img/tiles/boxes/boxShootDown.png");
GameFrame.game.map[x][y].shootingDir = "down";
GameFrame.game.shootingTiles.add(GameFrame.game.map[x][y]);
}
x+=1;
}
}
public static String[] inputDialogue(String mapFilePath) throws IOException {
String filePath = mapFilePath.replace(".txt", "-dialogue.txt");
return FileManager.readFile(filePath).split("\n");
}
public static ArrayList<String[]> inputSign(String signFilePath) throws IOException {
String filePath = signFilePath.replace(".txt", "-signs.txt");
String[] temporaryStringArray = FileManager.readFile(filePath).split("\n");
ArrayList<String[]> returnArray = new ArrayList<String[]>();
for (String s: temporaryStringArray) {
returnArray.add(s.split(" ", 3));
}
return returnArray;
}
public static void newTile(String filePath) throws IOException, SpriteException {
GameFrame.game.map[x][y]=(new SingleTile(TileX,TileY, new BufferedImageWrapper((filePath))));
}
}