phaser4-rex-plugins
Version:
789 lines (650 loc) • 22.6 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rexshakepositionplugin = factory());
})(this, (function () { 'use strict';
var EventEmitterMethods = {
setEventEmitter(eventEmitter, EventEmitterClass) {
if (EventEmitterClass === undefined) {
EventEmitterClass = Phaser.Events.EventEmitter; // Use built-in EventEmitter class by default
}
this._privateEE = (eventEmitter === true) || (eventEmitter === undefined);
this._eventEmitter = (this._privateEE) ? (new EventEmitterClass()) : eventEmitter;
return this;
},
destroyEventEmitter() {
if (this._eventEmitter && this._privateEE) {
this._eventEmitter.shutdown();
}
return this;
},
getEventEmitter() {
return this._eventEmitter;
},
on() {
if (this._eventEmitter) {
this._eventEmitter.on.apply(this._eventEmitter, arguments);
}
return this;
},
once() {
if (this._eventEmitter) {
this._eventEmitter.once.apply(this._eventEmitter, arguments);
}
return this;
},
off() {
if (this._eventEmitter) {
this._eventEmitter.off.apply(this._eventEmitter, arguments);
}
return this;
},
emit(event) {
if (this._eventEmitter && event) {
this._eventEmitter.emit.apply(this._eventEmitter, arguments);
}
return this;
},
addListener() {
if (this._eventEmitter) {
this._eventEmitter.addListener.apply(this._eventEmitter, arguments);
}
return this;
},
removeListener() {
if (this._eventEmitter) {
this._eventEmitter.removeListener.apply(this._eventEmitter, arguments);
}
return this;
},
removeAllListeners() {
if (this._eventEmitter) {
this._eventEmitter.removeAllListeners.apply(this._eventEmitter, arguments);
}
return this;
},
listenerCount() {
if (this._eventEmitter) {
return this._eventEmitter.listenerCount.apply(this._eventEmitter, arguments);
}
return 0;
},
listeners() {
if (this._eventEmitter) {
return this._eventEmitter.listeners.apply(this._eventEmitter, arguments);
}
return [];
},
eventNames() {
if (this._eventEmitter) {
return this._eventEmitter.eventNames.apply(this._eventEmitter, arguments);
}
return [];
},
};
const SceneClass = Phaser.Scene;
var IsSceneObject = function (object) {
return (object instanceof SceneClass);
};
var GetSceneObject = function (object) {
if ((object == null) || (typeof (object) !== 'object')) {
return null;
} else if (IsSceneObject(object)) { // object = scene
return object;
} else if (object.scene && IsSceneObject(object.scene)) { // object = game object
return object.scene;
} else if (object.parent && object.parent.scene && IsSceneObject(object.parent.scene)) { // parent = bob object
return object.parent.scene;
} else {
return null;
}
};
const GameClass = Phaser.Game;
var IsGame = function (object) {
return (object instanceof GameClass);
};
var GetGame = function (object) {
if ((object == null) || (typeof (object) !== 'object')) {
return null;
} else if (IsGame(object)) {
return object;
} else if (IsGame(object.game)) {
return object.game;
} else if (IsSceneObject(object)) { // object = scene object
return object.sys.game;
} else if (IsSceneObject(object.scene)) { // object = game object
return object.scene.sys.game;
}
};
const GetValue$3 = Phaser.Utils.Objects.GetValue;
class ComponentBase {
constructor(parent, config) {
this.setParent(parent); // gameObject, scene, or game
this.isShutdown = false;
// Event emitter, default is private event emitter
this.setEventEmitter(GetValue$3(config, 'eventEmitter', true));
// Register callback of parent destroy event, also see `shutdown` method
if (this.parent) {
if (this.parent === this.scene) { // parent is a scene
this.scene.sys.events.once('shutdown', this.onEnvDestroy, this);
} else if (this.parent === this.game) { // parent is game
this.game.events.once('shutdown', this.onEnvDestroy, this);
} else if (this.parent.once) { // parent is game object or something else
this.parent.once('destroy', this.onParentDestroy, this);
}
// bob object does not have event emitter
}
}
shutdown(fromScene) {
// Already shutdown
if (this.isShutdown) {
return;
}
// parent might not be shutdown yet
if (this.parent) {
if (this.parent === this.scene) { // parent is a scene
this.scene.sys.events.off('shutdown', this.onEnvDestroy, this);
} else if (this.parent === this.game) { // parent is game
this.game.events.off('shutdown', this.onEnvDestroy, this);
} else if (this.parent.once) { // parent is game object or something else
this.parent.off('destroy', this.onParentDestroy, this);
}
// bob object does not have event emitter
}
this.destroyEventEmitter();
this.parent = undefined;
this.scene = undefined;
this.game = undefined;
this.isShutdown = true;
}
destroy(fromScene) {
this.shutdown(fromScene);
}
onEnvDestroy() {
this.destroy(true);
}
onParentDestroy(parent, fromScene) {
this.destroy(fromScene);
}
setParent(parent) {
this.parent = parent; // gameObject, scene, or game
this.scene = GetSceneObject(parent);
this.game = GetGame(parent);
return this;
}
}
Object.assign(
ComponentBase.prototype,
EventEmitterMethods
);
const GetValue$2 = Phaser.Utils.Objects.GetValue;
class TickTask extends ComponentBase {
constructor(parent, config) {
super(parent, config);
this._isRunning = false;
this.isPaused = false;
this.tickingState = false;
this.setTickingMode(GetValue$2(config, 'tickingMode', 1));
// boot() later
}
// override
boot() {
if ((this.tickingMode === 2) && (!this.tickingState)) {
this.startTicking();
}
}
// override
shutdown(fromScene) {
// Already shutdown
if (this.isShutdown) {
return;
}
this.stop();
if (this.tickingState) {
this.stopTicking();
}
super.shutdown(fromScene);
}
setTickingMode(mode) {
if (typeof (mode) === 'string') {
mode = TICKINGMODE[mode];
}
this.tickingMode = mode;
}
// override
startTicking() {
this.tickingState = true;
}
// override
stopTicking() {
this.tickingState = false;
}
get isRunning() {
return this._isRunning;
}
set isRunning(value) {
if (this._isRunning === value) {
return;
}
this._isRunning = value;
if ((this.tickingMode === 1) && (value != this.tickingState)) {
if (value) {
this.startTicking();
} else {
this.stopTicking();
}
}
}
start() {
this.isPaused = false;
this.isRunning = true;
return this;
}
pause() {
// Only can ba paused in running state
if (this.isRunning) {
this.isPaused = true;
this.isRunning = false;
}
return this;
}
resume() {
// Only can ba resumed in paused state (paused from running state)
if (this.isPaused) {
this.isPaused = false;
this.isRunning = true;
}
return this;
}
stop() {
this.isPaused = false;
this.isRunning = false;
return this;
}
complete() {
this.isPaused = false;
this.isRunning = false;
this.emit('complete', this.parent, this);
}
}
const TICKINGMODE = {
'no': 0,
'lazy': 1,
'always': 2
};
const GetValue$1 = Phaser.Utils.Objects.GetValue;
const Clamp = Phaser.Math.Clamp;
class Timer {
constructor(config) {
this.resetFromJSON(config);
}
resetFromJSON(o) {
this.state = GetValue$1(o, 'state', IDLE);
this.timeScale = GetValue$1(o, 'timeScale', 1);
this.delay = GetValue$1(o, 'delay', 0);
this.repeat = GetValue$1(o, 'repeat', 0);
this.repeatCounter = GetValue$1(o, 'repeatCounter', 0);
this.repeatDelay = GetValue$1(o, 'repeatDelay', 0);
this.duration = GetValue$1(o, 'duration', 0);
this.nowTime = GetValue$1(o, 'nowTime', 0);
this.justRestart = GetValue$1(o, 'justRestart', false);
}
toJSON() {
return {
state: this.state,
timeScale: this.timeScale,
delay: this.delay,
repeat: this.repeat,
repeatCounter: this.repeatCounter,
repeatDelay: this.repeatDelay,
duration: this.duration,
nowTime: this.nowTime,
justRestart: this.justRestart,
}
}
destroy() {
}
setTimeScale(timeScale) {
this.timeScale = timeScale;
return this;
}
setDelay(delay) {
if (delay === undefined) {
delay = 0;
}
this.delay = delay;
return this;
}
setDuration(duration) {
this.duration = duration;
return this;
}
setRepeat(repeat) {
this.repeat = repeat;
return this;
}
setRepeatInfinity() {
this.repeat = -1;
return this;
}
setRepeatDelay(repeatDelay) {
this.repeatDelay = repeatDelay;
return this;
}
start() {
this.nowTime = (this.delay > 0) ? -this.delay : 0;
this.state = (this.nowTime >= 0) ? COUNTDOWN : DELAY;
this.repeatCounter = 0;
return this;
}
stop() {
this.state = IDLE;
return this;
}
update(time, delta) {
if (this.state === IDLE || this.state === DONE ||
delta === 0 || this.timeScale === 0
) {
return;
}
this.nowTime += (delta * this.timeScale);
this.justRestart = false;
if (this.nowTime >= this.duration) {
if ((this.repeat === -1) || (this.repeatCounter < this.repeat)) {
this.repeatCounter++;
this.justRestart = true;
this.nowTime -= this.duration;
if (this.repeatDelay > 0) {
this.nowTime -= this.repeatDelay;
this.state = REPEATDELAY;
}
} else {
this.nowTime = this.duration;
this.state = DONE;
}
} else if (this.nowTime >= 0) {
this.state = COUNTDOWN;
}
}
get t() {
var t;
switch (this.state) {
case IDLE:
case DELAY:
case REPEATDELAY:
t = 0;
break;
case COUNTDOWN:
t = this.nowTime / this.duration;
break;
case DONE:
t = 1;
break;
}
return Clamp(t, 0, 1);
}
set t(value) {
value = Clamp(value, -1, 1);
if (value < 0) {
this.state = DELAY;
this.nowTime = -this.delay * value;
} else {
this.state = COUNTDOWN;
this.nowTime = this.duration * value;
if ((value === 1) && (this.repeat !== 0)) {
this.repeatCounter++;
}
}
}
setT(t) {
this.t = t;
return this;
}
get isIdle() {
return this.state === IDLE;
}
get isDelay() {
return this.state === DELAY;
}
get isCountDown() {
return this.state === COUNTDOWN;
}
get isRunning() {
return this.state === DELAY || this.state === COUNTDOWN;
}
get isDone() {
return this.state === DONE;
}
get isOddIteration() {
return (this.repeatCounter & 1) === 1;
}
get isEvenIteration() {
return (this.repeatCounter & 1) === 0;
}
}
const IDLE = 0;
const DELAY = 1;
const COUNTDOWN = 2;
const REPEATDELAY = 3;
const DONE = -1;
const GetValue = Phaser.Utils.Objects.GetValue;
class ShakePosition extends TickTask {
constructor(gameObject, config) {
super(gameObject, config);
// this.parent = gameObject;
this.timer = new Timer();
this.resetFromJSON(config);
this.boot();
}
resetFromJSON(o) {
this.timer.resetFromJSON(GetValue(o, 'timer'));
this.setEnable(GetValue(o, 'enable', true));
this.setMode(GetValue(o, 'mode', 1));
this.isRunning = GetValue(o, 'isRunning', false);
this.setMagnitudeMode(GetValue(o, 'magnitudeMode', 1));
this.setAxisMode(GetValue(o, "axis", 0));
this.setDuration(GetValue(o, 'duration', 500));
this.setMagnitude(GetValue(o, 'magnitude', 10));
this.ox = GetValue(o, 'ox', undefined);
this.oy = GetValue(o, 'oy', undefined);
return this;
}
toJSON() {
return {
timer: this.timer.toJSON(),
enable: this.enable,
mode: this.mode,
isRunning: this.isRunning,
magnitudeMode: magnitudeMode,
duration: this.duration,
magnitude: this.magnitude,
ox: this.ox,
oy: this.oy,
};
}
// override
shutdown(fromScene) {
// Already shutdown
if (this.isShutdown) {
return;
}
super.shutdown(fromScene);
this.timer.destroy();
this.timer = undefined;
}
startTicking() {
super.startTicking();
if (this.mode === 0) { // Effect mode
this.scene.game.events.on('poststep', this.update, this);
this.scene.game.events.on('prestep', this.backToOrigin, this);
} else { // Behavior Mode
this.scene.sys.events.on('preupdate', this.update, this);
}
}
stopTicking() {
super.stopTicking();
if (this.scene) { // Scene might be destoryed
if (this.mode === 0) { // Effect mode
this.scene.game.events.off('poststep', this.update, this);
this.scene.game.events.off('prestep', this.backToOrigin, this);
} else { // Behavior Mode
this.scene.sys.events.off('preupdate', this.update, this);
}
}
}
setEnable(e) {
if (e == undefined) {
e = true;
}
this.enable = e;
return this;
}
setMode(mode) {
if (typeof (mode) === 'string') {
mode = MODE[mode];
}
this.mode = mode;
return this;
}
setMagnitudeMode(magnitudeMode) {
if (typeof (magnitudeMode) === 'string') {
magnitudeMode = MANITUDEMODE[magnitudeMode];
}
this.magnitudeMode = magnitudeMode;
return this;
}
setAxisMode(m) {
if (typeof (m) === 'string') {
m = DIRECTIONNODE[m];
}
this.axisMode = m;
return this;
}
setDuration(duration) {
this.duration = duration;
return this;
}
setMagnitude(magnitude) {
this.magnitude = magnitude;
return this;
}
start(duration, magnitude) {
if (typeof (duration) !== 'number') {
var config = duration;
magnitude = GetValue(config, 'magnitude', undefined);
duration = GetValue(config, 'duration', undefined);
}
if (magnitude !== undefined) {
this.setMagnitude(magnitude);
}
if (duration !== undefined) {
this.setDuration(duration);
}
this.timer
.setDuration(this.duration)
.start();
super.start();
return this;
}
shake(duration, magnitude) {
this.start(duration, magnitude);
return this;
}
update(time, delta) {
if ((!this.isRunning) || (!this.enable)) {
return this;
}
var gameObject = this.parent;
if (!gameObject.active) {
return this;
}
this.timer.update(time, delta);
if (this.timer.isDone) {
this.backToOrigin();
this.complete();
} else {
if (this.ox === undefined) {
this.ox = gameObject.x;
this.oy = gameObject.y;
}
var magnitude = this.magnitude;
if (this.magnitudeMode === 1) // decay
{
magnitude *= (1 - this.timer.t);
}
var a = Math.random() * Math.PI * 2;
var x = this.ox + (Math.cos(a) * magnitude);
var y = this.oy + (Math.sin(a) * magnitude);
switch (this.axisMode) {
case 1:
gameObject.x = x;
break;
case 2:
gameObject.y = y;
break;
default:
gameObject.x = x;
gameObject.y = y;
break;
}
}
return this;
}
backToOrigin() {
if ((!this.isRunning) || (!this.enable)) {
return this;
}
if (this.ox === undefined) {
return this;
}
var gameObject = this.parent;
switch (this.axisMode) {
case 1:
gameObject.x = this.ox;
break;
case 2:
gameObject.y = this.oy;
break;
default:
gameObject.x = this.ox;
gameObject.y = this.oy;
break;
}
this.ox = undefined;
this.oy = undefined;
return this;
}
}
const MODE = {
effect: 0,
behavior: 1,
};
const DIRECTIONNODE = {
'both': 0,
'h&v': 0,
'x&y': 0,
'horizontal': 1,
'h': 1,
'x': 1,
'vertical': 2,
'v': 2,
'y': 2
};
const MANITUDEMODE = {
constant: 0,
decay: 1,
};
class ShakePlugin extends Phaser.Plugins.BasePlugin {
constructor(pluginManager) {
super(pluginManager);
}
start() {
var eventEmitter = this.game.events;
eventEmitter.on('destroy', this.destroy, this);
}
add(gameObject, config) {
return new ShakePosition(gameObject, config);
}
}
return ShakePlugin;
}));