Added Game Events

This commit is contained in:
Richard Davey 2019-01-15 15:11:19 +00:00
parent 0e2e059546
commit 777eb1b44a
29 changed files with 303 additions and 123 deletions

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Add Animation Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Animation Complete Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Animation Repeat Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Animation Restart Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Animation Start Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Pause All Animations Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Remove Animation Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Resume All Animations Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Complete Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Key Complete Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Key Repeat Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Key Restart Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Key Start Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Key Update Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Repeat Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Restart Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Start Event.
*

View file

@ -1,3 +1,9 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sprite Animation Update Event.
*

View file

@ -16,6 +16,7 @@ var DebugHeader = require('./DebugHeader');
var Device = require('../device');
var DOMContentLoaded = require('../dom/DOMContentLoaded');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var InputManager = require('../input/InputManager');
var PluginCache = require('../plugins/PluginCache');
var PluginManager = require('../plugins/PluginManager');
@ -333,15 +334,6 @@ var Game = new Class({
DOMContentLoaded(this.boot.bind(this));
},
/**
* Game boot event.
*
* This is an internal event dispatched when the game has finished booting, but before it is ready to start running.
* The global systems use this event to know when to set themselves up, dispatching their own `ready` events as required.
*
* @event Phaser.Game#boot
*/
/**
* This method is called automatically when the DOM is ready. It is responsible for creating the renderer,
* displaying the Debug Header, adding the game canvas to the DOM and emitting the 'boot' event.
@ -435,71 +427,6 @@ var Game = new Class({
eventEmitter.on('focus', this.onFocus, this);
},
/**
* Game Pre-Step event.
*
* Listen for it using the event type `prestep`.
*
* This event is dispatched before the main Step starts.
* By this point none of the Scene updates have happened.
* Hook into it from plugins or systems that need to update before the Scene Manager does.
*
* @event Phaser.Game#prestepEvent
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
/**
* Game Step event.
*
* Listen for it using the event type `step`.
*
* This event is dispatched after Pre-Step and before the Scene Manager steps.
* Hook into it from plugins or systems that need to update before the Scene Manager does, but after core Systems.
*
* @event Phaser.Game#stepEvent
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
/**
* Game Post-Step event.
*
* Listen for it using the event type `poststep`.
*
* This event is dispatched after the Scene Manager has updated.
* Hook into it from plugins or systems that need to do things before the render starts.
*
* @event Phaser.Game#poststepEvent
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
/**
* Game Pre-Render event.
*
* Listen for it using the event type `prerender`.
*
* This event is dispatched immediately before any of the Scenes have started to render.
* The renderer will already have been initialized this frame, clearing itself and preparing to receive
* the Scenes for rendering, but it won't have actually drawn anything yet.
*
* @event Phaser.Game#prerenderEvent
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - A reference to the current renderer.
*/
/**
* Game Post-Render event.
*
* Listen for it using the event type `postrender`.
*
* This event is dispatched right at the end of the render process.
* Every Scene will have rendered and drawn to the canvas.
*
* @event Phaser.Game#postrenderEvent
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - A reference to the current renderer.
*/
/**
* The main Game Step. Called automatically by the Time Step, once per browser frame (typically as a result of
* Request Animation Frame, or Set Timeout on very old browsers.)
@ -509,11 +436,11 @@ var Game = new Class({
* It will then render each Scene in turn, via the Renderer. This process emits `prerender` and `postrender` events.
*
* @method Phaser.Game#step
* @fires Phaser.Game#prestepEvent
* @fires Phaser.Game#stepEvent
* @fires Phaser.Game#poststepEvent
* @fires Phaser.Game#prerenderEvent
* @fires Phaser.Game#postrenderEvent
* @fires Phaser.Game.Events#PRE_STEP_EVENT
* @fires Phaser.Game.Events#STEP_EVENT
* @fires Phaser.Game.Events#POST_STEP_EVENT
* @fires Phaser.Game.Events#PRE_RENDER_EVENT
* @fires Phaser.Game.Events#POST_RENDER_EVENT
* @since 3.0.0
*
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
@ -530,11 +457,11 @@ var Game = new Class({
// Global Managers like Input and Sound update in the prestep
eventEmitter.emit('prestep', time, delta);
eventEmitter.emit(Events.PRE_STEP, time, delta);
// This is mostly meant for user-land code and plugins
eventEmitter.emit('step', time, delta);
eventEmitter.emit(Events.STEP, time, delta);
// Update the Scene Manager and all active Scenes
@ -542,7 +469,7 @@ var Game = new Class({
// Our final event before rendering starts
eventEmitter.emit('poststep', time, delta);
eventEmitter.emit(Events.POST_STEP, time, delta);
var renderer = this.renderer;
@ -550,7 +477,7 @@ var Game = new Class({
renderer.preRender();
eventEmitter.emit('prerender', renderer, time, delta);
eventEmitter.emit(Events.PRE_RENDER, renderer, time, delta);
// The main render loop. Iterates all Scenes and all Cameras in those scenes, rendering to the renderer instance.
@ -562,7 +489,7 @@ var Game = new Class({
// The final event before the step repeats. Your last chance to do anything to the canvas before it all starts again.
eventEmitter.emit('postrender', renderer, time, delta);
eventEmitter.emit(Events.POST_RENDER, renderer, time, delta);
},
/**
@ -589,73 +516,53 @@ var Game = new Class({
// Global Managers
eventEmitter.emit('prestep', time, delta);
eventEmitter.emit(Events.PRE_STEP, time, delta);
eventEmitter.emit('step', time, delta);
eventEmitter.emit(Events.STEP, time, delta);
// Scenes
this.scene.update(time, delta);
eventEmitter.emit('poststep', time, delta);
eventEmitter.emit(Events.POST_STEP, time, delta);
// Render
eventEmitter.emit('prerender');
eventEmitter.emit(Events.PRE_RENDER);
eventEmitter.emit('postrender');
eventEmitter.emit(Events.POST_RENDER);
},
/**
* Game Pause event.
*
* Listen for it using the event type `pause`.
*
* This event is dispatched when the game loop enters a paused state, usually as a result of the Visibility Handler.
*
* @event Phaser.Game#pauseEvent
*/
/**
* Called automatically by the Visibility Handler.
* This will pause the main loop and then emit a pause event.
*
* @method Phaser.Game#onHidden
* @protected
* @fires Phaser.Game#pauseEvent
* @fires Phaser.Game.Events#PAUSE
* @since 3.0.0
*/
onHidden: function ()
{
this.loop.pause();
this.events.emit('pause');
this.events.emit(Events.PAUSE);
},
/**
* Game Resume event.
*
* Listen for it using the event type `resume`.
*
* This event is dispatched when the game loop leaves a paused state and resumes running.
*
* @event Phaser.Game#resumeEvent
*/
/**
* Called automatically by the Visibility Handler.
* This will resume the main loop and then emit a resume event.
*
* @method Phaser.Game#onVisible
* @protected
* @fires Phaser.Game#resumeEvent
* @fires Phaser.Game.Events#RESUME
* @since 3.0.0
*/
onVisible: function ()
{
this.loop.resume();
this.events.emit('resume');
this.events.emit(Events.RESUME);
},
/**
@ -716,14 +623,6 @@ var Game = new Class({
return this.loop.frame.time;
},
/**
* Game Destroy event.
*
* Listen for it using the event type `destroy`.
*
* @event Phaser.Game#destroyEvent
*/
/**
* Flags this Game instance as needing to be destroyed on the next frame.
* It will wait until the current frame has completed and then call `runDestroy` internally.
@ -732,7 +631,7 @@ var Game = new Class({
* memory being held by the core Phaser plugins. If you do need to create another game instance on the same page, leave this as `false`.
*
* @method Phaser.Game#destroy
* @fires Phaser.Game#destroyEvent
* @fires Phaser.Game.Events#DESTROY
* @since 3.0.0
*
* @param {boolean} removeCanvas - Set to `true` if you would like the parent canvas element removed from the DOM, or `false` to leave it in place.
@ -757,7 +656,7 @@ var Game = new Class({
*/
runDestroy: function ()
{
this.events.emit('destroy');
this.events.emit(Events.DESTROY);
this.events.removeAllListeners();

View file

@ -0,0 +1,15 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Boot Event.
*
* This event is dispatched when the Phaser Game instance has finished booting, but before it is ready to start running.
* The global systems use this event to know when to set themselves up, dispatching their own `ready` events as required.
*
* @event Phaser.Game.Events#BOOT
*/
module.exports = 'boot';

View file

@ -0,0 +1,16 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Destroy Event.
*
* This event is dispatched when the game instance has been told to destroy itself.
* Lots of internal systems listen to this event in order to clear themselves out.
* Custom plugins and game code should also do the same.
*
* @event Phaser.Game.Events#DESTROY
*/
module.exports = 'destroy';

View file

@ -0,0 +1,14 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Pause Event.
*
* This event is dispatched when the Game loop enters a paused state, usually as a result of the Visibility Handler.
*
* @event Phaser.Game.Events#PAUSE
*/
module.exports = 'pause';

View file

@ -0,0 +1,19 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Post-Render Event.
*
* This event is dispatched right at the end of the render process.
*
* Every Scene will have rendered and been drawn to the canvas by the time this event is fired.
* Use it for any last minute post-processing before the next game step begins.
*
* @event Phaser.Game.Events#POST_RENDER
*
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - A reference to the current renderer being used by the Game instance.
*/
module.exports = 'postrender';

View file

@ -0,0 +1,18 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Post-Step Event.
*
* This event is dispatched after the Scene Manager has updated.
* Hook into it from plugins or systems that need to do things before the render starts.
*
* @event Phaser.Game.Events#POST_STEP
*
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
module.exports = 'poststep';

View file

@ -0,0 +1,18 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Pre-Render Event.
*
* This event is dispatched immediately before any of the Scenes have started to render.
*
* The renderer will already have been initialized this frame, clearing itself and preparing to receive the Scenes for rendering, but it won't have actually drawn anything yet.
*
* @event Phaser.Game.Events#PRE_RENDER
*
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - A reference to the current renderer being used by the Game instance.
*/
module.exports = 'prerender';

View file

@ -0,0 +1,18 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Pre-Step Event.
*
* This event is dispatched before the main Game Step starts. By this point in the game cycle none of the Scene updates have yet happened.
* Hook into it from plugins or systems that need to update before the Scene Manager does.
*
* @event Phaser.Game.Events#PRE_STEP
*
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
module.exports = 'prestep';

View file

@ -0,0 +1,14 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Resume Event.
*
* This event is dispatched when the game loop leaves a paused state and resumes running.
*
* @event Phaser.Game.Events#RESUME
*/
module.exports = 'resume';

View file

@ -0,0 +1,18 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Game Step Event.
*
* This event is dispatched after the Game Pre-Step and before the Scene Manager steps.
* Hook into it from plugins or systems that need to update before the Scene Manager does, but after the core Systems have.
*
* @event Phaser.Game.Events#STEP
*
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
module.exports = 'step';

23
src/boot/events/index.js Normal file
View file

@ -0,0 +1,23 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* @namespace Phaser.Game.Events
*/
module.exports = {
BOOT: require('./BOOT_EVENT'),
DESTROY: require('./DESTROY_EVENT'),
PAUSE: require('./PAUSE_EVENT'),
POST_RENDER: require('./POST_RENDER_EVENT'),
POST_STEP: require('./POST_STEP_EVENT'),
PRE_RENDER: require('./PRE_RENDER_EVENT'),
PRE_STEP: require('./PRE_STEP_EVENT'),
RESUME: require('./RESUME_EVENT'),
STEP: require('./STEP_EVENT')
};