Initial commit

This commit is contained in:
Rocks011
2025-12-10 17:47:15 +08:00
commit 94fb922f1f
50 changed files with 1059 additions and 0 deletions

64
js/databus.js Normal file
View File

@@ -0,0 +1,64 @@
import Pool from './base/pool';
let instance;
/**
* 全局状态管理器
* 负责管理游戏的状态,包括帧数、分数、子弹、敌人和动画等
*/
export default class DataBus {
// 直接在类中定义实例属性
enemys = []; // 存储敌人
bullets = []; // 存储子弹
animations = []; // 存储动画
frame = 0; // 当前帧数
score = 0; // 当前分数
isGameOver = false; // 游戏是否结束
pool = new Pool(); // 初始化对象池
constructor() {
// 确保单例模式
if (instance) return instance;
instance = this;
}
// 重置游戏状态
reset() {
this.frame = 0; // 当前帧数
this.score = 0; // 当前分数
this.bullets = []; // 存储子弹
this.enemys = []; // 存储敌人
this.animations = []; // 存储动画
this.isGameOver = false; // 游戏是否结束
}
// 游戏结束
gameOver() {
this.isGameOver = true;
}
/**
* 回收敌人,进入对象池
* 此后不进入帧循环
* @param {Object} enemy - 要回收的敌人对象
*/
removeEnemy(enemy) {
const temp = this.enemys.splice(this.enemys.indexOf(enemy), 1);
if (temp) {
this.pool.recover('new_enemy', enemy); // 回收敌人到对象池
}
}
/**
* 回收子弹,进入对象池
* 此后不进入帧循环
* @param {Object} bullet - 要回收的子弹对象
*/
removeBullets(bullet) {
const temp = this.bullets.splice(this.bullets.indexOf(bullet), 1);
if (temp) {
this.pool.recover('whip_bullet', bullet); // 回收子弹到对象池
}
}
}