| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831 |
- // Learn cc.Class:
- // - https://docs.cocos.com/creator/manual/en/scripting/class.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
- var hitDirection = cc.Enum({
- left: 0,
- middle: 1,
- right: 2
- });
- cc.Class({
- extends: cc.Component,
- properties: {
- // foo: {
- // // ATTRIBUTES:
- // default: null, // The default value will be used only when the component attaching
- // // to a node for the first time
- // type: cc.SpriteFrame, // optional, default is typeof default
- // serializable: true, // optional, default is true
- // },
- // bar: {
- // get () {
- // return this._bar;
- // },
- // set (value) {
- // this._bar = value;
- // }
- // },
- //硬币初始值
- coin: {
- default: 3000,
- type: cc.Integer,
- tooltip: "初始分数",
- serializable: true,
- },
- coinNode: {
- default: null,
- type: cc.Node,
- tooltip: "显示可用分数",
- serializable: true,
- },
- index: {
- default: 0,
- type: cc.Integer,
- visible: false,
- serializable: false,
- },
- fruits: {
- default: [],
- visible: false,
- type: cc.Node,
- serializable: true
- },
- FruitsNode: {
- default: null,
- type: cc.Node,
- tooltip: "图标父节点",
- serializable: true,
- },
- FruitsMask: {
- default: null,
- type: cc.Node,
- tooltip: "选中的遮罩",
- serializable: true,
- },
- //减速时间==加速时间
- duration: {
- tooltip: "减速时间==加速时间",
- default: 2,
- type: cc.Float,
- max: 10,
- min: 1,
- },
- rewardFruits: {
- default: [],
- tooltip: "奖励的水果数组",
- visible: false,
- type: cc.Node,
- serializable: true
- },
- ResultNode: {
- default: null,
- type: cc.Node,
- tooltip: "返回结果的父节点",
- serializable: true,
- },
- resultFruits: {
- default: [],
- tooltip: "solt节点",
- visible: false,
- type: cc.Node,
- serializable: true
- },
- runSche: {
- default: null,
- visible: false,
- serializable: true,
- },
- //执行次数
- count: {
- default: 0,
- type: cc.Integer,
- visible: false,
- serializable: false,
- },
- //击中的次数
- hitCount: {
- default: 0,
- type: cc.Integer,
- visible: false,
- serializable: false,
- },
- //定义一个水果字典属性
- coinArray: {
- default: null,
- visible: false,
- serializable: true,
- },
- //计算结果时候奖励的金币
- rewardCoin: {
- default: 0,
- type: cc.Integer,
- visible: false,
- serializable: false,
- },
- rewardCoinNode: {
- default: null,
- type: cc.Node,
- tooltip: "当前奖励的金币数量",
- serializable: true,
- },
- //是否开始运行
- gameRun: {
- default: false,
- visible: false,
- serializable: false,
- },
- //是否完全停下来
- gameStop: {
- default: false,
- visible: false,
- serializable: false,
- },
- //是否在播放开始音乐
- audioGameStartPlay: {
- default: false,
- visible: false,
- serializable: false,
- },
- defaultSpriteFrame: {
- default: null,
- type: cc.spriteFrame,
- serializable: false,
- visible: false,
- },
- //左右两个进度条
- leftProgressBar: {
- default: null,
- type: cc.ProgressBar,
- serializable: true,
- },
- rightProgressBar: {
- default: null,
- serializable: true,
- type: cc.ProgressBar,
- },
- maxHitPower: {
- default: 200,
- type: cc.Integer,
- tooltip: "最大的力,公斤",
- serializable: false,
- },
- curHitPower: {
- default: 0,
- type: cc.Integer,
- tooltip: "当前的力,公斤",
- serializable: false,
- },
- upTime: {
- default: 3,
- type: cc.Integer,
- tooltip: "上升时间",
- serializable: true,
- },
- powerLabel: {
- default: null,
- type: cc.Label,
- tooltip: "显示的力量槽",
- serializable: true,
- },
- hitDire: {
- default: hitDirection.left,
- type: cc.Enum(hitDirection),
- visible: false
- },
- tipDire: {
- default: hitDirection.left,
- type: cc.Enum(hitDirection),
- visible: false,
- },
- tipDireBoxingNode: {
- default: [],
- visible: true,
- tooltip: "提示的拳击图标节点",
- type: cc.Node,
- serializable: true
- },
- explainUI: {
- default: null,
- type: cc.Node,
- tooltip: "说明ui节点",
- serializable: true,
- },
- StartButton: {
- default: null,
- type: cc.Node,
- tooltip: "开始按钮节点",
- serializable: true,
- },
- bHasPlayEnd: {
- default: false,
- tooltip: "是否播放完数字读音",
- serializable: true,
- },
- timeCount: {
- default: 0,
- type: cc.Integer,
- visible: false
- }
- },
- // LIFE-CYCLE CALLBACKS:
- syncCoin() {
- //显示初始分数
- this.coinNode.getComponent(cc.Label).string = this.coin;
- },
- syncReward() {
- //同步当前奖励的金币
- this.rewardCoinNode.getComponent(cc.Label).string = this.rewardCoin;
- },
- syncHitPower(value) {
- this.curHitPower = value;
- this.powerLabel.string = this.curHitPower;
- },
- onLoad() {
- //显示初始分数
- this.syncCoin();
- //同步当前奖励的金币
- this.syncReward();
- //同步当前打击力
- this.syncHitPower(0);
- //定义一个水果字典属性
- this.coinArray = {
- "apple": 100,
- "apple_d": 200,
- "bell": 300,
- "bell_d": 600,
- "seven": 700,
- "seven_d": 1400,
- "star": 1000,
- "star_d": 2000,
- "bar": 3000,
- "lucky": 0 //lucky不算钱
- }
- //获取水果的solt对象
- this.resultFruits = this.ResultNode.children;
- //保存一下默认的spriteFrame
- this.defaultSpriteFrame = this.resultFruits[0].getComponent(cc.Sprite).spriteFrame;
- //获取水果机的对象排序
- let topChild = this.FruitsNode.getChildByName("top").children;
- let rightChild = this.FruitsNode.getChildByName("right").children;
- let leftChild = this.FruitsNode.getChildByName("left").children.reverse();
- let bottomChild = this.FruitsNode.getChildByName("bottom").children.reverse();
- //这里要按顺时针 获取节点,以便循环
- //比如 top->right->bottom->left
- this.fruits = this.fruits.concat(topChild)
- .concat(rightChild)
- .concat(bottomChild)
- .concat(leftChild);
- console.log(this.fruits);
- //加入 fruits 数组后,后面两个需要再反转回来,不然显示会相反
- leftChild.reverse();
- bottomChild.reverse();
- this.schedule(() => {
- this.timeCount++;
- }, 1)
- },
- start() {
- //开一个计时器,5分钟判断一次
- this.schedule(function () {
- if (this.coin < 500) {
- this.coin += 3000;
- this.syncCoin();
- console.log("赠送金币", this.coin);
- }
- }.bind(this), 300);
- },
- //开始游戏
- onGameStart() {
- if (this.gameStop) return;
- if (this.gameRun) return;
- if (this.count >= 3) {
- console.log("已执行完三次打击");
- return;
- }
- if (this.coin == 0) {
- console.log("金币不能为零");
- myAudio.onNoCoin();
- return;
- }
- if (this.coin >= 500) {
- //计算金币
- this.coin -= 500;
- this.syncCoin();
- myAudio.onDeducCoin();
- } else {
- console.log("金币不足500!");
- myAudio.onNoCoin();
- return;
- }
- //开启runing
- this.runSche = () => {
- this.runing();
- }
- this.schedule(this.runSche, 0.05);
- this.gameRun = true;
- console.log("点击开始");
- //设置提示拳
- setTimeout(() => {
- this.onSetTipDirection();
- }, 500)
- },
- runing() {
- if (this.fruits.length == 0) {
- console.log("fruits 不能为空");
- return;
- }
- // this.index = 10;
- if (this.index == this.fruits.length - 1) {
- this.index = 0;
- } else {
- this.index++;
- }
- //获取target 的pos
- let tarPos = this.fruits[this.index].parent.convertToWorldSpaceAR(this.fruits[this.index].position);
- let endPos = this.FruitsMask.parent.convertToNodeSpaceAR(tarPos);
- this.FruitsMask.setPosition(endPos);
- },
- //设置提示的击打方向
- onSetTipDirection() {
- let direArray = [0, 1, 2];
- let index = Math.floor((Math.random() * direArray.length));
- for (let i = 0; i < this.tipDireBoxingNode.length; i++) {
- if (i == index) {
- this.tipDireBoxingNode[i].active = true;
- } else {
- this.tipDireBoxingNode[i].active = false;
- }
- }
- if (index == 0) {
- this.tipDire = hitDirection.left;
- } else if (index == 1) {
- this.tipDire = hitDirection.middle;
- } else if (index == 2) {
- this.tipDire = hitDirection.right;
- }
- //提示拳击方向
- myAudio.onPlayBoxingTip(index);
- console.log("设置的提示拳:", this.tipDire);
- },
- //手机加速计部分调用触发,
- //走完计算流程后,触发回调
- onHitFromDevice(data, callback) {
- console.log("onHitFromDevice:", data);
- if (data.direction == "xLCount") {
- //右勾拳
- this.onHitCall("right", data.hitPower, callback);
- } else if (data.direction == "xRCount") {
- //左勾拳
- this.onHitCall("left", data.hitPower, callback);
- } else if (data.direction == "zLCount" || data.direction == "zRCount") {
- //直拳
- this.onHitCall("middle", data.hitPower, callback);
- }
- },
- //调用打击
- onHitCall(direction, hitPower, callback) {
- if (!this.gameRun) {
- console.log("游戏未开始");
- return;
- }
- let ranPower = 0;
- if (!hitPower) {
- //1.计算力量,
- ranPower = Math.ceil(Math.random() * 200) + 100;
- } else {
- ranPower = hitPower;
- }
- if (ranPower > 999)
- ranPower = 999;
- //判断方向
- if (direction == "left") {
- this.hitDire = hitDirection.left;
- } else if (direction == "middle") {
- this.hitDire = hitDirection.middle;
- } else if (direction == "right") {
- this.hitDire = hitDirection.right;
- }
- // console.log(this.hitDire);
- //判断是否和提示的方向一样
- if (this.hitDire == this.tipDire) {
- this.onDelayEnd(ranPower);
- // console.log("击打正确");
- //读取播报数字
- this.bHasPlayEnd = false;
- myAudio.onPlayHitPower(ranPower.toString(), () => {
- this.bHasPlayEnd = true;
- console.log("播放完声音:", this.gameStop, this.gameRun, this.bHasPlayEnd);
- if (this.count == 3) {
- if (this.gameStop || this.gameRun) return;
- //计算奖励
- this.onRewards();
- } else {
- //如果count 不等于 3 ,继续开游戏
- //todo 播放音效后,继续游戏
- //开始游戏
- this.onGameStart();
- }
- });
- this.hitCount++;
- //播放击中的音效
- if (this.hitCount == 2) {
- myAudio.onTwoHits();
- } else if (this.hitCount == 3) {
- myAudio.onThreeHits();
- } else if (this.hitCount == 1) {
- myAudio.onHitSuccess();
- }
- } else {
- this.hitCount = 0;
- if (this.tipDire == 0) {
- console.log("请打击左勾拳");
- myAudio.onHitFail();
- //提示拳击方向
- myAudio.onPlayBoxingTip(0);
- if (callback)
- callback();
- } else if (this.tipDire == 1) {
- console.log("请打击直拳");
- myAudio.onHitFail();
- //提示拳击方向
- myAudio.onPlayBoxingTip(1);
- if (callback)
- callback();
- } else if (this.tipDire == 2) {
- console.log("请打击右勾拳");
- myAudio.onHitFail();
- //提示拳击方向
- myAudio.onPlayBoxingTip(2);
- if (callback)
- callback();
- }
- }
- },
- //
- onDelayEnd(hitPower) {
- if (!this.gameRun)
- return;
- this.gameRun = false;
- //假如已经触发停止按钮响应,但是还没完全停止
- this.gameStop = true;
- //2.根据力量值,来判断,如果超过max值,马上停止,反之停止的越慢
- let temPower = hitPower / this.maxHitPower;
- //3.计算剩余的时间time 比例,
- let time = (1 - temPower) * this.duration;
- if (time <= 0) {
- time = 0;
- }
- console.log("运行的时间:", time);
- //4.默认运行速度是0.05 最大值是0.5
- let obj = { a: 0.05 }
- let oldValue = 0.05;
- cc.tween(obj).to(time, { a: 0.5 }, {
- progress: (start, end, current, ratio) => {
- let tempValue = start + (end - start) * ratio;
- if (tempValue > oldValue * 2 + 0.001) {
- oldValue = tempValue;
- this.unschedule(this.runSche);
- this.schedule(this.runSche, oldValue);
- }
- return start + (end - start) * ratio;
- }
- })
- .call(() => {
- this.onEndShow(hitPower);
- })
- .start()
- //显示到ui
- this.syncHitPower(hitPower);
- this.onPowerBar(hitPower);
- console.log("点击停止");
- },
- onEndShow() {
- this.gameStop = false;
- this.count++;
- this.unschedule(this.runSche);
- this.rewardFruits.push(this.fruits[this.index]);
- console.log("选中的水果名字:", this.fruits[this.index].name);
- for (let i = 0; i < this.rewardFruits.length; i++) {
- //结果在resultFruits中显示
- this.resultFruits[i].getComponent(cc.Sprite).spriteFrame = this.rewardFruits[i].getComponent(cc.Sprite).spriteFrame;
- }
- webView.onResetAccState();
- console.log("this.bHasPlayEnd:", this.bHasPlayEnd);
- if (this.bHasPlayEnd) {
- if (this.count == 3) {
- //计算奖励
- this.onRewards();
- } else {
- //如果count 不等于 3 ,继续开游戏
- //todo 播放音效后,继续游戏
- //开始游戏
- console.log(this.gameStop, this.gameRun, this.bHasPlayEnd);
- this.onGameStart();
- }
- }
- },
- onRewards() {
- //计算分数
- //水果组合:
- //如果没有组合用户得不到任何金币(三个完全不同的水果)
- //三次打击完成后如果出现一对相同的水果(非双倍水果)金币加成这两个水果总金额的25%
- //三次打击完成后如果出现3个相同的水果(非双倍水果),金币加成这两个水果总金额的50% **
- //三次打击完成后如果出现一对相同的水果(双倍水果)金币加成这两个水果总金额的50%
- //三次打击完成后如果出现3个相同的水果(双倍水果),金币加成这两个水果总金额的100% **
- //三个相等
- if (this.rewardFruits[0].name == this.rewardFruits[1].name && this.rewardFruits[0].name == this.rewardFruits[2].name) {
- //不能是lucky图标,之后判断是双倍还是非双倍
- if (this.rewardFruits[0].name != "lucky") {
- let coinItem = this.coinArray[this.rewardFruits[0].name];
- if (this.rewardFruits[0].name.indexOf("_d") != -1) {
- //是双倍水果,加成总金额的100%
- this.rewardCoin = coinItem * 6;//coinItem * 3 * 2
- console.log("是双倍水果,加成总金额的100% ");
- } else {
- this.rewardCoin = coinItem * 4.5;//coinItem * 3 + coinItem * 1.5
- console.log("是单倍水果,加成总金额的50% ");
- }
- } else {
- console.log("三个lucky不加分");
- }
- }
- else {
- //记录一下相同和不同的对象
- let sameObj = null, defferentObj = null;
- if (this.rewardFruits[0].name == this.rewardFruits[1].name) {
- defferentObj = this.rewardFruits[2];
- sameObj = this.rewardFruits[0];
- } else if (this.rewardFruits[0].name == this.rewardFruits[2].name) {
- defferentObj = this.rewardFruits[1];
- sameObj = this.rewardFruits[0];
- } else if (this.rewardFruits[1].name == this.rewardFruits[2].name) {
- defferentObj = this.rewardFruits[0];
- sameObj = this.rewardFruits[1];
- }
- //判断两个相等的不是lucky,计算分数
- if (sameObj != null && sameObj.name != "lucky") {
- let coinItem = this.coinArray[sameObj.name];
- //判断不相等的那一个是不是lucky,如果是,则计算三个相等
- if (defferentObj.name == "lucky") {
- if (sameObj.name.indexOf("_d") != -1) {
- //是双倍水果,加成总金额的100%
- this.rewardCoin = coinItem * 6;//coinItem * 3 * 2
- console.log("是双倍水果,加成总金额的100% ");
- } else {
- //是单倍水果,加成总金额的50%
- this.rewardCoin = coinItem * 4.5;// coinItem * 3 + coinItem * 1.5
- console.log("是单倍水果,加成总金额的50% ");
- }
- } else {
- //处理只有两个相等情况
- if (sameObj.name.indexOf("_d") != -1) {
- //是双倍水果,加成总金额的50%
- this.rewardCoin = coinItem * 3; //coinItem * 2 + coinItem * 2 * 0.5
- console.log("是双倍水果,加成总金额的50% ");
- } else {
- //是单倍水果,加成总金额的25%
- this.rewardCoin = coinItem * 2.5;//coinItem * 2 + coinItem * 2 * 0.25
- console.log("是单倍水果,加成总金额的25% ");
- }
- }
- } else if (sameObj == null) {
- console.log("没有相同的项目,不加分");
- }
- }
- console.log("最后奖励的分数:", this.rewardCoin);
- if (0 == this.rewardCoin) {
- //失败
- myAudio.onPlayGameDefeat();
- } else {
- //成功
- myAudio.onPlayGameVictory();
- //金币音效
- myAudio.onGetCoin();
- this.coin += this.rewardCoin;
- this.syncCoin();
- }
- this.syncReward();
- //显示开始按钮
- this.StartButton.active = true;
- console.log("是否显示按钮:", this.StartButton.active);
-
- this.uploadScore();
- },
- //重置游戏,点击开始游戏调用
- onReset() {
- if (this.gameStop) return;
- if (this.gameRun) return;
- if (this.count < 3 && this.count != 0) {
- console.log("需要执行完才可以开始,count=", this.count);
- return;
- }
- this.count = 0;
- this.rewardCoin = 0;
- this.syncReward();
- this.rewardFruits = [];
- this.unschedule(this.runSche);
- for (let i = 0; i < this.resultFruits.length; i++) {
- //结果在resultFruits中显示
- this.resultFruits[i].getComponent(cc.Sprite).spriteFrame = this.defaultSpriteFrame;
- }
- this.syncHitPower(0);
- this.StartButton.active = false;
- console.log("onReset 是否显示按钮:", this.StartButton.active);
- if (this.audioGameStartPlay) return;
- this.audioGameStartPlay = true;
- myAudio.onPlayGameStart(() => {
- this.audioGameStartPlay = false;
- //开始游戏
- this.onGameStart();
- });
- },
- //重新初始化游戏和分数
- onResetGame() {
- this.coin = 3000;
- this.syncCoin();
- this.onReset();
- },
- onPowerBar(curHitPower) {
- let temPower = curHitPower / this.maxHitPower;
- //零到 1 之 间
- let obj = { a: 0 }
- cc.tween(obj).to(this.upTime, { a: temPower }, {
- progress: (start, end, current, ratio) => {
- let tempValue = start + (end - start) * ratio;
- this.leftProgressBar.progress = tempValue;
- this.rightProgressBar.progress = tempValue;
- return start + (end - start) * ratio;
- }
- })
- .start()
- },
- //说明规则ui
- onShowExplain() {
- this.explainUI.active = true;
- },
- onHideExplain() {
- this.explainUI.active = false;
- },
- uploadScore() {
- uni.postMessage({
- data: {
- funName: "uploadInfo",
- gameData: {
- gameScore: webView.AllPower? webView.AllPower : 0,//游戏得分
- gameTime: this.timeCount,//单位秒
- calorieBurned: webView.AllCalorie? webView.AllCalorie : 0,//消耗的卡路里
- }
- }
- });
- console.log("webView.AllPower:",webView.AllPower);
- console.log("webView.AllCalorie:",webView.AllCalorie);
- webView.onResetAllValue();
- }
- // update(dt) {
- // },
- });
|