diff --git a/scripts/Globals.js b/scripts/Globals.js index 1a21a055..b99ec19c 100644 --- a/scripts/Globals.js +++ b/scripts/Globals.js @@ -858,7 +858,7 @@ Audio_WebAudio=1, g_CurrentView =null, g_ParticleTextures =null, g_ParticleTypes =null, - g_ParticleSystems =null, + g_ParticleSystemManager =null, g_ActiveGrids =null, g_ActivePriorityQueues =null, g_BufferStorage = null, @@ -1166,7 +1166,7 @@ function InitAboyneGlobals() { g_ParticleChanges = []; // When a particle system changes depth g_ParticleTextures = []; g_ParticleTypes = []; - g_ParticleSystems = []; + g_ParticleSystemManager = new yyParticleSystemManager(); // @if function("ds_grid_*") g_ActiveGrids = new yyAllocate(5); // @endif diff --git a/scripts/_GameMaker.js b/scripts/_GameMaker.js index 5da79b1e..24d182db 100644 --- a/scripts/_GameMaker.js +++ b/scripts/_GameMaker.js @@ -1603,7 +1603,7 @@ function StartGame() function Run_EndGame(_reset) { g_ParticleTypes = []; - g_ParticleSystems = []; + g_ParticleSystemManager.RemoveAll(); types_created = 0; // Clear all instances - including persistant ones. diff --git a/scripts/functions/Function_Layers.js b/scripts/functions/Function_Layers.js index e4011bb5..32b11d70 100644 --- a/scripts/functions/Function_Layers.js +++ b/scripts/functions/Function_Layers.js @@ -672,9 +672,11 @@ LayerManager.prototype.BuildTilemapElementRuntimeData = function( _room ,_layer, LayerManager.prototype.BuildParticleElementRuntimeData = function( _room ,_layer,_element) { // @if feature("particles") - if (_element.m_ps != -1 && _element.m_systemID == -1) + if (_element.m_ps != -1) { - CParticleSystem.Get(_element.m_ps).MakeInstance(_layer.m_id, false, _element); + var particleSystem = g_ParticleSystemManager.Get(_element.m_systemID); + if (!particleSystem) + CParticleSystem.Get(_element.m_ps).MakeInstance(_layer.m_id, false, _element, _element.m_systemID); } // @endif _element.m_bRuntimeDataInitialised=true; @@ -2028,7 +2030,7 @@ LayerManager.prototype.BuildRoomLayers = function(_room,_roomLayers) var pParticle = pLayer.particles[i]; var NewParticle = new CLayerParticleElement(); - NewParticle.m_systemID = -1; + NewParticle.m_systemID = pParticle.sId; NewParticle.m_ps = pParticle.sIndex; NewParticle.m_imageScaleX = pParticle.sXScale; NewParticle.m_imageScaleY = pParticle.sYScale; @@ -2037,7 +2039,7 @@ LayerManager.prototype.BuildRoomLayers = function(_room,_roomLayers) NewParticle.m_imageAlpha = ((pParticle.sBlend>>24)&0xff) / 255.0; NewParticle.m_x = pParticle.sX; NewParticle.m_y = pParticle.sY; - NewParticle.m_pName = pParticle.sName; + NewParticle.m_name = pParticle.sName; this.AddNewElement(_room, NewLayer, NewParticle, false); } @@ -3124,6 +3126,188 @@ function layer_sprite_get_y( arg1) }; +// Particle element funtions +function layerParticleGetElement(_paricle_element_id) +{ + var room = g_pLayerManager.GetTargetRoomObj(); + var el = g_pLayerManager.GetElementFromID(room, _paricle_element_id); + if ((el != null) && (el.m_type === eLayerElementType_ParticleSystem)) return el; + return null; +} + +function layer_particle_xscale(_paricle_element_id, _scale) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_imageScaleX = yyGetReal(_scale); + } +} + +function layer_particle_yscale(_paricle_element_id, _scale) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_imageScaleY = yyGetReal(_scale); + } +} + +function layer_particle_angle(_paricle_element_id, _angle) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_imageAngle = yyGetReal(_angle); + } +} + +function layer_particle_blend(_paricle_element_id, _col) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_imageBlend = ConvertGMColour(yyGetInt32(_col)); + } +} + +function layer_particle_alpha(_paricle_element_id, _alpha) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_imageAlpha = yyGetReal(_alpha); + } +} + +function layer_particle_x(_paricle_element_id, _x) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_x = yyGetReal(_x); + } +} + +function layer_particle_y(_paricle_element_id, _y) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + el.m_y = yyGetReal(_y); + } +} + +function layer_particle_get_id(_layerid, _particlename) +{ + var room = g_pLayerManager.GetTargetRoomObj(); + if (room === null) return -1; + + var layer = layerGetObj(room, _layerid); + + if (layer != null) + { + var element = g_pLayerManager.GetElementFromName(layer, yyGetString(_particlename)); + if (element != null && element.m_type == eLayerElementType_ParticleSystem) + { + return element.m_id; + } + } + return -1; +} + +function layer_particle_get_instance(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + var id = -1; + if (el != null) + { + id = el.m_systemID; + } + return MAKE_REF(REFID_PART_SYSTEM, (id != -1) ? id : 0xffffffff); +} + +function layer_particle_get_system(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + var id = -1; + if (el != null) + { + id = el.m_ps; + } + return MAKE_REF(REFID_PARTICLESYSTEM, (id != -1) ? id : 0xffffffff); +} + +function layer_particle_get_xscale(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_imageScaleX; + } + return 1; +} + +function layer_particle_get_yscale(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_imageScaleY; + } + return 1; +} + +function layer_particle_get_angle(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_imageAngle; + } + return 0; +} + +function layer_particle_get_blend(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_imageBlend; + } + return 0; +} + +function layer_particle_get_alpha(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_imageAlpha; + } + return 0; +} + +function layer_particle_get_x(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_x; + } + return 0; +} + +function layer_particle_get_y(_paricle_element_id) +{ + var el = layerParticleGetElement(_paricle_element_id); + if (el != null) + { + return el.m_y; + } + return 0; +} + // Text element functions function layerTextGetElement(_text_element_id) { diff --git a/scripts/functions/Function_Particles.js b/scripts/functions/Function_Particles.js index a6a5b97e..9583c827 100644 --- a/scripts/functions/Function_Particles.js +++ b/scripts/functions/Function_Particles.js @@ -15,6 +15,11 @@ // // ********************************************************************************************************************** +function PartSystemInstanceExists(_id) +{ + return (g_ParticleSystemManager.Get(yyGetInt32(_id)) != null); +} + function GetParticleSystemResourceIndex(_arg, _optional) { return yyGetRef(_arg, REFID_PARTICLESYSTEM, CParticleSystem.instances.length, CParticleSystem.instances, _optional); @@ -22,7 +27,7 @@ function GetParticleSystemResourceIndex(_arg, _optional) function GetParticleSystemInstanceIndex(_arg, _optional) { - return yyGetRef(_arg, REFID_PART_SYSTEM, g_ParticleSystems.length, g_ParticleSystems, _optional); + return yyGetRefFn(_arg, REFID_PART_SYSTEM, PartSystemInstanceExists, _optional); } function GetParticleEmitterIndex(_ps, _arg, _optional) @@ -31,7 +36,7 @@ function GetParticleEmitterIndex(_ps, _arg, _optional) var arr = null; if (!_optional) { - arr = g_ParticleSystems[_ps].emitters; + arr = g_ParticleSystemManager.Get(_ps).emitters; count = arr.length; } return yyGetRef(_arg, REFID_PART_EMITTER, count, arr, _optional); @@ -86,7 +91,7 @@ function ParticleSystemGetInfoImpl(_ind, _isInstance) { // Particle system INSTANCE _ind = GetParticleSystemInstanceIndex(_ind); - var pPS = g_ParticleSystems[_ind]; + var pPS = g_ParticleSystemManager.Get(_ind); if (pPS != null) { pPSI = new GMLObject(); @@ -508,9 +513,9 @@ function part_system_drawit(_ind) { ps = GetParticleSystemInstanceIndex(_ind); - if (!ParticleSystem_Exists(ps)) return; + var pSystem = g_ParticleSystemManager.Get(ps); - var pSystem = g_ParticleSystems[ps]; + if (pSystem == null) return; var matWorldOld = WebGL_GetMatrix(MATRIX_WORLD); diff --git a/scripts/yyParticle.js b/scripts/yyParticle.js index ea292d76..44c04da3 100644 --- a/scripts/yyParticle.js +++ b/scripts/yyParticle.js @@ -256,6 +256,7 @@ function yyParticleSystem() /**@constructor*/ function ParticleSystem_ClearClass() { + this.id = -1; this.m_resourceID = -1; // the id of the particle system resource that this system was created from this.created = false; // whether created @@ -399,16 +400,18 @@ CParticleSystem.prototype.GetIndex = function () /// /// The layer to use. A new one is created if not defined. /// +/// /// The index of the created instance. -CParticleSystem.prototype.MakeInstance = function (_layerID, _persistent, _pParticleEl) +CParticleSystem.prototype.MakeInstance = function (_layerID, _persistent, _pParticleEl, _systemID) { if (_layerID === undefined) _layerID = -1; if (_persistent === undefined) _persistent = true; if (_pParticleEl === undefined) _pParticleEl = null; + if (_systemID === undefined) _systemID = -1; var ps = (_pParticleEl == null) - ? ParticleSystem_Create(_layerID, _persistent) - : ParticleSystem_Create_OnLayer(_layerID, _persistent, _pParticleEl); + ? ParticleSystem_Create(_layerID, _persistent, _systemID) + : ParticleSystem_Create_OnLayer(_layerID, _persistent, _pParticleEl, _systemID); if (ps == -1) { @@ -416,7 +419,7 @@ CParticleSystem.prototype.MakeInstance = function (_layerID, _persistent, _pPart return ps; } - var system = g_ParticleSystems[ps]; + var system = g_ParticleSystemManager.Get(ps); system.m_resourceID = this.index; system.oldtonew = (this.drawOrder == 0); system.globalSpaceParticles = this.globalSpaceParticles; @@ -463,6 +466,210 @@ CParticleSystem.prototype.MakeInstance = function (_layerID, _persistent, _pPart return ps; }; + +PARTICLE_ID_ROOM_MIN = 0x00800000; + +function yyParticleSystemManager() +{ + this.particlesRoom = []; // Array of particle system instances placed into a room from the IDE + this.cached = null; // Last particle system looked up with Find in Get + this.particlesRuntime = []; // Particle system instances created on runtime +} + +// For user particle systems! +yyParticleSystemManager.prototype.GetNext = function () +{ + var id = 0; + while ((id < this.particlesRuntime.length) && (this.particlesRuntime[id] != null)) { id = id + 1; } + if (id == this.particlesRuntime.length) + { + this.particlesRuntime.length = id + 1; + this.particlesRuntime.push(null); + } + + // assert(id < PARTICLE_ID_ROOM_MIN); // Run out of user particle system IDs! + + this.particlesRuntime[id] = new yyParticleSystem(); + this.particlesRuntime[id].id = id; + + return id; +}; + +// Only for room particle systems! +yyParticleSystemManager.prototype.Insert = function (id) +{ + // assert(id >= PARTICLE_ID_ROOM_MIN); + // assert(!this.Find(id)); + + var pos; + if (false) + { + // Linear search insert position + pos = 0; + while (pos < this.particlesRoom.length && this.particlesRoom[pos].id < id) + { + ++pos; + } + } + else + { + // Binary search insert position + var left = 0; + var right = this.particlesRoom.length; + while (left < right) + { + var mid = ~~(left + (right - left) / 2); + if (this.particlesRoom[mid].id < id) + { + left = mid + 1; + } + else + { + right = mid; + } + } + pos = left; + } + + this.particlesRoom.splice(pos, 0, new yyParticleSystem()); + this.particlesRoom[pos].id = id; +}; + +yyParticleSystemManager.prototype.Remove = function (id) +{ + // We don't have negative IDs + if (id < 0) + { + return; + } + + // Is a runtime created particle system instance + if (id < PARTICLE_ID_ROOM_MIN) + { + if (id >= this.particlesRuntime.length) + { + // Outside of range of indices + return; + } + this.particlesRuntime[id] = null; + return; + } + + // Is a IDE-placed particle system instance + if (this.cached && this.cached.id == id) + { + this.cached = null; + } + + var outData = { system: null, position: 0 }; + + if (!this.Find(id, outData)) + { + return; + } + + this.particlesRoom.splice(outData.position, 1); +}; + +yyParticleSystemManager.prototype.RemoveAll = function () +{ + this.particlesRoom = []; + this.cached = null; + this.particlesRuntime = []; +}; + +// Returns a particle system instance with given ID or NULL if it doesn't exist. +yyParticleSystemManager.prototype.Get = function (id) +{ + // We don't have negative indices + if (id < 0) + { + return null; + } + + // Is a particle system instance created on runtime + if (id < PARTICLE_ID_ROOM_MIN) + { + if (id >= this.particlesRuntime.length) + { + // Outside of range of indices + return null; + } + return this.particlesRuntime[id]; + } + + // Is a IDE-placed particle system instance + if (this.cached && this.cached.id == id) + { + return this.cached; + } + + var outData = { system: null, position: 0 }; + if (this.Find(id, outData)) + { + this.cached = outData.system; + } + return outData.system; +}; + +// Returns total number of existing particles +yyParticleSystemManager.prototype.Count = function () +{ + return (this.particlesRoom.length + this.particlesRuntime.length); +}; + +// Returns a particle system instance at given index. Can be NULL, even when not outside of range of indices! +yyParticleSystemManager.prototype.At = function (index) +{ + if (index < 0 || index >= this.Count()) + { + return null; + } + + if (index >= this.particlesRuntime.length) + { + return this.particlesRoom[index - this.particlesRuntime.length]; + } + + return this.particlesRuntime[index]; +}; + +yyParticleSystemManager.prototype.Find = function (id, outData) +{ + if (id < PARTICLE_ID_ROOM_MIN + || this.particlesRoom.length == 0) + { + return false; + } + + var left = 0; + var right = this.particlesRoom.length; + + while (left < right) + { + var mid = ~~(left + (right - left) / 2); + + if (this.particlesRoom[mid].id == id) + { + if (outData !== undefined) + { + outData.system = this.particlesRoom[mid]; + outData.position = mid; + } + return true; + } + else if (this.particlesRoom[mid].id < id) + { + left = mid + 1; + } + else + { + right = mid; + } + } + + return false; +}; // @endif particles // ############################################################################################# @@ -743,7 +950,7 @@ function CreateParticle(_system, _x, _y, _parttype) if (_system.globalSpaceParticles) { - Result.dir += RAD(Math.atan2(_system.matrix.m[_21], _system.matrix.m[_11])); + Result.dir += (180/Pi) * (Math.atan2(_system.matrix.m[_21], _system.matrix.m[_11])); } Result.ran = YYRandom(100000); @@ -1515,9 +1722,10 @@ function ParticleSystem_Emitter_Create(_ps) { _ps = yyGetInt32(_ps); - if (!ParticleSystem_Exists(_ps)) return -1; + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys == null) + return -1; - var pPartSys = g_ParticleSystems[_ps]; var ind = 0; var emitter = null; @@ -1565,7 +1773,7 @@ function ParticleSystem_Emitter_Destroy(_ps, _ind) if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return false; - var emitter = g_ParticleSystems[_ps].emitters[_ind]; + var emitter = g_ParticleSystemManager.Get(_ps).emitters[_ind]; emitter.created = false; emitter.zombie = true; @@ -1586,10 +1794,13 @@ function ParticleSystem_Emitter_Destroy(_ps, _ind) function ParticleSystem_Emitter_DestroyAll(_ps) { _ps = yyGetInt32(_ps); - if (!ParticleSystem_Exists(_ps)) return false; - for (var i = g_ParticleSystems[_ps].emitters.length - 1; i >= 0; --i) + var particleSystem = g_ParticleSystemManager.Get(_ps); + if (particleSystem != null) { - ParticleSystem_Emitter_Destroy(_ps, i); + for (var i = particleSystem.emitters.length - 1; i >= 0; --i) + { + ParticleSystem_Emitter_Destroy(_ps, i); + } } return true; } @@ -1610,10 +1821,11 @@ function ParticleSystem_Emitter_Enable(_ps, _ind, _enable) _ps = yyGetInt32(_ps); _ind = yyGetInt32(_ind); - if (!ParticleSystem_Exists(_ps)) return; + var particleSystem = g_ParticleSystemManager.Get(_ps); + if (particleSystem == null) return; if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - g_ParticleSystems[_ps].emitters[_ind].enabled = yyGetBool(_enable); + particleSystem.emitters[_ind].enabled = yyGetBool(_enable); } // ############################################################################################# @@ -1632,9 +1844,9 @@ function ParticleSystem_Emitter_Exists(_ps, _ind) _ps = yyGetInt32(_ps); _ind = yyGetInt32(_ind); - if (!ParticleSystem_Exists(_ps)) return false; + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys == null) return false; - var pPartSys = g_ParticleSystems[_ps]; if (_ind < 0 || _ind >= pPartSys.emitters.length) return false; var pEmitter = pPartSys.emitters[_ind]; @@ -1662,7 +1874,7 @@ function ParticleSystem_Emitter_Clear(_ps, _ind) if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return false; - g_ParticleSystems[_ps].emitters[_ind].Reset(); + g_ParticleSystemManager.Get(_ps).emitters[_ind].Reset(); } @@ -1691,7 +1903,7 @@ function ParticleSystem_Emitter_Region(_ps, _ind, _xmin, _xmax, _ymin, _ymax, _s if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - var pEmitter = g_ParticleSystems[_ps].emitters[_ind]; + var pEmitter = g_ParticleSystemManager.Get(_ps).emitters[_ind]; pEmitter.xmin = yyGetReal(_xmin); pEmitter.xmax = yyGetReal(_xmax); @@ -1877,7 +2089,7 @@ function ParticleSystem_Emitter_Burst(_ps, _ind, _ptype, _numb) if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - var system = g_ParticleSystems[_ps]; + var system = g_ParticleSystemManager.Get(_ps); var emitter = system.emitters[_ind]; if (!emitter.enabled) return; @@ -1912,7 +2124,7 @@ function ParticleSystem_Emitter_Stream( _ps, _ind, _ptype, _numb) if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - var pEmitter = g_ParticleSystems[_ps].emitters[_ind]; + var pEmitter = g_ParticleSystemManager.Get(_ps).emitters[_ind]; pEmitter.parttype = yyGetInt32(_ptype); pEmitter.number = yyGetReal(_numb); @@ -1946,7 +2158,7 @@ function EmitterRandomizeDelay(_emitter) function ParticleSystem_Emitter_Delay( _ps, _ind, _delay_min, _delay_max, _delay_unit) { if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - var pEmitter = g_ParticleSystems[_ps].emitters[_ind]; + var pEmitter = g_ParticleSystemManager.Get(_ps).emitters[_ind]; pEmitter.delayMin = yyGetInt32(_delay_min); pEmitter.delayMax = yyGetReal(_delay_max); pEmitter.delayUnit = yyGetReal(_delay_unit); @@ -1981,7 +2193,7 @@ function EmitterRandomizeInterval(_emitter) function ParticleSystem_Emitter_Interval( _ps, _ind, _interval_min, _interval_max, _interval_unit) { if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - var pEmitter = g_ParticleSystems[_ps].emitters[_ind]; + var pEmitter = g_ParticleSystemManager.Get(_ps).emitters[_ind]; pEmitter.intervalMin = yyGetInt32(_interval_min); pEmitter.intervalMax = yyGetReal(_interval_max); pEmitter.intervalUnit = yyGetReal(_interval_unit); @@ -2001,7 +2213,7 @@ function ParticleSystem_Emitter_Relative(_ps, _ind, _enable) if (!ParticleSystem_Emitter_Exists(_ps, _ind)) return; - g_ParticleSystems[_ps].emitters[_ind].relative = yyGetBool(_enable); + g_ParticleSystemManager.Get(_ps).emitters[_ind].relative = yyGetBool(_enable); } @@ -2021,7 +2233,7 @@ function ParticleSystem_Emitter_Relative(_ps, _ind, _enable) // ############################################################################################# function ParticleSystem_Particles_Create(_ps, _x, _y, _parttype, _numb) { - var system = g_ParticleSystems[_ps]; + var system = g_ParticleSystemManager.Get(_ps); var em = -1; for (var i = 0; i < system.emitters.length; ++i) @@ -2058,7 +2270,9 @@ function ParticleSystem_Particles_Create(_ps, _x, _y, _parttype, _numb) // ############################################################################################# function ParticleSystem_Particles_Create_Color( _ps, _x, _y, _parttype, _col, _numb) { - if ( !ParticleSystem_Exists(_ps) ) { + var system = g_ParticleSystemManager.Get(_ps); + + if (system == null) { console.log( "part_particles_create :: particle system does not exist!" ); return; } // end if @@ -2067,8 +2281,6 @@ function ParticleSystem_Particles_Create_Color( _ps, _x, _y, _parttype, _col, _n return; } // end if - var system = g_ParticleSystems[_ps]; - var em = -1; for (var i = 0; i < system.emitters.length; ++i) { @@ -2106,7 +2318,9 @@ function ParticleSystem_Particles_Create_Color( _ps, _x, _y, _parttype, _col, _n // ############################################################################################# function ParticleSystem_Particles_Burst(_ps, _x, _y, _partsys) { - if (!ParticleSystem_Exists(_ps)) { + var system = g_ParticleSystemManager.Get(_ps); + + if (system == null) { console.log("part_particles_burst :: particle system does not exist!"); return; } // end if @@ -2118,7 +2332,6 @@ function ParticleSystem_Particles_Burst(_ps, _x, _y, _partsys) return; } // end if - var system = g_ParticleSystems[_ps]; var emitterCount = asset.emitters.length; var emittersEnabled = []; @@ -2156,8 +2369,10 @@ function ParticleSystem_Particles_Burst(_ps, _x, _y, _partsys) // ############################################################################################# function ParticleSystem_Particles_Clear(_ps) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return false; + _ps = yyGetInt32(_ps); + + var pPartSys = g_ParticleSystemManager.Get(_ps); + if(pPartSys == null) return false; for (var i = pPartSys.emitters.length - 1; i >= 0; --i) { @@ -2196,8 +2411,9 @@ function ParticleSystem_Particles_Delete(_pParticles, _index) // ############################################################################################# function ParticleSystem_Particles_Count( _ps ) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return 0 ; + _ps = yyGetInt32(_ps); + var pPartSys = g_ParticleSystemManager.Get(_ps); + if(pPartSys == null) return 0 ; var count = 0; for (var i = pPartSys.emitters.length - 1; i >= 0; --i) @@ -2253,28 +2469,32 @@ function ParticleSystem_Create_GetLayer(_layerID) return pPartEl; } -function ParticleSystem_Create_OnLayer(_layerID, _persistent, _pPartEl) +function ParticleSystem_Create_OnLayer(_layerID, _persistent, _pPartEl, _systemID) { - var index; - for (index = 0; index < g_ParticleSystems.length; ++index) + if (_systemID === undefined) _systemID = -1; + + var index = -1; + if (_systemID == -1) { - if (g_ParticleSystems[index] == null) - { - break; - } + index = g_ParticleSystemManager.GetNext(); } - g_ParticleSystems[index] = new yyParticleSystem(); - g_ParticleSystems[index].id = index; // remember the ID - g_ParticleSystems[index].m_elementID = -1; + else + { + index = _systemID; + g_ParticleSystemManager.Insert(index); + } + + var particleSystem = g_ParticleSystemManager.Get(index); + particleSystem.m_elementID = -1; ParticleSystem_Clear(index, false); _pPartEl.m_systemID = index; - g_ParticleSystems[index].m_elementID = _pPartEl.m_id; - g_ParticleSystems[index].m_volatile = !_persistent; + particleSystem.m_elementID = _pPartEl.m_id; + particleSystem.m_volatile = !_persistent; if (_layerID != -1) { - //g_ParticleSystems[index].m_origLayerID = _layerID; - g_ParticleSystems[index].depth = _pPartEl.m_layer.depth; + //particleSystem.m_origLayerID = _layerID; + particleSystem.depth = _pPartEl.m_layer.depth; } return index; @@ -2289,25 +2509,27 @@ function ParticleSystem_Create_OnLayer(_layerID, _persistent, _pPartEl) /// ID of the new particle system /// // ############################################################################################# -function ParticleSystem_Create(_layerID,_persistent) +function ParticleSystem_Create(_layerID, _persistent, _systemID) { if (_layerID == undefined) _layerID = -1; else _layerID = yyGetInt32(_layerID); - if (_persistent == undefined) _persistent = true; else _persistent = yyGetBool(_persistent); + + if (_systemID === undefined) + _systemID = -1; var pPartEl = ParticleSystem_Create_GetLayer(_layerID); if (pPartEl == null) return -1; - return ParticleSystem_Create_OnLayer(_layerID, _persistent, pPartEl); + return ParticleSystem_Create_OnLayer(_layerID, _persistent, pPartEl, _systemID); } // ############################################################################################# @@ -2322,9 +2544,7 @@ function ParticleSystem_Create(_layerID,_persistent) // ############################################################################################# function ParticleSystem_Exists( _ps ) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return false; - return true; + return (g_ParticleSystemManager.Get(yyGetInt32(_ps)) != null); } @@ -2343,18 +2563,19 @@ function ParticleSystem_Destroy( _ps ) { _ps = yyGetInt32(_ps); - var pPartSys = g_ParticleSystems[_ps]; - if (pPartSys == null || pPartSys == undefined) return; + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys == null) return; ParticleSystem_Clear(_ps, false); if (g_isZeus) { // Remove this from any layer it happens to be on - g_pLayerManager.RemoveElementById(g_RunRoom, g_ParticleSystems[_ps].m_elementID, true); + g_pLayerManager.RemoveElementById(g_RunRoom, pPartSys.m_elementID, true); } - g_ParticleSystems[_ps] = null; + g_ParticleSystemManager.Remove(_ps); + return true; } @@ -2366,12 +2587,16 @@ function ParticleSystem_Destroy( _ps ) // ############################################################################################# function ParticleSystem_DestroyAll() { - for (var i = 0; i < g_ParticleSystems.length; i++) - { - ParticleSystem_Destroy(i); - } - - g_ParticleSystems = []; + var count = g_ParticleSystemManager.Count(); + var index = 0; + for (var i = 0; i < count; i++) + { + var particleSystem = g_ParticleSystemManager.At(index++); + if (!particleSystem) continue; + if (particleSystem.id >= PARTICLE_ID_ROOM_MIN) --index; + ParticleSystem_Destroy(particleSystem.id); + } + g_ParticleSystemManager.RemoveAll(); } @@ -2389,8 +2614,8 @@ function ParticleSystem_Clear(_ps, _reset_element_depth) { _ps = yyGetInt32(_ps); - var pPartSys = g_ParticleSystems[_ps]; - if (pPartSys == null || pPartSys == undefined) return; + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys == null) return; pPartSys.emitters = []; @@ -2429,11 +2654,12 @@ function ParticleSystem_Clear(_ps, _reset_element_depth) function ParticleSystem_GetLayer(_ps) { - if (!ParticleSystem_Exists(_ps)) - return -1; + var particleSystem = g_ParticleSystemManager.Get(_ps); + if (particleSystem == null) + return -1; var pLayer = null; - var elandlay = g_pLayerManager.GetElementAndLayerFromID(g_RunRoom, g_ParticleSystems[_ps].m_elementID); + var elandlay = g_pLayerManager.GetElementAndLayerFromID(g_RunRoom, particleSystem.m_elementID); if (elandlay == null) return -1; pLayer = elandlay.layer; @@ -2447,16 +2673,17 @@ function ParticleSystem_GetLayer(_ps) function ParticleSystem_Layer(_ps,_layerID) { - if (!ParticleSystem_Exists(_ps)) + var particleSystem = g_ParticleSystemManager.Get(_ps); + if (particleSystem == null) return; if (g_isZeus) { // Remove this from any layer it happens to be on and re-add it again - g_pLayerManager.RemoveElementById(g_RunRoom, g_ParticleSystems[_ps].m_elementID, true); + g_pLayerManager.RemoveElementById(g_RunRoom, particleSystem.m_elementID, true); var pPartEl = new CLayerParticleElement(); pPartEl.m_systemID = _ps; - //g_ParticleSystems[_ps].m_origLayerID = _layerID; // reset the associated layer id + //particleSystem.m_origLayerID = _layerID; // reset the associated layer id var room = g_pLayerManager.GetTargetRoomObj(); if (room != null) @@ -2468,18 +2695,18 @@ function ParticleSystem_Layer(_ps,_layerID) if (room == g_RunRoom) { - g_ParticleSystems[_ps].m_elementID = g_pLayerManager.AddNewElement(g_RunRoom, layer, pPartEl, true); - g_ParticleSystems[_ps].depth = layer.depth; // reset depth - if (g_ParticleSystems[_ps].m_elementID == -1) + particleSystem.m_elementID = g_pLayerManager.AddNewElement(g_RunRoom, layer, pPartEl, true); + particleSystem.depth = layer.depth; // reset depth + if (particleSystem.m_elementID == -1) { g_pLayerManager.RemoveElementById(g_RunRoom, pPartEl.m_id, true); } } } - if (g_ParticleSystems[_ps].m_elementID == -1) + if (particleSystem.m_elementID == -1) { - g_ParticleSystems[_ps].m_elementID = g_pLayerManager.AddNewElementAtDepth(g_RunRoom, g_ParticleSystems[_ps].depth, pPartEl, true, true); + particleSystem.m_elementID = g_pLayerManager.AddNewElementAtDepth(g_RunRoom, particleSystem.depth, pPartEl, true, true); } } } @@ -2498,8 +2725,9 @@ function ParticleSystem_Layer(_ps,_layerID) // ############################################################################################# function ParticleSystem_GlobalSpace(_ps, _enable) { - if (!ParticleSystem_Exists(_ps)) return; - g_ParticleSystems[_ps].globalSpaceParticles = _enable; + var particleSystem = g_ParticleSystemManager.Get(_ps); + if (particleSystem == null) return; + particleSystem.globalSpaceParticles = _enable; } // ############################################################################################# @@ -2513,16 +2741,15 @@ function ParticleSystem_GlobalSpace(_ps, _enable) /// // ############################################################################################# function ParticleSystem_ClearParticles() -{ - for (var ps = 0; ps < g_ParticleSystems.length; ps++) - { - if (!g_ParticleSystems.hasOwnProperty(ps)) continue; - - var pPartSys = g_ParticleSystems[ps]; - if (pPartSys) { - pPartSys.particles = []; - } - } +{ + var count = g_ParticleSystemManager.Count(); + for (var ps = 0; ps < count; ++ps) + { + var pPartSys = g_ParticleSystemManager.At(ps); + if (pPartSys) { + pPartSys.particles = []; + } + } } @@ -2539,9 +2766,8 @@ function ParticleSystem_ClearParticles() // ############################################################################################# function ParticleSystem_DrawOrder(_ps, _oldtonew) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; - + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.oldtonew = _oldtonew; } @@ -2558,8 +2784,8 @@ function ParticleSystem_Depth(_ps, _depth) { _ps = yyGetInt32(_ps); - var pPartSys = g_ParticleSystems[_ps]; - if( pPartSys ==null || pPartSys==undefined ) return; + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys == null) return; pPartSys.id = _ps; pPartSys.depth = yyGetReal(_depth); @@ -2573,8 +2799,8 @@ function ParticleSystem_Depth(_ps, _depth) g_pLayerManager.RemoveElementById(g_RunRoom, pPartSys.m_elementID, true); var pPartEl = new CLayerParticleElement(); pPartEl.m_systemID = _ps; - g_ParticleSystems[_ps].m_origLayerID = -1; // scrub any associated layer ID if we're manually changing depth - g_ParticleSystems[_ps].m_elementID = g_pLayerManager.AddNewElementAtDepth(g_RunRoom, g_ParticleSystems[_ps].depth, pPartEl, true, true); + g_ParticleSystemManager.m_origLayerID = -1; // scrub any associated layer ID if we're manually changing depth + g_ParticleSystemManager.m_elementID = g_pLayerManager.AddNewElementAtDepth(g_RunRoom, g_ParticleSystemManager.depth, pPartEl, true, true); }*/ } @@ -2592,8 +2818,8 @@ function ParticleSystem_Depth(_ps, _depth) // ############################################################################################# function ParticleSystem_Color(_ps, _color, _alpha) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.color = yyGetInt32(_color); pPartSys.alpha = yyGetReal(_alpha); @@ -2613,8 +2839,8 @@ function ParticleSystem_Color(_ps, _color, _alpha) // ############################################################################################# function ParticleSystem_Position(_ps, _x, _y) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.xdraw = yyGetReal(_x); pPartSys.ydraw = yyGetReal(_y); @@ -2633,8 +2859,8 @@ function ParticleSystem_Position(_ps, _x, _y) // ############################################################################################# function ParticleSystem_Angle(_ps, _angle) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.angle = yyGetReal(_angle); } @@ -2652,8 +2878,8 @@ function ParticleSystem_Angle(_ps, _angle) // ############################################################################################# function ParticleSystem_AutomaticUpdate( _ps, _automatic) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.automaticupdate = yyGetBool(_automatic); } @@ -2671,9 +2897,8 @@ function ParticleSystem_AutomaticUpdate( _ps, _automatic) // ############################################################################################# function ParticleSystem_AutomaticDraw(_ps, _automatic) { - var pPartSys = g_ParticleSystems[yyGetInt32(_ps)]; - if( pPartSys ==null || pPartSys==undefined ) return; - + var pPartSys = g_ParticleSystemManager.Get(yyGetInt32(_ps)); + if (pPartSys == null) return; pPartSys.automaticdraw = yyGetBool(_automatic); } @@ -2696,7 +2921,7 @@ function HandleLife( _ps, _em ) var i = 0; var numb = 0; - var pPartSys = g_ParticleSystems[_ps]; + var pPartSys = g_ParticleSystemManager.Get(_ps); var pEmitter = pPartSys.emitters[_em]; var pParticles = pEmitter.particles; @@ -2772,7 +2997,7 @@ function HandleMotion( _ps, _em ) var rd = 0.0; var rs = 0.0; - var pPartSys = g_ParticleSystems[_ps]; + var pPartSys = g_ParticleSystemManager.Get(_ps); var pParticles = pPartSys.emitters[_em].particles; for( i=0; i= PARTICLE_ID_ROOM_MIN) + --index; + + ParticleSystem_Destroy(pSystem.id); persistentsystemlayernames[i] = null; continue; } @@ -3389,17 +3625,13 @@ function ParticleSystem_RemoveAllFromLayers() function ParticleSystem_AutoDraw(_ps) { - _ps = yyGetInt32(_ps); - - if (true == ParticleSystem_Exists(_ps)) { - var pPartSys = g_ParticleSystems[_ps]; - - if (pPartSys != null) { - if (pPartSys.automaticdraw) { - ParticleSystem_Draw(_ps); - } - } - } + _ps = yyGetInt32(_ps); + var pPartSys = g_ParticleSystemManager.Get(_ps); + if (pPartSys != null) { + if (pPartSys.automaticdraw) { + ParticleSystem_Draw(_ps); + } + } }; @@ -3417,10 +3649,10 @@ function ParticleSystem_AutoDraw(_ps) { function ParticleSystem_LargestDepth() { var Result = -1000000000; - - for(var i=0 ; i0) { if (pPartSys.automaticdraw) @@ -3447,10 +3679,10 @@ function ParticleSystem_LargestDepth() function ParticleSystem_NextDepth(_d) { var Result = -1000000000; - - for(var i=0 ; i0) { if (pPartSys.automaticdraw) diff --git a/scripts/yyRoom.js b/scripts/yyRoom.js index 3a2c37d4..270c137d 100644 --- a/scripts/yyRoom.js +++ b/scripts/yyRoom.js @@ -2110,9 +2110,9 @@ yyRoom.prototype.DrawLayerParticleSystem = function(_rect,_layer,_el) // @if feature("particles") var ps = _el.m_systemID; - if (!ParticleSystem_Exists(ps)) return; + var pSystem = g_ParticleSystemManager.Get(ps); - var pSystem = g_ParticleSystems[ps]; + if (pSystem == null) return; if (!pSystem.automaticdraw) return; diff --git a/scripts/yySequence.js b/scripts/yySequence.js index 529dc6cf..de18de82 100644 --- a/scripts/yySequence.js +++ b/scripts/yySequence.js @@ -6041,7 +6041,7 @@ yySequenceManager.prototype.HandleParticleTrackUpdate = function (_pEl, _pSeq, _ // Re-burst emitters when the sequence loops if (_pInst.m_wrapped) { - var pPartSys = g_ParticleSystems[ps]; + var pPartSys = g_ParticleSystemManager.Get(ps); var pEmitters = pPartSys.emitters; if (pEmitters) { diff --git a/scripts/yyTypes.js b/scripts/yyTypes.js index 6dbbdc45..a4e5b956 100644 --- a/scripts/yyTypes.js +++ b/scripts/yyTypes.js @@ -389,6 +389,16 @@ function yyGetRef(_value, _ref, _maxNum, _array, _allowOutOfRange) { return ret; } +function yyGetRefFn(_value, _ref, _fnExists, _allowOutOfRange) { + var ret = yyGetRef(_value, _ref, 0, 0, true); + if (!_allowOutOfRange) { + if (_fnExists && !_fnExists(ret)) { + yyError("invalid reference to (" + RefName(_ref) + ")"); + } + } + return ret; +} + // ############################################################################################# /// Function: /// Converts the given type to a string if required and returns the result.