private void LoadMap() { var jsonTextFile = Resources.Load <TextAsset>("Maps/" + _mapName); RawMapData mapData = JsonUtility.FromJson <RawMapData>(jsonTextFile.text); foreach (RawTileData d in mapData.tiles) { minX = Math.Min(d.x, minX); maxX = Math.Max(d.x, maxX); minY = Math.Min(d.y, minY); maxY = Math.Max(d.y, maxY); } int totalXCount = Math.Abs(minX) + Math.Abs(maxX) + 1; int totalYCount = Math.Abs(minY) + Math.Abs(maxY) + 1; tileData = new CustomTileData[totalXCount, totalYCount]; foreach (RawTileData d in mapData.tiles) { tileData[d.x, d.y] = new CustomTileData(d); } }
public bool AlterLevelData(LevelIdentifier level, byte[] new_data, byte[] new_graphical, byte[] new_background, byte[] new_minimap, bool relocate) { RawMapData original = ExtractLevelData(level); if (new_data != null && original.RawData.SequenceEqual(new_data)) { new_data = null; } if (new_graphical != null && original.RawGraphical.SequenceEqual(new_graphical)) { new_graphical = null; } if (new_background != null && original.RawBackground.SequenceEqual(new_background)) { new_background = null; } if (new_minimap != null && original.RawMinimap.SequenceEqual(new_minimap)) { new_minimap = null; } if (new_data == null && new_graphical == null && new_background == null && new_minimap == null) { return(false); } // Write compressed data int w = (int)level.world; int l = level.level; LevelInfo info = GetLevelInfo(level); int pos0 = relocate ? ceilToMultiple((int)rom.Length, 4) : info.DataBaseAddress; level_entries[w][l].level_data_offset = pos0 - world_entries[w].WorldDataBaseAddress; level_entries[w][l].level_uncompressed_size = WriteDataWithCompression(level, original.RawData, original.CompressedData, new_data, pos0, relocate ? -1 : info.GraphicalBaseAddress); int pos1 = ceilToMultiple((int)rom.Position, 4); level_entries[w][l].graphical_data_offset = pos1 - world_entries[w].WorldDataBaseAddress; level_entries[w][l].graphical_uncompressed_size = WriteDataWithCompression(level, original.RawGraphical, original.CompressedGraphical, new_graphical, pos1, relocate ? -1 : info.BackgroundBaseAddress); int pos2 = ceilToMultiple((int)rom.Position, 4); level_entries[w][l].background_data_offset = pos2 - world_entries[w].WorldDataBaseAddress; level_entries[w][l].background_uncompressed_size = WriteDataWithCompression(level, original.RawBackground, original.CompressedBackground, new_background, pos2, relocate ? -1 : info.MinimapBaseAddress); int pos3 = ceilToMultiple((int)rom.Position, 4); level_entries[w][l].minimap_data_offset = pos3 - world_entries[w].WorldDataBaseAddress; byte[] minimap = new_minimap == null ? original.RawMinimap : new_minimap; int endAddr = (relocate ? pos3 : info.MinimapBaseAddress) + info.MinimapSize; rom.Seek(pos3, SeekOrigin.Begin); rom.Write(minimap, Math.Max(0, minimap.Length + pos3 - endAddr), Math.Min(endAddr - pos3, minimap.Length)); // Update LevelEntry structure rom.Seek(world_entries[w].WorldInfoBaseAddress + Marshal.SizeOf(typeof(WorldInfo)), SeekOrigin.Begin); BinaryWriter writer = new BinaryWriter(rom); foreach (LevelEntry entry in level_entries[w]) { Utils.TypeToByte(writer, entry); } return(true); }
public Level() { Random random = new Random(100); //========================================================================================== // Create the player: //========================================================================================== Creatures.Add(new Creature { Speed = 240, Appearance = MakeTileSpan(new TileIndex(0, 8), new TileIndex(1, 0), 4), }); //========================================================================================== // Parse map data: //========================================================================================== string[] raw = RawMapData.Split('\n').Select(x => x.Trim()).Where(x => x.Length > 0).ToArray(); MapWidth = raw[0].Length; MapHeight = raw.Length; Walls = new TileIndex[MapWidth, MapHeight]; Obstacles = new bool[MapWidth, MapHeight]; for (int row = 0; row < MapHeight; row++) { for (int column = 0; column < MapWidth; column++) { //========================================================================================== // Choose tile variations for the static map terrain: //========================================================================================== char c = raw[row][column]; char left = (column > 0) ? raw[row][column - 1] : '.'; char right = (column < MapWidth - 1) ? raw[row][column + 1] : '.'; char above = (row > 0) ? raw[row - 1][column] : '.'; char below = (row < MapHeight - 1) ? raw[row + 1][column] : '.'; TileIndex[] tiles; bool obstacle = false; if (c == '.') { tiles = MakeTileSpan(new TileIndex(0, 0)); } else if (c == 'W') { // Outer corners: if (below == 'W' && right == 'W' && above == '.') tiles = MakeTileSpan(new TileIndex(0, 1)); else if (below == 'W' && left == 'W' && above == '.') tiles = MakeTileSpan(new TileIndex(5, 1)); else if (above == 'W' && right == 'W' && below == '.') tiles = MakeTileSpan(new TileIndex(0, 5)); else if (above == 'W' && left == 'W' && below == '.') tiles = MakeTileSpan(new TileIndex(5, 5)); // Inside corners: else if (below == 'W' && right == 'W' && above != '.') tiles = MakeTileSpan(new TileIndex(8, 1)); else if (below == 'W' && left == 'W' && above != '.') tiles = MakeTileSpan(new TileIndex(11, 1)); // Horizontal walls: else if (above == '.' || (above == 'W' && (left == 'W' || right == 'W'))) tiles = MakeTileSpan(new TileIndex(1, 1), new TileIndex(1, 0), 4); else if (below == '.') tiles = MakeTileSpan(new TileIndex(1, 5), new TileIndex(1, 0), 4); // Vertical walls: else if (left == '.') tiles = MakeTileSpan(new TileIndex(0, 2), new TileIndex(0, 1), 3); else if (right == '.') tiles = MakeTileSpan(new TileIndex(5, 2), new TileIndex(0, 1), 3); else tiles = MakeTileSpan(new TileIndex(2, 0)); obstacle = true; } else { // All other entities are implicitly on a floor tile. tiles = MakeTileSpan(new TileIndex(4, 0), new TileIndex(1, 0), 12); } Walls[column, row] = Choose(random, tiles); Vector2 here = new Vector2(column, row) * Assets.CellSize; //========================================================================================== // Spawn creatures and props: //========================================================================================== if (c == '@') { Player.Position = here; } else if (c == 'P') { // Priest NPC: Creatures.Add(new Creature { Speed = 200, Position = here, Appearance = MakeTileSpan(new TileIndex(0, 9), new TileIndex(1, 0), 4), Facing = TextureMirror.Horizontal, Conversation = new[] { "Expedition Leader\n\n\"Phew!\"", "Expedition Leader\n\n\"We nearly didn't make it\n out of that level!\"", "Expedition Leader\n\n\"Now, quickly, the exit is\n just ahead.\"", "Expedition Leader\n\n\"Go!\"", }, Thought = Thought.WaitToJoin, FollowDistanceMin = 80, FollowDistanceMax = float.MaxValue, }); } else if (c == 'S') { // Skeleton: Creatures.Add(new Creature { Speed = 40, Position = here, Appearance = MakeTileSpan(new TileIndex(0, 5), new TileIndex(1, 0), 4), Facing = TextureMirror.Horizontal, CanKill = true, Thought = Thought.Follow, FollowDistanceMin = 0, FollowDistanceMax = 300, }); } else if (c == 'F') { // Flaming skull: Creatures.Add(new Creature { Speed = 210, Position = here, Appearance = MakeTileSpan(new TileIndex(0, 7), new TileIndex(1, 0), 4), Facing = TextureMirror.Horizontal, CanKill = true, Thought = Thought.Follow, FollowDistanceMin = 0, FollowDistanceMax = 260, }); } else if (c == 'L') { // Ladder: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(3, 0)), IsFlat = true, }); obstacle = true; } else if (c == 'B') { // Big crate: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(3, 3)), }); obstacle = true; } else if (c == 'b') { // Small crate: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(2, 3)), }); obstacle = true; } else if (c == 'f') { // Wall torch: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(3, 4)), }); } else if (c == 'C') { // Large cobweb: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(4, 1)), }); } else if (c == 'c') { // Small cobweb: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(5, 1)), }); } else if (c == 'r') { // Bones: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(7, 2)), IsFlat = true, }); } else if (c == 'T') { // Treasure: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(4, 3)), }); obstacle = true; } else if (c == 'h') { // Health potion: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(9, 3)), }); } else if (c == 'm') { // Mana potion: Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(7, 3)), }); } else if (c == 'D') { // Door (left): Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(0, 0)), CanWin = true, }); obstacle = true; } else if (c == 'O') { // Door (right): Creatures.Add(new Creature { Position = here, Appearance = MakeTileSpan(new TileIndex(1, 0)), CanWin = true, }); obstacle = true; } Obstacles[column, row] = obstacle; } } Engine.PlayMusic(Assets.LevelMusic, looping: true); }