Android Open Source - nadia Tile Map






From Project

Back to project page nadia.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project nadia listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * TileMap.java// ww w  . j a  v a 2s  .  co m
 * 
 * Copyright (c) 2013, Emmanuel Arana Corzo. All rights reserved.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package com.redarctic.nadia.baseengine.tileengine;

import java.util.ArrayList;

import com.redarctic.nadia.ext.MathHelper;
import com.redarctic.nadia.collision.Rectangle;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;


public class TileMap {
  ArrayList<Tileset> tilesets;
  ArrayList<MapLayer> mapLayers;
  
  static int mapWidth;
  static int mapHeight;
  
  public static int getWidthInPixels() {
    return mapWidth * Engine.getTileWidth();
  }
  
  public static int getHeightInPixels() {
    return mapHeight * Engine.getTileHeight();
  }
  
  public TileMap(ArrayList<Tileset> tilesets, ArrayList<MapLayer> layers) 
  throws TileException {
    this.tilesets = tilesets;
    this.mapLayers = layers;
    
    mapWidth = mapLayers.get(0).getWidth();
    mapHeight = mapLayers.get(0).getHeight();
    
    for (int i = 1; i < layers.size(); i++) {
      if (mapWidth != mapLayers.get(i).getWidth() || mapHeight != mapLayers.get(i).getHeight())
        throw new TileException("Map layer size exception");
    }
  }
  
  public TileMap(Tileset tileset, MapLayer layer) {
    this.tilesets = new ArrayList<Tileset>();
    this.tilesets.add(tileset);
    
    this.mapLayers = new ArrayList<MapLayer>();
    this.mapLayers.add(layer);
    
    mapWidth = mapLayers.get(0).getWidth();
    mapHeight = mapLayers.get(0).getHeight();
  }
  
  public void drawMe(Canvas canvas, Camera camera) {
    Point cameraPoint = Engine.vectorToCell(
        MathHelper.pointMulScalar(camera.getPosition(), 
            Math.round(1 / camera.getZoom()))
        );
    Point viewPoint = Engine.vectorToCell(
        new Point(
            (int)Math.round((camera.getPosition().x + camera.getViewportRectangle().width())
                * (1 / camera.getZoom())), 
            (int)Math.round((camera.getPosition().y + camera.getViewportRectangle().height()) 
                * (1 / camera.getZoom()))
            )
    );
    
    Point min = new Point();
    Point max = new Point();
    
    min.x = Math.max(0, cameraPoint.x - 1);
    min.y = Math.max(0, cameraPoint.y - 1);
    max.x = Math.min(viewPoint.x + 1, mapWidth);
    max.y = Math.min(viewPoint.y + 1, mapHeight);
    
    Rectangle destination = new Rectangle(0, 0, Engine.getTileWidth(), Engine.getTileHeight());
    Tile tile;
    
    for (MapLayer layer : this.mapLayers) {
      for (int y = min.y; y < max.y; y++) {
        destination.top = y * Engine.getTileHeight();
        destination.bottom = (y * Engine.getTileHeight()) + Engine.getTileHeight();
        
        for (int x = min.x; x < max.x; x++) {
          tile = layer.getTile(x, y);
          
          if (tile.getTileIndex() == -1 || tile.getTileset() == -1)
            continue;
          
          destination.left = x * Engine.getTileWidth();
          destination.right = (x * Engine.getTileWidth()) + Engine.getTileWidth();
          
          Rectangle rSrc = tilesets.get(tile.getTileset()).getSourceRectangles()[tile.getTileIndex()];
          Rect rSrc1 = new Rect((int)rSrc.left, (int)rSrc.top, (int)rSrc.right, (int)rSrc.bottom);          
          canvas.drawBitmap(
              tilesets.get(tile.getTileset()).getTexture(), 
              rSrc1,
              destination, 
              new Paint());
        }        
      }
    }
  }
  
  public void addLayer(MapLayer layer) throws TileException {
    if (layer != null) {
      if (layer.getWidth() != mapWidth && layer.getHeight() != mapHeight)
        throw new TileException("Map layer size exception");
      
      mapLayers.add(layer);
    }
  }
}




Java Source Code List

