/*
Tiamat - a role-playing game framework
Layer.java - a layer (or plane) that contains drawable items
 
 Copyright  (C) 2004-2005 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.graphics;

import com.huguesjohnson.PathResolver;
import com.huguesjohnson.serialization.Formatter;
import java.io.File;

/** class Layer - a layer (or plane) that contains drawable items
 * @author Hugues Johnson
 */
public class Layer{
/* begin public constants */
    /** 
     * A layer with a map associated to it.
     */
    public final static int TYPE_MAP=0;
    /** 
     * A layer where sprites move.
     */
    public final static int TYPE_SPRITE=1;
    /** 
     * A layer where dialogs are displayed.
     */
    public final static int TYPE_DIALOG=2;
    /** 
     * Default layer type.
     */
    public final static int DEFAULT_LAYER_TYPE=TYPE_SPRITE;
/* end public constants */

/* begin public properties */
    //what type of layer this is
    private int type;
    /**
     * Returns what type of layer this is, see TYPE constants.
     * @return What type of layer this is, see TYPE constants.
     */
    public int getType(){ return(this.type); }
    /**
     * Sets what type of layer this is, see TYPE constants.
     * @param type What type of layer this is, see TYPE constants.
     */
    public void setType(int type){ this.type=type; }
    //map for this layer, should be null unless type==MAP, change by setting the map path
    private transient Map map;
    /**
     * Returns the Map associated with this Layer.
     * Should be null unless type==MAP, change by setting the map path.
     * @return The Map associated with this Layer.
     */
    public Map getMap(){ 
        return(this.map); 
    }
    //load the map into memory and return it
    /**
     * Loads the Map into memory and return it.
     * @param formatter The Formatter used to deserialize the Map.
     * @return The Map that was loaded.
     */
    public Map loadMap(Formatter formatter){
        try{
            this.map=MapFactory.load(this.getMapPathAbsolute(),formatter);
            this.setMapPath(this.mapPath,formatter);
            return(this.map);
        } catch(UnsupportedMapException umx){
            umx.printStackTrace();
            return(null);
        }
    }
    //relative path to the file where the map is saved
    private String mapPath;
    /**
     * Returns the relative path to the file where the Map is saved.
     * @return The relative path to the file where the Map is saved.
     */
    public String getMapPath(){ return(this.mapPath); }
    /**
     * Returns the absolute path to the file where the Map is saved.
     * @return The absolute path to the file where the Map is saved.
     */
    public String getMapPathAbsolute(){ return(PathResolver.getAbsolutePath(this.absoluteDir,this.mapPath)); }
    /**
     * Sets the relative path to the file where the Map is saved.
     * @param mapPath The relative path to the file where the Map is saved.
     * @param formatter The Formatter used to deserialize the Map.
     */
    public void setMapPath(String mapPath,Formatter formatter){
        try{
            this.map=MapFactory.load(this.absoluteDir+mapPath,formatter);
            this.mapPath=mapPath;
        } catch(UnsupportedMapException umx){
            umx.printStackTrace();
        }
    }
    //absolute path to the directory where the map is saved
    private transient String absoluteDir;
    /**
     * Sets the absolute path to the directory where the Map is saved.
     * @param absoluteDir The absolute path to the directory where the Map is saved.
     */
    public void setAbsoluteDir(String absoluteDir){
        if(absoluteDir.endsWith(File.separator)){
            this.absoluteDir=absoluteDir;
        } else{
            this.absoluteDir=absoluteDir+File.separator;
        }
    }
/* end public properties */

/* begin constructors */
    /**
     * Creates new instance of Layer, uses DEFAULT_LAYER_TYPE. 
     */
    public Layer(){ 
        this.setType(Layer.DEFAULT_LAYER_TYPE);
    }
    /**
     * Creates new instance of Layer. 
     * @param type What type of layer this is, see TYPE constants.
     */
    public Layer(int type){
        this.setType(type);
    }
    //create a map layer with the map saved at mapPath, use MapFactory to load correct map type
    /**
     * Creates new instance of Layer, uses TYPE_MAP. 
     * @param mapPath The relative path to the file where the Map is saved.
     * @param formatter The Formatter used to deserialize the Map.
     */
    public Layer(String mapPath,Formatter formatter){
        this.setType(Layer.TYPE_MAP);
        this.setMapPath(mapPath,formatter);
    }
/* end constructors */
}