Commit c6b67d95 authored by Tt's avatar Tt

基础内容完成

parent 2d7650f8
......@@ -4089,7 +4089,7 @@
"__id__": 117
}
],
"_active": true,
"_active": false,
"_components": [
{
"__id__": 119
......
This diff is collapsed.
export enum FISH_OUT {
RUNNING,
TOUCH,
CATCH,
ESCAP,
OUT
}
export class Fish {
public isOut: number;
public node: cc.Node;
public isLeft: boolean;//在左侧 向右运动
constructor() {
this.isOut = 0;
this.isLeft = Math.random() < 0.5;
}
}
/**
* 选项类,表示游戏中的一个问题选项
*/
export class Option {
/** 选项类型 */
public type;
/** 选项文本 */
public txt;
/** 图片URL */
public picUrl;
/** 音频URL */
public audioUrl;
/** 是否为正确选项 */
public right: boolean;
/** 选项数据 */
public data;
/** 选项ID */
public id: number;
/** 计数器 */
public count: number;
/** 持续时间 */
public time: number;
/** 是否触摸正确 */
public touchRight: boolean;
/** 父对象引用 */
public parent;
/**
* 构造函数,初始化选项属性
* @param data 选项数据
* @param id 选项ID
*/
constructor(data, id) {
this.id = id;
this.type = data.type;
......@@ -38,61 +40,68 @@ export class Option {
this.touchRight = false;
}
}
export class Item {
public page;
public audio;
public duration;
public optionList;
public checkMore: boolean;
public rNum: number;
constructor(data, page) {
this.audio = data.questionAudio;
this.duration = data.duration;
let id = 0;
// this.optionList = data.options.map(o => {
// return new Option(o);
// })
this.optionList.sort(function () {
return (0.5 - Math.random());
});
this.rNum = 0;
for (let i = 0; i < this.optionList.length; i++) {
if (this.optionList[i].right) this.rNum++;
}
this.checkMore = this.rNum > 1;
this.page = page;
}
}
/**
* 角色基类,定义角色的基本属性和方法
*/
class Role {
/** 得分 */
public score: number;
/** 正确次数 */
public right: number;
/** 错误次数 */
public error: number;
/** 语音记录 */
public voices: Array<any>;
/**
* 构造函数,初始化角色属性
*/
constructor() {
this.score = 0;
this.error = 0;
this.right = 0;
this.voices = [];
}
/**
* 增加分数
* @param score 增加的分数,默认为1
*/
addScore(score: number = 1) {
this.score += score;
}
/**
* 增加错误次数
* @param score 增加的错误次数,默认为1
*/
addError(score: number = 1) {
this.error += score;
}
/**
* 增加正确次数
*/
addRight() {
this.right += 1;
}
/**
* 添加语音记录
* @param obj 语音对象
*/
addVoice(obj: any) {
this.voices.push(obj);
}
}
/**
* 玩家类,继承自角色基类
*/
export class Player extends Role {
/**
* 构造函数
*/
constructor() {
super();
}
/**
* 重置玩家数据
*/
reset() {
this.score = 0;
this.error = 0;
......@@ -100,50 +109,73 @@ export class Player extends Role {
this.voices = [];
}
}
export class Robot extends Role {
constructor() {
super();
}
reset() {
this.score = 0;
this.error = 0;
this.right = 0;
this.voices = [];
}
}
/**
* 游戏状态枚举
*/
export enum GAME_STATE {
/** 等待状态 */
WAIT,
/** 运行状态 */
RUNNING,
/** 结束状态 */
OVER
}
/**
* 游戏主控制类,负责管理游戏状态、问题选项和玩家信息
* 采用单例模式设计,确保全局只有一个游戏实例
*/
export default class Game {
/** 单例实例 */
private static ins: Game;
/**
* 获取游戏实例
* @returns Game实例
*/
public static getIns(): Game {
if (!Game.ins) Game.ins = new Game();
return Game.ins;
}
/** 游戏数据 */
private data: any;
/** 选项列表 */
private lists: Array<Option>
/** 玩家实例 */
public player: Player;
public robot: Robot;
/** 游戏状态 */
public state: GAME_STATE;
/** 问题总数 */
public total: number;
/**
* 构造函数,初始化游戏基本属性
*/
constructor() {
this.start = false;
this.lists = [];
this.player = new Player();
this.robot = new Robot();
this.state = GAME_STATE.WAIT;
}
/**
* 获取当前选项列表长度
* @returns 选项列表长度
*/
get len() {
return this.lists.length;
}
/** 是否为单人游戏模式 */
public singleGame: boolean;
/** 问题信息,包含文本和音频 */
public question: { text, audio };
/** 游戏标题 */
public title: string;
/** 背景音乐URL */
public bgAudio: string;
/** 问题文本 */
public questionText: string;
/**
* 初始化游戏数据
* @param data 游戏配置数据
*/
public init(data) {
this.singleGame = !data.onlineFlg;
this.question = { text: data.questionText, audio: data.questionTextAudio };
......@@ -154,11 +186,16 @@ export default class Game {
this.lists = [];
this.data = data.questions;
}
/** 游戏是否已开始 */
public start: boolean;
/** 当前页码/进度 */
public page: number;
/**
* 重置游戏状态
* 重置玩家数据,初始化问题列表,准备开始新游戏
*/
reset() {
this.player.reset();
this.robot.reset();
this.page = 1;
this.start = true;
this.lists = [];
......@@ -169,6 +206,11 @@ export default class Game {
this.state = GAME_STATE.WAIT;
this.total = this.lists.length;
}
/**
* 获取随机卡片信息
* 从列表中随机抽取一个选项并从列表中移除
* @returns 随机选项,如果列表为空则返回null
*/
public getCardInfo() {
let option: Option;
if (this.lists.length > 0) {
......@@ -180,43 +222,37 @@ export default class Game {
console.log(" this.cardInfo=======", option)
return option;
}
// checkSuccess(arr: Option[]) {
// let data = this.getCurrentPage();
// let options = data.optionList.concat();
// options.sort((A, B) => A.id - B.id)
// for (let i = 0; i < options.length; i++) {
// if (arr[i] && arr[i].id != options[i].id) {
// return false;
// }
// }
// return true;
// }
// getDataByPage(page): Item {
// return this.lists.filter(li => li.page == page)[0]
// }
// getCurrentPage(): Item {
// let page = this.page;
// return this.lists.filter(li => li.page == page)[0]
// }
/**
* 获取总页数/剩余问题数
* @returns 剩余问题数量
*/
getTotalPageNum() {
return this.lists.length;
}
/**
* 获取当前页码
* @returns 当前页码
*/
getCurrentPageNum() {
return this.page;
}
/**
* 页码加1,用于游戏进度更新
*/
addPage() {
this.page += 1;
}
/**
* 获取剩余总数
* @returns 剩余问题数量
*/
get getTotla() {
return this.lists.length;
// this.total = 0;
// this.lists.forEach((item) => {
// this.total += item.rNum;
// })
// return this.total;
}
/**
* 判断游戏是否结束
* @returns 当前页码是否超过问题总数
*/
get isOver() {
return this.page > this.lists.length;
}
......
This diff is collapsed.
/**
* @file MyCocosSceneComponent.ts
* @description 基础场景组件,提供了场景初始化、资源预加载、屏幕适配等基础功能
*/
import { defaultData } from "../script/defaultData";
/**
* @class MyCocosSceneComponent
* @extends cc.Component
* @description 基础场景组件类,继承自 cc.Component,提供场景通用功能
*/
export class MyCocosSceneComponent extends cc.Component {
// 生命周期 onLoad
/**
* @description 生命周期函数,组件加载时调用
* @override
*/
onLoad() {
this.initSceneData();
this.initSize();
}
/** @private 图片资源列表 */
_imageResList = null;
/** @private 音频资源列表 */
_audioResList = null;
/** @private 动画资源列表 */
_animaResList = null;
/**
* @description 初始化场景数据,创建资源列表数组
* @private
*/
initSceneData() {
this._imageResList = [];
this._audioResList = [];
this._animaResList = [];
}
_designSize = null; // 设计分辨率
_frameSize = null; // 屏幕分辨率
_mapScaleMin = null; // 场景中常用缩放(取大值)
_mapScaleMax = null; // 场景中常用缩放(取小值)
_cocosScale = null; // cocos 自缩放 (较少用到)
/** @private 设计分辨率 */
_designSize = null;
/** @private 屏幕分辨率 */
_frameSize = null;
/** @private 场景中常用缩放(取小值) */
_mapScaleMin = null;
/** @private 场景中常用缩放(取大值) */
_mapScaleMax = null;
/** @private cocos 自缩放(较少用到) */
_cocosScale = null;
/**
* @description 初始化屏幕尺寸和缩放比例,处理屏幕适配
* @private
*/
initSize() {
// 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小
let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height
......@@ -48,9 +79,13 @@ export class MyCocosSceneComponent extends cc.Component {
cc.director['_scene'].height = frameSize.height;
}
/** @public 游戏数据对象 */
data = null;
// 生命周期 start
/**
* @description 生命周期函数,组件启动时调用
* @override
*/
start() {
if (window && (<any>window).courseware && (<any>window).courseware.getData) {
(<any>window).courseware.getData((data) => {
......@@ -65,10 +100,17 @@ export class MyCocosSceneComponent extends cc.Component {
}
}
/**
* @description 获取默认游戏数据
* @returns {Object} 默认游戏数据对象
*/
getDefaultData() {
return defaultData;
}
/**
* @description 预加载所有资源项
*/
preloadItem() {
this.addPreloadImage();
this.addPreloadAudio();
......@@ -76,15 +118,31 @@ export class MyCocosSceneComponent extends cc.Component {
this.preload();
}
/**
* @description 添加需要预加载的图片资源
* @virtual 子类需要重写此方法
*/
addPreloadImage() {
}
/**
* @description 添加需要预加载的音频资源
* @virtual 子类需要重写此方法
*/
addPreloadAudio() {
}
/**
* @description 添加需要预加载的动画资源
* @virtual 子类需要重写此方法
*/
addPreloadAnima() {
}
/**
* @description 执行资源预加载
* @private
*/
preload() {
const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList);
cc.assetManager.loadAny(preloadArr, null, null, (err, data) => {
......@@ -105,6 +163,10 @@ export class MyCocosSceneComponent extends cc.Component {
});
}
/**
* @description 日志输出函数,优先使用 middleLayer 组件的 log 方法,如果不存在则使用 cc.log
* @param {string} str 要输出的日志内容
*/
log (str) {
const node = cc.find('middleLayer');
if(node){
......@@ -114,6 +176,10 @@ export class MyCocosSceneComponent extends cc.Component {
}
}
/**
* @description 资源加载完成后的回调函数
* @virtual 子类需要重写此方法
*/
onLoadEnd() {
}
......@@ -134,6 +200,11 @@ export class MyCocosSceneComponent extends cc.Component {
// ------------------------------------------------
/**
* @description 根据资源名称获取精灵节点
* @param {string} resName 资源名称
* @returns {cc.Node} 创建的精灵节点
*/
getSprNode(resName) {
const sf = cc.find('Canvas/res/img/' + resName).getComponent(cc.Sprite).spriteFrame;
const node = new cc.Node();
......@@ -142,6 +213,11 @@ export class MyCocosSceneComponent extends cc.Component {
}
/**
* @description 根据URL加载精灵帧
* @param {string} url 图片URL
* @param {Function} cb 加载完成后的回调函数,参数为加载的精灵帧
*/
getSpriteFrimeByUrl(url, cb) {
cc.loader.load({ url }, (err, img) => {
const spriteFrame = new cc.SpriteFrame(img)
......@@ -151,6 +227,11 @@ export class MyCocosSceneComponent extends cc.Component {
})
}
/**
* @description 根据URL创建精灵节点
* @param {string} url 图片URL
* @param {Function} cb 创建完成后的回调函数,参数为创建的节点
*/
getSprNodeByUrl(url, cb) {
const node = new cc.Node();
const spr = node.addComponent(cc.Sprite);
......@@ -162,6 +243,11 @@ export class MyCocosSceneComponent extends cc.Component {
})
}
/**
* @description 根据URL播放音频
* @param {string} audio_url 音频URL
* @param {Function} cb 音频播放完成后的回调函数
*/
playAudioByUrl(audio_url, cb = null) {
if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
......
/**
* @file defaultData.ts
* @description 默认游戏数据配置文件
*/
/**
* @constant defaultData
* @description 默认游戏数据对象,包含游戏的基本配置和问题数据
* @type {Object}
*/
export const defaultData = {
"score": 0,
"title": "砸彩蛋",
......
This diff is collapsed.
......@@ -4588,6 +4588,7 @@ declare namespace cc {
/** !#en Returns a normalized vector representing the forward direction (Z axis) of the node in world space.
!#zh 获取节点正前方(z 轴)面对的方向,返回值为世界坐标系下的归一化向量 */
forward: Vec3;
data: any;
/**
@param name name
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment