Change how maps are loaded so they are more dynamic. When going from gameplay to editing mode events may have been consumed.

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago committed by GitHub
parent 2a29e12be5
commit 107adc304c
  1. 5
      src/sig/RabiClone.java
  2. 18
      src/sig/map/DataTile.java
  3. 59
      src/sig/map/Map.java
  4. 2
      src/sig/map/Maps.java

@ -51,7 +51,7 @@ public class RabiClone {
public static ConfigureControls control_settings_menu; public static ConfigureControls control_settings_menu;
public static Player player; public static Player player;
public static Maps CURRENT_MAP = Maps.WORLD1; public static Maps CURRENT_MAP;
public static Controller[] CONTROLLERS = new Controller[] {}; public static Controller[] CONTROLLERS = new Controller[] {};
public static long lastControllerScan = System.currentTimeMillis(); public static long lastControllerScan = System.currentTimeMillis();
@ -73,6 +73,8 @@ public class RabiClone {
f.setUndecorated(true); f.setUndecorated(true);
f.setSize(BASE_WIDTH, BASE_HEIGHT); // 1024x576 (64x64) f.setSize(BASE_WIDTH, BASE_HEIGHT); // 1024x576 (64x64)
ChooseBestRatio(); ChooseBestRatio();
Map.LoadMap(Maps.WORLD1);
p = new Panel(f); p = new Panel(f);
@ -118,6 +120,7 @@ public class RabiClone {
if (!(level_renderer instanceof EditorRenderer)) { if (!(level_renderer instanceof EditorRenderer)) {
OBJ.clear(); OBJ.clear();
ResetGame(); ResetGame();
Map.LoadMap(Maps.WORLD1,CURRENT_MAP.getMap());
OBJ.add(level_renderer = new EditorRenderer(p)); OBJ.add(level_renderer = new EditorRenderer(p));
} }
} }

@ -0,0 +1,18 @@
package sig.map;
import sig.engine.String;
public enum DataTile {
NULL, //File is populated by 0s by default. This represents nothing.
BUN1("Spawns a blue bun"),
BUN2("Spawns a green bun"),
BUN3("Spawns a yellow bun"),
BUN4("Spawns a red bun");
String description;
DataTile(){}
DataTile(java.lang.String s) {
this.description=new String(s);
}
}

@ -3,8 +3,10 @@ package sig.map;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import sig.RabiClone; import sig.RabiClone;
import sig.engine.Sprite; import sig.engine.Sprite;
@ -13,6 +15,7 @@ public class Map {
//Maps contain 512x288 tiles, allowing for 16384x9216 pixels of action per map. //Maps contain 512x288 tiles, allowing for 16384x9216 pixels of action per map.
//Since a screen normally fits 16x9 tiles, you get 32x32 (1024) screens of gameplay per world. //Since a screen normally fits 16x9 tiles, you get 32x32 (1024) screens of gameplay per world.
// //
//Starts with 294912 bytes for visual tiles.
//After the map data, the next 1024 bytes will indicate the map view information. //After the map data, the next 1024 bytes will indicate the map view information.
//After that, the next 1024 bytes will indicate the background information. //After that, the next 1024 bytes will indicate the background information.
//After that, the next 1024 bytes will indicate the map color information. //After that, the next 1024 bytes will indicate the map color information.
@ -45,7 +48,14 @@ public class Map {
public static Map LoadMap(Maps map) { public static Map LoadMap(Maps map) {
try { try {
Map newMap = new Map(); if (RabiClone.CURRENT_MAP!=map) {
resetMapData(RabiClone.CURRENT_MAP.getMap());
} else {
resetAndReloadEventData(RabiClone.CURRENT_MAP);
return RabiClone.CURRENT_MAP.getMap();
}
RabiClone.CURRENT_MAP=map;
Map newMap = RabiClone.CURRENT_MAP.getMap()!=null?RabiClone.CURRENT_MAP.getMap():new Map();
DataInputStream stream = new DataInputStream(new FileInputStream(map.getFile())); DataInputStream stream = new DataInputStream(new FileInputStream(map.getFile()));
int marker=0; int marker=0;
int iterationCount=MAP_WIDTH*MAP_HEIGHT; int iterationCount=MAP_WIDTH*MAP_HEIGHT;
@ -107,6 +117,41 @@ public class Map {
return null; return null;
} }
private static void resetAndReloadEventData(Maps map) {
Arrays.fill(map.getMap().data,(char)0);
map.getMap().eventTileCount=0;
try {
DataInputStream stream = new DataInputStream(new FileInputStream(map.getFile()));
final long requestBytes = 294912+1024+1024+1024+1024;
long actualBytes = stream.skip(requestBytes);
if (actualBytes!=requestBytes) {
stream.close();
throw new IOException("Could not read "+requestBytes+" bytes! Read "+actualBytes+" instead.");
}
map.getMap().eventTileCount=stream.readInt();
int remainingCount = map.getMap().eventTileCount;
while (remainingCount-->0) {
int dataPacket = stream.readInt();
//First 14 bits are event info. Last 18 bits are index info.
char event = (char)(dataPacket>>>18);
int index = dataPacket&0b00000000000000111111111111111111;
map.getMap().data[index]=event;
}
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void resetMapData(Map newMap) {
Arrays.fill(newMap.tiles,(char)0);
Arrays.fill(newMap.views,(byte)0);
Arrays.fill(newMap.backgrounds,(byte)0);
Arrays.fill(newMap.colors,(byte)0);
Arrays.fill(newMap.types,(byte)0);
}
public static void SaveMap(Maps map) throws IOException { public static void SaveMap(Maps map) throws IOException {
DataOutputStream stream = new DataOutputStream(new FileOutputStream(map.getFile())); DataOutputStream stream = new DataOutputStream(new FileOutputStream(map.getFile()));
saveCharData(stream,map.map.tiles); saveCharData(stream,map.map.tiles);
@ -158,4 +203,16 @@ public class Map {
tiles[y*Map.MAP_WIDTH+x]=(char)(t.ordinal()); tiles[y*Map.MAP_WIDTH+x]=(char)(t.ordinal());
//System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+"."); //System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+".");
} }
public void ModifyDataTile(int x,int y,DataTile t) {
DataTile prevTile = DataTile.values()[data[y*Map.MAP_WIDTH+x]];
if (prevTile.ordinal()==0) {
eventTileCount++;
}
if (t.ordinal()==0) {
eventTileCount--;
}
data[y*Map.MAP_WIDTH+x]=(char)(t.ordinal());
//System.out.println("Tile "+(y*MAP_WIDTH+x)+" is now "+tiles[y*MAP_WIDTH+x]+".");
}
} }

@ -13,7 +13,7 @@ public enum Maps {
Maps(String filename) { Maps(String filename) {
file=new File(new File("..",MAPS_DIR),filename); file=new File(new File("..",MAPS_DIR),filename);
this.map=Map.LoadMap(this); //this.map=Map.LoadMap(this); //We don't necessarily have to load the map right away.
} }
public File getFile() { public File getFile() {

Loading…
Cancel
Save