#include "BaseObject.h"
#include "Actor/Map/GameMap.h"
#include "Actor/Map/GameMapCamera.h"

BaseObject::BaseObject()
{
    Init(); // 调用了RenderBase的Init函数 对象才会被执行回调
    SetAnchor({0.5f, 0.5f});
}

BaseObject::~BaseObject()
{
}

void BaseObject::Update(float deltaTime)
{
    if (_AffCamera != nullptr)
    {
        _AffCamera->SyncPos(deltaTime);
    }
    Actor::Update(deltaTime);
}

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

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

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

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

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

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

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

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

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