/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

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 gecco.game.testgame;

import gecco.server.core.*;

/**
 *  Description of the Class
 *
 */
public class FlyAction extends Action {

    /**
     *  Description of the Field
     */
    private double addX;
    /**
     *  Description of the Field
     */
    private double addY;
    /**
     *  Description of the Field
     */
    private double stepLength = 2.5;


    /**
     *  Constructor for the FlyAction object
     */
    public FlyAction() {
        super();
    }


    // Override superclass methods

    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle) {
        throw new IllegalArgumentException("This action must be instanciated with a point as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle   Description of Parameter
     * @param  _unitHandle     Description of Parameter
     * @param  _argUnitHandle  Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, int _argUnitHandle) {
        throw new IllegalArgumentException("This action must be instanciated with a point as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     * @param  _argX          Description of Parameter
     * @param  _argY          Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, double _argX, double _argY) {
        super.initiate(_actionHandle, _unitHandle, _argX, _argY);

        // Calculate addX and addY
        double curX = getUnit().getX();
        double curY = getUnit().getY();
        double dx = _argX - curX;
        double dy = _argY - curY;
        double dist = Math.sqrt(dx * dx + dy * dy);
        double coeff = dist / stepLength;
        addX = dx / coeff;
        addY = dy / coeff;

    }


    /**
     *  Description of the Method
     *
     * @return    Description of the Returned Value
     */
    public ActionReturn checkPoint() {

        // Current coordinates are to be found in the Unit-class
        // getArgumentX(),getArgumentY() are the destination coordinates
        // Property FUEL holds the amount of fuel left (Max 100, min 0)

        // Check if there is enough fuel to continue the movement.
        double fuel = getUnit().getDoubleProperty("Fuel");
        if (fuel < 10) {
            getUnit().addBlockedAction("Fly");
            return new ActionReturnError("Unit out of fuel - cannot move.");
        }

        // Set new fuel value
        getUnit().setProperty("Fuel", fuel - 1);

        double curX = getUnit().getX();
        double curY = getUnit().getY();
        double destX = getArgumentX();
        double destY = getArgumentY();

        // Check if we're close enough to the endpoint!
        if (Math.abs(curX - destX) < stepLength && Math.abs(curY - destY) < stepLength) {
            // We're done, set coordinates to destination coordinates and return OK!
            System.out.println("Fly action! Destination reached!");
            getUnit().setPosition(destX, destY);
            return new ActionReturnCompleted();
        }

        // Set new coordinates and requeue!
        getUnit().setPosition(curX + addX, curY + addY);

        return new ActionReturnRequeue(0.3);
    }
}
