﻿#pragma once

#include "Common.h"
#include "ImageManager.h"
#include "Stage.h"

// プレイヤー画像のどの動きを再生するかを表す
enum class PlayerAnimation
{
    Idle,
    Run,
    Jump,
    Fall
};

// プレイヤーの状態を表す構造体
struct Player
{
    // 位置と大きさは、左上を基準にした矩形で扱う
    int x;
    int y;
    int width;
    int height;

    // 縦横の速度と、1ピクセル未満の移動量
    float vx;
    float vy;
    float remainderX;
    float remainderY;

    // 接地しているかどうかのフラグ true の時のみジャンプ可能
    bool onGround;

    // 見た目にだけ使うアニメーション情報
    PlayerAnimation animation;
    int animationFrame;
    int animationTimer;
    bool facingRight;
};

// ゲーム全体を管理するクラス
class Game
{
public:
    bool Initialize();
    void MainLoop();

private:
    // ゲームの状態を表す列挙型
    enum GameState
    {
        Playing,
        GameClear,
        GameOver
    };

    void StartPlay();
    void ResetPlay();
    void ResetPlayer();

    void Update();
    void UpdatePlaying();
    void UpdateResult();
    void UpdatePlayerInput();
    void UpdatePlayerPhysics();
    void UpdatePlayerAnimation();
    void UpdateCamera();
    void UpdateDebugToggle();

    void MoveHorizontal(float move);
    void MoveVertical(float move);

    Rect PlayerRect() const;
    Rect PlayerImageRect() const;
    int PlayerFrameIndex() const;

    void Draw();
    void DrawBackground() const;
    void DrawPlaying() const;
    void DrawResult() const;
    void DrawPlayer() const;
    void DrawHud() const;
    void DrawDebug() const;

private:
    // 画面サイズ
    static const int screenWidth = 640;
    static const int screenHeight = 480;

    // プレイヤーの動きに関係する調整値
    static constexpr float accel = 0.35f;
    static constexpr float maxSpeed = 4.5f;
    static constexpr float friction = 0.25f;
    static constexpr float gravity = 0.55f;
    static constexpr float maxFallSpeed = 12.0f;
    static constexpr float jumpPower = -11.0f;

    // Player.pngは同じ大きさのフレームを横8枚に並べる
    static const int playerFrameCount = 8;

    // 画像の1フレームを画面へ表示する大きさ
    static const int playerDrawWidth = 32;
    static const int playerDrawHeight = 48;

    ImageManager images;
    Stage stage;
    Player player = {};
    int cameraX = 0;
    GameState gameState = Playing;
    bool debugVisible = false;
    bool previousF1 = false;
};
