Advertisement
rooster5105

Map Scrolling....I Need Help, Please?

Jul 7th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.60 KB | None | 0 0
  1. /**######
  2.  * Game.Java
  3.  *@author Robert
  4.  *@version 0.0.1
  5.  *Main Game class, handles the entire game.
  6.  */
  7.  
  8. package game;
  9. //Local Imports
  10. import game.core.GameCore;
  11. import game.input.GameAction;
  12. import game.input.InputManager;
  13. import game.world.World;
  14.  
  15. //Java Imports
  16. import java.awt.Graphics2D;
  17. import java.awt.event.KeyEvent;
  18. import java.awt.Point;
  19.  
  20.  
  21. public class Game extends GameCore {
  22.    
  23.     private Point cache = new Point();
  24.     private final static int SIZE_H = 256;
  25.     private final static int SIZE_W = 256;
  26.     private World world;
  27.     private WorldMapRenderer renderer;
  28.     private InputManager inputManager;
  29.    
  30.    
  31.     private WorldMap map;
  32.    
  33.     private GameAction exit;
  34.     private GameAction scrollMapLeft;
  35.     private GameAction scrollMapRight;
  36.     private GameAction scrollMapUp;
  37.     private GameAction scrollMapDown;
  38.  
  39.     public static void main(String args[]){
  40.         new Game().run();
  41.        
  42.     }
  43.     public void init(){
  44.         super.init();
  45.         initInput();
  46.         //create and start a new Resource Manager....Why?  Who Cares?!
  47.         ResourceManager rM = new ResourceManager(wm.getFullScreenWindow().getGraphicsConfiguration());
  48.         world = generateGameWorld(SIZE_H, SIZE_W);
  49.         //load our resources!
  50.         renderer = new WorldMapRenderer();
  51.         map = new WorldMap(world, SIZE_H, SIZE_W);
  52.        
  53.        
  54.     }
  55.     public void stop(){
  56.         super.stop();
  57.     }
  58.     private World generateGameWorld(int x, int y){
  59.         World gameWorld = new World(x, y);
  60.         return gameWorld;
  61.     }
  62.     @Override
  63.     public void draw(Graphics2D g) {
  64.         renderer.draw(g, map, wm.getWidth(), wm.getHeight());
  65.        
  66.     }
  67.    
  68.     public void initInput(){
  69.        
  70.         exit = new GameAction("exit");
  71.         scrollMapLeft = new GameAction("Scroll Map Left");
  72.         scrollMapRight = new GameAction("Scroll Map Right");
  73.         scrollMapUp = new GameAction("Scroll Map UP");
  74.         scrollMapDown = new GameAction("Scroll Map Down");
  75.        
  76.         inputManager = new InputManager(wm.getFullScreenWindow());
  77.         inputManager.mapToKey(scrollMapLeft, KeyEvent.VK_LEFT);
  78.         inputManager.mapToKey(scrollMapRight, KeyEvent.VK_RIGHT);
  79.         inputManager.mapToKey(scrollMapUp, KeyEvent.VK_UP);
  80.         inputManager.mapToKey(scrollMapDown, KeyEvent.VK_DOWN);
  81.        
  82.         inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
  83.        
  84.     }
  85.    
  86.     public void checkInput(long elapsedTime){
  87.         if (exit.isPressed()){
  88.             stop();
  89.         }
  90.        
  91.         if(scrollMapLeft.isPressed()){
  92.             if(map.getCenterX() < WorldMapRenderer.tilesToPixels(1)){
  93.                 map.setCenterX(0);
  94.             }
  95.             else{
  96.                 map.setCenterX(-1);
  97.             }
  98.         }
  99.         if(scrollMapRight.isPressed()){
  100.             if(WorldMapRenderer.pixelsToTiles(map.getCenterX()) >= WorldMapRenderer.tilesToPixels(map.getWidth())){
  101.                 map.setCenterX(0);
  102.             }
  103.             else{
  104.                 map.setCenterX(1);
  105.             }
  106.         }
  107.         if(scrollMapUp.isPressed()){
  108.             map.setCenterY(-1);
  109.         }
  110.         if(scrollMapDown.isPressed()){
  111.             map.setCenterY(1);
  112.         }
  113.     }
  114.    
  115.     public void update(long elapsedTime){
  116.         checkInput(elapsedTime);
  117.     }
  118.    
  119.    
  120. }
  121.  
  122. //###END GAME.JAVA
  123.  
  124. /**
  125.  * WorldMap.Java
  126.  * @author Robert
  127.  * Creates A World Map From the Generated World.
  128.  */
  129. package game;
  130.  
  131. import game.graphicsEngine.Sprite;
  132. import game.world.World;
  133.  
  134. import java.awt.Image;
  135. import java.awt.Point;
  136. import java.util.LinkedList;
  137. import java.util.Iterator;
  138. import java.util.TreeMap;
  139.  
  140. import javax.swing.ImageIcon;
  141.  
  142.  
  143. public class WorldMap {
  144.    
  145.     private Image[][] tiles;
  146.     private LinkedList<Sprite> sprites;
  147.     private Point centerPoint;
  148.    
  149.     public WorldMap(World world, int height, int width){
  150.         centerPoint = new Point();
  151.         centerPoint.x = WorldMapRenderer.tilesToPixels(width) / 2;
  152.         centerPoint.y = WorldMapRenderer.tilesToPixels(height) / 2;
  153.        
  154.         tiles = new Image[height][width];
  155.         for(int y = 0; y < height; y++){
  156.             for (int x = 0; x < width; x++){
  157.                 setTile(y, x, world.getTile(y, x).getImageIcon());
  158.             }
  159.         }
  160.        
  161.     }
  162.    
  163.     public void setTile(int y, int x, ImageIcon icon){
  164.         tiles[y][x] = icon.getImage();
  165.     }
  166.    
  167.     public Image getTile(int y, int x){
  168.         if (x < 0 || x >= getWidth()
  169.             || y < 0 || y>= getHeight()){
  170.             return null;
  171.         }
  172.         else{
  173.             return tiles[y][x];
  174.         }
  175.     }
  176.    
  177.     public int getWidth(){
  178.         return tiles[0].length;
  179.     }
  180.    
  181.     public int getHeight(){
  182.         return tiles.length;
  183.     }
  184.  
  185.     public void addSprite(Sprite sprite){
  186.         sprites.add(sprite);
  187.     }
  188.    
  189.     public void removeSprite(Sprite sprite){
  190.         sprites.remove(sprite);
  191.     }
  192.    
  193.    
  194.    
  195.     public Iterator<Sprite> getSprites() {
  196.         // TODO Auto-generated method stub
  197.         return sprites.iterator();
  198.     }
  199.  
  200.     /**
  201.      * @param width the width to set
  202.      */
  203.     public void setWidth(int width) {
  204.     }
  205.  
  206.     /**
  207.      * @param height the height to set
  208.      */
  209.     public void setHeight(int height) {
  210.     }
  211.    
  212.     public int getCenterX(){
  213.         return centerPoint.x;
  214.     }
  215.    
  216.     public int getCenterY(){
  217.         return centerPoint.y;
  218.     }
  219.     public void setCenterX(int delta){
  220.         centerPoint.x +=  WorldMapRenderer.tilesToPixels(delta);
  221.     }
  222.     public void setCenterY(int delta){
  223.         centerPoint.y += WorldMapRenderer.tilesToPixels(delta);
  224.     }
  225.    
  226. }
  227. //###End WorldMap.java###
  228.  
  229.  
  230. /**
  231.  * WorldMapRenderer.java
  232.  *@author Robert
  233.  * renders a worldMap to a displayable graphic
  234.  */
  235. package game;
  236.  
  237. import java.awt.Graphics2D;
  238. import java.awt.Image;
  239.  
  240. public class WorldMapRenderer {
  241.    
  242.     private static final int TILE_SIZE = 64;
  243.     /**
  244.      * The Size in Bits of Tile.
  245.      * Math.pow(2, TILE_SIZE_BITS) == TILE_SIZE;
  246.      */
  247.     private static final int TILE_SIZE_BITS = 6;
  248.    
  249.     public static int pixelsToTiles(float pixels){
  250.         return pixelsToTiles(Math.round(pixels));
  251.     }
  252.    
  253.     public static int pixelsToTiles(int pixels){
  254.         return pixels >> TILE_SIZE_BITS;
  255.     }
  256.    
  257.     public static int tilesToPixels(int numOfTiles){
  258.        
  259.         return numOfTiles << TILE_SIZE_BITS;
  260.        
  261.     }
  262.    
  263.     public void draw(Graphics2D g, WorldMap map, int screenWidth, int screenHeight){
  264.        
  265.         int mapWidth = tilesToPixels(map.getWidth());
  266.         int mapHeight = tilesToPixels(map.getHeight());
  267.        
  268.         //Not quite sure how to implement this yet.
  269.         int offsetX = ((screenWidth / 2) - (map.getCenterX() / 2));
  270.         //offsetX = Math.min(offsetX, 0);
  271.         //offsetX = Math.max(offsetX, screenWidth - mapWidth);
  272.        
  273.        
  274.        
  275.         //get offsetY for drawing Sprites.
  276.         int offsetY = ((screenHeight / 2) - (map.getCenterY() / 2));
  277.         //offsetY = Math.min(offsetY, 0);
  278.         //offsetY = Math.max(offsetY, screenHeight - mapHeight);
  279.            
  280.        
  281.         //draw the visible map.
  282.         int firstTileX = pixelsToTiles(- offsetX);
  283.         int firstTileY = pixelsToTiles(-offsetY);
  284.         if (firstTileX < 0){
  285.             firstTileX = 0;
  286.         }
  287.         if (firstTileY < 0){
  288.             firstTileY = 0;
  289.         }
  290.        
  291.         int lastTileX = firstTileX + pixelsToTiles(screenWidth) + 1;
  292.         int lastTileY = firstTileY + pixelsToTiles(screenHeight) + 1;
  293.         if (lastTileX >= map.getWidth()){
  294.             lastTileX = map.getWidth() - 1;
  295.         }
  296.         if (lastTileY >= map.getHeight()){
  297.             lastTileY = map.getHeight() - 1;
  298.         }
  299.         for (int y = firstTileY; y <= lastTileY; y++){
  300.             for(int x = firstTileX; x <= lastTileX; x++){
  301.                 Image image = map.getTile(y, x);
  302.                 if (image != null){
  303.                     g.drawImage(image,
  304.                                 tilesToPixels(x) + offsetX,
  305.                                 tilesToPixels(y) + offsetY,
  306.                                 null);
  307.                 }
  308.             }
  309.         }
  310.        
  311.         /*
  312.         //draw Sprites --(really do nothing for now, uncomment when you have unit animations and shit.  
  313.          * right now we're just trying to draw the map.)
  314.         Iterator i = map.getSprites();
  315.         while(i.hasNext()){
  316.             Sprite sprite = (Sprite)i.next();
  317.             int x = Math.round(sprite.getX() + offsetX);
  318.             int y = Math.round(sprite.getY() + offsetY);
  319.             g.draw(sprite.getImage(), x, y, null);
  320.        
  321.             //wake up the unit if its on screen.
  322.             if (sprite instanceof Unit && x >= 0 && x < screenWidth && y >= 0 && y < screenHeight){
  323.                 ((Unit)sprite).wakeup();
  324.             }
  325.         }
  326.         */
  327.     }
  328.  
  329.     public static int getTileSize() {
  330.         return TILE_SIZE;
  331.     }
  332. }
  333.  
  334. //###End WorldMapRenderer.java###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement