mirror of
https://github.com/photonstorm/phaser
synced 2024-11-22 04:33:31 +00:00
Moved Arcade and Impact Physics over to the new plugin system
This commit is contained in:
parent
d1cd5cbc1d
commit
ddba95873d
13 changed files with 257 additions and 199 deletions
|
@ -1,92 +0,0 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var GetValue = require('../../utils/object/GetValue');
|
||||
var Merge = require('../../utils/object/Merge');
|
||||
var NOOP = require('../../utils/NOOP');
|
||||
|
||||
// Physics Systems (TODO: Remove from here)
|
||||
var Arcade = require('../../physics/arcade/Arcade');
|
||||
var Impact = require('../../physics/impact/Impact');
|
||||
var Matter = require('../../physics/matter-js/Matter');
|
||||
|
||||
var PhysicsManager = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function PhysicsManager (scene)
|
||||
{
|
||||
this.scene = scene;
|
||||
|
||||
this.gameConfig = scene.sys.game.config.physics;
|
||||
this.defaultSystem = scene.sys.game.config.defaultPhysicsSystem;
|
||||
this.sceneConfig = scene.sys.settings.physics;
|
||||
|
||||
// This gets set to an instance of the physics system during boot
|
||||
this.system;
|
||||
|
||||
// This gets set by the physics system during boot
|
||||
this.world = { update: NOOP, postUpdate: NOOP, shutdown: NOOP, destroy: NOOP };
|
||||
|
||||
// This gets set by the physics system during boot
|
||||
this.add;
|
||||
},
|
||||
|
||||
boot: function ()
|
||||
{
|
||||
var sceneSystem = GetValue(this.sceneConfig, 'system', false);
|
||||
|
||||
if (!this.defaultSystem && !sceneSystem)
|
||||
{
|
||||
// No default physics system or system in this scene, so abort
|
||||
return;
|
||||
}
|
||||
|
||||
// Which physics system are we using in this Scene?
|
||||
var system = (sceneSystem !== false) ? sceneSystem : this.defaultSystem;
|
||||
|
||||
// Create the config for it
|
||||
var config = Merge(this.sceneConfig, GetValue(this.gameConfig, system, {}));
|
||||
|
||||
switch (system)
|
||||
{
|
||||
case 'arcade':
|
||||
this.system = new Arcade(this, config);
|
||||
break;
|
||||
|
||||
case 'impact':
|
||||
this.system = new Impact(this, config);
|
||||
break;
|
||||
|
||||
case 'matter':
|
||||
this.system = new Matter(this, config);
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
remove: function (object)
|
||||
{
|
||||
this.world.remove(object);
|
||||
},
|
||||
|
||||
update: function (time, delta)
|
||||
{
|
||||
this.world.update(time, delta);
|
||||
},
|
||||
|
||||
postUpdate: function ()
|
||||
{
|
||||
this.world.postUpdate();
|
||||
},
|
||||
|
||||
shutdown: function ()
|
||||
{
|
||||
this.world.shutdown();
|
||||
},
|
||||
|
||||
destroy: function ()
|
||||
{
|
||||
this.world.destroy();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = PhysicsManager;
|
|
@ -1,20 +0,0 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var Factory = require('./Factory');
|
||||
var World = require('./World');
|
||||
|
||||
var Arcade = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function Arcade (physicsManager, config)
|
||||
{
|
||||
this.config = config;
|
||||
|
||||
physicsManager.world = new World(physicsManager.scene, config);
|
||||
|
||||
physicsManager.add = new Factory(physicsManager.world);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = Arcade;
|
110
src/physics/arcade/ArcadePhysics.js
Normal file
110
src/physics/arcade/ArcadePhysics.js
Normal file
|
@ -0,0 +1,110 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var Factory = require('./Factory');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
var Merge = require('../../utils/object/Merge');
|
||||
var PluginManager = require('../../plugins/PluginManager');
|
||||
var World = require('./World');
|
||||
|
||||
// Phaser.Physics.Arcade.ArcadePhysics
|
||||
|
||||
var ArcadePhysics = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function ArcadePhysics (scene)
|
||||
{
|
||||
// The Scene that owns this plugin
|
||||
this.scene = scene;
|
||||
|
||||
this.systems = scene.sys;
|
||||
|
||||
this.mapping = 'physics';
|
||||
|
||||
this.systems.events.on('boot', this.boot, this);
|
||||
|
||||
this.config = this.getConfig();
|
||||
|
||||
this.world;
|
||||
|
||||
this.add;
|
||||
},
|
||||
|
||||
getConfig: function ()
|
||||
{
|
||||
var gameConfig = this.systems.game.config.physics;
|
||||
var sceneConfig = this.systems.settings.physics;
|
||||
|
||||
var config = Merge(
|
||||
GetFastValue(sceneConfig, 'arcade', {}),
|
||||
GetFastValue(gameConfig, 'arcade', {})
|
||||
);
|
||||
|
||||
return config;
|
||||
},
|
||||
|
||||
boot: function ()
|
||||
{
|
||||
this.world = new World(this.scene, this.config);
|
||||
this.add = new Factory(this.world);
|
||||
|
||||
this.systems.inject(this);
|
||||
|
||||
this.systems.events.on('update', this.update, this);
|
||||
this.systems.events.on('postupdate', this.postUpdate, this);
|
||||
this.systems.events.on('shutdown', this.shutdown, this);
|
||||
this.systems.events.on('destroy', this.destroy, this);
|
||||
},
|
||||
|
||||
update: function (time, delta)
|
||||
{
|
||||
this.world.update(time, delta);
|
||||
},
|
||||
|
||||
postUpdate: function ()
|
||||
{
|
||||
this.world.postUpdate();
|
||||
},
|
||||
|
||||
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext)
|
||||
{
|
||||
if (overlapCallback === undefined) { overlapCallback = null; }
|
||||
if (processCallback === undefined) { processCallback = null; }
|
||||
if (callbackContext === undefined) { callbackContext = overlapCallback; }
|
||||
|
||||
return this.world.collideObjects(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
||||
},
|
||||
|
||||
collide: function (object1, object2, collideCallback, processCallback, callbackContext)
|
||||
{
|
||||
if (collideCallback === undefined) { collideCallback = null; }
|
||||
if (processCallback === undefined) { processCallback = null; }
|
||||
if (callbackContext === undefined) { callbackContext = collideCallback; }
|
||||
|
||||
return this.world.collideObjects(object1, object2, collideCallback, processCallback, callbackContext, false);
|
||||
},
|
||||
|
||||
// Utils
|
||||
accelerateTo: require('./utils/AccelerateTo'),
|
||||
accelerateToObject: require('./utils/AccelerateToObject'),
|
||||
closest: require('./utils/Closest'),
|
||||
furthest: require('./utils/Furthest'),
|
||||
moveTo: require('./utils/MoveTo'),
|
||||
moveToObject: require('./utils/MoveToObject'),
|
||||
velocityFromAngle: require('./utils/VelocityFromAngle'),
|
||||
velocityFromRotation: require('./utils/VelocityFromRotation'),
|
||||
|
||||
shutdown: function ()
|
||||
{
|
||||
this.world.shutdown();
|
||||
},
|
||||
|
||||
destroy: function ()
|
||||
{
|
||||
this.world.destroy();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
PluginManager.register('arcadePhysics', ArcadePhysics);
|
||||
|
||||
module.exports = ArcadePhysics;
|
|
@ -1,5 +1,3 @@
|
|||
// Phaser.Physics.Arcade.World
|
||||
|
||||
var Body = require('./Body');
|
||||
var Class = require('../../utils/Class');
|
||||
var Collider = require('./Collider');
|
||||
|
@ -13,6 +11,8 @@ var Set = require('../../structs/Set');
|
|||
var StaticBody = require('./StaticBody');
|
||||
var Vector2 = require('../../math/Vector2');
|
||||
|
||||
// Phaser.Physics.Arcade.World
|
||||
|
||||
var World = new Class({
|
||||
|
||||
Extends: EventEmitter,
|
||||
|
@ -399,16 +399,6 @@ var World = new Class({
|
|||
collideGroupVsTilemapLayer: require('./inc/CollideGroupVsTilemapLayer'),
|
||||
collideSpriteVsTilemapLayer: require('./inc/CollideSpriteVsTilemapLayer'),
|
||||
|
||||
// Utils
|
||||
accelerateTo: require('./utils/AccelerateTo'),
|
||||
accelerateToObject: require('./utils/AccelerateToObject'),
|
||||
closest: require('./utils/Closest'),
|
||||
furthest: require('./utils/Furthest'),
|
||||
moveTo: require('./utils/MoveTo'),
|
||||
moveToObject: require('./utils/MoveToObject'),
|
||||
velocityFromAngle: require('./utils/VelocityFromAngle'),
|
||||
velocityFromRotation: require('./utils/VelocityFromRotation'),
|
||||
|
||||
// TODO
|
||||
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly)
|
||||
{
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
// Phaser.Physics.Arcade
|
||||
|
||||
// World updated to run off the Phaser main loop.
|
||||
// Body extended to support additional setter functions.
|
||||
|
||||
module.exports = {
|
||||
|
||||
ArcadePhysics: require('./ArcadePhysics'),
|
||||
Body: require('./Body'),
|
||||
Collider: require('./Collider'),
|
||||
Factory: require('./Factory'),
|
||||
Group: require('./PhysicsGroup'),
|
||||
Image: require('./ArcadeImage'),
|
||||
Sprite: require('./ArcadeSprite'),
|
||||
StaticBody: require('./StaticBody'),
|
||||
StaticGroup: require('./StaticPhysicsGroup'),
|
||||
World: require('./World')
|
||||
|
||||
};
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var Factory = require('./Factory');
|
||||
var World = require('./World');
|
||||
|
||||
var Impact = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function Impact (physicsManager, config)
|
||||
{
|
||||
this.config = config;
|
||||
|
||||
physicsManager.world = new World(physicsManager.scene, config);
|
||||
|
||||
physicsManager.add = new Factory(physicsManager.world);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = Impact;
|
76
src/physics/impact/ImpactPhysics.js
Normal file
76
src/physics/impact/ImpactPhysics.js
Normal file
|
@ -0,0 +1,76 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var Factory = require('./Factory');
|
||||
var GetFastValue = require('../../utils/object/GetFastValue');
|
||||
var Merge = require('../../utils/object/Merge');
|
||||
var PluginManager = require('../../plugins/PluginManager');
|
||||
var World = require('./World');
|
||||
|
||||
// Phaser.Physics.Impact.ImpactPhysics
|
||||
|
||||
var ImpactPhysics = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function ImpactPhysics (scene)
|
||||
{
|
||||
// The Scene that owns this plugin
|
||||
this.scene = scene;
|
||||
|
||||
this.systems = scene.sys;
|
||||
|
||||
this.mapping = 'impact';
|
||||
|
||||
this.systems.events.on('boot', this.boot, this);
|
||||
|
||||
this.config = this.getConfig();
|
||||
|
||||
this.world;
|
||||
|
||||
this.add;
|
||||
},
|
||||
|
||||
getConfig: function ()
|
||||
{
|
||||
var gameConfig = this.systems.game.config.physics;
|
||||
var sceneConfig = this.systems.settings.physics;
|
||||
|
||||
var config = Merge(
|
||||
GetFastValue(sceneConfig, 'impact', {}),
|
||||
GetFastValue(gameConfig, 'impact', {})
|
||||
);
|
||||
|
||||
return config;
|
||||
},
|
||||
|
||||
boot: function ()
|
||||
{
|
||||
this.world = new World(this.scene, this.config);
|
||||
this.add = new Factory(this.world);
|
||||
|
||||
this.systems.inject(this);
|
||||
|
||||
this.systems.events.on('update', this.update, this);
|
||||
this.systems.events.on('shutdown', this.shutdown, this);
|
||||
this.systems.events.on('destroy', this.destroy, this);
|
||||
},
|
||||
|
||||
update: function (time, delta)
|
||||
{
|
||||
this.world.update(time, delta);
|
||||
},
|
||||
|
||||
shutdown: function ()
|
||||
{
|
||||
this.world.shutdown();
|
||||
},
|
||||
|
||||
destroy: function ()
|
||||
{
|
||||
this.world.destroy();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
PluginManager.register('impactPhysics', ImpactPhysics);
|
||||
|
||||
module.exports = ImpactPhysics;
|
|
@ -216,11 +216,6 @@ var World = new Class({
|
|||
return this;
|
||||
},
|
||||
|
||||
postUpdate: function ()
|
||||
{
|
||||
// NOOP
|
||||
},
|
||||
|
||||
update: function (time, delta)
|
||||
{
|
||||
if (!this.enabled || this.bodies.size === 0)
|
||||
|
|
|
@ -15,10 +15,14 @@
|
|||
module.exports = {
|
||||
|
||||
Body: require('./Body'),
|
||||
Body: require('./ImpactBody'),
|
||||
COLLIDES: require('./COLLIDES'),
|
||||
CollisionMap: require('./CollisionMap'),
|
||||
Factory: require('./Factory'),
|
||||
Image: require('./ImpactImage'),
|
||||
ImpactPhysics: require('./ImpactPhysics'),
|
||||
Sprite: require('./ImpactSprite'),
|
||||
TYPE: require('./TYPE'),
|
||||
World: require('./World'),
|
||||
Factory: require('./Factory')
|
||||
World: require('./World')
|
||||
|
||||
};
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
// Phaser.Physics
|
||||
|
||||
module.exports = {
|
||||
|
||||
Arcade: require('./arcade'),
|
||||
Impact: require('./impact'),
|
||||
Matter: require('./matter-js/CustomMain'),
|
||||
PolyDecomp: require('./poly-decomp')
|
||||
|
||||
};
|
||||
|
|
|
@ -33,10 +33,10 @@ var PluginManager = new Class({
|
|||
{
|
||||
pluginKey = localPlugins[i];
|
||||
|
||||
// console.log('installing', p);
|
||||
|
||||
if (plugins[pluginKey])
|
||||
{
|
||||
// console.log('installing', pluginKey);
|
||||
|
||||
// Install a local reference inside of Systems
|
||||
sys[pluginKey] = new plugins[pluginKey](scene);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// These properties get injected into the Scene and map to local systems
|
||||
// The map key is the local system reference, the value is the property that is added to the Scene
|
||||
// The map key is the property that is added to the Scene, the value is the Scene.Systems reference
|
||||
// These defaults can be modified via the Scene config object
|
||||
|
||||
var InjectionMap = {
|
||||
|
@ -14,14 +14,16 @@ var InjectionMap = {
|
|||
|
||||
add: 'add',
|
||||
cameras: 'cameras',
|
||||
children: 'displayList',
|
||||
data: 'data',
|
||||
displayList: 'children',
|
||||
events: 'events',
|
||||
impact: 'impactPhysics',
|
||||
input: 'input',
|
||||
load: 'load',
|
||||
make: 'make',
|
||||
physicsManager: 'physics',
|
||||
sceneManager: 'scene',
|
||||
matter: 'matterPhysics',
|
||||
physics: 'arcadePhysics',
|
||||
scene: 'sceneManager',
|
||||
time: 'time',
|
||||
tweens: 'tweens'
|
||||
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
var Class = require('../utils/Class');
|
||||
var Settings = require('./Settings');
|
||||
var EventEmitter = require('eventemitter3');
|
||||
var GetFastValue = require('../utils/object/GetFastValue');
|
||||
var ScenePlugin = require('./ScenePlugin');
|
||||
|
||||
// var Data = require('../../data/Data');
|
||||
// var DataStore = require('../../data/DataStore');
|
||||
// var PhysicsManager = require('../plugins/PhysicsManager');
|
||||
var Settings = require('./Settings');
|
||||
|
||||
var Systems = new Class({
|
||||
|
||||
|
@ -69,15 +66,52 @@ var Systems = new Class({
|
|||
[ 'displayList', 'updateList', 'sceneManager', 'time', 'cameras', 'add', 'make', 'load', 'tweens', 'input' ]
|
||||
);
|
||||
|
||||
// Optional Scene plugins - not referenced by core systems, can be overridden with user code
|
||||
var physics = this.getPhysicsSystem();
|
||||
|
||||
// this.data = new Data(scene);
|
||||
// this.dataStore = new DataStore(scene);
|
||||
// this.physicsManager = new PhysicsManager(scene);
|
||||
if (physics)
|
||||
{
|
||||
game.plugins.install(scene, [], physics);
|
||||
}
|
||||
|
||||
this.events.emit('boot', this);
|
||||
},
|
||||
|
||||
getPhysicsSystem: function ()
|
||||
{
|
||||
var defaultSystem = this.game.config.defaultPhysicsSystem;
|
||||
var sceneSystems = GetFastValue(this.settings, 'physics', false);
|
||||
|
||||
if (!defaultSystem && !sceneSystems)
|
||||
{
|
||||
// No default physics system or systems in this scene
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's build the systems array
|
||||
var output = [];
|
||||
|
||||
if (defaultSystem)
|
||||
{
|
||||
output.push(defaultSystem + 'Physics');
|
||||
}
|
||||
|
||||
if (sceneSystems)
|
||||
{
|
||||
for (var key in sceneSystems)
|
||||
{
|
||||
key = key.concat('Physics');
|
||||
|
||||
if (output.indexOf(key) === -1)
|
||||
{
|
||||
output.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An array of Physics systems to start for this Scene
|
||||
return output;
|
||||
},
|
||||
|
||||
inject: function (plugin)
|
||||
{
|
||||
var map = this.settings.map;
|
||||
|
@ -99,13 +133,9 @@ var Systems = new Class({
|
|||
|
||||
this.events.emit('update', time, delta);
|
||||
|
||||
// this.physicsManager.update(time, delta);
|
||||
|
||||
this.scene.update.call(this.scene, time, delta);
|
||||
|
||||
this.events.emit('postupdate', time, delta);
|
||||
|
||||
// this.physicsManager.postUpdate();
|
||||
},
|
||||
|
||||
render: function (interpolation, renderer)
|
||||
|
@ -141,10 +171,7 @@ var Systems = new Class({
|
|||
|
||||
this.settings.active = false;
|
||||
|
||||
if (this.scene.pause)
|
||||
{
|
||||
this.scene.pause.call(this.scene);
|
||||
}
|
||||
this.events.emit('pause', this);
|
||||
},
|
||||
|
||||
resume: function ()
|
||||
|
@ -153,10 +180,7 @@ var Systems = new Class({
|
|||
|
||||
this.settings.active = true;
|
||||
|
||||
if (this.scene.resume)
|
||||
{
|
||||
this.scene.resume.call(this.scene);
|
||||
}
|
||||
this.events.emit('resume', this);
|
||||
},
|
||||
|
||||
sleep: function ()
|
||||
|
@ -166,10 +190,7 @@ var Systems = new Class({
|
|||
this.settings.active = false;
|
||||
this.settings.visible = false;
|
||||
|
||||
if (this.scene.sleep)
|
||||
{
|
||||
this.scene.sleep.call(this.scene);
|
||||
}
|
||||
this.events.emit('sleep', this);
|
||||
},
|
||||
|
||||
wake: function ()
|
||||
|
@ -179,10 +200,7 @@ var Systems = new Class({
|
|||
this.settings.active = true;
|
||||
this.settings.visible = true;
|
||||
|
||||
if (this.scene.wake)
|
||||
{
|
||||
this.scene.wake.call(this.scene);
|
||||
}
|
||||
this.events.emit('wake', this);
|
||||
},
|
||||
|
||||
start: function (data)
|
||||
|
@ -193,6 +211,8 @@ var Systems = new Class({
|
|||
|
||||
this.settings.active = true;
|
||||
this.settings.visible = true;
|
||||
|
||||
this.events.emit('start', this);
|
||||
},
|
||||
|
||||
shutdown: function ()
|
||||
|
@ -203,21 +223,11 @@ var Systems = new Class({
|
|||
this.settings.visible = false;
|
||||
|
||||
this.events.emit('shutdown', this);
|
||||
|
||||
if (this.scene.shutdown)
|
||||
{
|
||||
this.scene.shutdown.call(this.scene);
|
||||
}
|
||||
},
|
||||
|
||||
destroy: function ()
|
||||
{
|
||||
this.events.emit('destroy', this);
|
||||
|
||||
if (this.scene.destroy)
|
||||
{
|
||||
this.scene.destroy.call(this.scene);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue