92 行
2.3 KiB
C++
92 行
2.3 KiB
C++
#ifndef PINGPONG_GAME_H
|
|
#define PINGPONG_GAME_H
|
|
|
|
#include <godot_cpp/classes/node2d.hpp>
|
|
#include <godot_cpp/classes/label.hpp>
|
|
#include <godot_cpp/classes/input_event.hpp>
|
|
|
|
namespace godot {
|
|
|
|
class PingPongBall;
|
|
|
|
// ═══════════════════════════════════════════════════════
|
|
// 视觉常量 — 以后替换素材时删除本段
|
|
// (颜色值定义在 pingpong_game.cpp 中)
|
|
// ═══════════════════════════════════════════════════════
|
|
static constexpr float FIELD_INSET = 15.0f;
|
|
static constexpr float CENTER_LINE_DASH = 20.0f;
|
|
static constexpr float CENTER_LINE_GAP = 12.0f;
|
|
|
|
// 游戏状态
|
|
enum GameState {
|
|
WAITING,
|
|
COUNTDOWN,
|
|
PLAYING,
|
|
SCORED,
|
|
GAME_OVER
|
|
};
|
|
|
|
// 倒计时参数
|
|
static constexpr float COUNTDOWN_INTERVAL = 0.75f;
|
|
static constexpr int COUNTDOWN_START = 3;
|
|
static constexpr float SCORED_PAUSE = 1.2f;
|
|
|
|
class PingPongGame : public Node2D {
|
|
GDCLASS(PingPongGame, Node2D)
|
|
|
|
private:
|
|
int left_score = 0;
|
|
int right_score = 0;
|
|
int max_score = 5;
|
|
|
|
// 状态机
|
|
GameState state = WAITING;
|
|
float state_timer = 0.0f;
|
|
int countdown_number = COUNTDOWN_START;
|
|
|
|
// 子节点引用
|
|
Label *left_score_label = nullptr;
|
|
Label *right_score_label = nullptr;
|
|
Label *message_label = nullptr;
|
|
PingPongBall *ball = nullptr;
|
|
|
|
void update_score_display();
|
|
void set_state(GameState p_state);
|
|
void show_message(const String &p_text);
|
|
|
|
protected:
|
|
static void _bind_methods();
|
|
|
|
public:
|
|
PingPongGame();
|
|
~PingPongGame();
|
|
|
|
void _ready() override;
|
|
void _process(double delta) override;
|
|
void _draw() override;
|
|
void _input(const Ref<InputEvent> &event) override;
|
|
|
|
// 得分处理
|
|
void _on_ball_scored(int which);
|
|
|
|
// 重置整局游戏
|
|
void reset_game();
|
|
void start_countdown();
|
|
|
|
// 属性 getter/setter
|
|
void set_left_score(int p_score);
|
|
int get_left_score() const;
|
|
|
|
void set_right_score(int p_score);
|
|
int get_right_score() const;
|
|
|
|
void set_max_score(int p_score);
|
|
int get_max_score() const;
|
|
|
|
// 信号: game_over(int winner) — 0=左边赢, 1=右边赢
|
|
};
|
|
|
|
}
|
|
|
|
#endif // PINGPONG_GAME_H
|