#include "BaseObject.h"
#include "Actor/Map/GameMap.h"
BaseObject::BaseObject()
{
    Init(); // 调用了RenderBase的Init函数 对象才会被执行回调
    SetAnchor({0.5f, 0.5f});
}

BaseObject::~BaseObject()
{
}

void BaseObject::SetPosition(VecFPos3 pos)
{
    if (pos.y != this->Position.y)
    {
        SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
    }
    this->Position = pos;
    Actor::SetPos(VecFPos{this->Position.x, this->Position.y - this->Position.z});
}

VecFPos3 BaseObject::GetPosition()
{
    return this->Position;
}

void BaseObject::SetXpos(float x)
{
    this->Position.x = x;
    Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
}

void BaseObject::SetYpos(float y)
{
    if (y != this->Position.y)
    {
        SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
    }
    this->Position.y = y;
    Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
}

void BaseObject::SetZpos(float z)
{
    this->Position.z = z;
    Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
}

int BaseObject::GetXpos()
{
    return this->Position.x;
}

int BaseObject::GetYpos()
{
    return this->Position.y;
}

int BaseObject::GetZpos()
{
    return this->Position.z;
}

void BaseObject::MoveBy(VecFPos3 pos)
{
    // 只有moveby移动时判断所在地图中是否能够这样移动
    VecFPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), pos);
    if (pos.y != 0)
    {
        SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
    }
    this->Position = RealPos;
    Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
}

void BaseObject::MoveBy(float x, float y, float z)
{
    // 只有moveby移动时判断所在地图中是否能够这样移动
    VecFPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), VecFPos3({x, y, z}));
    if (y != 0)
    {
        SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
    }
    this->Position = RealPos;
    Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
}

void BaseObject::SetDirection(int dir)
{
    this->Direction = dir;
    VecFPos sc = GetScale();
    // 朝右
    if (dir == 0)
    {
        SetScale(VecFPos({SDL_fabsf(sc.x), sc.y}));
    }
    // 朝左
    else if (dir == 1)
    {
        SetScale(VecFPos({-SDL_fabsf(sc.x), sc.y}));
    }
}

int BaseObject::GetDirection()
{
    return this->Direction;
}

ObjectVars &BaseObject::GetObjectVars()
{
    return _ObjectVars;
}
