2026-03-02 04:50:28 +08:00
|
|
|
|
#include "game_scene.h"
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
// PlayerNode 实现
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
|
|
|
|
PlayerNode::PlayerNode() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
setName("Player");
|
|
|
|
|
|
setTag(1);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 设置玩家尺寸
|
|
|
|
|
|
setSize(64.0f, 64.0f);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 设置锚点为中心点
|
|
|
|
|
|
setAnchor(0.5f, 0.5f);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 添加精灵渲染组件
|
|
|
|
|
|
auto sprite = makePtr<SpriteRenderer>();
|
|
|
|
|
|
sprite->setColor(Color::Blue);
|
|
|
|
|
|
addComponent(sprite);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerNode::onUpdate(float dt) {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
time_ += dt;
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 简单的圆周运动
|
|
|
|
|
|
float radius = 200.0f;
|
|
|
|
|
|
float x = 640.0f + radius * std::cos(time_ * 0.5f);
|
|
|
|
|
|
float y = 360.0f + radius * std::sin(time_ * 0.5f);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
setPosition(x, y);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 根据移动方向旋转(顺时针,使用负角度)
|
|
|
|
|
|
float angle = -time_ * 0.5f * 57.2958f + 90.0f;
|
|
|
|
|
|
setRotation(angle);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
// RotatingDecoration 实现
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
|
|
|
|
RotatingDecoration::RotatingDecoration() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
setName("Decoration");
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 设置尺寸
|
|
|
|
|
|
setSize(32.0f, 32.0f);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 设置锚点为中心
|
|
|
|
|
|
setAnchor(0.5f, 0.5f);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 添加精灵渲染组件
|
|
|
|
|
|
auto sprite = makePtr<SpriteRenderer>();
|
|
|
|
|
|
sprite->setColor(Color::Yellow);
|
|
|
|
|
|
addComponent(sprite);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RotatingDecoration::onUpdate(float dt) {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 自转(顺时针,使用负角度)
|
|
|
|
|
|
setRotation(getRotation() - rotationSpeed_ * dt);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
// GameScene 实现
|
|
|
|
|
|
// ========================================
|
|
|
|
|
|
|
|
|
|
|
|
GameScene::GameScene() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 场景初始化
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameScene::onEnter() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
Scene::onEnter();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 创建相机
|
|
|
|
|
|
createCamera();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 创建玩家
|
|
|
|
|
|
createPlayer();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 创建装饰物
|
|
|
|
|
|
createDecorations();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameScene::onExit() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 清理资源
|
|
|
|
|
|
player_.reset();
|
|
|
|
|
|
decorations_.clear();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
Scene::onExit();
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-03 12:18:32 +08:00
|
|
|
|
|
2026-03-02 04:50:28 +08:00
|
|
|
|
void GameScene::update(float dt) {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
Scene::update(dt);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
sceneTime_ += dt;
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 更新玩家
|
|
|
|
|
|
if (player_) {
|
|
|
|
|
|
player_->onUpdate(dt);
|
|
|
|
|
|
}
|
2026-03-02 04:50:28 +08:00
|
|
|
|
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 更新装饰物
|
|
|
|
|
|
for (auto &decoration : decorations_) {
|
|
|
|
|
|
decoration->onUpdate(dt);
|
|
|
|
|
|
}
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameScene::createPlayer() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
player_ = makePtr<PlayerNode>();
|
|
|
|
|
|
player_->setPosition(640.0f, 360.0f);
|
|
|
|
|
|
addChild(player_);
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameScene::createDecorations() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 创建围绕玩家旋转的装饰物
|
|
|
|
|
|
int count = 8;
|
|
|
|
|
|
float radius = 150.0f;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
|
|
auto decoration = makePtr<RotatingDecoration>();
|
|
|
|
|
|
|
|
|
|
|
|
// 计算位置(圆形分布)
|
|
|
|
|
|
float angle = (2.0f * 3.14159f * i) / count;
|
|
|
|
|
|
float x = 640.0f + radius * std::cos(angle);
|
|
|
|
|
|
float y = 360.0f + radius * std::sin(angle);
|
|
|
|
|
|
|
|
|
|
|
|
decoration->setPosition(x, y);
|
|
|
|
|
|
decoration->setRotationSpeed(45.0f + i * 10.0f);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置不同颜色
|
|
|
|
|
|
auto sprite = decoration->getComponent<SpriteRenderer>();
|
|
|
|
|
|
if (sprite) {
|
|
|
|
|
|
float hue = static_cast<float>(i) / count;
|
|
|
|
|
|
// 简单的HSV到RGB转换
|
|
|
|
|
|
float r = std::abs(hue * 6.0f - 3.0f) - 1.0f;
|
|
|
|
|
|
float g = 2.0f - std::abs(hue * 6.0f - 2.0f);
|
|
|
|
|
|
float b = 2.0f - std::abs(hue * 6.0f - 4.0f);
|
|
|
|
|
|
sprite->setColor(Color(std::max(0.0f, std::min(1.0f, r)),
|
|
|
|
|
|
std::max(0.0f, std::min(1.0f, g)),
|
|
|
|
|
|
std::max(0.0f, std::min(1.0f, b)), 1.0f));
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
2026-03-03 03:48:55 +08:00
|
|
|
|
|
|
|
|
|
|
decorations_.push_back(decoration);
|
|
|
|
|
|
addChild(decoration);
|
|
|
|
|
|
}
|
2026-03-02 04:50:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameScene::createCamera() {
|
2026-03-03 03:48:55 +08:00
|
|
|
|
// 创建相机节点
|
|
|
|
|
|
auto cameraNode = makePtr<Node>();
|
|
|
|
|
|
cameraNode->setName("MainCamera");
|
|
|
|
|
|
// 相机位置在(0, 0),这样世界坐标直接映射到屏幕
|
|
|
|
|
|
cameraNode->setPosition(0.0f, 0.0f);
|
|
|
|
|
|
|
|
|
|
|
|
// 添加相机组件
|
|
|
|
|
|
auto camera = makePtr<CameraComponent>();
|
|
|
|
|
|
// 使用标准的2D投影:左上角为(0, 0),右下角为(1280, 720)
|
|
|
|
|
|
// Y轴向下:bottom=720, top=0
|
|
|
|
|
|
camera->setOrtho(0.0f, 1280.0f, 720.0f, 0.0f, -1.0f, 1.0f);
|
|
|
|
|
|
cameraNode->addComponent(camera);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置为主相机
|
|
|
|
|
|
setMainCamera(camera);
|
|
|
|
|
|
|
|
|
|
|
|
// 添加相机节点到场景
|
|
|
|
|
|
addChild(cameraNode);
|
|
|
|
|
|
}
|