Moved Camera3D to optional plugin

Also included build flag to allow it to be compiled into the standard lib if required (saves a bunch of KB in duplicated classes)
This commit is contained in:
Richard Davey 2018-08-23 15:30:21 +01:00
parent beb286297c
commit b25ff9e065
16 changed files with 18485 additions and 56 deletions

View file

@ -19,6 +19,7 @@
"build": "webpack",
"watch": "webpack --watch",
"dist": "webpack --config webpack.dist.config.js",
"plugin.cam3d": "webpack --config plugins/camera3d/webpack.config.js",
"lint": "eslint --config .eslintrc.json \"src/**/*.js\"",
"lintfix": "eslint --config .eslintrc.json \"src/**/*.js\" --fix",
"sloc": "node-sloc \"./src\" --include-extensions \"js\"",

18076
plugins/camera3d/dist/camera3d.js vendored Normal file

File diff suppressed because it is too large Load diff

1
plugins/camera3d/dist/camera3d.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,340 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var BuildGameObject = require('../../../src/gameobjects/BuildGameObject');
var BuildGameObjectAnimation = require('../../../src/gameobjects/BuildGameObjectAnimation');
var Class = require('../../../src/utils/Class');
var GetAdvancedValue = require('../../../src/utils/object/GetAdvancedValue');
var OrthographicCamera = require('./OrthographicCamera');
var PerspectiveCamera = require('./PerspectiveCamera');
var ScenePlugin = require('../../../src/plugins/ScenePlugin');
var Sprite3D = require('./sprite3d/Sprite3D');
/**
* @classdesc
* The Camera 3D Plugin adds a new Camera type to Phaser that allows for movement and rendering
* in 3D space. It displays a special type of Sprite called a Sprite3D that is a billboard sprite,
* with a z-axis allowing for perspective depth.
*
* This is an external plugin which you can include in your game by preloading it:
*
* ```javascript
* this.load.scenePlugin({
* key: 'Camera3DPlugin',
* url: 'plugins/camera3d.min.js',
* sceneKey: 'cameras3d'
* });
* ```
*
* Once loaded you can create a 3D Camera using the `camera3d` property of a Scene:
*
* `var camera = this.cameras3d.add(85).setZ(500).setPixelScale(128);`
*
* See the examples for more information.
*
* @class Camera3DPlugin
* @constructor
*
* @param {Phaser.Scene} scene - The Scene to which this plugin is being installed.
* @param {Phaser.Plugins.PluginManager} pluginManager - A reference to the Phaser Plugin Manager.
*/
var Camera3DPlugin = new Class({
Extends: ScenePlugin,
initialize:
function Camera3DPlugin (scene, pluginManager)
{
ScenePlugin.call(this, scene, pluginManager);
/**
* An Array of the Camera objects being managed by this Camera Manager.
*
* @name Camera3DPlugin#cameras
* @type {Phaser.Cameras.Sprite3D.Camera[]}
* @since 3.0.0
*/
this.cameras = [];
// Register the Sprite3D Game Object
pluginManager.registerGameObject('sprite3D', this.sprite3DFactory, this.sprite3DCreator);
},
/**
* Creates a new Sprite3D Game Object and adds it to the Scene.
*
* @method Phaser.GameObjects.GameObjectFactory#sprite3D
* @since 3.0.0
*
* @param {number} x - The horizontal position of this Game Object.
* @param {number} y - The vertical position of this Game Object.
* @param {number} z - The z position of this Game Object.
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
*
* @return {Phaser.GameObjects.Sprite3D} The Game Object that was created.
*/
sprite3DFactory: function (x, y, z, key, frame)
{
var sprite = new Sprite3D(this.scene, x, y, z, key, frame);
this.displayList.add(sprite.gameObject);
this.updateList.add(sprite.gameObject);
return sprite;
},
/**
* Creates a new Sprite3D Game Object and returns it.
*
* @method Phaser.GameObjects.GameObjectCreator#sprite3D
* @since 3.0.0
*
* @param {object} config - The configuration object this Game Object will use to create itself.
* @param {boolean} [addToScene] - Add this Game Object to the Scene after creating it? If set this argument overrides the `add` property in the config object.
*
* @return {Phaser.GameObjects.Sprite3D} The Game Object that was created.
*/
sprite3DCreator: function (config, addToScene)
{
if (config === undefined) { config = {}; }
var key = GetAdvancedValue(config, 'key', null);
var frame = GetAdvancedValue(config, 'frame', null);
var sprite = new Sprite3D(this.scene, 0, 0, key, frame);
if (addToScene !== undefined)
{
config.add = addToScene;
}
BuildGameObject(this.scene, sprite, config);
// Sprite specific config options:
BuildGameObjectAnimation(sprite, config);
return sprite;
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.Cameras.Scene3D.CameraManager#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.systems.events.once('destroy', this.destroy, this);
},
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
*
* @method Camera3DPlugin#start
* @private
* @since 3.5.0
*/
start: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.on('update', this.update, this);
eventEmitter.once('shutdown', this.shutdown, this);
},
/**
* [description]
*
* @method Camera3DPlugin#add
* @since 3.0.0
*
* @param {number} [fieldOfView=80] - [description]
* @param {number} [width] - [description]
* @param {number} [height] - [description]
*
* @return {Phaser.Cameras.Sprite3D.PerspectiveCamera} [description]
*/
add: function (fieldOfView, width, height)
{
return this.addPerspectiveCamera(fieldOfView, width, height);
},
/**
* [description]
*
* @method Camera3DPlugin#addOrthographicCamera
* @since 3.0.0
*
* @param {number} width - [description]
* @param {number} height - [description]
*
* @return {Phaser.Cameras.Sprite3D.OrthographicCamera} [description]
*/
addOrthographicCamera: function (width, height)
{
var config = this.scene.sys.game.config;
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new OrthographicCamera(this.scene, width, height);
this.cameras.push(camera);
return camera;
},
/**
* [description]
*
* @method Camera3DPlugin#addPerspectiveCamera
* @since 3.0.0
*
* @param {number} [fieldOfView=80] - [description]
* @param {number} [width] - [description]
* @param {number} [height] - [description]
*
* @return {Phaser.Cameras.Sprite3D.PerspectiveCamera} [description]
*/
addPerspectiveCamera: function (fieldOfView, width, height)
{
var config = this.scene.sys.game.config;
if (fieldOfView === undefined) { fieldOfView = 80; }
if (width === undefined) { width = config.width; }
if (height === undefined) { height = config.height; }
var camera = new PerspectiveCamera(this.scene, fieldOfView, width, height);
this.cameras.push(camera);
return camera;
},
/**
* [description]
*
* @method Camera3DPlugin#getCamera
* @since 3.0.0
*
* @param {string} name - [description]
*
* @return {(Phaser.Cameras.Sprite3D.OrthographicCamera|Phaser.Cameras.Sprite3D.PerspectiveCamera)} [description]
*/
getCamera: function (name)
{
for (var i = 0; i < this.cameras.length; i++)
{
if (this.cameras[i].name === name)
{
return this.cameras[i];
}
}
return null;
},
/**
* [description]
*
* @method Camera3DPlugin#removeCamera
* @since 3.0.0
*
* @param {(Phaser.Cameras.Sprite3D.OrthographicCamera|Phaser.Cameras.Sprite3D.PerspectiveCamera)} camera - [description]
*/
removeCamera: function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex !== -1)
{
this.cameras.splice(cameraIndex, 1);
}
},
/**
* [description]
*
* @method Camera3DPlugin#removeAll
* @since 3.0.0
*
* @return {(Phaser.Cameras.Sprite3D.OrthographicCamera|Phaser.Cameras.Sprite3D.PerspectiveCamera)} [description]
*/
removeAll: function ()
{
while (this.cameras.length > 0)
{
var camera = this.cameras.pop();
camera.destroy();
}
return this.main;
},
/**
* [description]
*
* @method Camera3DPlugin#update
* @since 3.0.0
*
* @param {number} timestep - [description]
* @param {number} delta - [description]
*/
update: function (timestep, delta)
{
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].update(timestep, delta);
}
},
/**
* The Scene that owns this plugin is shutting down.
* We need to kill and reset all internal properties as well as stop listening to Scene events.
*
* @method Camera3DPlugin#shutdown
* @private
* @since 3.0.0
*/
shutdown: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.off('update', this.update, this);
eventEmitter.off('shutdown', this.shutdown, this);
this.removeAll();
},
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
*
* @method Camera3DPlugin#destroy
* @private
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.pluginManager = null;
this.game = null;
this.scene = null;
this.systems = null;
}
});
module.exports = Camera3DPlugin;

