Files
dino/assets/seripts/scenes/LoadingSceneControl.ts
2026-05-12 06:46:31 +08:00

177 lines
4.3 KiB
TypeScript

import {
_decorator,
AudioClip,
Color,
Component,
director,
Label,
Node,
resources,
Sprite,
tween,
Vec3,
} from "cc";
import Common from "../libs/Common";
import { GlobalData } from "../config/GlobalData";
import AudioEffect from "../libs/AudioEffect";
const { ccclass, property } = _decorator;
@ccclass("LoadingSceneControl")
export class LoadingSceneControl extends Component {
// 小恐龙节点
@property(Node)
dinoNode: Node = null;
// 加载文案 Label
@property(Label)
loadingLabel: Label = null;
// 开始按钮节点
@property(Node)
startButtonNode: Node = null;
// 资源目录(图片资源和预制体资源)
private resourceDir = ["images", "prefabs"];
// 定义总的需要加载资源的数量
private totalResourceCount: number = 5;
// 标记加载进度
private loadingProgress: number = 0;
// 标记是否完成加载
private isFinished: boolean = false;
start() {
// 设置当前页面的背景颜色
Common.setPageBackgroundColorAsDino(this.node);
// 设置小恐龙颜色
this.setDinoColor();
// 播放小恐龙动画
this.playDinoAnimation();
// 播放加载文案动画
this.playLoadingAnimation();
// 预加载资源
this.preloadResources();
}
setDinoColor() {
this.dinoNode.getComponent(Sprite).color = new Color(
GlobalData.currentDinoInfo.dinoColor,
);
}
playDinoAnimation() {
// 获取小恐龙当前的位置
const position = this.dinoNode.getPosition();
// 执行小恐龙动画
//执行缓动动画
tween(this.dinoNode)
.repeatForever(
tween()
.set({ scale: new Vec3(-1, 1, 1) })
.to(1, {
position: new Vec3(150, position.y, position.z),
})
.set({ scale: new Vec3(1, 1, 1) })
.to(1, {
position: new Vec3(-150, position.y, position.z),
}),
) //无限循环
.start();
}
playLoadingAnimation() {
//每隔1秒修改一次loading...文案,主要是修改后面的三个点
tween(this.loadingLabel)
.repeatForever(
tween()
.set({ string: "加载中." })
.delay(0.5)
.set({ string: "加载中.." })
.delay(0.5)
.set({ string: "加载中..." })
.delay(0.5),
) //无限循环
.start();
}
preloadResources() {
// 1. 预加载首页场景
director.preloadScene("home", (err) => {
// 修改进度
this.loadingProgress += 1;
});
// 2. 预加载游戏场景
director.preloadScene("game", (err) => {
// 修改进度
this.loadingProgress += 1;
});
// 3. 预加载图片和预制体资源
this.preloadImageResources();
// 4. 预加载音频资源
this.preloadAudioResources();
}
// 预加载图片和预制体资源
preloadImageResources() {
// 预加载图片资源
this.resourceDir.forEach((dir) => {
resources.loadDir(dir, (err, assets) => {
// 修改进度
this.loadingProgress += 1;
});
});
}
// 预加载音频资源
preloadAudioResources() {
// 只加载当前选择的音乐
// 获取当前选择的音乐 id
const musicId = GlobalData.selectedMusicID;
// 获取音乐文件的路径
const musicPath = `audios/music/music_${musicId}`;
resources.load(musicPath, AudioClip, (err, asset) => {
// 修改进度
this.loadingProgress += 1;
});
}
// 跳转到首页
onStartGame() {
// 播放点击音效
AudioEffect.playClickAudio();
// 跳转到首页场景
this.scheduleOnce(() => {
director.loadScene("home");
}, 0.2);
}
// 开放开始按钮
openStartButton() {
// 隐藏加载文案节点
this.loadingLabel.node.active = false;
// 开始按钮显示
this.startButtonNode.active = true;
// 设置按钮动画
tween(this.startButtonNode)
.repeatForever(
tween()
.to(0.5, { scale: new Vec3(1.1, 1.1, 1.1) })
.to(0.5, { scale: new Vec3(1, 1, 1) }),
) //无限循环
.start();
}
update(deltaTime: number) {
// 如果已经完成加载
if (this.isFinished) return;
// 如果加载进度等于预定的数量 就是完成了
if (this.loadingProgress >= this.totalResourceCount) {
this.isFinished = true;
// 开放开始按钮
this.openStartButton();
}
}
}