//导演类,控制游戏的逻辑
import {DataStore} from "./base/DataStore.js";
import {UpPencil} from "./runtime/UpPencil.js";
import {DownPencil} from "./runtime/DownPencil.js";
export class Director {
static getInstance() {
if (!Director.instance) {
Director.instance = new Director();
}
return Director.instance;
}
constructor() {
this.dataStore = DataStore.getInstance();
this.moveSpeed = 2;
}
createPencil() {
const minTop = DataStore.getInstance().canvas.height / 8;
const maxTop = DataStore.getInstance().canvas.height / 2;
const top = minTop + Math.random() * (maxTop - minTop);
this.dataStore.get('pencils').push(new UpPencil(top));
this.dataStore.get('pencils').push(new DownPencil(top));
}
supermanEvent() {
for (let i = 0; i <= 2; i++) {
this.dataStore.get('superman').y[i] =
this.dataStore.get('superman').supermanY[i];
}
this.dataStore.get('superman').time = 0;
}
//判断超人是否和障碍撞击
static isStrike(bird, pencil) {
let s = false;
if (bird.top > pencil.bottom ||
bird.bottom < pencil.top ||
bird.right < pencil.left ||
bird.left > pencil.right
) {
s = true;
}
return !s;
}
//判断超人是否撞击地板和障碍
check() {
const superman = this.dataStore.get('superman');
const land = this.dataStore.get('land');
const pencils = this.dataStore.get('pencils');
const score = this.dataStore.get('score');
//地板的撞击判断
if (superman.supermanY[0] + superman.supermanHeight[0] >= land.y) {
console.log('撞击地板啦');
this.isGameOver = true;
return;
}
//超人的边框模型
const supermanBorder = {
top: superman.y[0],
bottom: superman.supermanY[0] + superman.supermanHeight[0],
left: superman.supermanX[0],
right: superman.supermanX[0] + superman.supermanWidth[0]
};
const length = pencils.length;
for (let i = 0; i < length; i++) {
const pencil = pencils[i];
const pencilBorder = {
top: pencil.y,
bottom: pencil.y + pencil.height,
left: pencil.x,
right: pencil.x + pencil.width
};
if (Director.isStrike(supermanBorder, pencilBorder)) {
console.log('撞到水管啦');
this.isGameOver = true;
return;
}
}
//加分逻辑
if (superman.supermanX[0] > pencils[0].x + pencils[0].width
&& score.isScore) {
wx.vibrateShort({
success: function () {
console.log('振动成功');
}
});
score.isScore = false;
score.scoreNumber++;
}
}
run() {
this.check();
if (this.isReady){
this.dataStore.get('background').draw();
this.dataStore.get('land').draw();
this.dataStore.get('logo').draw();
this.dataStore.get('openButton').draw();
this.dataStore.get('superman').draw();
}else if (!this.isGameOver) {
this.dataStore.get('background').draw();
const pencils = this.dataStore.get('pencils');
if (pencils[0].x + pencils[0].width <= 0 &&
pencils.length === 4) {
pencils.shift();
pencils.shift();
this.dataStore.get('score').isScore = true;
}
if (pencils[0].x <= (DataStore.getInstance().canvas.width - pencils[0].width) / 2 &&
pencils.length === 2) {
this.createPencil();
}
this.dataStore.get('pencils').forEach(function (value) {
value.draw();
});
this.dataStore.get('land').draw();
this.dataStore.get('score').draw();
this.dataStore.get('superman').draw();
let timer = requestAnimationFrame(() => this.run());
this.dataStore.put('timer', timer);
} else {
console.log('游戏结束');
this.dataStore.get('end').draw();
cancelAnimationFrame(this.dataStore.get('timer'));
this.dataStore.destroy();
//触发微信小游戏垃圾回收
wx.triggerGC();
}
}
}