Commit bf2127ca authored by Tt's avatar Tt

游戏完成

parent b0d610b9
...@@ -34,16 +34,16 @@ export default class NewClass extends cc.Component { ...@@ -34,16 +34,16 @@ export default class NewClass extends cc.Component {
private fishs: cc.Node[]; private fishs: cc.Node[];
gameStart() { gameStart() {
//游戏开始。允许出鱼 //游戏开始。允许出鱼
let page = Game.getIns().getCurrentPage(); // let page = Game.getIns().getCurrentPage();
let list = page.optionList.concat(); // let list = page.optionList.concat();
list = JSON.parse(JSON.stringify(list)); // list = JSON.parse(JSON.stringify(list));
list.sort((A, B) => { return Math.random() < 0.5 }); // list.sort((A, B) => { return Math.random() < 0.5 });
this.list = list; // this.list = list;
this.count = 0; // this.count = 0;
this.lastCount = null; // this.lastCount = null;
this.fishs = []; // this.fishs = [];
this.viewFishs = []; // this.viewFishs = [];
this.touchFishs = []; // this.touchFishs = [];
//根据顺序生成对应的鱼。 //根据顺序生成对应的鱼。
//顺序根据时间来进行跳动。当顺序跳动之后,就会产生新的鱼。 //顺序根据时间来进行跳动。当顺序跳动之后,就会产生新的鱼。
} }
......
This diff is collapsed.
import { asyncDelay, onHomeworkFinish } from "../script/util"; import { asyncDelay, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent"; import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";
import Game, { GAME_STATE } from "./tool/Game"; import Game, { GAME_STATE, Option } from "./tool/Game";
import pg from "./tool/pg"; import pg from "./tool/pg";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
...@@ -29,7 +29,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -29,7 +29,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.initView(); this.initView();
this.initEvent(); this.initEvent();
this.log("statr!!!!!!!!!!!!!") this.log("statr!!!!!!!!!!!!!")
this.scheduleOnce(() => { pg.event.emit("game_start") }, 0.5); this.gameStart()
} }
protected onDestroy(): void { protected onDestroy(): void {
pg.event.clear(); pg.event.clear();
...@@ -43,11 +43,13 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -43,11 +43,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
this._cantouch = true; this._cantouch = true;
} }
private audioId: any; private audioId: any;
async initView() { } async initView() {
let boat_left = pg.view.find(this, 'layout_bottom/boat_left')
let boat_right = pg.view.find(this, 'layout_bottom/boat_right')
pg.view.setString(pg.view.find(boat_left, 'text'), Game.getIns().boatLeft.text)
pg.view.setString(pg.view.find(boat_right, 'text'), Game.getIns().boatRight.text)
}
initEvent() { initEvent() {
pg.event.on("game_start", () => {
this.gameStart();
})
pg.event.on("game_time_over", () => { pg.event.on("game_time_over", () => {
// alert("game_time_over") // alert("game_time_over")
//这里的事件会发送的很早。但是我们需要等待动画执行完了之后再开始后续的内容 //这里的事件会发送的很早。但是我们需要等待动画执行完了之后再开始后续的内容
...@@ -63,16 +65,144 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -63,16 +65,144 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.audio.playLocalAudio(cc.find(`Canvas/res/audio/btn`)); pg.audio.playLocalAudio(cc.find(`Canvas/res/audio/btn`));
if (this.audioId > -1) { if (this.audioId > -1) {
cc.audioEngine.stopAllEffects(); cc.audioEngine.stopAllEffects();
// pg.audio.stopAudio(this.audioId);
} }
Game.getIns().state = GAME_STATE.RUNNING; Game.getIns().state = GAME_STATE.RUNNING;
//游戏开始小鱼出现等等 this.initFishs();
if (this.audioId > -1 && Game.getIns().state != GAME_STATE.WAIT) { }
cc.audioEngine.stopAllEffects(); initFishs() {
// pg.audio.stopAudio(this.audioId); // 出现鱼
let fishList = Game.getIns().getFishList();
let layout_fish = pg.view.find(this, 'layout_bottom/layout_fish')
let item = pg.view.find(this, 'layout_bottom/item')
fishList.forEach((fish, index) => {
let node = cc.instantiate(item);
layout_fish.addChild(node);
this.updateItem(node, fish);
});
}
updateItem(item, data: Option) {
item.data = data;
console.log(data);
let text = pg.view.find(item, 'layout_text/text')
pg.view.setString(text, data.txt)
let count = data.id % 10 + 1;
for (let i = 1; i <= 10; i++) {
let fishNode = pg.view.find(item, `fish/fish_${i}`)
fishNode.active = i == count
} }
//左往右
if (Math.random() < 0.5) {
item.x = -(1920 / 2 + Math.random() * 200)
item.y = -315 - Math.random() * 150;
item.scaleX = -1;
text.scaleX = -1;
cc.tween(item).repeatForever(cc.tween().by(7 + Math.random() * 8, { x: 1920 + 400 }).call(() => {
item.scaleX = -item.scaleX;
text.scaleX = -text.scaleX;
}).by(7 + Math.random() * 8, { x: - 1920 - 400 }).call(() => {
item.scaleX = -item.scaleX;
text.scaleX = -text.scaleX;
})).start();
} else {
item.x = 1920 / 2 + Math.random() * 200
item.y = -315 - Math.random() * 150;
item.scaleX = 1;
text.scaleX = 1;
cc.tween(item).repeatForever(cc.tween().by(7 + Math.random() * 8, { x: -1920 - 400 }).call(() => {
item.scaleX = -item.scaleX;
text.scaleX = -text.scaleX;
}).by(7 + Math.random() * 8, { x: 1920 + 400 }).call(() => {
item.scaleX = -item.scaleX;
text.scaleX = -text.scaleX;
})).start();
}
item.on(cc.Node.EventType.TOUCH_START, this.onItemTouchStart, this);
item.on(cc.Node.EventType.TOUCH_MOVE, this.onItemTouchMove, this);
item.on(cc.Node.EventType.TOUCH_END, this.onItemTouchEnd, this);
item.on(cc.Node.EventType.TOUCH_CANCEL, this.onItemTouchCancel, this);
}
onTouchItem(e) {
let item = e.target;
let data = item.data;
item.x = 0;
item.y = 0;
cc.Tween.stopAllByTarget(item);
setTimeout(() => {
this.updateItem(item, data);
}, 100);
}
private setTouchPos(e) {
let item = e.target;
let location = e.getLocation();
cc.Tween.stopAllByTarget(item);
let pos = location.sub(cc.v2(1920 / 2, 1080 / 2));
item.x = pos.x;
item.y = pos.y;
} }
onItemTouchStart(e) {
let target: cc.Node = e.target;
let data = target.data;
if (data.isChecked) return;
this.setTouchPos(e);
}
onItemTouchMove(e) {
let target: cc.Node = e.target;
let data = target.data;
if (data.isChecked) return;
this.setTouchPos(e);
}
onItemTouchEnd(e) {
//
let target: cc.Node = e.target;
let data = target.data;
if (data.isChecked) return;
// let rect = cc.rect(target.x, target.y, target.width, target.height);
let rect = cc.rect(target.x, target.y, 50, 50);
let boat_left = pg.view.find(this, 'layout_bottom/boat_left')
let boat_right = pg.view.find(this, 'layout_bottom/boat_right')
let leftRect = cc.rect(boat_left.x - boat_left.width / 2, boat_left.y - boat_left.height / 2, boat_left.width, boat_left.height);
let rightRect = cc.rect(boat_right.x - boat_right.width / 2, boat_right.y - boat_right.height / 2, boat_right.width, boat_right.height);
let isRight = false;
let currentRect = null;
if (rect.intersects(leftRect)) {
isRight = !!Game.getIns().boatLeft.isChild(data);
Game.getIns().boatLeft.setRight(data);
currentRect = leftRect;
} else if (rect.intersects(rightRect)) {
isRight = !!Game.getIns().boatRight.isChild(data);
Game.getIns().boatRight.setRight(data);
currentRect = rightRect;
}
if (isRight) {
// target
let item = target;
let lastArea = 100;
if (item.scaleX == -1) {
// 计算当前坐标距离最左侧的距离
let space = currentRect.width - ((rect.x + rect.width) - currentRect.x) - lastArea / 5;
let time = space / 10;
cc.tween(item).by(time, { x: space }).start();
} else {
// 计算当前坐标距离最左侧的距离
let space = rect.x - currentRect.x - lastArea;
let time = space / 10;
cc.tween(item).by(time, { x: -space }).start();
}
} else {
this.updateItem(target, data);
}
//判断是否结束了
if (Game.getIns().boatLeft.isAllRight() && Game.getIns().boatRight.isAllRight()) {
this.showGameOver();
}
}
onItemTouchCancel() {
}
private showGameOver() { private showGameOver() {
let gameover = cc.find("gameover", this.node); let gameover = cc.find("gameover", this.node);
gameover.scale = 3; gameover.scale = 3;
......
...@@ -41,8 +41,10 @@ export class Item { ...@@ -41,8 +41,10 @@ export class Item {
private static ID: number = 1000; private static ID: number = 1000;
public audio: string; public audio: string;
public text: string; public text: string;
public optionList; public optionList: Array<Option>;
public rightList: Array<Option>;
constructor(data) { constructor(data) {
this.rightList = []
this.audio = data.audio; this.audio = data.audio;
this.text = data.text; this.text = data.text;
this.optionList = data.options.map(o => { this.optionList = data.options.map(o => {
...@@ -55,7 +57,15 @@ export class Item { ...@@ -55,7 +57,15 @@ export class Item {
for (let i = 0; i < this.optionList.length; i++) { for (let i = 0; i < this.optionList.length; i++) {
this.optionList[i].count = i; this.optionList[i].count = i;
} }
}
isChild(data) {
return this.optionList.find(option => option.id == data.id)
}
setRight(data) {
this.rightList.push(data);
}
isAllRight() {
return this.optionList.length == this.rightList.length;
} }
} }
class Role { class Role {
...@@ -112,8 +122,8 @@ export default class Game { ...@@ -112,8 +122,8 @@ export default class Game {
this.data = data.questions; this.data = data.questions;
} }
public start: boolean; public start: boolean;
private boatLeft: Item; public boatLeft: Item;
private boatRight: Item; public boatRight: Item;
reset() { reset() {
this.player.reset(); this.player.reset();
this.start = true; this.start = true;
...@@ -122,24 +132,20 @@ export default class Game { ...@@ -122,24 +132,20 @@ export default class Game {
this.boatRight = new Item(this.data[1]); this.boatRight = new Item(this.data[1]);
this.state = GAME_STATE.WAIT; this.state = GAME_STATE.WAIT;
} }
checkSuccess(arr: Option[]) { getFishList() {
let data = this.getCurrentPage(); return this.boatLeft.optionList.concat(this.boatRight.optionList)
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;
} }
getCurrentPage(): Item { checkSuccess(arr: Option[]) {
if (!this.lists) return null; return false;
} // let data = this.getCurrentPage();
getTotalPageNum() { // let options = data.optionList.concat();
return this.lists.length; // options.sort((A, B) => A.id - B.id)
} // for (let i = 0; i < options.length; i++) {
getCurrentPageNum() { // if (arr[i] && arr[i].id != options[i].id) {
// return false;
// }
// }
// return true;
} }
addPage() { addPage() {
} }
......
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