View file

@ -57,7 +57,6 @@ var CameraManager = new Class({
scene.sys.events.on('start', this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.

View file

@ -16,19 +16,3 @@ module.exports = {
PerspectiveCamera: require('./PerspectiveCamera')
};
/*
var GameObjects = {
Sprite3D: require('./sprite3d/Sprite3D'),
Factories: {
Sprite3D: require('./sprite3d/Sprite3DFactory')
},
Creators: {
Sprite3D: require('./sprite3d/Sprite3DCreator')
}
};
*/

View file

@ -4,11 +4,11 @@
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var GameObject = require('../GameObject');
var Sprite = require('../sprite/Sprite');
var Vector2 = require('../../math/Vector2');
var Vector4 = require('../../math/Vector4');
var Class = require('../../../../src/utils/Class');
var GameObject = require('../../../../src/gameobjects/GameObject');
var Sprite = require('../../../../src/gameobjects/sprite/Sprite');
var Vector2 = require('../../../../src/math/Vector2');
var Vector4 = require('../../../../src/math/Vector4');
/**
* @classdesc

View file

@ -4,10 +4,10 @@
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var BuildGameObject = require('../BuildGameObject');
var BuildGameObjectAnimation = require('../BuildGameObjectAnimation');
var GameObjectCreator = require('../GameObjectCreator');
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
var BuildGameObject = require('../../../../src/gameobjects/BuildGameObject');
var BuildGameObjectAnimation = require('../../../../src/gameobjects/BuildGameObjectAnimation');
var GameObjectCreator = require('../../../../src/gameobjects/GameObjectCreator');
var GetAdvancedValue = require('../../../../src/utils/object/GetAdvancedValue');
var Sprite3D = require('./Sprite3D');
/**

View file

@ -5,7 +5,7 @@
*/
var Sprite3D = require('./Sprite3D');
var GameObjectFactory = require('../GameObjectFactory');
var GameObjectFactory = require('../../../../src/gameobjects/GameObjectFactory');
/**
* Creates a new Sprite3D Game Object and adds it to the Scene.

View file

@ -1,22 +1,47 @@
'use strict';
const webpack = require('webpack');
const exec = require('child_process').exec;
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
mode: 'development',
mode: 'production',
context: `${__dirname}/src/`,
entry: {
camera3d: './index.js'
camera3d: './Camera3DPlugin.js',
'camera3d.min': './Camera3DPlugin.js'
},
output: {
path: `${__dirname}/build/`,
path: `${__dirname}/dist/`,
filename: '[name].js',
library: 'PhaserCamera3DPlugin',
libraryTarget: 'umd',
umdNamedDefine: true
}
library: 'Camera3DPlugin',
libraryTarget: 'var'
},
performance: { hints: false },
optimization: {
minimizer: [
new UglifyJSPlugin({
include: /\.min\.js$/,
parallel: true,
sourceMap: false,
uglifyOptions: {
compress: true,
ie8: false,
ecma: 5,
output: {comments: false},
warnings: false
},
warningsFilter: () => false
})
]
},
plugins: [
new CleanWebpackPlugin([ 'dist' ])
]
};

View file

@ -48,6 +48,16 @@ var Phaser = {
};
// Merge in the optional plugins
if (typeof PLUGIN_CAMERA3D)
{
Phaser.Cameras.Sprite3D = require('../plugins/camera3d/src');
Phaser.GameObjects.Sprite3D = require('../plugins/camera3d/src/sprite3d/Sprite3D');
Phaser.GameObjects.Factories.Sprite3D = require('../plugins/camera3d/src/sprite3d/Sprite3DFactory');
Phaser.GameObjects.Creators.Sprite3D = require('../plugins/camera3d/src/sprite3d/Sprite3DCreator');
}
// Merge in the consts
Phaser = Extend(false, Phaser, CONST);

View file

@ -86,4 +86,9 @@ var DefaultPlugins = {
};
if (typeof PLUGIN_CAMERA3D)
{
DefaultPlugins.DefaultScene.push('CameraManager3D');
}
module.exports = DefaultPlugins;

View file

@ -32,26 +32,7 @@ var ScenePlugin = new Class({
{
BasePlugin.call(this, pluginManager);
/**
* A reference to the Scene that has installed this plugin.
* This property is only set when the plugin is instantiated and added to the Scene, not before.
*
* @name Phaser.Plugins.ScenePlugin#scene
* @type {?Phaser.Scene}
* @protected
* @since 3.8.0
*/
this.scene = scene;
/**
* A reference to the Scene Systems of the Scene that has installed this plugin.
* This property is only set when the plugin is instantiated and added to the Scene, not before.
*
* @name Phaser.Plugins.ScenePlugin#systems
* @type {?Phaser.Scenes.Systems}
* @protected
* @since 3.8.0
*/
this.systems = scene.sys;
scene.sys.events.once('boot', this.boot, this);

View file

@ -46,4 +46,9 @@ var InjectionMap = {
};
if (typeof PLUGIN_CAMERA3D)
{
InjectionMap.cameras3d = 'cameras3d';
}
module.exports = InjectionMap;

View file

@ -29,7 +29,8 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
"typeof CANVAS_RENDERER": JSON.stringify(true),
"typeof WEBGL_RENDERER": JSON.stringify(true)
"typeof WEBGL_RENDERER": JSON.stringify(true),
"typeof PLUGIN_CAMERA3D": JSON.stringify(false)
}),
{
apply: (compiler) => {

View file

@ -49,7 +49,8 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
"typeof CANVAS_RENDERER": JSON.stringify(true),
"typeof WEBGL_RENDERER": JSON.stringify(true)
"typeof WEBGL_RENDERER": JSON.stringify(true),
"typeof PLUGIN_CAMERA3D": JSON.stringify(false)
}),
new CleanWebpackPlugin([ 'dist' ])