From 0526f49e9b06d77f979d3e3ae67ce4f8b827a362 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Fri, 30 Jan 2009 14:03:13 -0500 Subject: Created Viewpoint --- .../gamestate/mapview/MapViewGameState.java | 50 ++--------- .../gamestate/mapview/event/SpecialEvent.java | 8 -- .../mapview/viewpoint/AutomaticViewpoint.java | 97 ++++++++++++++++++++++ .../gamestate/mapview/viewpoint/Viewpoint.java | 17 ++++ 4 files changed, 121 insertions(+), 51 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java create mode 100644 src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/Viewpoint.java diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index e4ecf44..45eadc9 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -17,10 +17,11 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.EventHandler; import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList; import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent; import com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove.MoveEventThread; +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 java.awt.Graphics2D; -import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.logging.Level; @@ -35,12 +36,13 @@ public class MapViewGameState implements GameState { public boolean debugWalkthrough = false; boolean processInput = true; Map currentMap; + Viewpoint currentViewpoint = null; public MapViewGameState(String map, int x, int y) { - //currentMap = ObjectLoader.getMap(map); setCurrentMap(map); Game.getSaveFile().getHero().setLocation(x, y); + currentViewpoint = new AutomaticViewpoint(currentMap); } public void initalize() @@ -165,52 +167,14 @@ public class MapViewGameState implements GameState { public void render(Graphics2D g) { - int x,y; - HeroEvent hero = Game.getHeroEvent(); - Point origLoc = hero.getLocation(); - Point endLoc = new Point(hero.getLocation()); - if (hero.isMoving()) - { - switch (hero.getDirection()) - { - case North: endLoc.translate(0, -1); break; - case West: endLoc.translate(-1, 0); break; - case South: endLoc.translate(0, 1); break; - case East: endLoc.translate(1, 0); break; - } - } - - if (Math.max(endLoc.x,origLoc.x) > 10) - { - if (Math.max(endLoc.x,origLoc.x) < (currentMap.getSize().width - 9)) - { - x = (origLoc.x - 10) * 16; - x += hero.getMovingX(); - } else { - x = (currentMap.getSize().width - 20) * 16; - } - } else { - x = 0; - } - - if (Math.max(endLoc.y,origLoc.y) > 7) - { - if (Math.max(endLoc.y,origLoc.y) < (currentMap.getSize().height - 7)) - { - y = (origLoc.y - 7) * 16; - y += hero.getMovingY(); - } else { - y = (currentMap.getSize().height - 15) * 16; - } - } else { - y = 0; - } + int x = currentViewpoint.getX(); + int y = currentViewpoint.getY(); g.drawImage(currentMap.renderLower(), 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); BufferedImage eventLayer = new BufferedImage(currentMap.getSize().width*16, currentMap.getSize().height*16, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = eventLayer.createGraphics(); - hero.render(g2); + Game.getHeroEvent().render(g2); EventList events = currentMap.getEvents(); for (LayerEvent event : events) diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index 52b75de..615396b 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -17,14 +17,6 @@ import java.util.logging.Logger; * @author hatkirby */ public class SpecialEvent { - - /* TODO Create a manager for SpecialEvent action threads that - * controls when they are executed and allows MapViewGameState - * to poll it to see if it is currently managing any action - * threads. If it is, MapViewGameState should be able to prevent - * certain actions from occuring (unless the action thread is - * ParallelProcess) such as keyboard input. - */ /** * Display a message on the screen. diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java new file mode 100644 index 0000000..6f6d6b0 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java @@ -0,0 +1,97 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; + +import com.fourisland.fourpuzzle.Game; +import com.fourisland.fourpuzzle.gamestate.mapview.Map; +import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; +import java.awt.Point; + +/** + * + * @author hatkirby + */ +public class AutomaticViewpoint implements Viewpoint { + + private Map map; + private Point heroLoc; + private Point viewpoint; + + public AutomaticViewpoint(Map map) + { + this.map = map; + heroLoc = Game.getHeroEvent().getLocation(); + refresh(); + } + + public void refresh() + { + int x,y; + HeroEvent hero = Game.getHeroEvent(); + heroLoc = hero.getLocation(); + + Point endLoc = new Point(hero.getLocation()); + if (hero.isMoving()) + { + switch (hero.getDirection()) + { + case North: endLoc.translate(0, -1); break; + case West: endLoc.translate(-1, 0); break; + case South: endLoc.translate(0, 1); break; + case East: endLoc.translate(1, 0); break; + } + } + + if (Math.max(endLoc.x,heroLoc.x) > 10) + { + if (Math.max(endLoc.x,heroLoc.x) < (map.getSize().width - 9)) + { + x = (heroLoc.x - 10) * 16; + x += hero.getMovingX(); + } else { + x = (map.getSize().width - 20) * 16; + } + } else { + x = 0; + } + + if (Math.max(endLoc.y,heroLoc.y) > 7) + { + if (Math.max(endLoc.y,heroLoc.y) < (map.getSize().height - 7)) + { + y = (heroLoc.y - 7) * 16; + y += hero.getMovingY(); + } else { + y = (map.getSize().height - 15) * 16; + } + } else { + y = 0; + } + + viewpoint = new Point(x,y); + } + + public int getX() + { + if (Game.getHeroEvent().getLocation().equals(heroLoc)) + { + refresh(); + } + + return viewpoint.x; + } + + public int getY() + { + if (!Game.getHeroEvent().getLocation().equals(heroLoc)) + { + refresh(); + } + + return viewpoint.y; + } + +} diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/Viewpoint.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/Viewpoint.java new file mode 100644 index 0000000..c46b280 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/Viewpoint.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint; + +/** + * + * @author hatkirby + */ +public interface Viewpoint { + + public int getX(); + public int getY(); + +} -- cgit 1.4.1