diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.scene b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.scene index bb6b39e..e374f94 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.scene +++ b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.scene @@ -33,11 +33,11 @@ "_active": true, "_components": [], "_prefab": { - "__id__": 57 + "__id__": 58 }, "autoReleaseAssets": false, "_globals": { - "__id__": 60 + "__id__": 61 }, "_id": "0d3889f6-dc9c-424e-b8cd-6fa78d63af15" }, @@ -483,9 +483,6 @@ ], "_active": true, "_components": [ - { - "__id__": 53 - }, { "__id__": 54 }, @@ -494,6 +491,9 @@ }, { "__id__": 56 + }, + { + "__id__": 57 } ], "_prefab": null, @@ -811,7 +811,7 @@ }, { "__type__": "cc.Node", - "_name": "Button", + "_name": "btnAttack", "_objFlags": 0, "_parent": { "__id__": 25 @@ -831,6 +831,9 @@ }, { "__id__": 51 + }, + { + "__id__": 53 } ], "_prefab": null, @@ -949,7 +952,7 @@ "b": 0, "a": 255 }, - "_string": "射", + "_string": "攻击", "_horizontalAlign": 1, "_verticalAlign": 1, "_actualFontSize": 40, @@ -978,8 +981,8 @@ "__prefab": null, "_contentSize": { "__type__": "cc.Size", - "width": 100, - "height": 100 + "width": 120, + "height": 120 }, "_anchorPoint": { "__type__": "cc.Vec2", @@ -1104,6 +1107,18 @@ "handler": "onBtnAttack", "customEventData": "" }, + { + "__type__": "cc.UIOpacity", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 45 + }, + "_enabled": true, + "__prefab": null, + "_opacity": 255, + "_id": "49xySc2HlNZ7kO4VRCiutC" + }, { "__type__": "cc.UITransform", "_name": "", @@ -1196,6 +1211,9 @@ "camera": { "__id__": 4 }, + "btnAttack": { + "__id__": 45 + }, "_id": "86PJf2PFRK5LGUONuKqmzS" }, { @@ -1203,14 +1221,14 @@ "fileId": "", "targetOverrides": [ { - "__id__": 58 + "__id__": 59 } ] }, { "__type__": "cc.TargetOverrideInfo", "source": { - "__id__": 56 + "__id__": 57 }, "sourceInfo": null, "propertyPath": [ @@ -1220,7 +1238,7 @@ "__id__": 28 }, "targetInfo": { - "__id__": 59 + "__id__": 60 } }, { @@ -1232,16 +1250,16 @@ { "__type__": "cc.SceneGlobals", "ambient": { - "__id__": 61 - }, - "shadows": { "__id__": 62 }, - "_skybox": { + "shadows": { "__id__": 63 }, - "fog": { + "_skybox": { "__id__": 64 + }, + "fog": { + "__id__": 65 } }, { diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts index ac80240..e0d90ea 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts @@ -1,5 +1,5 @@ -import { Component, instantiate, Node, Prefab, Vec2, _decorator } from 'cc'; +import { Button, Component, instantiate, Node, Prefab, UIOpacity, Vec2, _decorator } from 'cc'; import { Arrow } from '../../prefabs/Arrow/Arrow'; import { Joystick } from '../../prefabs/Joystick/Joystick'; import { Player } from '../../prefabs/Player/Player'; @@ -9,18 +9,6 @@ import { gameConfig } from '../../scripts/shared/game/gameConfig'; import { ArrowState } from '../../scripts/shared/game/state/ArrowState'; const { ccclass, property } = _decorator; -/** - * Predefined variables - * Name = GameScene - * DateTime = Thu Dec 02 2021 18:43:36 GMT+0800 (中国标准时间) - * Author = k8w - * FileBasename = GameScene.ts - * FileBasenameNoExtension = GameScene - * URL = db://assets/scenes/GameScene/GameScene.ts - * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/ - * - */ - @ccclass('GameScene') export class GameScene extends Component { @@ -40,6 +28,9 @@ export class GameScene extends Component { @property(FollowCamera) camera: FollowCamera = null as any; + @property(Node) + btnAttack: Node = null as any; + gameManager!: GameManager; private _playerInstances: { [playerId: number]: Player | undefined } = {}; @@ -49,6 +40,7 @@ export class GameScene extends Component { onLoad() { (window as any).game = this; + // 初始化摇杆 this.joyStick.options = { onOperate: v => { if (!this._selfSpeed) { @@ -64,9 +56,10 @@ export class GameScene extends Component { this.gameManager = new GameManager(); // 监听数据状态事件 + // 新箭矢发射(仅表现) this.gameManager.gameSystem.onNewArrow.push(v => { this._onNewArrow(v) }); - // 断线一秒后重连 + // 断线 2 秒后自动重连 this.gameManager.client.flows.postDisconnectFlow.push(v => { setTimeout(() => { this.gameManager.join(); @@ -172,5 +165,13 @@ export class GameScene extends Component { targetPos: { x: targetPos.x, y: targetPos.y }, targetTime: this.gameManager.state.now + gameConfig.arrowFlyTime } as any) + + // 冷却时间 1 秒 + this.btnAttack.getComponent(Button)!.interactable = false; + this.btnAttack.getComponent(UIOpacity)!.opacity = 120; + this.scheduleOnce(() => { + this.btnAttack.getComponent(Button)!.interactable = true; + this.btnAttack.getComponent(UIOpacity)!.opacity = 255; + }, 1) } } \ No newline at end of file diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scripts/components/FollowCamera.ts b/examples/cocos-creator-multiplayer/frontend/assets/scripts/components/FollowCamera.ts index eb40da3..0ff84f2 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/components/FollowCamera.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/components/FollowCamera.ts @@ -5,7 +5,7 @@ const { ccclass, property } = _decorator; const v3_1 = new Vec3; /** - * 跟随摄像机 + * 自动跟随目标,并平滑移动的摄像机 */ @ccclass export class FollowCamera extends Component { diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts index 5d0c8dc..256f65a 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts @@ -94,7 +94,8 @@ export class GameManager { pendingInputMsgs: MsgClientInput[] = []; sendClientInput(input: ClientInput) { - if (!this.selfPlayerId) { + // 已掉线或暂未加入,忽略本地输入 + if (!this.selfPlayerId || !this.client.isConnected) { return; }