-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
72 lines (63 loc) · 1.93 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ResourceLoader } from "./js/base/resourceLoader";
import { Manager } from './js/manager';
import { Background } from './js/runtime/background';
import { DataStore } from './js/base/dataStore';
import { Land } from './js/runtime/land';
import { Birds } from './js/player/birds';
import { StartButton } from "./js/player/startButton";
import { Score } from "./js/player/score";
// 游戏入口,初始化整个游戏
export class Main {
constructor() {
this.canvas = wx.createCanvas();
this.ctx = this.canvas.getContext('2d');
this.dataStore = DataStore.getInstance();
this.manager = Manager.getInstance();
const loader = ResourceLoader.create();
loader.onLoaded(map => this.onResourceLoaded(map));
}
// 资源加载完成后执行
onResourceLoaded(map) {
this.dataStore.canvas = this.canvas;
this.dataStore.ctx = this.ctx;
this.dataStore.res = map;
this.createBackgroundMusic();
this.init();
}
// 创建背景音乐
createBackgroundMusic() {
const bgm = wx.createInnerAudioContext();
bgm.autoplay = true;
bgm.loop = true;
bgm.src = 'res/bgm.mp3';
}
init() {
// 重置游戏
this.manager.isGameOver = false;
this.dataStore.put('pencils', [])
.put('background', new Background())
.put('land', new Land())
.put('bird', new Birds())
.put('startButton', new StartButton())
.put('score', new Score());
this.registerEvent();
this.manager.createPencil();
this.manager.run();
}
// 注册点击事件
registerEvent() {
// this.canvas.addEventListener('touchstart', e => {
// // 屏蔽事件冒泡
// e.preventDefault();
// console.log('被摸我');
// });
wx.onTouchStart(() => {
if (this.manager.isGameOver) {
console.log('游戏开始');
this.init();
}else {
this.manager.birdsEvent();
}
});
}
}