feat: 新增资源分包与启动场景,支持音效播放

This commit is contained in:
richarjiang
2025-09-28 08:54:00 +08:00
parent f27a27d2ce
commit e09c9a84cb
20 changed files with 1110 additions and 232 deletions

View File

@@ -1,4 +1,4 @@
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation, Collider2D, Contact2DType, Label, Color, Canvas, UITransform } from 'cc';
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation, Collider2D, Contact2DType, Label, Color, Canvas, UITransform, AudioSource } from 'cc';
import { TiledMapPathfinder } from './TiledMapPathfinder';
const { ccclass, property } = _decorator;
@@ -19,6 +19,9 @@ export class PlayerController extends Component {
@property(TiledMapPathfinder)
pathfinder: TiledMapPathfinder | null = null; // 寻路组件
@property(Node)
attackAudio: Node | null = null; // 攻击音效节点
@property({ range: [1, 300] })
moveSpeed: number = 300; // 移动速度(像素/秒)
@@ -400,6 +403,15 @@ export class PlayerController extends Component {
const monsterHp = parseInt(monsterLabel.string) || 0;
console.log('玩家生命值:', playerHp, '怪物生命值:', monsterHp);
// 播放攻击音效
if (this.attackAudio) {
const audioSource = this.attackAudio.getComponent(AudioSource);
if (audioSource) {
audioSource.play();
console.log('播放攻击音效');
}
}
// 播放攻击动画
const monsterAnimation = otherCollider.node.getComponent(Animation);
if (monsterAnimation) {
@@ -469,6 +481,15 @@ export class PlayerController extends Component {
}
this.isAttacking = false;
// 停止攻击音效
if (this.attackAudio) {
const audioSource = this.attackAudio.getComponent(AudioSource);
if (audioSource) {
audioSource.stop();
console.log('停止攻击音效');
}
}
}, 2);
}

111
assets/scripts/Start.ts Normal file
View File

@@ -0,0 +1,111 @@
import { _decorator, Component, Node, assetManager, AssetManager, director, Scene } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Start')
export class Start extends Component {
// 需要加载的 bundle 列表 - 可以根据实际情况修改
@property({ type: [String], tooltip: '需要加载的 bundle 名称列表' })
private bundlesToLoad: string[] = ['bundle1'];
// 目标场景名称
@property({ type: String, tooltip: '加载完成后跳转的场景名称' })
private targetScene: string = 'main';
// 已加载的 bundle 数量
private loadedBundleCount: number = 0;
// 存储已加载的 bundle
private loadedBundles: Map<string, AssetManager.Bundle> = new Map();
protected onLoad(): void {
// 加载所有 bundle
this.loadAllBundles();
}
start() {
}
update(deltaTime: number) {
}
/**
* 加载所有 bundle
*/
private loadAllBundles(): void {
if (this.bundlesToLoad.length === 0) {
console.log('没有需要加载的 bundle直接跳转到目标场景');
this.loadTargetScene();
return;
}
console.log(`开始加载 ${this.bundlesToLoad.length} 个 bundle:`, this.bundlesToLoad);
for (const bundleName of this.bundlesToLoad) {
this.loadBundle(bundleName);
}
}
/**
* 加载单个 bundle
*/
private loadBundle(bundleName: string): void {
assetManager.loadBundle(bundleName, (err: Error | null, bundle: AssetManager.Bundle | null) => {
if (err) {
console.error(`加载 ${bundleName} 分包失败:`, err);
// 即使加载失败也继续,确保不会因为一个 bundle 失败而卡住
this.onBundleLoadComplete();
return;
}
if (bundle) {
console.log(`${bundleName} 分包加载成功`);
this.loadedBundles.set(bundleName, bundle);
this.onBundleLoadComplete();
}
});
}
/**
* bundle 加载完成后的处理
*/
private onBundleLoadComplete(): void {
this.loadedBundleCount++;
console.log(`已加载 ${this.loadedBundleCount}/${this.bundlesToLoad.length} 个 bundle`);
// 检查是否所有 bundle 都已加载完成
if (this.loadedBundleCount >= this.bundlesToLoad.length) {
this.onAllBundlesLoaded();
}
}
/**
* 所有 bundle 加载完成后的回调
*/
private onAllBundlesLoaded(): void {
console.log('所有 bundle 加载完成!');
console.log('成功加载的 bundles:', Array.from(this.loadedBundles.keys()));
// 所有 bundle 加载完成后,跳转到目标场景
this.loadTargetScene();
}
/**
* 加载目标场景
*/
private loadTargetScene(): void {
console.log(`开始加载 ${this.targetScene} 场景...`);
director.loadScene(this.targetScene, (err: Error | null, scene: Scene | null) => {
if (err) {
console.error(`加载 ${this.targetScene} 场景失败:`, err);
return;
}
if (scene) {
console.log(`${this.targetScene} 场景加载成功!`);
}
});
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "e4e646ca-d82f-4ac4-8b1d-083e73f1b05b",
"files": [],
"subMetas": {},
"userData": {}
}