/*
Tiamat - a role-playing game framework
Sequence.java - class to represent a sequence
    
 Copyright  (C) 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.serialization.Formatter;
import com.huguesjohnson.serialization.Storable;
import com.huguesjohnson.tiamat.Event;
import com.huguesjohnson.tiamat.graphics.SequenceFrame;

import java.util.ArrayList;

/**Sequence - class to represent a sequence
 * @author Hugues Johnson
 */
public class Sequence implements Storable{
/* begin public properties */
    //the underlying frames
    ArrayList<SequenceFrame> frames;
    /** Returns the number of frames in this sequence.
     * @return The number of frames in this sequence.
     */
    public int getFrameCount(){ return(this.frames.size()); }
    /** Adds a frame.
     * @param frame The frame to add.
     */
    public void addFrame(SequenceFrame frame){ this.frames.add(frame); } 
    /** Removes a frame.
     * @param index The index of the frame to remove.
     */
    public void removeFrame(int index){ 
        if((index>=0)&&(index<this.frames.size())){ 
            this.frames.remove(index); 
        } 
    } 
    /** Returns the frame at the specified index.
     * @param index The index of the frame to get.
     * @return The frame at the specified index, null if index<0 || index>length-1
     */
    public SequenceFrame getFrame(int index){ 
        if((index>=0)&&(index<this.frames.size())){ 
            return(this.frames.get(index));
        }
		return(null); 
    }
    /** Returns All frames in this sequence.
     * @return All frames in this sequence.
     */
    public SequenceFrame[] getFrames(){
        return(this.frames.toArray(new SequenceFrame[0]));
    }
    //the events to fire when this sequence is complete
    ArrayList<Event> exitEvents;
    /** Returns the number of exit events in this sequence.
     * @return The number of exit events in this sequence.
     */
    public int getExitEventCount(){ return(this.exitEvents.size()); }
    /** Adds an exit event.
     * @param event The exit event to add.
     */
    public void addExitEvent(Event event){ this.exitEvents.add(event); } 
    /** Removes an exit event.
     * @param index The index of the exit event to remove.
     */
    public void removeExitEvent(int index){ 
        if((index>=0)&&(index<this.exitEvents.size())){ 
            this.exitEvents.remove(index); 
        } 
    } 
    /** Returns the exit event at the specified index.
     * @param index The index of the exit event to get.
     * @return The exit event at the specified index, null if index<0 || index>length-1
     */
    public Event getExitEvent(int index){ 
        if((index>=0)&&(index<this.exitEvents.size())){ 
            return(this.exitEvents.get(index));
        }
		return(null); 
    }
    /** Returns All exit events in this sequence.
     * @return All exit events in this sequence.
     */
    public Event[] getExitEvents(){
        return(this.exitEvents.toArray(new Event[0]));
    }    
/* end public properties */

/* begin constructors */
    /** Create a new instance of Sequence.
     * @param frames The frames in this sequence.
     * @param exitEvents The events to fire when this sequence is complete.
     */
    public Sequence(SequenceFrame[] frames,Event[] exitEvents){
        //copy frames
        int length=frames.length;
        this.frames=new ArrayList<SequenceFrame>(length);
        for(int index=0;index<length;index++){
            this.frames.add(frames[index]);
        }
        //copy exitExits
        length=exitEvents.length;
        this.exitEvents=new ArrayList<Event>(length);
        for(int index=0;index<length;index++){
            this.exitEvents.add(exitEvents[index]);
        }
    }
/* end constructors */
    
/* begin overides */
    /** The onDeserialize event should be fired after an object is de-serialized.
     * This allows objects to load transient members, set their working directory, and load Storable members if needed.
     * @param formatter the formatter to use to load any storable members
     * @param absoluteDir the working directory since all paths stored in the object should be relative
     */
    public void onDeserialize(Formatter formatter,String absoluteDir){
        //call onDeserialize for all frames
        int length=this.frames.size();
        for(int index=0;index<length;index++){
            (this.frames.get(index)).onDeserialize(formatter,absoluteDir);
        }
    }
/* end overides */        
}