From 25cac8db589a689df121b2a9f4142fdc1cef2fee Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Thu, 12 Feb 2009 09:20:07 -0500 Subject: Engine: Rewrote Map abstraction Instead of requiring users to extend Map, all that is required now is the creation of a Map object, which should then be configured and added to the Database via Database.addMap() --- src/com/fourisland/fourpuzzle/Game.java | 2 +- .../fourisland/fourpuzzle/database/Database.java | 13 +++++ .../fourpuzzle/gamestate/mapview/Map.java | 64 +++++++++++++++------- .../gamestate/mapview/MapViewGameState.java | 22 +------- 4 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/com/fourisland/fourpuzzle/Game.java b/src/com/fourisland/fourpuzzle/Game.java index d2e8943..432d8ff 100755 --- a/src/com/fourisland/fourpuzzle/Game.java +++ b/src/com/fourisland/fourpuzzle/Game.java @@ -17,7 +17,7 @@ public class Game { public static final int WIDTH = 320; public static final int HEIGHT = 240; - public static final int FPS = (1000 / 30); // 30 fps + public static final int FPS = (1000 / 20); // 20 fps private static SaveFile saveFile; public static SaveFile getSaveFile() diff --git a/src/com/fourisland/fourpuzzle/database/Database.java b/src/com/fourisland/fourpuzzle/database/Database.java index 6d61f37..1493a3f 100755 --- a/src/com/fourisland/fourpuzzle/database/Database.java +++ b/src/com/fourisland/fourpuzzle/database/Database.java @@ -5,7 +5,9 @@ package com.fourisland.fourpuzzle.database; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; import com.fourisland.fourpuzzle.transition.Transition; +import java.util.HashMap; /** * @@ -117,5 +119,16 @@ public class Database { { key.setValue(value); } + + private static java.util.Map maps = new HashMap(); + public static void addMap(String key, Map value) + { + maps.put(key, value); + } + + public static Map getMap(String key) + { + return maps.get(key); + } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java index 124ea95..1511fea 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java @@ -14,20 +14,53 @@ import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.HashMap; +import java.util.List; import java.util.Vector; /** * * @author hatkirby */ -public abstract class Map { - - public abstract void initalize(); +public class Map { + + /** + * Creates a new Map + * + * @param width The width of the Map in tiles + * @param height The height of the Map in tiles + * @param chipSet The name of the ChipSet to use + * @param music The name of the Music file to play in the background + */ + public Map(int width, int height, String chipSet, String music) + { + setSize(new Dimension(width, height)); + setChipSet(chipSet); + setMusic(music); + } - protected void initalize(Dimension size) + /** + * Creates a new Map + * + * @param width The width of the Map in tiles + * @param height The height of the Map in tiles + * @param chipSet The name of the ChipSet to use + * @param musicType The non-Specified Music mode to use + * + * @throws IllegalArgumentException if MapMusicType.Specified is passed in + * musicType. This constructor is for MapMusicType.NoMusic or + * MapMusicType.NoChange. If you wish to specify a Music file, use the other + * constructor (int, int, String, String) + */ + public Map(int width, int height, String chipSet, MapMusicType musicType) { - setSize(size); - mapData = new Vector>(); + if (musicType == MapMusicType.Specified) + { + throw new IllegalArgumentException("MapMusicType.Specified is not a valid value for musicType. If you wish to specify a music type, use the other constructor (int, int, String, String)"); + } + + setSize(new Dimension(width, height)); + setChipSet(chipSet); + setMusicType(musicType); } private Dimension size; @@ -172,8 +205,8 @@ public abstract class Map { this.chipSet = chipSet; } - private Vector> mapData; - public Vector> getMapData() { + private List> mapData = new Vector>(); + public List> getMapData() { return mapData; } @@ -182,17 +215,8 @@ public abstract class Map { { return music; } - - /** - * Sets the name of the Music file to play when this Map loads - * - * When this function is run, it also sets the MusicType to Specified - * automatically as the only time this function would be used would be when - * the MusicType was Specified. - * - * @param music - */ - protected void setMusic(String music) + + private void setMusic(String music) { this.music = music; this.musicType = MapMusicType.Specified; @@ -204,7 +228,7 @@ public abstract class Map { return musicType; } - protected void setMusicType(MapMusicType musicType) + private void setMusicType(MapMusicType musicType) { this.musicType = musicType; } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 8f411af..3ed23c3 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -13,6 +13,7 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; import com.fourisland.fourpuzzle.Game; import com.fourisland.fourpuzzle.Layer; import com.fourisland.fourpuzzle.PuzzleApplication; +import com.fourisland.fourpuzzle.database.Database; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventCallTime; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; @@ -22,13 +23,10 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventTh import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; import com.fourisland.fourpuzzle.util.Functions; -import com.fourisland.fourpuzzle.util.ResourceNotFoundException; import com.fourisland.fourpuzzle.window.MessageWindow; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; -import java.util.logging.Level; -import java.util.logging.Logger; /** * @@ -238,26 +236,10 @@ public class MapViewGameState implements GameState { } } - public void initCurrentMap(String mapName) - { - try { - Class mapClass = Class.forName(PuzzleApplication.INSTANCE.getGamePackage() + ".gamedata.map." + mapName); - Object mapObject = mapClass.newInstance(); - Map map = (Map) mapObject; - map.initalize(); - currentMap = map; - } catch (InstantiationException ex) { - Logger.getLogger(MapViewGameState.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - Logger.getLogger(MapViewGameState.class.getName()).log(Level.SEVERE, null, ex); - } catch (ClassNotFoundException ex) { - throw new ResourceNotFoundException("Map", mapName); - } - } public void setCurrentMap(String mapName) { Game.getSaveFile().setCurrentMap(mapName); - initCurrentMap(mapName); + currentMap = Database.getMap(mapName); } public Map getCurrentMap() -- cgit 1.4.1