From aa3d2a1e1d13b04a8c5801629e077668214bc3ff Mon Sep 17 00:00:00 2001
From: Starla Insigna <hatkirby@fourisland.com>
Date: Fri, 30 Jan 2009 09:40:40 -0500
Subject: Added viewpoint scrolling

Now, maps can be larger than (20,15) and the map will scroll as the hero walks across the middle. However, Southward and Eastward middle-traversing appears to warp reality just a little and there are a few kinks that need to be straightened out.

Map layer caching has also been added because the lower and upper layers of a map never change, so they are cached after the first rendering.
---
 .../fourpuzzle/gamestate/mapview/Map.java          | 59 ++++++++++++++++
 .../gamestate/mapview/MapViewGameState.java        | 80 ++++++++++------------
 .../gamestate/mapview/event/AbstractEvent.java     | 28 +++++---
 .../fourpuzzle/gamestate/mapview/event/Event.java  |  3 +
 .../gamestate/mapview/event/EventCall.java         |  1 +
 5 files changed, 118 insertions(+), 53 deletions(-)

(limited to 'src')

diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java
index bc0073e..d02c7ed 100644
--- a/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java
+++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java
@@ -11,6 +11,8 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList;
 import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent;
 import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
 import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
 import java.util.HashMap;
 import java.util.Vector;
 
@@ -185,4 +187,61 @@ public abstract class Map {
         this.music = music;
     }
     
+    BufferedImage lowerLayer = null;
+    public BufferedImage renderLower()
+    {
+        if (lowerLayer == null)
+        {
+            lowerLayer = new BufferedImage(size.width*16, size.height*16, BufferedImage.TYPE_INT_ARGB);
+            Graphics2D g = lowerLayer.createGraphics();
+            ChipSet chipSetObj = ChipSet.getChipSet(chipSet);
+            int i,x,y;
+            for (i=0;i<mapData.size();i++)
+            {
+                for (y=0;y<size.height;y++)
+                {
+                    for (x=0;x<size.width;x++)
+                    {
+                        int tile = mapData.get(i).get(x+(y*size.width));
+                        if (chipSetObj.getChipSetData().get(tile).getLayer() != Layer.Above)
+                        {
+                            g.drawImage(chipSetObj.getImage(tile), x*16, y*16, null);
+                        }
+                    }
+                }
+            }
+        }
+        
+        return lowerLayer;
+        
+    }
+    
+    BufferedImage upperLayer = null;
+    public BufferedImage renderUpper()
+    {
+        if (upperLayer == null)
+        {
+            upperLayer = new BufferedImage(size.width*16, size.height*16, BufferedImage.TYPE_INT_ARGB);
+            Graphics2D g = upperLayer.createGraphics();
+            ChipSet chipSetObj = ChipSet.getChipSet(chipSet);
+            int i,x,y;
+            for (i=0;i<mapData.size();i++)
+            {
+                for (y=0;y<size.height;y++)
+                {
+                    for (x=0;x<size.width;x++)
+                    {
+                        int tile = mapData.get(i).get(x+(y*size.width));
+                        if (chipSetObj.getChipSetData().get(tile).getLayer() == Layer.Above)
+                        {
+                            g.drawImage(chipSetObj.getImage(tile), x*16, y*16, null);
+                        }
+                    }
+                }
+            }
+        }
+        
+        return upperLayer;
+    }
+    
 }
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
index 36406af..dbc8aca 100644
--- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
+++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
@@ -21,6 +21,7 @@ import com.fourisland.fourpuzzle.util.Functions;
 import com.fourisland.fourpuzzle.util.ResourceNotFoundException;
 import java.awt.Graphics2D;
 import java.awt.event.KeyEvent;
+import java.awt.image.BufferedImage;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -162,59 +163,54 @@ public class MapViewGameState implements GameState {
     }
 
     public void render(Graphics2D g)
