GameMap.java :  » Game » applet-game-framework » org » applet » game » app » rpg » map » Java Open Source

Java Open Source » Game » applet game framework 
applet game framework » org » applet » game » app » rpg » map » GameMap.java
package org.applet.game.app.rpg.map;

import java.awt.Event;
import java.awt.Graphics2D;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import org.applet.game.app.rpg.Sprite;
import org.applet.game.framework.core.SceneObject;
import org.applet.game.framework.core.Scene;
import org.applet.game.framework.core.common.Picture;
import org.applet.game.framework.core.common.Point;
import org.applet.game.framework.map.MapLoader;
import org.applet.game.framework.map.Node;
import org.applet.game.framework.map.PathCounter;
import org.applet.game.framework.util.ImageLoader;

public class GameMap extends SceneObject {

  public static final int GRIDSIZE = 20;
  
  Scene scene;
  
  Picture view;
  Node[][] mapSource;
  
  int width;
  int height;

  int gridWidht;
  int gridHeight;
  
  Sprite hero;
  ArrayList<Sprite> sprites = new ArrayList<Sprite>();
  
  Point targetPlace = new Point();
  
  public GameMap(String mapName)
  {
    this.view = ImageLoader.loadPicture("image/map/"+mapName);
    try {
      mapSource = MapLoader.loadMapDescription("image/map/"+mapName.split("\\.")[0]+".txt");
      System.out.println(mapSource.length);
      gridHeight = mapSource.length;
      if(gridHeight == 0)
        throw new RuntimeException("null Map");
      gridWidht = mapSource[0].length;
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    setWidth(view.getImageWidth());
    setHeight(view.getImageHeight());
    
  }
  
  public void addSprite(Sprite sprite)
  {
    sprites.add(sprite);
    sprite.setScene(scene);
    sprite.setCurrentMap(this);
  }
  
  public final void heroWalk(LinkedList<Node> path)
  {
    hero.walk(path);
  }
  
  @Override
  public void init() {
    hero.init();
    for(Sprite sprite : sprites)
    {
      sprite.init();
    }
  }
  
  @Override
  public boolean mouseDown(Event e) {
    int gridX = (hero.getCenterX()-5)/GRIDSIZE;
    int gridY = (hero.getCenterY()-5)/GRIDSIZE;
    int gridSceneX = scene.getX()/GRIDSIZE;
    int gridSceneY = scene.getY()/GRIDSIZE;
    int roleGridX = (targetPlace.getX()-5)/GRIDSIZE;
    int roleGridY = (targetPlace.getY()-5)/GRIDSIZE;
    System.out.println(gridSceneX+","+gridSceneY);
    LinkedList<Node> path = countPath(gridX, gridY, roleGridX, roleGridY, gridSceneX, gridSceneY, gridSceneX+39, gridSceneY+30);
    if(path.size() <= 1)
    {
      return false;
    }
    scene.walkLineOffsetCount = 0;//
    scene.walkLineOffset.clear();
    scene.walkLine = path;
    hero.walk(path);
    return false;
  }
  
  int centerX;
  int centerY;
  
  int screenWidht;
  int screenHeight;
  
  /**
   * 
   * @param x
   * @param y
   */
  public void centerToPixel(int x, int y)
  {
    centerX = x;
    centerY = y;
    int xx = x - screenWidht/2;
    if(xx < 0)
      xx = 0;
    if(xx > (gridWidht*GRIDSIZE-screenWidht))
      xx = gridWidht*GRIDSIZE - screenWidht;
    view.setImageX(xx);
    
    int yy = y - screenHeight/2;
    if(yy<0)
      yy = 0;
    if(yy > (gridHeight*GRIDSIZE-screenHeight))
      yy = gridHeight*GRIDSIZE-screenHeight;
    view.setImageY(yy);
  }
  
  public final LinkedList<Node> countPath(int sx, int sy, int dx, int dy, int rangeSX, int rangeSY, int rangeDX, int rangeDY)
  {
    return new PathCounter().count(getMapSource(), sx, sy, dx, dy, rangeSX, rangeSY, rangeDX, rangeDY);
  }
  

  public void render(Graphics2D painter, ImageObserver observer) {
    painter.drawImage(view.getView(), 0, 0, screenWidht, screenHeight, scene.getX(), scene.getY(), scene.getDX(), scene.getDY(), observer);
    //painter.drawImage(view.getView(), 0, 0, screenWidht, screenHeight, view.getImageX(), view.getImageY(), view.getImageDX(), view.getImageDY(), observer);
    for(Sprite sprite:sprites)
    {
      sprite.render(painter, observer);
    }
    hero.render(painter, observer);
  }

  public int getScreenWidth() {
    return screenWidht;
  }

  public static final Point screen2Page(int x, int y)
  {
    Point point = new Point();
    point.setX((x/GRIDSIZE) + ((x%GRIDSIZE)>=5?0:-1));//System.out.println(x+" " + ( (x/20) + ((x%20)>=10?1:0)) );
    point.setY((y/GRIDSIZE) + ((y%GRIDSIZE)>=5?0:-1));
    return point;
  }
  
  public static final Point page2Screen(int x, int y)
  {
    Point point = new Point();
    point.setX(x*GRIDSIZE + GRIDSIZE/2);
    point.setY(y*GRIDSIZE + GRIDSIZE/2);
    return point;
  }
  
  
  /**
   * 
   * @param screenWidht
   * @param screenHeight
   */
  public void setScreenSize(int screenWidht, int screenHeight) {
    this.screenWidht = screenWidht;
    this.screenHeight = screenHeight;
    view.setImageWidth(screenWidht);
    view.setImageHeight(screenHeight);
  }

  public int getScreenHeight() {
    return screenHeight;
  }

  /**
   * 
   * @return
   */
  public int getCenterX() {
    return centerX;
  }

  /**
   * 
   * @return
   */
  public int getCenterY() {
    return centerY;
  }
  
  /**
   * 
   * @return
   */
  public final int getImageX()
  {
    return view.getImageX();
  }
  
  /**
   * 
   * @return
   */
  public final int getImageY()
  {
    return view.getImageY();
  }
  
  public Node[][] getMapSource() {
    return mapSource;
  }

  public void setMapSource(Node[][] mapSource) {
    this.mapSource = mapSource;
  }

  public int getGridWidth() {
    return gridWidht;
  }

  public void setMapWidht(int mapWidht) {
    this.gridWidht = mapWidht;
  }

  public int getGridHeight() {
    return gridHeight;
  }

  public void setMapHeight(int mapHeight) {
    this.gridHeight = mapHeight;
  }

  public final Picture getView() {
    return view;
  }

  public final void setView(Picture view) {
    this.view = view;
  }

  /**
   * 
   * @return
   */
  public final int getGridX() {
    return view.getImageX()/GRIDSIZE;
  }

  /**
   * 
   */
  public final int getGridY() {
    return view.getImageY()/GRIDSIZE;
  }
  

  /**
   * 
   * @return
   */
  public final int getGridDX() {
    return view.getImageDX()/GRIDSIZE;
  }

  /**
   * 
   */
  public final int getGridDY() {
    return view.getImageDY()/GRIDSIZE;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public Sprite getHero() {
    return hero;
  }

  public void setHero(Sprite hero) {
    this.hero = hero;
    hero.setScene(scene);
  }

  public ArrayList<Sprite> getNpc() {
    return sprites;
  }

  public void setNpc(ArrayList<Sprite> npc) {
    this.sprites = npc;
  }

  public Scene getScene() {
    return scene;
  }

  public void setScene(Scene scene) {
    this.scene = scene;
  }

  public Point getTargetPlace() {
    return targetPlace;
  }

  public ArrayList<Sprite> getSprites() {
    return sprites;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.