244 lines
6.4 KiB
TypeScript
244 lines
6.4 KiB
TypeScript
import {
|
|
_decorator,
|
|
Color,
|
|
Component,
|
|
director,
|
|
Label,
|
|
Node,
|
|
PageView,
|
|
Sprite,
|
|
} from "cc";
|
|
import { GlobalData } from "../config/GlobalData";
|
|
import Common from "../libs/Common";
|
|
import AudioEffect from "../libs/AudioEffect";
|
|
import { EventDispatcher } from "../libs/EventDispatcher";
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass("LoveStorySceneControl")
|
|
export class LoveStorySceneControl extends Component {
|
|
// 总金币的标签
|
|
@property(Label)
|
|
totalScoreLabel: Label = null;
|
|
|
|
// 故事简介的标签
|
|
@property(Label)
|
|
storyDescLabel: Label = null;
|
|
|
|
// pageview 组件
|
|
@property(PageView)
|
|
pageviewComp: PageView = null;
|
|
|
|
// 按钮组节点
|
|
@property(Node)
|
|
buttonGroupNode: Node = null;
|
|
|
|
// 临时保存当前小恐龙的 id
|
|
private selectDinoID: number = 0;
|
|
|
|
//恐龙列表
|
|
dinoList: any[] = [
|
|
{
|
|
id: 1,
|
|
name: "小恐龙1",
|
|
desc: "爱你",
|
|
isLocked: 0,
|
|
unlockGold: 0,
|
|
dinoColor: "#ffffff",
|
|
bgColor: "#cce5cd",
|
|
},
|
|
{
|
|
id: 2,
|
|
name: "小恐龙2",
|
|
desc: "我的宝",
|
|
isLocked: 1,
|
|
unlockGold: 1000,
|
|
dinoColor: "#FFE064",
|
|
bgColor: "#f3ffc8",
|
|
},
|
|
{
|
|
id: 3,
|
|
name: "小恐龙3",
|
|
desc: "哎萌萌",
|
|
isLocked: 1,
|
|
unlockGold: 2000,
|
|
dinoColor: "#FD6445",
|
|
bgColor: "#ffaf6d",
|
|
},
|
|
{
|
|
id: 4,
|
|
name: "小恐龙4",
|
|
desc: "嘻嘻",
|
|
isLocked: 1,
|
|
unlockGold: 3000,
|
|
dinoColor: "#9164FF",
|
|
bgColor: "#a4a2ff",
|
|
},
|
|
{
|
|
id: 5,
|
|
name: "小恐龙5",
|
|
desc: "cccc",
|
|
isLocked: 1,
|
|
unlockGold: 4000,
|
|
dinoColor: "#FD64F2",
|
|
bgColor: "#f19dff",
|
|
},
|
|
];
|
|
|
|
onLoad() {
|
|
// 设置页面背景色
|
|
this.setBackgroundColor();
|
|
// 初始化小恐龙列表的解锁状态
|
|
this.initDinoListLockStatus();
|
|
}
|
|
|
|
start() {
|
|
// 设置总的金币数标签
|
|
this.setTotalScoreLabel();
|
|
// 初始刷pageView
|
|
this.initPageView();
|
|
}
|
|
|
|
private setTotalScoreLabel() {
|
|
this.totalScoreLabel.string = GlobalData.totalScore.toString();
|
|
}
|
|
// 初始化小恐龙列表的解锁状态
|
|
private initDinoListLockStatus() {
|
|
// 默认解锁小恐龙id为1的小恐龙
|
|
Common.unlockDino(1);
|
|
// 遍历小恐龙列表设置哪几个小恐龙解锁了
|
|
this.dinoList.forEach((item) => {
|
|
item.isLocked = Common.isDinoUnlocked(item.id) ? 0 : 1;
|
|
});
|
|
}
|
|
|
|
// 设置当前页面的背景色
|
|
private setBackgroundColor() {
|
|
// 获取当前小恐龙信息
|
|
const dionInfo = GlobalData.currentDinoInfo;
|
|
const bgColor = dionInfo.bgColor;
|
|
Common.setPageBackgroundColor(
|
|
this.node.getChildByName("bg"),
|
|
new Color(bgColor),
|
|
);
|
|
}
|
|
|
|
// 三个按钮方法
|
|
// 回到首页
|
|
onBackToHome() {
|
|
// 播放点击音效
|
|
AudioEffect.playClickAudio();
|
|
// 加载首页
|
|
director.loadScene("home");
|
|
}
|
|
|
|
// 购买小恐龙
|
|
onBuyDino() {
|
|
// 播放点击音效
|
|
AudioEffect.playClickAudio();
|
|
// 获取当前选择的小恐龙信息
|
|
const dionInfo = this.dinoList.find(
|
|
(item) => item.id === this.selectDinoID,
|
|
);
|
|
if (!dionInfo) {
|
|
return;
|
|
}
|
|
// 当前金币数如果大于所需要的金币数
|
|
if (GlobalData.totalScore >= dionInfo.unlockGold) {
|
|
// 扣除所需要的金币数
|
|
GlobalData.totalScore -= dionInfo.unlockGold;
|
|
// 更新总的金币数标签
|
|
this.setTotalScoreLabel();
|
|
// 解锁当前小恐龙
|
|
Common.unlockDino(dionInfo.id);
|
|
// 获取当前选中小恐龙索引
|
|
const currentIndex = this.selectDinoID - 1;
|
|
// 设置当前选中的小恐龙信息为解锁状态
|
|
this.dinoList[currentIndex].isLocked = 0;
|
|
// 刷新按钮显示
|
|
this.refreshButtonDisplay(currentIndex);
|
|
} else {
|
|
// 发送提示消息
|
|
EventDispatcher.getTarget().emit(EventDispatcher.TIP_MSG, "金币不足");
|
|
}
|
|
}
|
|
|
|
// 确认购买
|
|
onConfirmBuyDino() {
|
|
// 播放点击音效
|
|
AudioEffect.playClickAudio();
|
|
// 设置当前选择小恐龙的 id
|
|
if (this.selectDinoID > 0) {
|
|
GlobalData.selectedDinoID = this.selectDinoID;
|
|
// 设置小恐龙信息
|
|
GlobalData.currentDinoInfo = this.dinoList[this.selectDinoID - 1];
|
|
}
|
|
// 回到首页
|
|
this.scheduleOnce(() => {
|
|
director.loadScene("home");
|
|
}, 0.4);
|
|
}
|
|
|
|
// 轮播滑动事件
|
|
onSlideEvent(event: any) {
|
|
// 获取当前的索引
|
|
const currentIndex = parseInt(event.curPageIdx);
|
|
// 获取选中小恐龙信息
|
|
const dionInfo = this.dinoList[currentIndex];
|
|
// 更新当前临时选择的小恐龙 id
|
|
this.selectDinoID = dionInfo.id || 1;
|
|
// 设置故事简介
|
|
this.storyDescLabel.string = dionInfo.desc;
|
|
|
|
// 设置页面的背景颜色
|
|
Common.setPageBackgroundColor(
|
|
this.node.getChildByName("bg"),
|
|
new Color(dionInfo.bgColor),
|
|
);
|
|
|
|
// 刷新按钮显示
|
|
this.refreshButtonDisplay(currentIndex);
|
|
}
|
|
|
|
// 刷新按钮显示
|
|
private refreshButtonDisplay(currentIndex: number) {
|
|
// 保存当前临时保存的小恐龙 id
|
|
this.selectDinoID = this.dinoList[currentIndex].id || 1;
|
|
// 获取当前选中的小恐龙信息
|
|
const dionInfo = this.dinoList[currentIndex];
|
|
// 如果当前小恐龙未解锁
|
|
if (dionInfo.isLocked) {
|
|
this.buttonGroupNode.getChildByName("button_buy").active = true;
|
|
this.buttonGroupNode.getChildByName("button_cancel").active = true;
|
|
this.buttonGroupNode.getChildByName("button_ok").active = false;
|
|
// 设置购买的金额
|
|
this.buttonGroupNode
|
|
.getChildByName("button_buy")
|
|
.getChildByName("gold_num")
|
|
.getComponent(Label).string = dionInfo.unlockGold.toString() ?? "0";
|
|
} else {
|
|
this.buttonGroupNode.getChildByName("button_buy").active = false;
|
|
this.buttonGroupNode.getChildByName("button_cancel").active = false;
|
|
this.buttonGroupNode.getChildByName("button_ok").active = true;
|
|
}
|
|
}
|
|
|
|
// 初始化 pageview 组件
|
|
private initPageView() {
|
|
// 设置默认显示第几个小恐龙 index
|
|
const index = this.dinoList.findIndex(
|
|
(item) => item.id === GlobalData.selectedDinoID,
|
|
);
|
|
if (index !== -1) {
|
|
this.pageviewComp.setCurrentPageIndex(index);
|
|
// 获取小恐龙信息
|
|
const dionInfo = this.dinoList[index];
|
|
// 设置故事简介
|
|
this.storyDescLabel.string = dionInfo.desc;
|
|
// 刷新按钮显示
|
|
this.refreshButtonDisplay(index);
|
|
}
|
|
}
|
|
|
|
update(deltaTime: number) {}
|
|
}
|