UNPKG

mm_os

Version:

MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。

187 lines (172 loc) 4.61 kB
const { Drive, Manager } = require('mm_machine'); const { System } = require('../system/index.js'); const { Logic } = require('../logic/index.js'); const { Factory } = require('../factory/index.js'); const { Component } = require('../component/index.js'); const { Scene } = require('../scene/index.js'); /** * 游戏 */ class Game extends Drive { static config = { name: 'default', }; /** * 构造函数 * @param {object} config 游戏配置 * @param {object} parent 父驱动 */ constructor(config, parent = null) { super({...Game.config, ...config }, parent); // // 游戏场景集合 // this.scene = {}; // 游戏系统集合 this.system = {}; // 游戏逻辑集合 this.logic = {}; // 游戏工厂集合 this.factory = {}; // 游戏组件集合 this.component = {}; // 管理器集合 this.manager = {}; } } // /** // * 预设游戏配置 // */ // Game.prototype._preset = function () { // this.file = null; // this.sql = null; // this.cache = null; // }; /** * 初始化游戏核心 * @param {object} server 游戏服务器 * @param {object} eventer 事件分发器 * @param {object} logger 日志记录器 */ Game.prototype._initCore = async function (server, eventer, logger) { if (!server) { throw new TypeError('游戏服务器不能为空'); } else { this.getServer = function () { return server; }; } if (logger) { this.setLogger(logger); } if (eventer) { this.getEventer = function () { return eventer; }; } // 初始化管理 await this._initManager(); // 加载资源 await this._loadSources(); }; /** * 初始化管理器集合 * @returns {Promise<void>} 初始化完成 */ Game.prototype._initManager = async function () { // 创建所有管理器实例 var component = this._createManager('component', '组件', Component); var factory = this._createManager('factory', '工厂', Factory); var logic = this._createManager('logic', '逻辑', Logic); var system = this._createManager('system', '系统', System); var scene = this._createManager('scene', '场景', Scene); // 并行执行所有管理器的初始化操作 var initPromises = [ component.do('init'), factory.do('init'), logic.do('init'), system.do('init'), scene.do('init'), ]; await Promise.all(initPromises); }; /** * 创建管理器实例 * @param {string} name 管理器名称 * @param {string} title 管理器标题 * @param {Function} cls 管理器类 * @returns {object} 管理器实例 */ Game.prototype._createManager = function (name, title, cls) { let dir = this.getDir(); var manager = new Manager({ name: name, title: title, filename: name + '.json', tpl_dir: `../${name}/`.fullname(__dirname), base_dir: '', dir: `./${name}`.fullname(dir) }, this, this[name], cls); this.manager[name] = manager; return manager; }; /** * 加载资源 */ Game.prototype._loadSources = async function () { // 加载无需依赖关系,所以可以同时加载 await Promise.all([ this.manager.component.runAll('load'), this.manager.factory.runAll('load'), this.manager.logic.runAll('load'), this.manager.system.runAll('load'), this.manager.scene.runAll('load') ]); }; /** * 初始化资源 */ Game.prototype._initSources = async function () { // 初始化顺序 scene -> system -> logic -> factory -> entity -> component ,需要依赖下级,所以自下而上初始化 await this.manager.component.runAll('init', this.getLogger()); await this.manager.factory.runAll('init', this.component, this.getEventer(), this.getLogger()); await this.manager.logic.runAll('init', this.getLogger()); await this.manager.system.runAll('init', this, this.getEventer(), this.getLogger()); await this.manager.scene.runAll('init', this, this.getEventer(), this.getLogger()); }; /** * 启动核心 */ Game.prototype._startCore = async function () { // 初始化资源 await this._initSources(); // 启动资源 await this._startSources(); }; /** * 启动资源 */ Game.prototype._startSources = async function () { // 并行启动游戏系统和场景 await Promise.all([ // 启动游戏系统 this.manager.system.runAll('start'), // 启动游戏场景 this.manager.scene.runAll('start') ]); }; module.exports = { Game };