// "New pool" object size poolmanager.create ("New pool", size, object); // "New pool" coord poolmanager.spawn ("New pool",coord);
exports.my_object = function() { this.inUse = false; // this.init = function() { }; // this.spawn = function (coord) { this.inUse = true; } // this.despawn = function() { this.inUse = false; }; }
... this.fileName = "bullet1.json"; this.name = "bullet1"; this.id; this.init = function(data_id) { this.id = data_id; }; …
this.spawn = function (coord) { this.inUse = true; var obj = m_scene.get_object_by_name(this.name,this.id); m_trans.set_translation_v(obj, coord); m_scene.show_object(obj); console.log("Spawn!"); }
this.despawn = function() { this.inUse = false; var obj = m_scene.get_object_by_name(this.name,this.id); m_scene.hide_object(obj); console.log("Despawn!"); };
// var _pools = {}; var _pools_size = {};
//--------------------------------------------------- // //name - //size - //obj - exports.create = function(name, size, obj) { var temp_pool = new Array(); _pools[name]=temp_pool; _pools_size [name] = size; for (var i = 0; i < size; i++) { var clone_obj = new obj(); var data_id = m_data.load(APP_ASSETS_PATH + clone_obj.fileName,null,null,false,true); temp_pool[i] = clone_obj; clone_obj.init (data_id); }
//--------------------------------------------------- // //pool_name - //coord - exports.spawn = function(pool_name,coord) { var pool = _pools [pool_name]; var size = _pools_size [pool_name]; if(!pool[size - 1].inUse) { pool[size - 1].spawn(coord); pool.unshift(pool.pop()); } // for (var i = 0; i < size; i++) { if (!pool[i].inUse) { pool.push((pool.splice(i,1))[0]); } } };
"use strict" b4w.register("g_pool_manager", function(exports, require) { // import modules used by the app var m_app = require("app"); var m_cfg = require("config"); var m_data = require("data"); // automatically detect assets path var APP_ASSETS_PATH = m_cfg.get_std_assets_path() + "Danger Space/"; // var _pools = {}; var _pools_size = {}; //--------------------------------------------------- // //name - //size - //obj - exports.create = function(name, size, obj) { var temp_pool = new Array(); _pools[name]=temp_pool; _pools_size [name] = size; for (var i = 0; i < size; i++) { var clone_obj = new obj(); var data_id = m_data.load(APP_ASSETS_PATH + clone_obj.fileName,null,null,false,true); temp_pool[i] = clone_obj; clone_obj.init (data_id); } //--------------------------------------------------- // //pool_name - //coord - exports.spawn = function(pool_name,coord) { var pool = _pools [pool_name]; var size = _pools_size [pool_name]; if(!pool[size - 1].inUse) { pool[size - 1].spawn(coord); pool.unshift(pool.pop()); } // for (var i = 0; i < size; i++) { if (!pool[i].inUse) { pool.push((pool.splice(i,1))[0]); } } }; //--------------------------------------------------- // //pool_name - //obj - exports.despawn = function(pool_name, obj) { }; } });
"use strict" b4w.register("g_bullet", function(exports, require) { // import modules used by the app var m_cfg = require("config"); var m_data = require("data"); var m_ctl = b4w.require("controls"); var m_trans = b4w.require("transform"); var m_scene = b4w.require("scenes"); var m_vec3 = require("vec3"); var m_obj = b4w.require("objects"); var m_poolmanager = b4w.require("g_pool_manager"); var m_phy = require("physics"); var m_vars = require("g_vars"); //------------------------------------------------------------------------------ exports.Bullet = function() { this.inUse = false; this.fileName = "bullet1.json"; this.name = "bullet1"; this.id; this.init = function(data_id) { this.id = data_id; }; this.object = function(data_id) { return m_scene.get_object_by_name(this.name,this.id); }; this.spawn = function (coord) { this.inUse = true; var obj = m_scene.get_object_by_name(this.name,this.id); m_trans.set_translation_v(obj, coord); m_scene.show_object(obj); console.log("Spawn!"); // (2 ) var timer_bulet1 = m_ctl.create_timer_sensor(2,false); m_ctl.create_sensor_manifold(this, "TIMER_B", m_ctl.CT_SHOT, [timer_bulet1], null, timerB_cb); } // this.despawn = function() { this.inUse = false; var obj = m_scene.get_object_by_name(this.name,this.id); m_scene.hide_object(obj); console.log("Despawn!"); }; } function timerB_cb (obj, id) { obj.despawn(); } });
"use strict" b4w.register("g_player", function(exports, require) { // import modules used by the app var m_trans = b4w.require("transform"); var m_poolmanager = b4w.require("g_pool_manager"); var m_bullet1 = b4w.require("g_bullet"); exports.init = function() { // "Bullet1", 10 m_bullet.Bullet m_poolmanager.create ("Bullet1", 10, m_bullet.Bullet); // var gun1_coord = m_trans.get_translation(_gun1); m_poolmanager.spawn ("Bullet1",gun1_coord); // "Bullet2", 20 m_bullet.Bullet m_poolmanager.create ("Bullet2", 20, m_bullet.Bullet); // var gun1_coord = m_trans.get_translation(_gun1); m_poolmanager.spawn ("Bullet1",gun1_coord); m_poolmanager.spawn ("Bullet1",gun1_coord); m_poolmanager.spawn ("Bullet1",gun1_coord); } } });
Source: https://habr.com/ru/post/275307/
All Articles