﻿#include <algorithm>
#include <cmath>

#include "Shooting/ImageManager.h"
#include "Shooting/Input.h"
#include "Shooting/Tuning/GameTuning.h"

#include "Shooting/Player.h"

namespace dxgame::shooting
{
    using Action = Input::Action;

    void Player::Reset()
    {
        // 第01回で実装する
    }

    void Player::Update(const Input& input, float deltaTime)
    {
        Vector2 direction;

        // 第01回: 入力から移動方向を求める
        static_cast<void>(input);

        // 斜め入力を長さ1に収める
        // 方向と長さの計算は第05回で詳しく扱う
        float length = std::sqrt(direction.x * direction.x + direction.y * direction.y);

        if (length > 1.0f)
        {
            direction.x /= length;
            direction.y /= length;
        }

        // 第01回: 方向、速度、1フレーム分の時間から位置を更新する
        static_cast<void>(deltaTime);

        // 命中範囲全体が画面内に残るよう移動範囲を制限する
        // 命中範囲と制限の計算は第04回で詳しく扱う
        const Rect& hitbox = tuning::player::Hitbox;

        float minX = -hitbox.x;
        float minY = -hitbox.y;
        float maxX = tuning::ScreenWidth - hitbox.x - hitbox.width;
        float maxY = tuning::ScreenHeight - hitbox.y - hitbox.height;
        position.x = std::clamp(position.x, minX, maxX);
        position.y = std::clamp(position.y, minY, maxY);
    }

    void Player::Draw(const ImageManager&) const
    {
        // 第01回で実装する
    }

    Rect Player::GetDrawRect() const
    {
        return tuning::player::DrawRect.MovedBy(position);
    }
}
