Package kku.cs.fgl

Source Code of kku.cs.fgl.TilemapViewPort

/*
* Foogl (Friendly Object Oriented Game Library)
* Copyright (c) 2008 Wachirawut Thamviset.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package kku.cs.fgl;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import kku.cs.util.StringUtil;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.util.ResourceLoader;

/**
* �� ViewPort �����ʴ������ѧ��Ẻ TileMap
*
* @author Wachirawut Thamviset
*
*/
public class TilemapViewPort extends ViewPort {
  private SpriteSheet sheet;

  private int map[][];

  private int mapwidth, mapheight;

  private int tileWidth = 10, tileHeight = 10;

  private boolean drawGrid = false;
  private org.newdawn.slick.Color gridColor = Color.red;

  public boolean isDrawGrid() {
    return drawGrid;
  }

  public void setDrawGrid(boolean drawGrid) {
    this.drawGrid = drawGrid;
  }

  public TilemapViewPort(int id) {
    super(id);
  }

  public TilemapViewPort(int id, AbstractScene scene, float x, float y) {
    super(id, scene, x, y);
  }

  public SpriteCell getCell(int tx, int ty) {
    if (sheet != null){
      int no = getTile(tx, ty);
      return sheet.getCell(no);
    }
    return null;
  }

  public int getTile(int tx, int ty) {
    if (map == null)
      return -1;
    if (tx >= 0 && tx < mapwidth && ty >= 0 && ty < mapheight) {
      return map[ty][tx];
    }
    return -1;
  }

  /**
   * paint map Paint only cells are in view.
   */
  public void paint(Graphics g) {
    if (mapwidth > 0 && mapheight > 0) {
      for (int j = 0; j < mapheight; j++) {
        for (int i = 0; i < mapwidth; i++) {
          Shape r = getTileShape(i, j);
          if (isInView(r)) {
            renderCell(g, i, j, r);
          }
        }
      }
    }
  }

  /**
   * Render SpriteCell
   *
   * @param g
   * @param tx
   *            :tile x
   * @param ty
   *            :tile y
   * @param x
   *            :location to render
   * @param y
   *            :location to render
   */
  private void renderCell(Graphics g, int tx, int ty, Shape r) {
    if (drawGrid) {
      g.setColor(gridColor);
      g.draw(r);
    }
    //if (sheet != null) {
      SpriteCell cell = getCell(tx, ty);
      paintCell(g,tx,ty,r,cell);
    //}
  }

  public void paintCell(Graphics g, int tx, int ty, Shape r, SpriteCell cell) {
    if (cell != null) {     
      cell.paint(g, (int) r.getX(), (int) r.getY(), getBgColor());
    }   
  }

  public void setMap(int[][] amap) {
    // this.map = map;
    mapwidth = amap[0].length;
    for (int i = 1; i < amap.length; i++) {
      if (amap[i].length > mapwidth)
        mapwidth = amap[i].length;
    }
    mapheight = amap.length;
    setMapSize(mapwidth, mapheight);
    for (int i = 0; i < amap.length; i++) {
      for (int j = 0; j < amap[i].length; j++)
        this.map[i][j] = amap[i][j];
    }
  }

  public void setMapSize(int w, int h) {
    int[][] newmap = new int[h][w];
    mapwidth = w;
    mapheight = h;
    if (map != null) {
      for (int i = 0; i < h; i++) {
        if (i < map.length) {
          for (int j = 0; j < w; j++) {
            if (j < map[i].length)
              newmap[i][j] = map[i][j];
          }
        }
      }
    }
    map = newmap;
    setBounds(0, 0, mapwidth * tileWidth, mapheight * tileHeight);
  }

  public void setTile(int tx, int ty, int cellno) {
    if (tx >= 0 && tx < mapwidth && ty >= 0 && ty < mapheight) {
      map[ty][tx] = cellno;
    }
  }

  public void setTileSize(int tw, int th) {
    tileWidth = tw;
    tileHeight = th;
    setBounds(0, 0, mapwidth * tileWidth, mapheight * tileHeight);
  }

  public SpriteSheet getSheet() {
    return sheet;
  }

  public void setSheet(SpriteSheet sheet) {
    this.sheet = sheet;
  }

  /**
   * ��Ŵ������Ἱ���ҡ��� ������Ἱ����� text ������ٻẺ�ѧ���
   * ��÷Ѵ��� 1 ��Сͺ���� mapwidth,mapheight,tileWidth,tileHeight
   * ��÷Ѵ���� �繢����Ţͧ���Ъ�ͧ
   *
   * @param filename
   */
  public void loadMap(String filename) {
    InputStreamReader reader = new InputStreamReader(
        ResourceLoader.getResourceAsStream(filename));
    BufferedReader in = new BufferedReader(reader);
    try {
      String header[] = StringUtil.split(in.readLine(), ',');
      int w = Integer.parseInt(header[0]);
      int h = Integer.parseInt(header[1]);
      int tw = Integer.parseInt(header[2]);
      int th = Integer.parseInt(header[3]);
      setMapSize(w, h);
      for (int i = 0; i < h; i++) {
        String line = in.readLine();
        if (line == null)
          break;
        String data[] = StringUtil.split(line, ",");
        for (int j = 0; j < data.length; j++) {
          map[i][j] = Integer.parseInt(data[j].trim());
        }
      }
      setTileSize(tw, th);
      System.out.println("w:"+tw+" h:"+th);
      in.close();
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public int getTileHeight() {
    return tileHeight;
  }

  public void setTileHeight(int tileHeight) {
    this.tileHeight = tileHeight;
  }

  public int getTileWidth() {
    return tileWidth;
  }

  public void setTileWidth(int tileWidth) {
    this.tileWidth = tileWidth;
  }

  public int[][] getMap() {
    return map;
  }

  public int getMapheight() {
    return mapheight;
  }

  public int getMapwidth() {
    return mapwidth;
  }

  public Point getCellFromXY(float x, float y) {
    Point p = new Point((int) (x / tileWidth), (int) (x / tileHeight));
    return p;
  }

  public Shape getTileShapeFromXY(float x, float y) {
    Point p = new Point((int) (x / tileWidth), (int) (x / tileHeight));
    return getTileShape(p.x, p.y);
  }

  public Point getCellFromScreenXY(float screenX, float screenY) {
    return getCellFromXY(screenX + getX(), screenY + getY());
  }

  public Shape getTileShapeFromScreenXY(float screenX, float screenY) {
    return getTileShapeFromXY(screenX + getX(), screenY + getY());
  }

  public Point getTileLocation(int col, int row) {
    return new Point(col * tileWidth, row * tileHeight);
  }

  public Shape getTileShape(int col, int row) {
    Point p = getTileLocation(col, row);
    return new Rectangle(p.x, p.y, tileWidth, tileHeight);
  }

  public org.newdawn.slick.Color getGridColor() {
    return gridColor;
  }

  public void setGridColor(org.newdawn.slick.Color gridColor) {
    this.gridColor = gridColor;
  }

}
TOP

Related Classes of kku.cs.fgl.TilemapViewPort

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.