com.ashokgelal.samaya.DateTimeFormatter.java
com.ashokgelal.samaya.DateTimeInterval.java
com.ashokgelal.samaya.DateTimeParser.java
com.ashokgelal.samaya.DateTime.java
com.ashokgelal.samaya.ModelUtil.java
com.ashokgelal.samaya.Samaya.java
com.ashokgelal.samaya.TimeSpan.java
com.ashokgelal.samaya.ToStringUtil.java
com.ashokgelal.samaya.Util.java
com.redarctic.nadia.baseengine.BaseGameState.java
com.redarctic.nadia.baseengine.ColorPallete.java
com.redarctic.nadia.baseengine.DrawableGameComponent.java
com.redarctic.nadia.baseengine.DrawableObject.java
com.redarctic.nadia.baseengine.GameComponent.java
com.redarctic.nadia.baseengine.GameStateManager.java
com.redarctic.nadia.baseengine.GameState.java
com.redarctic.nadia.baseengine.GameSurfaceView.java
com.redarctic.nadia.baseengine.GameSurface.java
com.redarctic.nadia.baseengine.SpriteFont.java
com.redarctic.nadia.baseengine.Sprite.java
com.redarctic.nadia.baseengine.character.BaseModifier.java
com.redarctic.nadia.baseengine.character.Classes.java
com.redarctic.nadia.baseengine.character.FormulaModifierException.java
com.redarctic.nadia.baseengine.character.Player.java
com.redarctic.nadia.baseengine.item.ArmorLocation.java
com.redarctic.nadia.baseengine.item.Armor.java
com.redarctic.nadia.baseengine.item.BaseItem.java
com.redarctic.nadia.baseengine.item.ItemManager.java
com.redarctic.nadia.baseengine.item.ItemType.java
com.redarctic.nadia.baseengine.item.Shield.java
com.redarctic.nadia.baseengine.item.Weapon.java
com.redarctic.nadia.baseengine.item.WeaponsType.java
com.redarctic.nadia.baseengine.sprite.AnimatedSprite.java
com.redarctic.nadia.baseengine.sprite.AnimationException.java
com.redarctic.nadia.baseengine.sprite.AnimationKey.java
com.redarctic.nadia.baseengine.sprite.Animation.java
com.redarctic.nadia.baseengine.tileengine.Camera.java
com.redarctic.nadia.baseengine.tileengine.Engine.java
com.redarctic.nadia.baseengine.tileengine.MapLayer.java
com.redarctic.nadia.baseengine.tileengine.TileException.java
com.redarctic.nadia.baseengine.tileengine.TileMap.java
com.redarctic.nadia.baseengine.tileengine.Tile.java
com.redarctic.nadia.baseengine.tileengine.Tileset.java
com.redarctic.nadia.baseengine.world.Level.java
com.redarctic.nadia.baseengine.world.World.java
com.redarctic.nadia.collision.Circle.java
com.redarctic.nadia.collision.CollisionPolygon.java
com.redarctic.nadia.collision.ConvexPolygonException.java
com.redarctic.nadia.collision.Rectangle.java
com.redarctic.nadia.controls.ButtonPad.java
com.redarctic.nadia.controls.DirectionalPad.java
com.redarctic.nadia.controls.menu.CharacterDialog.java
com.redarctic.nadia.controls.menu.ControlBorder.java
com.redarctic.nadia.controls.menu.ControlManager.java
com.redarctic.nadia.controls.menu.Control.java
com.redarctic.nadia.controls.menu.Label.java
com.redarctic.nadia.controls.menu.LeftRightSelector.java
com.redarctic.nadia.controls.menu.LinkLabel.java
com.redarctic.nadia.controls.menu.Menu.java
com.redarctic.nadia.controls.menu.PictureBox.java
com.redarctic.nadia.controls.menu.SimpleConversation.java
com.redarctic.nadia.ext.MathHelper.java
com.redarctic.nadia.ext.StringHelper.java
com.redarctic.nadia.ext.simplesignalslot.ISignalProvider.java
com.redarctic.nadia.ext.simplesignalslot.ISlotProvider.java
com.redarctic.nadia.ext.simplesignalslot.PrimitiveWrapper.java
com.redarctic.nadia.ext.simplesignalslot.SignalSlotMap.java
com.redarctic.nadia.ext.simplesignalslot.SignalSlotPair.java
com.redarctic.nadia.ext.simplesignalslot.SlotProviderMethodPair.java