/*Copyright (C) 2010-2012 Duncan Deveaux

This file is part of Hikou no mizu.

Hikou no mizu 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 3 of the License, or
(at your option) any later version.

Hikou no mizu 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 Hikou no mizu.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Human.hpp"

Human::Human() : Player()
{
    
}

Human::Human(const std::string &name, float health, float strength, float speed, float jumpEnergy) :
Player(name, health, strength, speed, jumpEnergy)
{
    
}

void Human::act(float frameTime)
{
    if (!m_animations.animationBlocking())
    {
        Uint8 *keystate = SDL_GetKeyState(NULL);
        
        if (keystate[ m_keys.getMoveLeftKey() ]) move(0, frameTime); //Update moves
        else if (keystate[ m_keys.getMoveRightKey() ]) move(1, frameTime);
        else if (m_condition.isMoving()) stop();
        
        if (keystate[ m_keys.getJumpKey() ]) jump(); //Other actions
        else if (keystate[ m_keys.getAttackKey() ]) attack();
    }
}

void Human::setKeys(const PlayerKeys &keys)
{
    m_keys = keys;
}

const PlayerKeys &Human::getKeys() const
{
    return m_keys;
}

//Static methods
Human Human::loadFromXML(const std::string &name, const std::string &xmlPath)
{
    //TinyXML initialization
    TiXmlDocument xmlFile;
    xmlFile.LoadFile(xmlPath.c_str());
    
    TiXmlHandle hdl(&xmlFile);
    TiXmlElement *xmlElement = hdl.FirstChildElement().FirstChildElement().Element();
    
    int i = 0;
    while (xmlElement)
    {
        std::string itemName = xmlElement->Attribute("name");
        if (itemName == name)
        {
            //Get informations
            float strenght = 0, speed = 0, jumpEnergy = 0;
            
            xmlElement->QueryFloatAttribute("strenght", &strenght);
            xmlElement->QueryFloatAttribute("speed", &speed);
            xmlElement->QueryFloatAttribute("jumpEnergy", &jumpEnergy);
            
            //Create player
            return Human(name, 100, strenght, speed, jumpEnergy);
        }
        
        xmlElement = xmlElement->NextSiblingElement();
        i++;
    }
    
    return Human();
}
