import { _decorator, Component, Node, director, resources, instantiate, Vec3, math } from 'cc';
import { GameData } from './GameData';
import { A_guagouyidong } from './A_guagouyidong';
import { B_pengzhuangDefenxitong } from './B_pengzhuangDefenxitong';
import { ditu } from './C_ditu';
import { A_HUAHUAweizhi } from './A_HUAHUAweizhi';
import { zizhuandaishangwawa2 } from './zizhuandaishangwawa2';
import { fuhuoUI } from './fuhuoUI';
import { GameOverUI } from './GameOverUI';
const { ccclass, property } = _decorator;
@ccclass('AA_GameManager')
export class GameManager extends Component {
@property(Node)
clawNode: Node = null; // 爪子节点
@property(Node)
collisionSystemNode: Node = null; // 碰撞系统节点
@property(Node)
mapNode: Node = null; // 地图节点
@property(Node)
flowerSpawnerNode: Node = null; // 花朵生成器节点
@property(Node)
rotationComponentNode: Node = null; // 旋转组件节点
@property(Node)
cameraFollowNode: Node = null; // 相机跟随节点
@property(Node)
reviveUINode: Node = null; // 复活UI节点
@property(Node)
gameOverUINode: Node = null; // 游戏结束UI节点
public isGameActive: boolean = false;
private currentLevel: number = 1;
onLoad() {
this.initializeGame();
}
/**
* 初始化游戏
*/
private initializeGame() {
// 重置游戏数据
GameData.reset();
// 获取或初始化组件
if (!this.clawNode) this.clawNode = this.node.getChildByName('Claw');
if (!this.collisionSystemNode) this.collisionSystemNode = this.node.getChildByName('CollisionSystem');
if (!this.mapNode) this.mapNode = this.node.getChildByName('Map');
if (!this.flowerSpawnerNode) this.flowerSpawnerNode = this.node.getChildByName('FlowerSpawner');
if (!this.rotationComponentNode) this.rotationComponentNode = this.node.getChildByName('RotationComponent');
if (!this.cameraFollowNode) this.cameraFollowNode = this.node.getChildByName('CameraFollow');
if (!this.reviveUINode) this.reviveUINode = this.node.getChildByName('ReviveUI');
if (!this.gameOverUINode) this.gameOverUINode = this.node.getChildByName('GameOverUI');
// 初始化游戏状态
this.isGameActive = true;
}
/**
* 开始游戏
*/
public startGame() {
if (!this.isGameActive) {
// 重置所有游戏组件
this.resetGameComponents();
// 初始化UI
this.resetUI();
// 设置游戏状态
this.isGameActive = true;
this.currentLevel = 1;
// 开始游戏循环
this.scheduleOnce(() => {
this.enableGameComponents(true);
}, 0.1);
}
}
/**
* 重置UI状态
*/
private resetUI() {
if (this.reviveUINode?.isValid) {
const reviveUI = this.reviveUINode.getComponent(fuhuoUI);
if (reviveUI) reviveUI.hide();
}
if (this.gameOverUINode?.isValid) {
const gameOverUI = this.gameOverUINode.getComponent(GameOverUI);
if (gameOverUI) gameOverUI.hide();
}
}
/**
* 重置游戏
*/
public resetGame() {
// 1. 停止所有游戏活动
this.isGameActive = false;
this.enableGameComponents(false);
// 2. 重置游戏数据
GameData.reset();
// 3. 重置所有UI状态
this.resetUI();
// 4. 重置游戏组件
this.resetGameComponents();
// 5. 延迟开始游戏确保完全重置
this.scheduleOnce(() => {
this.startGame();
}, 0.3); // 增加延迟时间确保完全重置
}
/**
* 重置游戏组件
*/
private resetGameComponents() {
// 重置爪子
if (this.clawNode?.isValid) {
const clawMovement = this.clawNode.getComponent(A_guagouyidong);
if (clawMovement) {
if (clawMovement.resetPosition) {
clawMovement.resetPosition();
} else {
clawMovement.stopMovement();
clawMovement.isFirstClick = true;
}
}
}
// 确保设置碰撞系统的回调
if (this.collisionSystemNode?.isValid) {
const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong);
if (collisionSystem) {
collisionSystem.setGameOverCallback(this.gameOver.bind(this));
// 确保UI引用正确
collisionSystem.GameOverUI = this.gameOverUINode?.getComponent(GameOverUI);
}
}
// 重置碰撞系统
if (this.collisionSystemNode?.isValid) {
const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong);
if (collisionSystem) {
collisionSystem.stopAllMovements();
}
}
// 重置地图
if (this.mapNode?.isValid) {
const mapComponent = this.mapNode.getComponent(ditu);
if (mapComponent) {
mapComponent.generateMap();
}
}
// 重置花朵生成器
if (this.flowerSpawnerNode?.isValid) {
const flowerSpawner = this.flowerSpawnerNode.getComponent(A_HUAHUAweizhi);
if (flowerSpawner) {
flowerSpawner.showFlowers();
}
}
// 重置旋转组件
if (this.rotationComponentNode?.isValid) {
const rotationComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2);
if (rotationComponent) {
rotationComponent.enabled = true;
}
}
}
/**
* 启用或禁用游戏组件
* @param enable 是否启用
*/
private enableGameComponents(enable: boolean) {
// 启用/禁用爪子移动
if (this.clawNode?.isValid) {
const clawMovement = this.clawNode.getComponent(A_guagouyidong);
if (clawMovement) {
if (enable) {
clawMovement.resumeMovement();
} else {
clawMovement.stopMovement();
}
}
}
// 启用/禁用旋转组件
if (this.rotationComponentNode?.isValid) {
const rotationComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2);
if (rotationComponent) {
rotationComponent.enabled = enable;
}
}
// 启用/禁用碰撞系统
if (this.collisionSystemNode?.isValid) {
const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong);
if (collisionSystem) {
// 碰撞系统本身会根据游戏状态处理
}
}
}
/**
* 游戏结束
* @param isSuccess 是否成功
*/
public gameOver(isSuccess: boolean) {
// 确保只处理一次
if (!this.isGameActive) return;
this.isGameActive = false;
this.enableGameComponents(false);
// 延迟显示确保UI更新
this.scheduleOnce(() => {
if (this.gameOverUINode?.isValid) {
const gameOverUI = this.gameOverUINode.getComponent(GameOverUI);
if (gameOverUI) {
gameOverUI.show(GameData.getScore());
}
}
}, 0.1);
}
/**
* 返回主页
*/
public goToHome() {
this.unscheduleAllCallbacks();
director.loadScene('HomeScene');
}
/**
* 下一关
*/
public nextLevel() {
this.currentLevel++;
this.resetGame();
}
/**
* 暂停游戏
*/
public pauseGame() {
if (this.isGameActive) {
this.isGameActive = false;
this.enableGameComponents(false);
}
}
/**
* 恢复游戏
*/
public resumeGame() {
if (!this.isGameActive) {
this.isGameActive = true;
this.enableGameComponents(true);
}
}
}。第一个。import { _decorator, Collider2D, Component, Contact2DType, Label, Node, isValid, Camera, UITransform, Vec3, view} from 'cc';
import { GameOverUI } from './GameOverUI';
import { GameData } from './GameData';
import { Tags } from './Tags';
import { A_guagouyidong } from './A_guagouyidong';
import { fuhuoUI } from './fuhuoUI';
const { ccclass, property } = _decorator;
@ccclass('B_pengzhuangDefenxitong')
export class B_pengzhuangDefenxitong extends Component {
// 使用单个Map替代多个Set
private processedColliders: Map<string, Tags> = new Map();
@property(fuhuoUI)
fuhuoUI: fuhuoUI = null;
@property(GameOverUI)
GameOverUI: GameOverUI = null;
@property(Node)
zizhuandaishangwawa2: Node = null;
@property(Camera)
mainCamera: Camera = null;
@property(Label)
chanceLabel: Label = null;
// 游戏结束回调函数
private onGameOverCallback: (isSuccess: boolean) => void = null;
onLoad() {
// 碰撞检测
const collider = this.getComponent(Collider2D);
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
}
// 更新机会显示
this.updateChanceUI();
}
/**
* 设置游戏结束回调
* @param callback 回调函数
*/
public setGameOverCallback(callback: (isSuccess: boolean) => void) {
this.onGameOverCallback = callback;
}
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
const colliderId = otherCollider.uuid;
// 如果已处理过该碰撞体,直接返回
if (this.processedColliders.has(colliderId)) return;
switch (otherCollider.tag) {
case Tags.HUAHUA:
this.processedColliders.set(colliderId, Tags.HUAHUA);
this.addScore(1);
// 延迟销毁花朵
this.scheduleOnce(() => {
if (isValid(otherCollider.node)) {
otherCollider.node.destroy();
}
}, 0.001);
break;
case Tags.WAWA:
this.processedColliders.set(colliderId, Tags.WAWA);
this.addScore(10);
// 统一通过回调处理游戏结束
if (this.onGameOverCallback) {
this.onGameOverCallback(true);
} else {
// 如果没有设置回调,使用本地方法
this.gameOver(true);
}
break;
case Tags.DITU:
this.processedColliders.set(colliderId, Tags.DITU);
GameData.loseChance();
this.updateChanceUI();
// 检查是否需要显示复活界面
if (GameData.shouldShowRevive()) {
this.stopAllMovements();
// 延迟显示复活界面
this.scheduleOnce(() => {
if (this.fuhuoUI && isValid(this.fuhuoUI.node)) {
this.fuhuoUI.show();
}
}, 0.2);
}
break;
}
}
onEndContact(selfCollider: Collider2D, otherCollider: Collider2D) {
const colliderId = otherCollider.uuid;
// 只移除地图碰撞记录
if (otherCollider.tag === Tags.DITU && this.processedColliders.has(colliderId)) {
this.processedColliders.delete(colliderId);
}
}
updateChanceUI() {
if (this.chanceLabel && isValid(this.chanceLabel.node)) {
this.chanceLabel.string = GameData.getChances().toString();
}
}
public stopAllMovements() {
// 停止爪子移动
if (this.node?.isValid) {
const moveComp = this.node.getComponent(A_guagouyidong);
if (moveComp) {
moveComp.stopMovement();
}
}
// 停止旋转组件
if (this.zizhuandaishangwawa2?.isValid) {
const component = this.zizhuandaishangwawa2.getComponent("zizhuandaishangwawa2");
if (component) {
component.enabled = false;
}
}
}
public gameOver(isSuccess: boolean) {
// 调用停止移动的方法
if (this.node?.isValid) {
const moveComp = this.node.getComponent(A_guagouyidong);
if (moveComp) {
moveComp.stopMovement();
}
}
// 停止旋转组件
if (this.zizhuandaishangwawa2?.isValid) {
const component = this.zizhuandaishangwawa2.getComponent("zizhuandaishangwawa2");
if (component) {
component.enabled = false;
}
}
// 显示游戏结束界面
if (this.GameOverUI?.isValid) {
const cameraWorldPos = this.mainCamera.node.worldPosition;
this.GameOverUI.node.setWorldPosition(cameraWorldPos);
this.GameOverUI.show(GameData.getScore());
}
}
private addScore(count: number = 1) {
GameData.addScore(count);
}
}第二个。import { _decorator, Button, Component, misc, Node } from 'cc';
import { GameData } from './GameData';
import { GameManager } from './AA_GameManager';
import { zizhuandaishangwawa2 } from './zizhuandaishangwawa2';
const { ccclass, property } = _decorator;
@ccclass('chongwananniu')
export class chongwananniu extends Component {
@property(GameManager)
gameManager: GameManager = null;
@property(Node)
rotationComponentNode: Node = null; // 旋转组件节点(娃娃出现系统)
private isEventBound = false;
private currentWawaData: {x: number, y: number, angle: number}[] = []; // 保存娃娃位置和角度数据
onLoad() {
this.node.on(Button.EventType.CLICK, this.onReplayClick, this);
this.isEventBound = true;
}
private onReplayClick() {
// 简单直接调用游戏管理器重置
if (this.gameManager && this.gameManager.node?.isValid) {
this.gameManager.resetGame();
}
}
private saveCurrentWawas() {
if (this.rotationComponentNode) {
const wawaComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2);
if (wawaComponent) {
this.currentWawaData = [];
for (const child of wawaComponent.children) {
if (child && child.isValid) {
const pos = child.position;
this.currentWawaData.push({
x: pos.x,
y: pos.y,
angle: child.angle
});
}
}
}
}
}
private restoreWawas() {
if (this.rotationComponentNode && this.currentWawaData.length > 0) {
const wawaComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2);
if (wawaComponent) {
// 确保有足够的娃娃节点
if (wawaComponent.children.length < this.currentWawaData.length) {
console.warn("Not enough wawa nodes to restore");
return;
}
// 恢复位置和角度
for (let i = 0; i < this.currentWawaData.length; i++) {
if (i < wawaComponent.children.length &&
wawaComponent.children[i] &&
wawaComponent.children[i].isValid) {
const child = wawaComponent.children[i];
const data = this.currentWawaData[i];
child.setPosition(data.x, data.y);
child.angle = data.angle;
child.active = true;
}
}
// 确保组件启用
wawaComponent.enabled = true;
}
}
}
onDestroy() {
if (this.isEventBound) {
this.node.off(Button.EventType.CLICK, this.onReplayClick, this);
}
}
}。第三个。import { _decorator, Component, EventTouch, Input, input, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('A_guagouyidong')
export class A_guagouyidong extends Component {
private isGameActive: boolean = true;
public isFirstClick: boolean = true; // 是否第一次点击
private direction: Vec2 = new Vec2(0, -1); // 初始方向 -Y
private initialPosition: Vec3 = new Vec3();//初始位置
@property
private speed: number = 20; // 移动速度
private angle: number = -90; // 当前角度 (-90度代表-y方向)
private currentAngle: number = -90; // 初始角度为 -90 度
onLoad() {
// 保存初始位置
this.initialPosition = this.node.position.clone();
// 只在游戏活跃时监听触摸事件
if (this.isGameActive) {
// 监听触摸事件
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
}
}
/**
* 重置爪子位置到初始位置
*/
public resetPosition() {
// 确保节点有效
if (!this.node || !this.node.isValid) return;
// 重置位置
this.node.position = this.initialPosition.clone();
this.stopMovement();
this.isFirstClick = true;
this.direction = new Vec2(0, -1); // 重置方向为 -Y
this.angle = 0; // 重置角度
this.currentAngle = 0; // 重置当前角度
// 重置旋转
this.node.setRotationFromEuler(0, 0, this.currentAngle);
// 重新激活移动
this.isGameActive = true;
}
stopMovement() {
this.isGameActive = false; // 设置游戏状态为结束
this.isFirstClick = true; // 重置点击状态(防止继续移动)
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); // 移除触摸监听
}
// 在 A_guagouyidong-001.ts 中添加
resumeMovement() {
this.isGameActive = true;
this.isFirstClick = true; // 重置点击状态
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this); // 重新绑定
}
onTouchStart(event: EventTouch) {
if (!this.isGameActive) return; // 游戏结束时不再响应
if (this.isFirstClick) {
// 第一次点击,开始向 -Y 方向移动
this.isFirstClick = false;
this.moveInDirection(this.direction);
} else {
this.toggleDirection(); // 调用切换方向的方法
}
}
toggleDirection() {
this.angle = (this.angle === -135) ? -45 : -135;
this.direction = new Vec2(Math.cos(this.angle * (Math.PI / 180)), Math.sin(this.angle * (Math.PI / 180)));
this.moveInDirection(this.direction);
// 切换当前角度
this.currentAngle = (this.currentAngle === 315) ? 45 : 315;
this.node.setRotationFromEuler(0, 0, this.currentAngle);
}
moveInDirection(direction: Vec2) {
// 获取当前节点的位置
const currentPosition = this.node.position;
// 更新节点位置
const newPosition = new Vec3(
currentPosition.x + direction.x * this.speed * 0.02,
currentPosition.y + direction.y * this.speed * 0.02,
currentPosition.z);
this.node.setPosition(newPosition);
}
update(deltaTime: number) {
if (!this.isGameActive) return; // 游戏结束时不再移动
// 如果不是第一次点击,持续移动
if (!this.isFirstClick) {
this.moveInDirection(this.direction);
}
}
onDestroy() {
// 移除事件监听
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
}
}第四个。现在游戏重玩的部分还是有bug。首先点击重玩回到游戏界面,需要点击两次,主角开始才移动,不像没有重玩之前(刚开始游戏时),点击一次就能移动了。就是3次的机会数字没有更新,例如重玩前数字是2,重玩后还是显示2,不过碰撞后几次次数却正常的,例如重玩后显示数字是2,碰撞一次减一次机会后,数字就正常变为2了,然后就是正常2,1,0,复活后又开始3次机会,就是重玩后显示的机会数字有异常。最后就是重玩后,只要重玩到4次,5次左右,碰撞到wawa后就不会出现gameoverUI界面的,我希望可以一直可以点击重玩。请参考以上代码,帮我找出问题所在。就能优化和不需要做幅度改动。同时都帮我列出完整代码和注释还修改的部分。