/*
 * HalflingSkills.java	1.0 Oct  9, 2002
 * 
 * Copyright (c) 2002 Cabochon Technologies, L.L.C.  All Rights Reserved.
 */


package wyvern.lib.skills;

import wyvern.lib.*;
import wyvern.lib.properties.AddRemoveNotify;

/**
 * Implements the "hide" and "show" commands.
 *
 * @version	1.0, Oct  9, 2002
 * @author	Steve Yegge
 */

public class HalflingSkills
    implements AddRemoveNotify, Command
{

    /**
     * Notifies the property that it's being added.  
     * @param obj the GameObject whose property list we're being
     * added to (in this case, the rakshasa player)
     * @author rhialto
     */
    public void notifyAdd ( PropertyList obj )
    {
	if ( !(obj instanceof Player ) ) {
	    return;
	}

	Player p = (Player) obj;
	p.registerCommand ( "hide", this );
	p.registerCommand ( "unhide", this );

    } // end notifyAdd

    /**
     * Notifies the property that it's being removed.  
     * @param obj the GameObject whose property list we're being
     * removed from (in this case, the naga player)
     * @author rhialto
     */
    public void notifyRemove ( PropertyList obj )
    {
	if ( !(obj instanceof Player )) return;

	Player p = (Player) obj;
	p.unregisterCommand ( "hide", this );
	p.unregisterCommand ( "unhide", this );

    } // end notifyRemove

    /**
     * Returns true for our commands.
     * @author rhialto
     */
    public boolean knowsCommand ( String cmd )
    {
	return cmd.equals ( "hide" ) || cmd.equals ( "unhide" );

    } // end of knowsCommand

    /**
     * Returns our event.
     * @author rhialto
     */
    public CommandEvent createEvent ( CommandEvent initial )
    {
	return initial;

    } // end of createEvent

    /**
     * Executes the event.
     * @author rhialto
     */
    public boolean execute ( CommandEvent event )
    {
	Commandable agent = event.getAgent();
	String verb = event.getVerb();

	if ( verb.equals ( "hide" )) {

	    if ( agent.hasProperty ( "invisible" )) {
		agent.message ( "You are already invisible." );
		return false;
	    }

	    agent.message ( "You blend into the shadows." );
	    agent.setIntProperty ( "alpha", 50 );
	    agent.adjustIntProperty ( "invisible", 1 );
	    agent.invalidate();
	}

	else {

	    if ( !agent.hasProperty ( "invisible" )) {
		agent.message ( "You are already visible." );
		return false;
	    }

	    agent.adjustIntProperty ( "invisible", -1 );
	    if ( !agent.hasProperty ( "invisible" )) {
		agent.message ( "You emerge from the shadows." );
		agent.removeProperty ( "alpha" );
	    } else {
		agent.message ( "You are still invisible." );
	    }
	    agent.invalidate();
	}

	return true;

    } // end of execute

} // end class HalflingSkills