/*Copyright (C) 2010 Deveaux Duncan

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 "AI.hpp"

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

void AI::act()
{
    x = getXPosition();
    
    move(direction);
    
    if (x == getXPosition())
    {
        if (direction == 1)
            direction = 0;
        else
            direction = 1;
    }
}

//Static methods
AI *AI::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, speed, jumpEnergy;
            
            xmlElement->QueryFloatAttribute("strenght", &strenght);
            xmlElement->QueryFloatAttribute("speed", &speed);
            xmlElement->QueryFloatAttribute("jumpEnergy", &jumpEnergy);
            
            //Create player
            AI *player = new AI(name, 100, strenght, speed, jumpEnergy);
            
            return player;
        }
        
        xmlElement = xmlElement->NextSiblingElement();
        i++;
    }
    
    return NULL;
}