带宽测试
这个提交包含在:
+48
-10
@@ -1,6 +1,7 @@
|
||||
#include "pingpong_paddle.h"
|
||||
|
||||
#include <godot_cpp/core/class_db.hpp>
|
||||
#include <godot_cpp/variant/utility_functions.hpp>
|
||||
#include <godot_cpp/classes/input.hpp>
|
||||
#include <godot_cpp/classes/engine.hpp>
|
||||
#include <godot_cpp/classes/node.hpp>
|
||||
@@ -9,8 +10,13 @@
|
||||
|
||||
using namespace godot;
|
||||
|
||||
// ═══════════════════════════════════════════════════════
|
||||
// 视觉颜色常量 — 以后替换素材时删除本段
|
||||
// ═══════════════════════════════════════════════════════
|
||||
static const Color PADDLE_PLAYER_COLOR = Color(0.2f, 0.7f, 1.0f);
|
||||
static const Color PADDLE_AI_COLOR = Color(1.0f, 0.35f, 0.25f);
|
||||
|
||||
void PingPongPaddle::_bind_methods() {
|
||||
// 属性
|
||||
ClassDB::bind_method(D_METHOD("get_speed"), &PingPongPaddle::get_speed);
|
||||
ClassDB::bind_method(D_METHOD("set_speed", "value"), &PingPongPaddle::set_speed);
|
||||
ClassDB::add_property("PingPongPaddle", PropertyInfo(Variant::FLOAT, "speed"), "set_speed", "get_speed");
|
||||
@@ -27,14 +33,33 @@ void PingPongPaddle::_bind_methods() {
|
||||
PingPongPaddle::PingPongPaddle() {
|
||||
speed = 350.0f;
|
||||
is_player = true;
|
||||
ai_reaction_counter = 0.0f;
|
||||
ai_target_y = 0.0f;
|
||||
}
|
||||
|
||||
PingPongPaddle::~PingPongPaddle() {
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════
|
||||
// 视觉层 — 圆角球拍(几何体占位)
|
||||
// 替换方法:删除本方法 → 添加 Sprite2D 子节点
|
||||
// ═══════════════════════════════════════════════════════
|
||||
void PingPongPaddle::_draw() {
|
||||
// 画一个白色矩形表示球拍
|
||||
draw_rect(Rect2(Vector2(-10.0f, -60.0f), Vector2(20.0f, 120.0f)), Color(1.0f, 1.0f, 1.0f));
|
||||
Color color = is_player ? PADDLE_PLAYER_COLOR : PADDLE_AI_COLOR;
|
||||
float hw = PADDLE_WIDTH / 2.0f;
|
||||
float hh = PADDLE_HEIGHT / 2.0f;
|
||||
float cr = PADDLE_CORNER_RADIUS;
|
||||
|
||||
// 主体矩形
|
||||
draw_rect(Rect2(Vector2(-hw, -hh + cr), Vector2(PADDLE_WIDTH, PADDLE_HEIGHT - cr * 2.0f)), color);
|
||||
// 上下两端矩形
|
||||
draw_rect(Rect2(Vector2(-hw + cr, -hh), Vector2(PADDLE_WIDTH - cr * 2.0f, cr)), color);
|
||||
draw_rect(Rect2(Vector2(-hw + cr, hh - cr), Vector2(PADDLE_WIDTH - cr * 2.0f, cr)), color);
|
||||
// 四角圆形
|
||||
draw_circle(Vector2(-hw + cr, -hh + cr), cr, color);
|
||||
draw_circle(Vector2(hw - cr, -hh + cr), cr, color);
|
||||
draw_circle(Vector2(-hw + cr, hh - cr), cr, color);
|
||||
draw_circle(Vector2(hw - cr, hh - cr), cr, color);
|
||||
}
|
||||
|
||||
void PingPongPaddle::_physics_process(double delta) {
|
||||
@@ -42,7 +67,7 @@ void PingPongPaddle::_physics_process(double delta) {
|
||||
float move_amount = 0.0f;
|
||||
|
||||
if (is_player) {
|
||||
// 玩家控制:读取上下输入
|
||||
// ── 玩家控制 ──
|
||||
Input *input = Input::get_singleton();
|
||||
if (input->is_action_pressed("ui_up")) {
|
||||
move_amount = -1.0f;
|
||||
@@ -50,17 +75,29 @@ void PingPongPaddle::_physics_process(double delta) {
|
||||
move_amount = 1.0f;
|
||||
}
|
||||
} else {
|
||||
// AI 控制:追踪球的位置
|
||||
// ── AI 控制(有延迟和不精确性)──
|
||||
if (!ball_path.is_empty()) {
|
||||
Node *node = get_node_or_null(ball_path);
|
||||
if (node) {
|
||||
Node2D *ball = Object::cast_to<Node2D>(node);
|
||||
if (ball) {
|
||||
float ball_y = ball->get_global_position().y;
|
||||
|
||||
// 反应延迟计数器
|
||||
ai_reaction_counter -= (float)delta;
|
||||
if (ai_reaction_counter <= 0.0f) {
|
||||
// 更新目标位置(加入随机偏移)
|
||||
float noise = (UtilityFunctions::randf() - 0.5f) * 2.0f * AI_TRACKING_NOISE;
|
||||
ai_target_y = ball_y + noise;
|
||||
// 随机重置延迟
|
||||
ai_reaction_counter = AI_REACTION_DELAY_MIN +
|
||||
UtilityFunctions::randf() * (AI_REACTION_DELAY_MAX - AI_REACTION_DELAY_MIN);
|
||||
}
|
||||
|
||||
float paddle_y = get_global_position().y;
|
||||
if (ball_y < paddle_y - 20.0f) {
|
||||
if (ai_target_y < paddle_y - 15.0f) {
|
||||
move_amount = -1.0f;
|
||||
} else if (ball_y > paddle_y + 20.0f) {
|
||||
} else if (ai_target_y > paddle_y + 15.0f) {
|
||||
move_amount = 1.0f;
|
||||
}
|
||||
}
|
||||
@@ -68,11 +105,12 @@ void PingPongPaddle::_physics_process(double delta) {
|
||||
}
|
||||
}
|
||||
|
||||
// 移动
|
||||
Vector2 velocity = Vector2(0.0f, move_amount * speed * (float)delta);
|
||||
// 移动(AI 速度略慢)
|
||||
float effective_speed = is_player ? speed : speed * AI_SPEED_FACTOR;
|
||||
Vector2 velocity = Vector2(0.0f, move_amount * effective_speed * (float)delta);
|
||||
move_and_collide(velocity);
|
||||
|
||||
// 限制移动范围(不超出屏幕)
|
||||
// 限制移动范围
|
||||
Viewport *viewport = get_viewport();
|
||||
if (viewport) {
|
||||
Vector2 screen_size = viewport->get_visible_rect().size;
|
||||
|
||||
在新工单中引用
屏蔽一个用户