Phaser.Creature = function (game, x, y, key, mesh, animation){ if (animation === undefined) { animation = 'default'; } this.type = Phaser.CREATURE; if (!game.cache.checkJSONKey(mesh)) { console.warn('Phaser.Creature: Invalid mesh key given. Not found in Phaser.Cache'); return ; } var meshData = game.cache.getJSON(mesh); this._creature = new Creature(meshData); this.animation = new CreatureAnimation(meshData, animation, this._creature); this.manager = new CreatureManager(this._creature); this.timeDelta = 0.05; if (typeof key === 'string') { var texture = game.cache.getPixiTexture(key); } else { var texture = key; } this.texture = texture; PIXI.DisplayObjectContainer.call(this); this.dirty = true ; this.blendMode = PIXI.blendModes.NORMAL; this.creatureBoundsMin = new Phaser.Point(); this.creatureBoundsMax = new Phaser.Point(); var target = this.manager.target_creature; this.vertices = new PIXI.Float32Array(target.total_num_pts * 2); this.uvs = new PIXI.Float32Array(target.total_num_pts * 2); this.indices = new PIXI.Uint16Array((_AN_Read_length('length', target.global_indices))); for (var i = 0; i < _AN_Read_length('length', this.indices); i++ ){ this.indices[i] = target.global_indices[i]; } this.colors = new PIXI.Float32Array([1, 1, 1, 1] ); this.updateRenderData(target.global_pts, target.global_uvs); this.manager.AddAnimation(this.animation); this.manager.SetActiveAnimationName(animation, false ); Phaser.Component.Core.init.call(this, game, x, y); } ; Phaser.Creature.prototype = Object.create(PIXI.DisplayObjectContainer.prototype); Phaser.Creature.prototype.constructor = Phaser.Creature; Phaser.Component.Core.install.call(Phaser.Creature.prototype, ['Angle', 'AutoCull', 'BringToTop', 'Destroy', 'FixedToCamera', 'LifeSpan', 'Reset'] ); Phaser.Creature.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate; Phaser.Creature.prototype.preUpdateCore = Phaser.Component.Core.preUpdate; Phaser.Creature.prototype.preUpdate = function (){ if (!this.preUpdateInWorld()) { return false ; } this.manager.Update(this.timeDelta); this.updateData(); return this.preUpdateCore(); } ; Phaser.Creature.prototype._initWebGL = function (renderSession){ var gl = renderSession.gl; this._vertexBuffer = gl.createBuffer(); this._indexBuffer = gl.createBuffer(); this._uvBuffer = gl.createBuffer(); this._colorBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.DYNAMIC_DRAW); gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvs, gl.DYNAMIC_DRAW); gl.bindBuffer(gl.ARRAY_BUFFER, this._colorBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.colors, gl.STATIC_DRAW); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indices, gl.STATIC_DRAW); } ; Phaser.Creature.prototype._renderWebGL = function (renderSession){ if (!this.visible || this.alpha <= 0) { return ; } renderSession.spriteBatch.stop(); if (!this._vertexBuffer) { this._initWebGL(renderSession); } renderSession.shaderManager.setShader(renderSession.shaderManager.stripShader); this._renderCreature(renderSession); renderSession.spriteBatch.start(); } ; Phaser.Creature.prototype._renderCreature = function (renderSession){ var gl = renderSession.gl; var projection = renderSession.projection; var offset = renderSession.offset; var shader = renderSession.shaderManager.stripShader; renderSession.blendModeManager.setBlendMode(this.blendMode); gl.uniformMatrix3fv(shader.translationMatrix, false , this.worldTransform.toArray(true )); gl.uniform2f(shader.projectionVector, projection.x, - projection.y); gl.uniform2f(shader.offsetVector, - offset.x, - offset.y); gl.uniform1f(shader.alpha, this.worldAlpha); if (!this.dirty) { gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer); gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices); gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false , 0, 0); gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer); gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false , 0, 0); gl.activeTexture(gl.TEXTURE0); if (this.texture.baseTexture._dirty[gl.id]) { renderSession.renderer.updateTexture(this.texture.baseTexture); } else { gl.bindTexture(gl.TEXTURE_2D, this.texture.baseTexture._glTextures[gl.id]); } gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indexBuffer); } else { this.dirty = false ; gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false , 0, 0); gl.bindBuffer(gl.ARRAY_BUFFER, this._uvBuffer); gl.bufferData(gl.ARRAY_BUFFER, this.uvs, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(shader.aTextureCoord, 2, gl.FLOAT, false , 0, 0); gl.activeTexture(gl.TEXTURE0); if (this.texture.baseTexture._dirty[gl.id]) { renderSession.renderer.updateTexture(this.texture.baseTexture); } else { gl.bindTexture(gl.TEXTURE_2D, this.texture.baseTexture._glTextures[gl.id]); } gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indices, gl.STATIC_DRAW); } gl.drawElements(gl.TRIANGLES, _AN_Read_length('length', this.indices), gl.UNSIGNED_SHORT, 0); } ; Phaser.Creature.prototype.updateCreatureBounds = function (){ var target = this.manager.target_creature; target.ComputeBoundaryMinMax(); this.creatureBoundsMin.set(target.boundary_min[0], - target.boundary_min[1]); this.creatureBoundsMax.set(target.boundary_max[0], - target.boundary_max[1]); this.worldTransform.apply(this.creatureBoundsMin, this.creatureBoundsMin); this.worldTransform.apply(this.creatureBoundsMax, this.creatureBoundsMax); } ; Phaser.Creature.prototype.updateData = function (){ var target = this.manager.target_creature; var read_pts = target.render_pts; var read_uvs = target.global_uvs; this.updateRenderData(read_pts, read_uvs); this.updateCreatureBounds(); this.dirty = true ; } ; Phaser.Creature.prototype.updateRenderData = function (verts, uvs){ var target = this.manager.target_creature; var pt_index = 0; var uv_index = 0; var write_pt_index = 0; for (var i = 0; i < target.total_num_pts; i++ ){ this.vertices[write_pt_index] = verts[pt_index]; this.vertices[write_pt_index + 1] = - verts[pt_index + 1]; this.uvs[uv_index] = uvs[uv_index]; this.uvs[uv_index + 1] = uvs[uv_index + 1]; pt_index += 3; uv_index += 2; write_pt_index += 2; } } ; Phaser.Creature.prototype.setAnimation = function (key){ this.manager.SetActiveAnimationName(key, true ); } ; Phaser.Creature.prototype.play = function (loop){ if (loop === undefined) { loop = false ; } this.loop = loop; this.manager.SetIsPlaying(true ); this.manager.RunAtTime(0); } ; Phaser.Creature.prototype.stop = function (){ this.manager.SetIsPlaying(false ); } ; Object.defineProperty(Phaser.Creature.prototype, 'isPlaying', { get: function (){ return this.manager.GetIsPlaying(); } , set: function (value){ this.manager.SetIsPlaying(value); } } ); Object.defineProperty(Phaser.Creature.prototype, 'loop', { get: function (){ return this.manager.should_loop; } , set: function (value){ this.manager.SetShouldLoop(value); } } );