-    {
-        /* TODO Add code that checks to see if the map has been switched
-         * or if the hero has moved in such a way so as to move the
-         * viewpoint. If one or more of these conditions are filled, the
-         * already existing below code should run along with a snippet that
-         * saves a cache of the current viewpoint. If not, the previously
-         * mentioned cache should be displayed rather than re-rendering the
-         * map.
+    {   
+        /* TODO Fix viewpoint scrolling code. Currently, when the Hero moves
+         * South or East across the scroll barrier, it warps reality a little,
+         * while the other two directions scroll fine.
          */
-        ChipSet chipSet = ChipSet.getChipSet(currentMap.getChipSet());
-        int i,x,y;
-        for (i=0;i<currentMap.getMapData().size();i++)
+        
+        int x,y;
+        HeroEvent hero = Game.getHeroEvent();
+        if (hero.getLocation().x > 10)
         {
-            for (y=0;y<currentMap.getSize().height;y++)
+            if (hero.getLocation().x < (currentMap.getSize().width - 9))
             {
-                for (x=0;x<currentMap.getSize().width;x++)
-                {
-                    int tile = currentMap.getMapData().get(i).get(x+(y*currentMap.getSize().width));
-                    if (chipSet.getChipSetData().get(tile).getLayer() != Layer.Above)
-                    {
-                        g.drawImage(chipSet.getImage(tile), x*16, y*16, null);
-                    }
-                }
+                x = (hero.getLocation().x - 10) * 16;
+                x += hero.getMovingX();
+            } else {
+                x = (currentMap.getSize().width - 20) * 16;
             }
+        } else {
+            x = 0;
         }
-/*
-        MapViewer mv = new MapViewer(currentMap, new com.alienfactory.javamappy.viewer.render.J2SE14Renderer(currentMap), Game.WIDTH, Game.HEIGHT);
-        mv.setBlockX(Game.getSaveFile().getHero().getLocation().x);
-        mv.setBlockY(Game.getSaveFile().getHero().getLocation().y);
-        mv.setPixelX((4 - Game.getSaveFile().getHero().getMoveTimer()) * 4);
-        mv.draw(g, true);*/
-        Game.getSaveFile().getHero().render(g);
+        
+        if (hero.getLocation().y > 7)
+        {
+            if (hero.getLocation().y < (currentMap.getSize().height - 7))
+            {
+                y = (hero.getLocation().y - 7) * 16;
+                y += hero.getMovingY();
+            } else {
+                y = (currentMap.getSize().height - 15) * 16;
+            }
+        } else {
+            y = 0;
+        }
+        
+        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);
 
         EventList events = currentMap.getEvents();
         for (LayerEvent event : events)
         {
-            event.render(g);
+            event.render(g2);
         }
 
-        for (i=0;i<currentMap.getMapData().size();i++)
-        {
-            for (y=0;y<currentMap.getSize().height;y++)
-            {
-                for (x=0;x<currentMap.getSize().width;x++)
-                {
-                    int tile = currentMap.getMapData().get(i).get(x+(y*currentMap.getSize().width));
-                    if (chipSet.getChipSetData().get(tile).getLayer() == Layer.Above)
-                    {
-                        g.drawImage(chipSet.getImage(tile), x*16, y*16, null);
-                    }
-                }
-            }
-        }
+        g.drawImage(eventLayer, 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null);
+        g.drawImage(currentMap.renderUpper(), 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null);
     }
     
     public void initCurrentMap(String mapName)
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java
index c0ea634..7e8dd0d 100644
--- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java
+++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java
@@ -124,37 +124,43 @@ public abstract class AbstractEvent implements Event {
     
     public int getRenderX()
     {
-        int x = (getLocation().x * 16) - 4;
-        
+        return (getLocation().x * 16) - 4 + getMovingX();
+    }
+    
+    public int getRenderY()
+    {
+        return (getLocation().y * 16) - 16 + getMovingY();
+    }
+    
+    public int getMovingX()
+    {
         if (isMoving())
         {
             if (getDirection() == Direction.West)
             {
-                x -= (4 - moveTimer) * 4;
+                return -((4 - moveTimer) * 4);
             } else if (getDirection() == Direction.East)
             {
-                x += (4 - moveTimer) * 4;
+                return (4 - moveTimer) * 4;
             }
         }
         
-        return x;
+        return 0;
     }
     
-    public int getRenderY()
+    public int getMovingY()
     {
-        int y = (getLocation().y * 16) - 16;
-        
         if (isMoving())
         {
             if (getDirection() == Direction.North)
             {
-                y -= (4 - moveTimer) * 4;
+                return -((4 - moveTimer) * 4);
             } else if (getDirection() == Direction.South)
             {
-                y += (4 - moveTimer) * 4;
+                return (4 - moveTimer) * 4;
             }
         }
         
-        return y;
+        return 0;
     }
 }
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java
index 310c2a2..887e52b 100644
--- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java
+++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/Event.java
@@ -28,6 +28,9 @@ public interface Event {
     public int getRenderX();
     public int getRenderY();
     
+    public int getMovingX();
+    public int getMovingY();
+    
     public Direction getDirection();
     public void setDirection(Direction direction);
 
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java
index 64ca592..c0c2c3b 100644
--- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java
+++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/EventCall.java
@@ -5,6 +5,7 @@
 
 package com.fourisland.fourpuzzle.gamestate.mapview.event;
 
+import com.fourisland.fourpuzzle.gamestate.mapview.Map;
 import java.util.concurrent.Future;
 
 /**
-- 
cgit 1.4.1