From 99cf22814df75b50be273d840027db1ce1769664 Mon Sep 17 00:00:00 2001 From: Oskar Wiksten Date: Mon, 23 Jul 2012 17:41:00 +0200 Subject: [PATCH] Separate the worldmap into segments, so that only one segment at a time is displayed. This way, even dungeon maps can be displayed. --- AndorsTrail/res/values/strings.xml | 1 + AndorsTrail/res/xml/worldmap.xml | 624 +++++++----------- .../activity/DisplayWorldMapActivity.java | 30 +- .../AndorsTrail/activity/MainActivity.java | 3 +- .../rpg/AndorsTrail/controller/Constants.java | 3 +- .../controller/WorldMapController.java | 108 +-- .../AndorsTrail/model/map/MapCollection.java | 9 + .../AndorsTrail/model/map/PredefinedMap.java | 4 +- .../model/map/TMXMapFileParser.java | 30 +- .../model/map/WorldMapSegment.java | 25 + .../resource/parsers/WorldMapParser.java | 40 +- .../util/XmlResourceParserUtils.java | 29 + 12 files changed, 437 insertions(+), 469 deletions(-) create mode 100644 AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java create mode 100644 AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/XmlResourceParserUtils.java diff --git a/AndorsTrail/res/values/strings.xml b/AndorsTrail/res/values/strings.xml index 4d0a2ade7..b64ec9494 100644 --- a/AndorsTrail/res/values/strings.xml +++ b/AndorsTrail/res/values/strings.xml @@ -506,5 +506,6 @@ About Map Map cannot be displayed. + The map cannot be displayed from this location. diff --git a/AndorsTrail/res/xml/worldmap.xml b/AndorsTrail/res/xml/worldmap.xml index bd6bb9fcc..f43aedee5 100644 --- a/AndorsTrail/res/xml/worldmap.xml +++ b/AndorsTrail/res/xml/worldmap.xml @@ -1,378 +1,250 @@ -
pwcave4
-
waytobrimhaven2
-
waytobrimhaven3
-
waytobrimhaven0
-
waytobrimhaven1
-
roadtocarntower1
-
roadtocarntower0
-
woodsettlement0
-
fallhaven_arcir_basement
-
roadtocarntower2
-
lodarcave7
-
lodarcave5
-
lodarcave6
-
lodarcave3
-
lodarcave4
-
lodarcave1
-
lodarcave2
-
fields9
-
lodarcave0
-
waterway9
-
fields6
-
waterway8
-
fields5
-
fields8
-
crossglen_cave
-
fields7
-
fields2
-
fields1
-
fields4
-
fields3
-
fields0
-
wild14_clearing
-
blackwater_mountain7
-
lodarhouse1
-
blackwater_mountain6
-
lodarhouse0
-
blackwater_mountain5
-
mountainlake10a
-
blackwater_mountain4
-
blackwater_mountain9
-
blackwater_mountain8
-
crossglen_farmhouse
-
fallhaven_storage
-
pwcave2
-
pwcave3
-
pwcave0
-
pwcave1
-
wild16_cave
-
lodarcave4a
-
wild3
-
wild2
-
wild5
-
wild4
-
wild7
-
wild6
-
remgard_weapon
-
wild9
-
clearing_level2
-
wild8
-
clearing_level1
-
blackwater_mountain0
-
blackwater_mountain1
-
blackwater_mountain2
-
blackwater_mountain3
-
vilegard_erttu
-
flagstone_upper
-
wild1
-
wild0
-
wild17
-
wild15
-
mountainlake13a
-
wild16
-
wild13
-
flagstone0
-
wild14
-
wild11
-
wild12
-
wild10
-
waterway6
-
waterway7
-
waterway4
-
waterway5
-
flagstone1
-
waterway2
-
flagstone2
-
loneford10
-
waterway3
-
woodhouse3
-
waterway0
-
lodar8cave0
-
flagstone3
-
waterway1
-
flagstone4
-
road4_gargoylecave
-
fallhaven_prison
-
lonelyhouse1
-
lonelyhouse0
-
minerhouse0
-
waytobrimhavencave3b
-
remgard_villager4
-
minerhouse4
-
remgard_villager3
-
minerhouse3
-
minerhouse2
-
minerhouse1
-
remgard_villager5
-
minerhouse8
-
fallhaven_nocmar
-
minerhouse7
-
remgard_villager2
-
minerhouse6
-
remgard_villager1
-
minerhouse5
-
waytobrimhavencave3a
-
lodar12cave0
-
crossglen_hall
-
lodar12cave1
-
vilegard_ogam
-
minerhouse9
-
fallhaven_church
-
vilegard_chapel
-
remgard_clothes
-
foaming_flask
-
remgard_tavern1
-
roadcave0
-
roadcave1
-
vilegard_sw
-
lostmine3
-
lostmine4
-
lostmine5
-
lostmine6
-
gapfiller1
-
gapfiller3
-
lostmine0
-
lostmine1
-
gapfiller4
-
lostmine2
-
waytolake0
-
lostmine9
-
fallhaven_farmer
-
vilegard_n
-
lostmine8
-
lostmine7
-
waytolostmine0
-
waytolake5
-
waytolake3
-
waytolake4
-
vilegard_s
-
waytolake1
-
waytolake2
-
road5_house
-
waytolostmine2
-
waytolostmine1
-
waytolostmine3
-
waytobrimhavencave1a
-
lostmine10
-
lostmine11
-
fallhaven_potions
-
remgard_armour
-
mountainlake13
-
mountainlake12
-
mountainlake11
-
loneford4
-
mountainlake10
-
loneford3
-
loneford2
-
loneford1
-
mountaincave3
-
mountaincave2
-
crossglen_farmhouse_basement
-
mountaincave1
-
mountaincave0
-
crossglen
-
fallhaven_clothes
-
loneford5
-
hauntedhouse2
-
loneford6
-
hauntedhouse1
-
loneford7
-
loneford8
-
loneford9
-
fallhaven_tavern
-
hauntedhouse4
-
hauntedhouse3
-
road1
-
road3
-
road2
-
road5
-
road4
-
jan_pitcave1
-
lodar5cave1
-
lodar5cave0
-
lodar21
-
jan_pitcave3
-
lodar5cave2
-
jan_pitcave2
-
fallhaven_gravedigger
-
lodar20
-
waterwayhouse
-
fallhaven_rigmor
-
waytominingtown1a
-
lostmine2a
-
wild6_house
-
lodar8
-
lodar9
-
lodar6
-
lodar7
-
lodar4
-
lodar5
-
lodar2
-
lodar3
-
lodar10
-
lodar12
-
lodar11
-
lodar14
-
lodar13
-
lodar16
-
lodar15
-
lodar18
-
woodcave1
-
lodar17
-
woodcave0
-
lostmine1a
-
lodar19
-
fallhaven_nw
-
waterway12
-
waterway11
-
waterway14
-
waterway13
-
waterway15
-
fallhaven_derelict
-
vilegard_armorer
-
remgard_school
-
remgard_farmer2
-
remgard_farmer3
-
waterway11_east
-
fallhaven_ne
-
remgard_farmer1
-
waterway10
-
oldcave0
-
oldcave1
-
home
-
roadbeforecrossroads
-
roadbeforecrossroads8
-
roadbeforecrossroads9
-
roadbeforecrossroads6
-
roadbeforecrossroads7
-
roadbeforecrossroads4
-
roadbeforecrossroads5
-
roadbeforecrossroads2
-
roadbeforecrossroads3
-
lodar1cave0
-
fallhaven_alaun
-
remgard_barn
-
lodar1
-
lodar0
-
remgard_tavern0
-
vilegard_wrye
-
waytomountaincave1
-
waytomountaincave0
-
waytomountaincave2
-
remgard_church
-
houseatcrossroads2
-
blackwater_mountain39
-
houseatcrossroads3
-
wild11_clearing
-
fallhaven_sw
-
blackwater_mountain37
-
houseatcrossroads4
-
waytobrimhavencave0
-
houseatcrossroads5
-
waterwayextention
-
blackwater_mountain38
-
waytominingtown0
-
mountainlake3
-
waytobrimhavencave1
-
mountainlake2
-
waytominingtown1
-
waytobrimhavencave2
-
mountainlake1
-
houseatcrossroads0
-
waytobrimhavencave3
-
waytominingtown2
-
mountainlake0
-
houseatcrossroads1
-
waytominingtown3
-
waytobrimhavencave4
-
blackwater_mountain31
-
mountainlake7
-
blackwater_mountain51
-
blackwater_mountain32
-
blackwater_mountain52
-
mountainlake6
-
mountainlake5
-
mountainlake4
-
blackwater_mountain30
-
fallhaven_lumberjack
-
blackwater_mountain35
-
blackwater_mountain36
-
mountainlake9
-
blackwater_mountain33
-
blackwater_mountain34
-
mountainlake8
-
blackwater_mountain50
-
woodhouse0
-
woodhouse2
-
woodhouse1
-
fallhaven_se
-
fields10
-
blackwater_mountain48
-
blackwater_mountain49
-
fields11
-
fallhaven_barn
-
fields12
-
vilegard_tavern
-
roadbeforecrossroads1
-
fallhaven_athamyr
-
blackwater_mountain40
-
blackwater_mountain41
-
blackwater_mountain42
-
blackwater_mountain43
-
blackwater_mountain44
-
blackwater_mountain45
-
blackwater_mountain46
-
blackwater_mountain47
-
crossglen_smith
-
pwcave2a
-
blackwater_mountain19
-
blackwater_mountain17
-
snakecave2
-
snakecave1
-
blackwater_mountain18
-
blackwater_mountain15
-
snakecave3
-
blackwater_mountain16
-
blackwater_mountain13
-
blackwater_mountain14
-
blackwater_mountain11
-
blackwater_mountain12
-
blackwater_mountain10
-
flagstone_inner
-
fallhaven_derelict2
-
vilegard_kaori
-
fallhaven_arcir
-
remgard_prison
-
catacombs1
-
catacombs2
-
vilegard_smith
-
waterwaycave
-
blackwater_mountain26
-
blackwater_mountain27
-
blackwater_mountain28
-
wild15_house
-
catacombs3
-
catacombs4
-
blackwater_mountain29
-
blackwater_mountain22
-
blackwater_mountain23
-
gargoylecave3
-
remgard1
-
blackwater_mountain24
-
remgard0
-
gargoylecave4
-
blackwater_mountain25
-
remgard3
-
gargoylecave1
-
remgard2
-
gargoylecave2
-
blackwater_mountain20
-
remgard4
-
blackwater_mountain21
-
wild14_cave
-
tradehouse0
-
tradehouse1
-
crossroads
-
\ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DisplayWorldMapActivity.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DisplayWorldMapActivity.java index b94f4e1d2..2eae1157e 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DisplayWorldMapActivity.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DisplayWorldMapActivity.java @@ -2,6 +2,7 @@ package com.gpl.rpg.AndorsTrail.activity; import java.io.File; +import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; @@ -11,13 +12,15 @@ import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; import com.gpl.rpg.AndorsTrail.R; import com.gpl.rpg.AndorsTrail.context.WorldContext; import com.gpl.rpg.AndorsTrail.controller.WorldMapController; -import com.gpl.rpg.AndorsTrail.util.Coord; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap; import com.gpl.rpg.AndorsTrail.util.L; public class DisplayWorldMapActivity extends Activity { private WorldContext world; private WebView displayworldmap_webview; + private String worldMapSegmentName; @Override public void onCreate(Bundle savedInstanceState) { @@ -34,7 +37,14 @@ public class DisplayWorldMapActivity extends Activity { displayworldmap_webview = (WebView) findViewById(R.id.displayworldmap_webview); displayworldmap_webview.setBackgroundColor(getResources().getColor(android.R.color.black)); displayworldmap_webview.getSettings().setBuiltInZoomControls(true); - displayworldmap_webview.getSettings().setJavaScriptEnabled(true); + enableJavascript(); + + worldMapSegmentName = getIntent().getStringExtra("worldMapSegmentName"); + } + + @SuppressLint("SetJavaScriptEnabled") + public void enableJavascript() { + displayworldmap_webview.getSettings().setJavaScriptEnabled(true); } @Override @@ -44,21 +54,21 @@ public class DisplayWorldMapActivity extends Activity { } private void update() { - File worldmap = WorldMapController.getCombinedWorldMapFile(); + File worldmap = WorldMapController.getCombinedWorldMapFile(worldMapSegmentName); if (!worldmap.exists()) { Toast.makeText(this, getResources().getString(R.string.menu_button_worldmap_failed), Toast.LENGTH_LONG).show(); this.finish(); } + + WorldMapSegment segment = world.maps.worldMapSegments.get(worldMapSegmentName); + WorldMapSegmentMap map = segment.maps.get(world.model.currentMap.name); + if (map == null) this.finish(); - String url = "file://" + worldmap.getAbsolutePath(); - Coord playerWorldPosition = WorldMapController.getPlayerWorldPosition(world); - if (playerWorldPosition != null) { - url += "?" - + playerWorldPosition.x * WorldMapController.WORLDMAP_DISPLAY_TILESIZE + String url = "file://" + worldmap.getAbsolutePath() + "?" + + (world.model.player.position.x + map.worldPosition.x) * WorldMapController.WORLDMAP_DISPLAY_TILESIZE + "," - + (playerWorldPosition.y-1) * WorldMapController.WORLDMAP_DISPLAY_TILESIZE; - } + + (world.model.player.position.y + map.worldPosition.y-1) * WorldMapController.WORLDMAP_DISPLAY_TILESIZE; L.log("Showing " + url); displayworldmap_webview.loadUrl(url); } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java index 18d7d00ee..d2d648f65 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java @@ -11,6 +11,7 @@ import com.gpl.rpg.AndorsTrail.context.ViewContext; import com.gpl.rpg.AndorsTrail.context.WorldContext; import com.gpl.rpg.AndorsTrail.controller.CombatController; import com.gpl.rpg.AndorsTrail.controller.MovementController; +import com.gpl.rpg.AndorsTrail.controller.WorldMapController; import com.gpl.rpg.AndorsTrail.model.actor.Monster; import com.gpl.rpg.AndorsTrail.model.actor.Player; import com.gpl.rpg.AndorsTrail.model.item.ItemContainer.ItemEntry; @@ -215,7 +216,7 @@ public final class MainActivity extends Activity { .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem arg0) { - startActivity(new Intent(MainActivity.this, DisplayWorldMapActivity.class)); + WorldMapController.displayWorldMap(MainActivity.this, world); return true; } }); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Constants.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Constants.java index 52ff77b62..66711501a 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Constants.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Constants.java @@ -36,7 +36,8 @@ public final class Constants { public static final String FILENAME_SAVEGAME_QUICKSAVE = "savegame"; public static final String FILENAME_SAVEGAME_DIRECTORY = "andors-trail"; public static final String FILENAME_WORLDMAP_DIRECTORY = "worldmap"; - public static final String FILENAME_WORLDMAP_HTMLFILE = "worldmap.html"; + public static final String FILENAME_WORLDMAP_HTMLFILE_PREFIX = "worldmap_"; + public static final String FILENAME_WORLDMAP_HTMLFILE_SUFFIX = ".html"; public static final String FILENAME_SAVEGAME_FILENAME_PREFIX = "savegame"; diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java index eeeecf6c4..3f466d8cd 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java @@ -5,6 +5,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; @@ -12,13 +13,18 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.os.AsyncTask; import android.os.Environment; +import android.widget.Toast; import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; import com.gpl.rpg.AndorsTrail.R; +import com.gpl.rpg.AndorsTrail.activity.DisplayWorldMapActivity; +import com.gpl.rpg.AndorsTrail.activity.MainActivity; import com.gpl.rpg.AndorsTrail.context.WorldContext; import com.gpl.rpg.AndorsTrail.model.map.LayeredTileMap; import com.gpl.rpg.AndorsTrail.model.map.MapLayer; import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap; import com.gpl.rpg.AndorsTrail.resource.tiles.TileCollection; import com.gpl.rpg.AndorsTrail.util.Coord; import com.gpl.rpg.AndorsTrail.util.L; @@ -30,55 +36,53 @@ public final class WorldMapController { public static void updateWorldMap(final WorldContext world, final PredefinedMap map, final LayeredTileMap mapTiles, final TileCollection cachedTiles, final Resources res) { - if (!shouldUpdateWorldMap(map)) return; + final String worldMapSegmentName = world.maps.getWorldMapSegmentNameForMap(map.name); + if (worldMapSegmentName == null) return; + + if (!shouldUpdateWorldMap(map, worldMapSegmentName)) return; (new AsyncTask() { @Override protected Void doInBackground(Void... arg0) { final MapRenderer renderer = new MapRenderer(world, map, mapTiles, cachedTiles); - updateCachedBitmap(map, renderer); try { - updateCombinedWorldMap(res, world); - } catch (IOException e) {} + updateCachedBitmap(map, renderer); + updateWorldMapSegment(res, world, worldMapSegmentName); + } catch (IOException e) { + L.log("Error creating worldmap file for map " + map.name + " : " + e.toString()); + } return null; } }).execute(); } - private static boolean isVisibleOnWorldMap(PredefinedMap map) { - return map.isOutdoors; - } - - private static boolean shouldUpdateWorldMap(PredefinedMap map) { - if (!isVisibleOnWorldMap(map)) return false; - if (!map.visited) return true; + private static boolean shouldUpdateWorldMap(PredefinedMap map, String worldMapSegmentName) { + if (!map.visited) return true; File file = getFileForMap(map); if (!file.exists()) return true; if (map.lastVisitVersion < AndorsTrailApplication.CURRENT_VERSION) return true; - file = getCombinedWorldMapFile(); + file = getCombinedWorldMapFile(worldMapSegmentName); if (!file.exists()) return true; return false; } - private static void updateCachedBitmap(PredefinedMap map, MapRenderer renderer) { + private static void updateCachedBitmap(PredefinedMap map, MapRenderer renderer) throws IOException { File file = getFileForMap(map); - if (file.exists()) return; - - try { - Bitmap image = renderer.drawMap(); - ensureWorldmapDirectoryExists(); - FileOutputStream fos = new FileOutputStream(file); - image.compress(Bitmap.CompressFormat.PNG, 70, fos); - fos.flush(); - fos.close(); - image.recycle(); - } catch (IOException e) { - L.log("Error creating worldmap file for map " + map.name + " : " + e.toString()); + if (file.exists()) { + if (map.lastVisitVersion == AndorsTrailApplication.CURRENT_VERSION) return; } + + Bitmap image = renderer.drawMap(); + ensureWorldmapDirectoryExists(); + FileOutputStream fos = new FileOutputStream(file); + image.compress(Bitmap.CompressFormat.PNG, 70, fos); + fos.flush(); + fos.close(); + image.recycle(); } private static final class MapRenderer { @@ -135,35 +139,39 @@ public final class WorldMapController { dir = new File(dir, Constants.FILENAME_WORLDMAP_DIRECTORY); if (!dir.exists()) dir.mkdir(); } - public static File getFileForMap(PredefinedMap map) { - File root = getWorldmapDirectory(); - return new File(root, map.name + ".png"); + public static File getFileForMap(PredefinedMap map) { return getFileForMap(map.name); } + public static File getFileForMap(String mapName) { + return new File(getWorldmapDirectory(), mapName + ".png"); } public static File getWorldmapDirectory() { File dir = Environment.getExternalStorageDirectory(); dir = new File(dir, Constants.FILENAME_SAVEGAME_DIRECTORY); return new File(dir, Constants.FILENAME_WORLDMAP_DIRECTORY); } - public static File getCombinedWorldMapFile() { - return new File(getWorldmapDirectory(), Constants.FILENAME_WORLDMAP_HTMLFILE); + public static File getCombinedWorldMapFile(String segmentName) { + return new File(getWorldmapDirectory(), Constants.FILENAME_WORLDMAP_HTMLFILE_PREFIX + segmentName + Constants.FILENAME_WORLDMAP_HTMLFILE_SUFFIX); } - private static String getCombinedWorldMapAsHtml(Resources res, WorldContext world) throws IOException { + private static String getWorldMapSegmentAsHtml(Resources res, WorldContext world, String segmentName) throws IOException { + WorldMapSegment segment = world.maps.worldMapSegments.get(segmentName); + StringBuffer sb = new StringBuffer(); Coord offsetWorldmapTo = new Coord(999999, 999999); - for (PredefinedMap map : world.maps.predefinedMaps) { - File f = WorldMapController.getFileForMap(map); + for (WorldMapSegmentMap map : segment.maps.values()) { + File f = WorldMapController.getFileForMap(map.mapName); if (!f.exists()) continue; - offsetWorldmapTo.x = Math.min(offsetWorldmapTo.x, map.worldMapPosition.x); - offsetWorldmapTo.y = Math.min(offsetWorldmapTo.y, map.worldMapPosition.y); + offsetWorldmapTo.x = Math.min(offsetWorldmapTo.x, map.worldPosition.x); + offsetWorldmapTo.y = Math.min(offsetWorldmapTo.y, map.worldPosition.y); } - for (PredefinedMap map : world.maps.predefinedMaps) { - File f = WorldMapController.getFileForMap(map); + for (WorldMapSegmentMap segmentMap : segment.maps.values()) { + File f = WorldMapController.getFileForMap(segmentMap.mapName); if (!f.exists()) continue; + PredefinedMap map = world.maps.findPredefinedMap(segmentMap.mapName); + sb .append(""); } @@ -186,21 +194,23 @@ public final class WorldMapController { .replace("{{offsety}}", Integer.toString(offsetWorldmapTo.y * WorldMapController.WORLDMAP_DISPLAY_TILESIZE)); } - private static void updateCombinedWorldMap(Resources res, WorldContext world) throws IOException { - String mapAsHtml = getCombinedWorldMapAsHtml(res, world); - File outputFile = new File(WorldMapController.getWorldmapDirectory(), Constants.FILENAME_WORLDMAP_HTMLFILE); + private static void updateWorldMapSegment(Resources res, WorldContext world, String segmentName) throws IOException { + String mapAsHtml = getWorldMapSegmentAsHtml(res, world, segmentName); + File outputFile = getCombinedWorldMapFile(segmentName); PrintWriter pw = new PrintWriter(outputFile); pw.write(mapAsHtml); pw.close(); } - public static Coord getPlayerWorldPosition(WorldContext world) { - PredefinedMap map = world.model.currentMap; - if (!isVisibleOnWorldMap(map)) return null; + public static void displayWorldMap(MainActivity mainActivity, WorldContext world) { + String worldMapSegmentName = world.maps.getWorldMapSegmentNameForMap(world.model.currentMap.name); + if (worldMapSegmentName == null) { + Toast.makeText(mainActivity, mainActivity.getResources().getString(R.string.display_worldmap_not_available), Toast.LENGTH_LONG).show(); + return; + } - return new Coord( - world.model.player.position.x + map.worldMapPosition.x, - world.model.player.position.y + map.worldMapPosition.y - ); + Intent intent = new Intent(mainActivity, DisplayWorldMapActivity.class); + intent.putExtra("worldMapSegmentName", worldMapSegmentName); + mainActivity.startActivity(intent); } } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/MapCollection.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/MapCollection.java index e442cec63..95a2dc4a8 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/MapCollection.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/MapCollection.java @@ -4,6 +4,7 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; import com.gpl.rpg.AndorsTrail.context.WorldContext; @@ -11,6 +12,7 @@ import com.gpl.rpg.AndorsTrail.util.L; public final class MapCollection { public final ArrayList predefinedMaps = new ArrayList(); + public final HashMap worldMapSegments = new HashMap(); public MapCollection() {} @@ -29,6 +31,13 @@ public final class MapCollection { m.reset(); } } + + public String getWorldMapSegmentNameForMap(String mapName) { + for (WorldMapSegment segment : worldMapSegments.values()) { + if (segment.containsMap(mapName)) return segment.name; + } + return null; + } // ====== PARCELABLE =================================================================== diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java index eb2d0b67d..50982e0ae 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java @@ -24,7 +24,6 @@ public final class PredefinedMap { public final int xmlResourceId; public final String name; public final Size size; - public final Coord worldMapPosition = new Coord(); public final MapObject[] eventObjects; public final MonsterSpawnArea[] spawnAreas; public final ArrayList groundBags = new ArrayList(); @@ -35,7 +34,7 @@ public final class PredefinedMap { public final boolean[][] isWalkable; public final ArrayList splatters = new ArrayList(); - + public PredefinedMap(int xmlResourceId, String name, Size size, boolean[][] isWalkable, MapObject[] eventObjects, MonsterSpawnArea[] spawnAreas, boolean hasFOW, boolean isOutdoors) { this.xmlResourceId = xmlResourceId; this.name = name; @@ -220,6 +219,7 @@ public final class PredefinedMap { } groundBags.clear(); visited = false; + lastVisitVersion = 0; } public boolean isRecentlyVisited() { diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/TMXMapFileParser.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/TMXMapFileParser.java index 763311942..bbba42666 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/TMXMapFileParser.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/TMXMapFileParser.java @@ -10,6 +10,7 @@ import org.xmlpull.v1.XmlPullParserException; import com.gpl.rpg.AndorsTrail.util.Base64; import com.gpl.rpg.AndorsTrail.util.L; +import com.gpl.rpg.AndorsTrail.util.XmlResourceParserUtils; import android.content.res.Resources; import android.content.res.XmlResourceParser; @@ -41,7 +42,7 @@ public final class TMXMapFileParser { map.height = xrp.getAttributeIntValue(null, "height", -1); map.tilewidth = xrp.getAttributeIntValue(null, "tilewidth", -1); map.tileheight = xrp.getAttributeIntValue(null, "tileheight", -1); - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { if (tagName.equals("tileset")) { tileSets.add(readTMXTileSet(xrp)); @@ -81,7 +82,7 @@ public final class TMXMapFileParser { if (s.equals("map")) { map.width = xrp.getAttributeIntValue(null, "width", -1); map.height = xrp.getAttributeIntValue(null, "height", -1); - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { if (tagName.equals("tileset")) { tileSets.add(readTMXTileSet(xrp)); @@ -111,7 +112,7 @@ public final class TMXMapFileParser { ts.name = xrp.getAttributeValue(null, "name"); ts.tilewidth = xrp.getAttributeIntValue(null, "tilewidth", -1); ts.tileheight = xrp.getAttributeIntValue(null, "tileheight", -1); - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) { if (tagName.equals("image")) { ts.imageSource = xrp.getAttributeValue(null, "source"); @@ -130,7 +131,7 @@ public final class TMXMapFileParser { group.name = xrp.getAttributeValue(null, "name"); group.width = xrp.getAttributeIntValue(null, "width", 1); group.height = xrp.getAttributeIntValue(null, "height", 1); - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { if (tagName.equals("object")) { group.objects.add(readTMXObject(xrp)); @@ -148,7 +149,7 @@ public final class TMXMapFileParser { object.y = xrp.getAttributeIntValue(null, "y", -1); object.width = xrp.getAttributeIntValue(null, "width", -1); object.height = xrp.getAttributeIntValue(null, "height", -1); - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { if (tagName.equals("property")) { object.properties.add(readTMXProperty(xrp)); @@ -164,7 +165,7 @@ public final class TMXMapFileParser { layer.width = xrp.getAttributeIntValue(null, "width", 1); layer.height = xrp.getAttributeIntValue(null, "height", 1); layer.gids = new int[layer.width][layer.height]; - readCurrentTagUntilEnd(xrp, new TagHandler() { + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { if (tagName.equals("data")) { readTMXMapLayerData(xrp, layer); @@ -226,23 +227,6 @@ public final class TMXMapFileParser { } } - private interface TagHandler { - void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException; - } - private static void readCurrentTagUntilEnd(XmlResourceParser xrp, TagHandler handler) throws XmlPullParserException, IOException { - String outerTagName = xrp.getName(); - String tagName; - int eventType; - while ((eventType = xrp.next()) != XmlResourceParser.END_DOCUMENT) { - if (eventType == XmlResourceParser.START_TAG) { - tagName = xrp.getName(); - handler.handleTag(xrp, tagName); - } else if (eventType == XmlResourceParser.END_TAG) { - tagName = xrp.getName(); - if (tagName.equals(outerTagName)) return; - } - } - } /* private static String getHexString(byte v) { String result = Integer.toHexString(v & 0xff); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java new file mode 100644 index 000000000..1c529af47 --- /dev/null +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java @@ -0,0 +1,25 @@ +package com.gpl.rpg.AndorsTrail.model.map; + +import java.util.HashMap; + +import com.gpl.rpg.AndorsTrail.util.Coord; + +public final class WorldMapSegment { + public final String name; + public final HashMap maps = new HashMap(); + + public WorldMapSegment(String name) { + this.name = name; + } + + public boolean containsMap(String mapName) { return maps.containsKey(mapName); } + + public static final class WorldMapSegmentMap { + public final String mapName; + public final Coord worldPosition; + public WorldMapSegmentMap(String mapName, Coord worldPosition) { + this.mapName = mapName; + this.worldPosition = worldPosition; + } + } +} diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java index 24ea1ec1e..20c8c801c 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java @@ -1,11 +1,18 @@ package com.gpl.rpg.AndorsTrail.resource.parsers; +import java.io.IOException; + +import org.xmlpull.v1.XmlPullParserException; + import android.content.res.Resources; import android.content.res.XmlResourceParser; import com.gpl.rpg.AndorsTrail.model.map.MapCollection; -import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap; +import com.gpl.rpg.AndorsTrail.util.Coord; import com.gpl.rpg.AndorsTrail.util.L; +import com.gpl.rpg.AndorsTrail.util.XmlResourceParserUtils; public final class WorldMapParser { public static void read(Resources r, int xmlResourceId, final MapCollection maps) { @@ -18,12 +25,9 @@ public final class WorldMapParser { while ((eventType = xrp.next()) != XmlResourceParser.END_DOCUMENT) { if (eventType == XmlResourceParser.START_TAG) { String s = xrp.getName(); - if (s.equals("div")) { - String mapName = xrp.getAttributeValue(null, "id"); - PredefinedMap map = maps.findPredefinedMap(mapName); - if (map == null) continue; - map.worldMapPosition.x = xrp.getAttributeIntValue(null, "x-posx", -1); - map.worldMapPosition.y = xrp.getAttributeIntValue(null, "x-posy", -1); + if (s.equals("segment")) { + WorldMapSegment segment = parseSegment(xrp, maps); + maps.worldMapSegments.put(segment.name, segment); } } } @@ -33,4 +37,26 @@ public final class WorldMapParser { } } + private static WorldMapSegment parseSegment(XmlResourceParser xrp, MapCollection maps) throws XmlPullParserException, IOException { + String segmentName = xrp.getAttributeValue(null, "id"); + final WorldMapSegment segment = new WorldMapSegment(segmentName); + + XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { + @Override + public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { + if (tagName.equals("map")) { + String mapName = xrp.getAttributeValue(null, "id"); + Coord mapPosition = new Coord( + xrp.getAttributeIntValue(null, "x", -1), + xrp.getAttributeIntValue(null, "y", -1) + ); + WorldMapSegmentMap map = new WorldMapSegmentMap(mapName, mapPosition); + segment.maps.put(mapName, map); + } + } + }); + + return segment; + } + } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/XmlResourceParserUtils.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/XmlResourceParserUtils.java new file mode 100644 index 000000000..1eab988bf --- /dev/null +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/XmlResourceParserUtils.java @@ -0,0 +1,29 @@ +package com.gpl.rpg.AndorsTrail.util; + +import java.io.IOException; + +import org.xmlpull.v1.XmlPullParserException; + +import android.content.res.XmlResourceParser; + +public final class XmlResourceParserUtils { + + public static interface TagHandler { + void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException; + } + + public static void readCurrentTagUntilEnd(XmlResourceParser xrp, TagHandler handler) throws XmlPullParserException, IOException { + String outerTagName = xrp.getName(); + String tagName; + int eventType; + while ((eventType = xrp.next()) != XmlResourceParser.END_DOCUMENT) { + if (eventType == XmlResourceParser.START_TAG) { + tagName = xrp.getName(); + handler.handleTag(xrp, tagName); + } else if (eventType == XmlResourceParser.END_TAG) { + tagName = xrp.getName(); + if (tagName.equals(outerTagName)) return; + } + } + } +}