Comparing version 0.7.1 to 0.7.2
@@ -8,5 +8,12 @@ # Change Log | ||
## [Unreleased][unreleased] | ||
- [FEATURE] Add CHANGELOG.md file | ||
Beta-0 | ||
## [0.7.2] - 2015-06-22 | ||
Wake up kissing | ||
- [#11](https://github.com/darlingjs/darlingjs/issues/11) [feature] add npm support - organize source in node way with `require` of deps | ||
- [#3](https://github.com/darlingjs/darlingjs/issues/3) [fix] fix travis ci | ||
- [#4](https://github.com/darlingjs/darlingjs/issues/4) update libs | ||
- [#6](https://github.com/darlingjs/darlingjs/issues/6) add CHANGELOG.md file | ||
## [0.7.1] - 2013-07-17 | ||
- It is final stable version of game engine that was exist for a long time |
module.exports = function(grunt) { | ||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
concat: { | ||
options: { | ||
banner: grunt.file.read('src/core/core.banner') | ||
browserify: { | ||
engine: { | ||
files: { | ||
'build/<%= pkg.shortName %>.js': [ | ||
'index.js', | ||
'src/utils/**/*.js', | ||
'src/core/**/*.js' | ||
] | ||
} | ||
}, | ||
dist: { | ||
src: [ | ||
'src/core/core.prefix', | ||
'src/utils/**/*.js', | ||
'src/core/**/*.js', | ||
'src/core/core.suffix' | ||
], | ||
dest: 'build/<%= pkg.shortName %>.js' | ||
tests: { | ||
files: { | ||
'build/test_bundle.js': [ | ||
//'index.js', | ||
//'src/utils/**/*.js', | ||
//'src/core/**/*.js', | ||
'spec/**/*.js' | ||
] | ||
} | ||
} | ||
@@ -34,11 +42,11 @@ }, | ||
jasmine: { | ||
pivotal: { | ||
engine: { | ||
src: [ | ||
'src/utils/**/*.js', | ||
'src/core/**/*.js', | ||
'spec/lib/**/*.js' | ||
'build/<%= pkg.shortName %>.js' | ||
], | ||
options: { | ||
specs: 'spec/suites/core/**/*Spec.js', | ||
helpers: ['spec/**/*Helper.js', 'node_modules/jasmine-sinon/lib/jasmine-sinon.js'] | ||
specs: 'build/test_bundle.js', | ||
helpers: [ | ||
'node_modules/jasmine-sinon/lib/jasmine-sinon.js' | ||
] | ||
} | ||
@@ -49,3 +57,4 @@ } | ||
docs: ['docs/'], | ||
build: ['build/**/*', 'build/.git'] | ||
engine: ['build/**/*', 'build/.git'], | ||
test: ['build/test_bundle.js'] | ||
}, | ||
@@ -98,9 +107,9 @@ copy: { | ||
// Default task(s). | ||
grunt.registerTask('default', ['jasmine', 'concat', 'uglify', 'copy', 'version']); | ||
grunt.registerTask('build', ['clean:build', 'concat', 'uglify', 'copy', 'version']); | ||
grunt.registerTask('test', ['concat', 'jasmine']); | ||
grunt.registerTask('default', ['test', 'build']); | ||
grunt.registerTask('build', ['clean:engine', 'browserify:engine', 'uglify', 'copy', 'version']); | ||
grunt.registerTask('test', ['browserify', 'jasmine', 'clean:test']); | ||
//grunt.registerTask('docs', ['clean', 'yuidoc']); | ||
grunt.registerTask('docs', ['clean', 'jsdoc']); | ||
grunt.registerTask('docs', ['clean:docs', 'jsdoc']); | ||
grunt.loadNpmTasks('grunt-contrib-concat'); | ||
grunt.loadNpmTasks('grunt-browserify'); | ||
grunt.loadNpmTasks('grunt-contrib-copy'); | ||
@@ -107,0 +116,0 @@ grunt.loadNpmTasks('grunt-contrib-uglify'); |
@@ -5,3 +5,3 @@ { | ||
"title": "DarlingJS Game Engine", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"homepage": "http://darlingjs.github.io/", | ||
@@ -39,5 +39,5 @@ "description": "Component & Entity based javascript game engine. Decoupled from any visualization, physics, and so on. With injections and modules based on AngularJS.", | ||
"grunt": "~0.4.5", | ||
"grunt-browserify": "^3.8.0", | ||
"grunt-cli": "~0.1", | ||
"grunt-contrib-clean": "~0.6.0", | ||
"grunt-contrib-concat": "~0.5.1", | ||
"grunt-contrib-copy": "^0.8.0", | ||
@@ -51,4 +51,6 @@ "grunt-contrib-jasmine": "~0.8.2", | ||
"jasmine": "^2.3.1", | ||
"jasmine-sinon": "^0.4.0" | ||
"jasmine-sinon": "^0.4.0", | ||
"sinon": "^1.15.3", | ||
"to-have-property": "^0.2.2" | ||
} | ||
} |
@@ -6,4 +6,9 @@ /** | ||
var darlingjs = require('./../../../'); | ||
var sinon = require('sinon'); | ||
describe('darling', function() { | ||
'use strict'; | ||
afterEach(function() { | ||
@@ -10,0 +15,0 @@ darlingjs.removeModule('theModule'); |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,3 +6,8 @@ * Project: GameEngine. | ||
var darlingjs = require('./../../../'); | ||
var sinon = require('sinon'); | ||
describe('entity', function() { | ||
'use strict'; | ||
var module, | ||
@@ -10,0 +14,0 @@ world; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,3 +6,8 @@ * Project: GameEngine. | ||
var List = require('./../../../src/utils/list'); | ||
var sinon = require('sinon'); | ||
describe('list', function() { | ||
'use strict'; | ||
it('should be empty', function() { | ||
@@ -10,0 +14,0 @@ var list = new List(); |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,3 +6,8 @@ * Project: GameEngine. | ||
var darlingjs = require('./../../../'); | ||
var sinon = require('sinon'); | ||
describe('module', function() { | ||
'use strict'; | ||
var module; | ||
@@ -10,0 +14,0 @@ beforeEach(function() { |
@@ -6,2 +6,5 @@ /** | ||
var darlingjs = require('./../../../'); | ||
var sinon = require('sinon'); | ||
describe('system', function() { | ||
@@ -8,0 +11,0 @@ 'use strict'; |
/*jslint node: true */ | ||
'use strict'; | ||
@@ -9,3 +8,8 @@ /** | ||
var darlingjs = require('./../../../'); | ||
var requestAnimationFrame = require('./../../../src/utils/animationFrame').request; | ||
var sinon = require('sinon'); | ||
describe('World', function() { | ||
'use strict'; | ||
var world; | ||
@@ -292,3 +296,3 @@ var module; | ||
function subscribeRequestAnimationFrame() { | ||
window.requestAnimationFrame(increase); | ||
requestAnimationFrame(increase); | ||
} | ||
@@ -295,0 +299,0 @@ |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,3 +6,7 @@ * Project: GameEngine. | ||
var darlingjs = require('./../../../../'); | ||
var sinon = require('sinon'); | ||
describe('box2dEmscriptenAdapter', function() { | ||
'use strict'; | ||
var world; | ||
@@ -10,0 +13,0 @@ beforeEach(function() { |
@@ -7,3 +7,11 @@ 'use strict'; | ||
describe('box2dModule', function() { | ||
var darlingjs = require('./../../../../'); | ||
var sinon = require('sinon'); | ||
//TODO: will fix later there https://github.com/darlingjs/darlingjs/issues/14 | ||
//require('./../../../../src/extensions/space/flatland.js'); | ||
//require('./../../../../src/extensions/box2dweb/box2dWebModule.js'); | ||
xdescribe('box2dModule', function() { | ||
var world; | ||
@@ -10,0 +18,0 @@ beforeEach(function() { |
@@ -7,2 +7,6 @@ 'use strict'; | ||
var darlingjs = require('./../../../../'); | ||
var sinon = require('sinon'); | ||
require('./../../../../src/extensions/space/flatland.js'); | ||
describe('flatland', function() { | ||
@@ -9,0 +13,0 @@ var world; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -6,3 +5,11 @@ * Project: darlingjs / GameEngine. | ||
*/ | ||
var darlingjs = require('./../../../../'); | ||
var sinon = require('sinon'); | ||
require('./../../../../src/extensions/space/flatland.js'); | ||
require('./../../../../src/extensions/generators/infinity1D.js'); | ||
describe('infinity1DWorldGenerator', function() { | ||
'use strict'; | ||
var world, | ||
@@ -9,0 +16,0 @@ viewPort; |
@@ -6,2 +6,9 @@ /** | ||
var darlingjs = require('./../../../../'); | ||
var sinon = require('sinon'); | ||
require('to-have-property'); | ||
require('./../../../../src/extensions/particles/particleSystem.js'); | ||
require('./../../../../src/extensions/space/flatland.js'); | ||
describe('Particle System', function() { | ||
@@ -11,28 +18,5 @@ 'use strict'; | ||
var world; | ||
beforeEach(function() { | ||
world = darlingjs.world('theWorld', ['ngParticleSystem', 'ngFlatland']); | ||
this.addMatchers({ | ||
toHas: function(expected) { | ||
var actual = this.actual; | ||
var notText = this.isNot ? ' not' : ''; | ||
this.message = function () { | ||
return 'Expected entity "' + actual.$name + '" ' + notText + ' has component "' + expected + '"'; | ||
}; | ||
return actual.$has(expected); | ||
}, | ||
calledWith: function() { | ||
var notText = this.isNot ? ' not' : ''; | ||
this.message = function() { | ||
return 'Expected that function ' + notText + ' has called with ' + Array.prototype.join.call(arguments, ', '); | ||
}; | ||
return this.actual.calledWith.apply(this.actual, arguments); | ||
} | ||
}); | ||
}); | ||
@@ -60,3 +44,3 @@ | ||
world.$update(1000); | ||
expect(emitter).toHas('ngEmit'); | ||
expect(emitter).toHaveProperty('ngEmit'); | ||
expect(emitter.ngEmit.count).toBeGreaterThan(0); | ||
@@ -88,3 +72,3 @@ expect(emitter.ngEmit.count).toBeLessThan(3); | ||
emitter.$add('ngEmit'); | ||
world.$update(100); | ||
expect(world.$numEntities()).toBe(2); | ||
@@ -147,3 +131,3 @@ var particle = world.$getByName('particle'); | ||
expect(factory).calledWith(emitter); | ||
expect(factory).toHaveBeenCalledWith(emitter); | ||
}); | ||
@@ -150,0 +134,0 @@ |
@@ -5,2 +5,6 @@ /** | ||
*/ | ||
var darlingutil = require('./../../../src/utils/utils'); | ||
var sinon = require('sinon'); | ||
describe('darlingutil', function() { | ||
@@ -7,0 +11,0 @@ 'use strict'; |
@@ -6,11 +6,12 @@ /** | ||
/** | ||
* Previous instance of facade of darlingjs engine | ||
* | ||
* @ignore | ||
* @type {darlingjs} | ||
* @private | ||
*/ | ||
var _darlingjs = window.darlingjs; | ||
var List = require('./../utils/list'); | ||
var isArray = require('./../utils/utils').isArray; | ||
var isDefined = require('./../utils/utils').isDefined; | ||
var isString = require('./../utils/utils').isString; | ||
var isUndefined = require('./../utils/utils').isUndefined; | ||
var utils = require('./../utils/utils'); | ||
var Module = require('./module'); | ||
var World = require('./world'); | ||
/** | ||
@@ -23,3 +24,3 @@ * @class darlingjs | ||
*/ | ||
var darlingjs = window.darlingjs || (window.darlingjs = {}); | ||
var darlingjs = {}; | ||
darlingjs.version = '0.0.0'; | ||
@@ -31,14 +32,3 @@ | ||
/** | ||
* Restores the previous global value of darlingjs and returns the current instance. Other libraries may already use the | ||
* darlingjs namespace. Or a previous version of darlingjs is already loaded on the page. In these cases you may want to | ||
* restore the previous namespace and keep a reference to darlingjs. | ||
* | ||
* @return {darlingjs} The current darlingjs namespace | ||
*/ | ||
darlingjs.noConflict = function() { | ||
var a = window.darlingjs; | ||
window.darlingjs = _darlingjs; | ||
return a; | ||
}; | ||
darlingjs.List = List; | ||
@@ -53,6 +43,6 @@ /** | ||
* @param {String} name The name of new module | ||
* @param {Array} [requires] The array of modules that new module it depends on | ||
* @param {Array} [deps] The array of modules that new module it depends on | ||
* @return {Module} | ||
*/ | ||
darlingjs.m = darlingjs.module = function(name, requires) { | ||
darlingjs.m = darlingjs.module = function(name, deps) { | ||
if (isDefined(modules[name])) { | ||
@@ -63,3 +53,3 @@ throw new Error('Module "' + name + '" has already been defined.'); | ||
moduleInstance.$name = name; | ||
moduleInstance.requires = requires; | ||
moduleInstance.requires = deps; | ||
@@ -87,3 +77,3 @@ modules[name] = moduleInstance; | ||
*/ | ||
darlingjs.w = darlingjs.world = function(name, requires) { | ||
darlingjs.w = darlingjs.world = function(name, deps) { | ||
if (isDefined(worlds[name])) { | ||
@@ -97,5 +87,5 @@ throw new Error('World "' + name + '" has already been defined.'); | ||
if (isArray(requires)) { | ||
for (var index = 0, count = requires.length; index < count; index++) { | ||
var moduleName = requires[index]; | ||
if (isArray(deps)) { | ||
for (var index = 0, count = deps.length; index < count; index++) { | ||
var moduleName = deps[index]; | ||
var moduleInstance = modules[moduleName]; | ||
@@ -186,2 +176,4 @@ if (isUndefined(moduleInstance)) { | ||
darlingjs.utils = utils; | ||
module.exports = darlingjs; |
@@ -6,2 +6,13 @@ /** | ||
'use strict'; | ||
var Events = require('./../utils/events'); | ||
var isArray = require('./../utils/utils').isArray; | ||
var isDefined = require('./../utils/utils').isDefined; | ||
var isFunction = require('./../utils/utils').isFunction; | ||
var isObject = require('./../utils/utils').isObject; | ||
var isString = require('./../utils/utils').isString; | ||
var isUndefined = require('./../utils/utils').isUndefined; | ||
var mixin = require('./../utils/utils').mixin; | ||
/** | ||
@@ -146,10 +157,10 @@ * @class Entity | ||
Entity.prototype.$applyModifier = function(modifier) { | ||
if (darlingutil.isFunction(modifier)) { | ||
if (isFunction(modifier)) { | ||
this.$$applyModifierFunction(modifier); | ||
} else { | ||
if (darlingutil.isString(modifier)) { | ||
if (isString(modifier)) { | ||
this.$add(modifier); | ||
} else if (darlingutil.isArray(modifier)) { | ||
} else if (isArray(modifier)) { | ||
this.$applyModifierArray(modifier); | ||
} else if (darlingutil.isObject(modifier)) { | ||
} else if (isObject(modifier)) { | ||
this.$applyModifierObject(modifier); | ||
@@ -167,3 +178,3 @@ } else { | ||
modifier = modifier.call(this); | ||
if (darlingutil.isDefined(modifier)) { | ||
if (isDefined(modifier)) { | ||
this.$applyModifier(modifier); | ||
@@ -190,3 +201,3 @@ } | ||
var config = modifier[key]; | ||
if (darlingutil.isFunction(config)) { | ||
if (isFunction(config)) { | ||
config = config.call(this); | ||
@@ -210,8 +221,8 @@ } | ||
Entity.prototype.$revertModifier = function(modifier) { | ||
if (!darlingutil.isFunction(modifier)) { | ||
if (darlingutil.isString(modifier)) { | ||
if (!isFunction(modifier)) { | ||
if (isString(modifier)) { | ||
this.$remove(modifier); | ||
} else if (darlingutil.isArray(modifier)) { | ||
} else if (isArray(modifier)) { | ||
this.$revertModifierArray(modifier); | ||
} else if (darlingutil.isObject(modifier)) { | ||
} else if (isObject(modifier)) { | ||
this.$revertModifierObject(modifier); | ||
@@ -246,2 +257,4 @@ } else { | ||
return isObject(value) && isDefined(value.$name); | ||
} | ||
} | ||
module.exports = Entity; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -12,2 +11,7 @@ * Project: GameEngine. | ||
'use strict'; | ||
var List = require('./../utils/list'); | ||
var isDefined = require('./../utils/utils').isDefined; | ||
/** | ||
@@ -73,2 +77,4 @@ * @private | ||
return e.$$familyMarker && e.$$familyMarker[this.$$marker]; | ||
}; | ||
}; | ||
module.exports = Family; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,2 +6,7 @@ * Project: GameEngine. | ||
'use strict'; | ||
var isDefined = require('./../utils/utils').isDefined; | ||
var isUndefined = require('./../utils/utils').isUndefined; | ||
/** | ||
@@ -132,2 +136,4 @@ * @class Module | ||
return this; | ||
}; | ||
}; | ||
module.exports = Module; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -7,2 +6,14 @@ * Project: GameEngine. | ||
'use strict'; | ||
var Events = require('./../utils/events'); | ||
var isArray = require('./../utils/utils').isArray; | ||
var isDefined = require('./../utils/utils').isDefined; | ||
var isFunction = require('./../utils/utils').isFunction; | ||
var isObject = require('./../utils/utils').isObject; | ||
var isString = require('./../utils/utils').isString; | ||
var isUndefined = require('./../utils/utils').isUndefined; | ||
var List = require('./../utils/list'); | ||
var mixin = require('./../utils/utils').mixin; | ||
/** | ||
@@ -90,2 +101,4 @@ * @class System | ||
}; | ||
}; | ||
}; | ||
module.exports = System; |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -15,2 +13,24 @@ * Project: GameEngine. | ||
'use strict'; | ||
var Entity = require('./entity'); | ||
var Family = require('./family'); | ||
var annotate = require('./../utils/utils').annotate; | ||
var copy = require('./../utils/utils').copy; | ||
var factoryOfFastFunction = require('./../utils/utils').factoryOfFastFunction; | ||
var factoryOfFastFunctionWithMatcher = require('./../utils/utils').factoryOfFastFunctionWithMatcher; | ||
var isArray = require('./../utils/utils').isArray; | ||
var isDefined = require('./../utils/utils').isDefined; | ||
var isObject = require('./../utils/utils').isObject; | ||
var isString = require('./../utils/utils').isString; | ||
var isUndefined = require('./../utils/utils').isUndefined; | ||
var List = require('./../utils/list') | ||
var noop = require('./../utils/utils').noop; | ||
var swallowCopy = require('./../utils/utils').swallowCopy; | ||
var System = require('./system'); | ||
var requestAnimationFrame = require('./../utils/animationFrame').request; | ||
var cancelAnimationFrame = require('./../utils/animationFrame').cancel; | ||
/** | ||
@@ -924,3 +944,3 @@ * @class World | ||
if (self.$playing) { | ||
self.$requestAnimationFrameId = window.requestAnimationFrame(step); | ||
self.$requestAnimationFrameId = requestAnimationFrame(step); | ||
} | ||
@@ -939,3 +959,5 @@ })(0); | ||
window.cancelAnimationFrame(this.$requestAnimationFrameId); | ||
}; | ||
cancelAnimationFrame(this.$requestAnimationFrameId); | ||
}; | ||
module.exports = World; |
@@ -6,3 +6,8 @@ /** | ||
(function(darlingjs, darlingutil) { | ||
var darlingjs = require('./../../../'); | ||
var darlingutil = require('./../../../src/utils/utils'); | ||
//TODO: | ||
//request Box2D module | ||
'use strict'; | ||
@@ -16,2 +21,3 @@ | ||
var Body = Box2D.Dynamics.b2Body; | ||
var ContactListener = Box2D.Dynamics.b2ContactListener; | ||
var FixtureDef = Box2D.Dynamics.b2FixtureDef; | ||
@@ -877,3 +883,3 @@ var Fixture = Box2D.Dynamics.b2Fixture; | ||
_addContactListener: function (callbacks) { | ||
var listener = new Box2D.Dynamics.b2ContactListener(); | ||
var listener = new ContactListener(); | ||
@@ -893,3 +899,1 @@ if(callbacks.PostSolve) { | ||
}); | ||
})(darlingjs, darlingutil); |
@@ -18,85 +18,86 @@ /** | ||
(function(darlingjs, darlingutil) { | ||
'use strict'; | ||
var darlingjs = require('./../../../'); | ||
var darlingutil = require('./../../utils/utils'); | ||
var List = require('./../../utils/list'); | ||
var m = darlingjs.module('ngInfinity1DWorld'); | ||
/** | ||
* System for generation infinity 1d world | ||
* | ||
* | ||
*/ | ||
m.$s('ngInfinity1DWorld', { | ||
_list: null, | ||
'use strict'; | ||
_lastLeftClampTile: null, | ||
_lastRightClampTile: null, | ||
var m = darlingjs.module('ngInfinity1DWorld'); | ||
/** | ||
* System for generation infinity 1d world | ||
* | ||
* | ||
*/ | ||
m.$s('ngInfinity1DWorld', { | ||
_list: null, | ||
seed: null, | ||
generator: null, | ||
removeUnseen: true, | ||
_lastLeftClampTile: null, | ||
_lastRightClampTile: null, | ||
$added: ['ng2DViewPort', '$world', function(ng2DViewPort, $world) { | ||
if (this.generator === null) { | ||
throw new Error('To use ngInfinity1DWorld you should define generator function. To create next node of infinity world'); | ||
} | ||
this._list = new darlingutil.List(); | ||
this._generate(ng2DViewPort, $world); | ||
}], | ||
seed: null, | ||
generator: null, | ||
removeUnseen: true, | ||
$update: ['ng2DViewPort', '$world', function(ng2DViewPort, $world) { | ||
this._generate(ng2DViewPort, $world); | ||
}], | ||
$added: ['ng2DViewPort', '$world', function(ng2DViewPort, $world) { | ||
if (this.generator === null) { | ||
throw new Error('To use ngInfinity1DWorld you should define generator function. To create next node of infinity world'); | ||
} | ||
this._list = new List(); | ||
this._generate(ng2DViewPort, $world); | ||
}], | ||
_generate: function(ng2DViewPort, $world) { | ||
var width = ng2DViewPort.width, | ||
half = 0.5 * ng2DViewPort.width, | ||
center = ng2DViewPort.lookAt.x, | ||
leftClamp = center - half, | ||
rightClamp = center + half; | ||
$update: ['ng2DViewPort', '$world', function(ng2DViewPort, $world) { | ||
this._generate(ng2DViewPort, $world); | ||
}], | ||
var tile = this._list.$tail; | ||
_generate: function(ng2DViewPort, $world) { | ||
var width = ng2DViewPort.width, | ||
half = 0.5 * ng2DViewPort.width, | ||
center = ng2DViewPort.lookAt.x, | ||
leftClamp = center - half, | ||
rightClamp = center + half; | ||
//add new from right side | ||
while(!tile || tile.rightEdge < rightClamp) { | ||
var newRightTile = this._list.add(); | ||
this.generator(newRightTile, tile || this.seed, null); | ||
tile = newRightTile; | ||
} | ||
var tile = this._list.$tail; | ||
//this._lastRightClampTile = rightClampTile; | ||
//add new from right side | ||
while(!tile || tile.rightEdge < rightClamp) { | ||
var newRightTile = this._list.add(); | ||
this.generator(newRightTile, tile || this.seed, null); | ||
tile = newRightTile; | ||
} | ||
tile = this._list.$head; | ||
//this._lastRightClampTile = rightClampTile; | ||
tile = this._list.$head; | ||
// var leftClampTile = this._lastLeftClampTile || this._head; | ||
//add new from left side | ||
while(!tile || tile.leftEdge > leftClamp) { | ||
var newLeftTile = this._list.addHead(); | ||
this.generator(newLeftTile, null, tile || this.seed); | ||
tile = newLeftTile; | ||
} | ||
//remove old from right side | ||
tile = this._list.$head; | ||
var edge = leftClamp - width; | ||
while(tile && tile.rightEdge < edge) { | ||
var next = tile.$next; | ||
this._list.remove(tile); | ||
removeAllEntitiesFrom($world, tile.entities); | ||
tile.entities = null; | ||
tile = next; | ||
} | ||
//add new from left side | ||
while(!tile || tile.leftEdge > leftClamp) { | ||
var newLeftTile = this._list.addHead(); | ||
this.generator(newLeftTile, null, tile || this.seed); | ||
tile = newLeftTile; | ||
} | ||
}); | ||
function removeAllEntitiesFrom($world, entities) { | ||
//console.log('removeAllEntitesFrom'); | ||
if (darlingutil.isUndefined(entities) || entities === null) { | ||
return; | ||
//remove old from right side | ||
tile = this._list.$head; | ||
var edge = leftClamp - width; | ||
while(tile && tile.rightEdge < edge) { | ||
var next = tile.$next; | ||
this._list.remove(tile); | ||
removeAllEntitiesFrom($world, tile.entities); | ||
tile.entities = null; | ||
tile = next; | ||
} | ||
for (var i = 0, count = entities.length; i < count; i++) { | ||
//console.log('remove entity : ' + entities[i].$name); | ||
$world.$remove(entities[i]); | ||
} | ||
} | ||
}); | ||
})(darlingjs, darlingutil); | ||
function removeAllEntitiesFrom($world, entities) { | ||
//console.log('removeAllEntitesFrom'); | ||
if (darlingutil.isUndefined(entities) || entities === null) { | ||
return; | ||
} | ||
for (var i = 0, count = entities.length; i < count; i++) { | ||
//console.log('remove entity : ' + entities[i].$name); | ||
$world.$remove(entities[i]); | ||
} | ||
} |
@@ -12,335 +12,336 @@ /** | ||
(function(darlingjs) { | ||
'use strict'; | ||
'use strict'; | ||
var m = darlingjs.module('ngParticleSystem'); | ||
var darlingjs = require('./../../../'); | ||
var darlingutil = require('./../../utils/utils'); | ||
m.$c('ngEmitter', { | ||
generate: null | ||
}); | ||
var m = darlingjs.module('ngParticleSystem'); | ||
/** | ||
* Marker that use for emitter to note that it should emit particle | ||
*/ | ||
m.$c('ngEmit', { | ||
//number of particles to emit | ||
count: 1 | ||
}); | ||
m.$c('ngEmitter', { | ||
generate: null | ||
}); | ||
/** | ||
* Emit Particle in square area | ||
*/ | ||
m.$s('ngCircleEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng2D', 'ng2DSize'], | ||
/** | ||
* Marker that use for emitter to note that it should emit particle | ||
*/ | ||
m.$c('ngEmit', { | ||
//number of particles to emit | ||
count: 1 | ||
}); | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng2D, $entity.ng2DSize, $entity.ngEmitter.generate, $world); | ||
}], | ||
/** | ||
* Emit Particle in square area | ||
*/ | ||
m.$s('ngSquareEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng2D', 'ng2DSize'], | ||
_emit: function($entity, ng2D, ng2DSize, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng2D, $entity.ng2DSize, $entity.ngEmitter.generate, $world); | ||
}], | ||
var emitCount = $entity.ngEmit.count; | ||
if (darlingutil.isArray(generate)) { | ||
for(var i = 0, count = generate.length; i < count; i++) { | ||
this._emitOne($entity, ng2D, ng2DSize, generate[i], $world, emitCount); | ||
} | ||
} else { | ||
this._emitOne($entity, ng2D, ng2DSize, generate, $world, emitCount); | ||
_emit: function($entity, ng2D, ng2DSize, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
var emitCount = $entity.ngEmit.count; | ||
if (darlingutil.isArray(generate)) { | ||
for(var i = 0, count = generate.length; i < count; i++) { | ||
this._emitOne($entity, ng2D, ng2DSize, generate[i], $world, emitCount); | ||
} | ||
} else { | ||
this._emitOne($entity, ng2D, ng2DSize, generate, $world, emitCount); | ||
} | ||
$entity.$remove('ngEmit'); | ||
}, | ||
$entity.$remove('ngEmit'); | ||
}, | ||
_emitOne: function($entity, ng2D, ng2DSize, generate, $world, count) { | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
_emitOne: function($entity, ng2D, ng2DSize, generate, $world, count) { | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
generate.ng2D = generate.ng2D || {}; | ||
generate.ng2D.x = ng2D.x + ng2DSize.width * Math.random(); | ||
generate.ng2D.y = ng2D.y + ng2DSize.height * Math.random(); | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
generate.ng2D = generate.ng2D || {}; | ||
generate.ng2D.x = ng2D.x + ng2DSize.width * Math.random(); | ||
generate.ng2D.y = ng2D.y + ng2DSize.height * Math.random(); | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
}); | ||
} | ||
}); | ||
/** | ||
* Emit Particle in circle area | ||
*/ | ||
m.$s('ngSquareEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng2D', 'ng2DCircle'], | ||
/** | ||
* Emit Particle in circle area | ||
*/ | ||
m.$s('ngCircleEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng2D', 'ng2DCircle'], | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng2D, $entity.ng2DCircle, $entity.ngEmitter.generate, $world); | ||
}], | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng2D, $entity.ng2DCircle, $entity.ngEmitter.generate, $world); | ||
}], | ||
_emit: function($entity, ng2D, ng2DCircle, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
_emit: function($entity, ng2D, ng2DCircle, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
var emitCount = $entity.ngEmit.count; | ||
if (darlingutil.isArray(generate)) { | ||
for(var i = 0, count = generate.length; i < count; i++) { | ||
this._emitOne($entity, ng2D, ng2DCircle, generate[i], $world, emitCount); | ||
} | ||
} else { | ||
this._emitOne($entity, ng2D, ng2DCircle, generate, $world, emitCount); | ||
var emitCount = $entity.ngEmit.count; | ||
if (darlingutil.isArray(generate)) { | ||
for(var i = 0, count = generate.length; i < count; i++) { | ||
this._emitOne($entity, ng2D, ng2DCircle, generate[i], $world, emitCount); | ||
} | ||
} else { | ||
this._emitOne($entity, ng2D, ng2DCircle, generate, $world, emitCount); | ||
} | ||
$entity.$remove('ngEmit'); | ||
}, | ||
$entity.$remove('ngEmit'); | ||
}, | ||
_emitOne: function($entity, ng2D, ng2DCircle, generate, $world, count) { | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
_emitOne: function($entity, ng2D, ng2DCircle, generate, $world, count) { | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
generate.ng2D = generate.ng2D || {}; | ||
var angle = 2 * Math.random() * Math.PI; | ||
generate.ng2D.x = ng2D.x + ng2DCircle.radius * Math.random() * Math.cos(angle); | ||
generate.ng2D.y = ng2D.y + ng2DCircle.radius * Math.random() * Math.sin(angle); | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
generate.ng2D = generate.ng2D || {}; | ||
var angle = 2 * Math.random() * Math.PI; | ||
generate.ng2D.x = ng2D.x + ng2DCircle.radius * Math.random() * Math.cos(angle); | ||
generate.ng2D.y = ng2D.y + ng2DCircle.radius * Math.random() * Math.sin(angle); | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
}); | ||
} | ||
}); | ||
/** | ||
* Emit Particle in cubic area | ||
*/ | ||
m.$s('ngCubicEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng3D', 'ng3DSize'], | ||
/** | ||
* Emit Particle in cubic area | ||
*/ | ||
m.$s('ngCubicEmitterSystem', { | ||
$require: ['ngEmit', 'ngEmitter', 'ng3D', 'ng3DSize'], | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng3D, $entity.ng3DSize, $entity.ngEmitter.generate, $world); | ||
}], | ||
$addEntity: ['$entity', '$world', function($entity, $world) { | ||
this._emit($entity, $entity.ng3D, $entity.ng3DSize, $entity.ngEmitter.generate, $world); | ||
}], | ||
_emit: function($entity, ng3D, ng3DSize, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
_emit: function($entity, ng3D, ng3DSize, generate, $world) { | ||
if (darlingutil.isFunction(generate)) { | ||
generate = generate($entity); | ||
} | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
if (generate === null || darlingutil.isUndefined(generate)) { | ||
throw new Error('generate factory should be defined as config object with components or like factory function that return same object.'); | ||
} | ||
generate.ng3D = generate.ng3D || {}; | ||
generate.ng3D.x = ng3D.x + ng3DSize.width * Math.random(); | ||
generate.ng3D.y = ng3D.y + ng3DSize.height * Math.random(); | ||
generate.ng3D.z = ng3D.z + ng3DSize.depth * Math.random(); | ||
var count = $entity.ngEmit.count; | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
$entity.$remove('ngEmit'); | ||
generate.ng3D = generate.ng3D || {}; | ||
generate.ng3D.x = ng3D.x + ng3DSize.width * Math.random(); | ||
generate.ng3D.y = ng3D.y + ng3DSize.height * Math.random(); | ||
generate.ng3D.z = ng3D.z + ng3DSize.depth * Math.random(); | ||
var count = $entity.ngEmit.count; | ||
while(--count>=0) { | ||
$world.$e(generate); | ||
} | ||
}); | ||
$entity.$remove('ngEmit'); | ||
} | ||
}); | ||
/** | ||
* The Random counter causes the emitter to emit particles continuously | ||
* at a variable random rate between two limits. | ||
*/ | ||
m.$c('ngEmitterRandomCounter', { | ||
//The maximum number of particles to emit per second. | ||
maxRate: 0, | ||
//The minimum number of particles to emit per second. | ||
minRate: 0 | ||
}); | ||
/** | ||
* The Random counter causes the emitter to emit particles continuously | ||
* at a variable random rate between two limits. | ||
*/ | ||
m.$c('ngEmitterRandomCounter', { | ||
//The maximum number of particles to emit per second. | ||
maxRate: 0, | ||
//The minimum number of particles to emit per second. | ||
minRate: 0 | ||
}); | ||
m.$s('ngRandomEmitterSystem', { | ||
$require: ['ngEmitterRandomCounter'], | ||
m.$s('ngRandomEmitterSystem', { | ||
$require: ['ngEmitterRandomCounter'], | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
var counter = $entity.ngEmitterRandomCounter; | ||
if (!counter._timeout) { | ||
counter._timeout = this._timeInterval(counter); | ||
} | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
var counter = $entity.ngEmitterRandomCounter; | ||
if (!counter._timeout) { | ||
counter._timeout = this._timeInterval(counter); | ||
} | ||
counter._timeout -= $time; | ||
while (counter._timeout <= 0) { | ||
if ($entity.ngEmit) { | ||
$entity.ngEmit.count++; | ||
} else { | ||
$entity.$add('ngEmit', { | ||
count: 1 | ||
}); | ||
} | ||
counter._timeout += this._timeInterval(counter); | ||
counter._timeout -= $time; | ||
while (counter._timeout <= 0) { | ||
if ($entity.ngEmit) { | ||
$entity.ngEmit.count++; | ||
} else { | ||
$entity.$add('ngEmit', { | ||
count: 1 | ||
}); | ||
} | ||
//ngEmitter._timeout = ngEmitter.intervalMin + (ngEmitter.intervalMax - ngEmitter.intervalMin) * Math.random(); | ||
}], | ||
_timeInterval: function(counter) { | ||
return 1000.0 / (counter.minRate + (counter.maxRate - counter.minRate) * Math.random()); | ||
counter._timeout += this._timeInterval(counter); | ||
} | ||
}); | ||
//ngEmitter._timeout = ngEmitter.intervalMin + (ngEmitter.intervalMax - ngEmitter.intervalMin) * Math.random(); | ||
}], | ||
m.$c('ngRectangleZone', { | ||
right: 0.0, | ||
left: 0.0, | ||
top: 0.0, | ||
bottom: 0.0 | ||
}); | ||
_timeInterval: function(counter) { | ||
return 1000.0 / (counter.minRate + (counter.maxRate - counter.minRate) * Math.random()); | ||
} | ||
}); | ||
m.$s('ngRectangleZone', { | ||
$require: ['ngRectangleZone', 'ng2D'], | ||
m.$c('ngRectangleZone', { | ||
right: 0.0, | ||
left: 0.0, | ||
top: 0.0, | ||
bottom: 0.0 | ||
}); | ||
$update: ['$entity', function($entity) { | ||
if(this._isInside($entity.ngRectangleZone, $entity.ng2D)) { | ||
$entity.$remove('ngOutOfZone'); | ||
if (!$entity.ngInsideZone) { | ||
$entity.$add('ngInsideZone'); | ||
} | ||
} else { | ||
$entity.$remove('ngInsideZone'); | ||
if (!$entity.ngOutOfZone) { | ||
$entity.$add('ngOutOfZone'); | ||
} | ||
m.$s('ngRectangleZone', { | ||
$require: ['ngRectangleZone', 'ng2D'], | ||
$update: ['$entity', function($entity) { | ||
if(this._isInside($entity.ngRectangleZone, $entity.ng2D)) { | ||
$entity.$remove('ngOutOfZone'); | ||
if (!$entity.ngInsideZone) { | ||
$entity.$add('ngInsideZone'); | ||
} | ||
}], | ||
_isInside: function(zone, ng2D) { | ||
return zone.left < ng2D.x && ng2D.x < zone.right && | ||
zone.top < ng2D.y && ng2D.y < zone.bottom; | ||
} else { | ||
$entity.$remove('ngInsideZone'); | ||
if (!$entity.ngOutOfZone) { | ||
$entity.$add('ngOutOfZone'); | ||
} | ||
} | ||
}); | ||
}], | ||
m.$c('ngOutOfZone', {}); | ||
_isInside: function(zone, ng2D) { | ||
return zone.left < ng2D.x && ng2D.x < zone.right && | ||
zone.top < ng2D.y && ng2D.y < zone.bottom; | ||
} | ||
}); | ||
m.$c('ngInsideZone', {}); | ||
m.$c('ngOutOfZone', {}); | ||
m.$c('ngLifeZone', { | ||
lifeReduce: 0.1 | ||
}); | ||
m.$c('ngInsideZone', {}); | ||
m.$c('ngDeathZone', {}); | ||
m.$c('ngLifeZone', { | ||
lifeReduce: 0.1 | ||
}); | ||
m.$c('ngLife', { | ||
life: 1.0 | ||
}); | ||
m.$c('ngDeathZone', {}); | ||
m.$c('ngDamage', { | ||
damage: 0.1 | ||
}); | ||
m.$c('ngLife', { | ||
life: 1.0 | ||
}); | ||
m.$c('ngContinuousDamage', { | ||
damage: 0.1 | ||
}); | ||
m.$c('ngDamage', { | ||
damage: 0.1 | ||
}); | ||
m.$c('ngLifeIsGrooving', { | ||
delta: 0.1, | ||
max: 1.0 | ||
}); | ||
m.$c('ngContinuousDamage', { | ||
damage: 0.1 | ||
}); | ||
m.$s('ngDecreaseLifeOnDamage', { | ||
$require: ['ngLife', 'ngDamage', 'ngLive'], | ||
m.$c('ngLifeIsGrooving', { | ||
delta: 0.1, | ||
max: 1.0 | ||
}); | ||
$addEntity: function($entity) { | ||
$entity.ngLife.life -= $entity.ngDamage.damage; | ||
$entity.$remove('ngDamage'); | ||
} | ||
}); | ||
m.$s('ngDecreaseLifeOnDamage', { | ||
$require: ['ngLife', 'ngDamage', 'ngLive'], | ||
m.$s('ngDecreaseLifeOnContinuousDamage', { | ||
$require: ['ngLife', 'ngContinuousDamage', 'ngLive'], | ||
$addEntity: function($entity) { | ||
$entity.ngLife.life -= $entity.ngDamage.damage; | ||
$entity.$remove('ngDamage'); | ||
} | ||
}); | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life -= 0.001 * $time * $entity.ngContinuousDamage.damage; | ||
}] | ||
}); | ||
m.$s('ngDecreaseLifeOnContinuousDamage', { | ||
$require: ['ngLife', 'ngContinuousDamage', 'ngLive'], | ||
m.$s('ngLifeIsGrooving', { | ||
$require: ['ngLifeIsGrooving', 'ngLife', 'ngLive'], | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life -= 0.001 * $time * $entity.ngContinuousDamage.damage; | ||
}] | ||
}); | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life += 0.001 * $time * $entity.ngLifeIsGrooving.delta; | ||
if ($entity.ngLife.life >= $entity.ngLifeIsGrooving.max) { | ||
$entity.ngLife.life = $entity.ngLifeIsGrooving.max; | ||
$entity.$remove('ngLifeIsGrooving'); | ||
} | ||
}] | ||
}); | ||
m.$s('ngLifeIsGrooving', { | ||
$require: ['ngLifeIsGrooving', 'ngLife', 'ngLive'], | ||
/** | ||
* FIXME: Better use event-based approach | ||
*/ | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life += 0.001 * $time * $entity.ngLifeIsGrooving.delta; | ||
if ($entity.ngLife.life >= $entity.ngLifeIsGrooving.max) { | ||
$entity.ngLife.life = $entity.ngLifeIsGrooving.max; | ||
$entity.$remove('ngLifeIsGrooving'); | ||
} | ||
}] | ||
}); | ||
m.$c('ngOnLifeChange', { | ||
handler: null | ||
}); | ||
/** | ||
* FIXME: Better use event-based approach | ||
*/ | ||
/** | ||
* Handle life changing | ||
*/ | ||
m.$s('ngLifeHandler', { | ||
$require: ['ngLife', 'ngOnLifeChange'], | ||
m.$c('ngOnLifeChange', { | ||
handler: null | ||
}); | ||
$update: ['$entity', function($entity) { | ||
var ngOnLifeChange = $entity.ngOnLifeChange; | ||
var ngLife = $entity.ngLife; | ||
if (ngLife.life !== ngOnLifeChange.previousLife) { | ||
ngOnLifeChange.handler($entity, ngLife.life); | ||
ngOnLifeChange.previousLife = ngLife.life; | ||
} | ||
}] | ||
}); | ||
/** | ||
* Handle life changing | ||
*/ | ||
m.$s('ngLifeHandler', { | ||
$require: ['ngLife', 'ngOnLifeChange'], | ||
m.$c('ngLive', {}); | ||
$update: ['$entity', function($entity) { | ||
var ngOnLifeChange = $entity.ngOnLifeChange; | ||
var ngLife = $entity.ngLife; | ||
if (ngLife.life !== ngOnLifeChange.previousLife) { | ||
ngOnLifeChange.handler($entity, ngLife.life); | ||
ngOnLifeChange.previousLife = ngLife.life; | ||
} | ||
}] | ||
}); | ||
m.$c('ngDead', {}); | ||
m.$c('ngLive', {}); | ||
m.$c('ngRemoveIfDead', { | ||
}); | ||
m.$c('ngDead', {}); | ||
m.$s('ngRemoveIfDead', { | ||
$require: ['ngRemoveIfDead', 'ngDead'], | ||
m.$c('ngRemoveIfDead', { | ||
}); | ||
$addEntity: ['$world', '$entity', function($world, $entity) { | ||
setTimeout(function(){ | ||
$world.$remove($entity); | ||
}, 0); | ||
}] | ||
}); | ||
m.$s('ngRemoveIfDead', { | ||
$require: ['ngRemoveIfDead', 'ngDead'], | ||
m.$s('ngReduceLifeIfOutOfLifeZone', { | ||
$require: ['ngLifeZone', 'ngOutOfZone', 'ngLife', 'ngLive'], | ||
$addEntity: ['$world', '$entity', function($world, $entity) { | ||
setTimeout(function(){ | ||
$world.$remove($entity); | ||
}, 0); | ||
}] | ||
}); | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life -= 0.001 * $entity.ngLifeZone.lifeReduce * $time; | ||
}] | ||
}); | ||
m.$s('ngReduceLifeIfOutOfLifeZone', { | ||
$require: ['ngLifeZone', 'ngOutOfZone', 'ngLife', 'ngLive'], | ||
m.$s('ngDeadIfOutOfLife', { | ||
$require: ['ngLife', 'ngLive'], | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ngLife.life -= 0.001 * $entity.ngLifeZone.lifeReduce * $time; | ||
}] | ||
}); | ||
$update: ['$entity', function($entity) { | ||
if ($entity.ngLife.life <= 0) { | ||
$entity.$add('ngDead'); | ||
$entity.$remove('ngLive'); | ||
} | ||
}] | ||
}); | ||
m.$s('ngDeadIfOutOfLife', { | ||
$require: ['ngLife', 'ngLive'], | ||
m.$s('ngDeathIfOutOfLifeZone', { | ||
$require: ['ngLifeZone', 'ngOutOfZone', 'ngLive'], | ||
$addEntity: function($entity) { | ||
$update: ['$entity', function($entity) { | ||
if ($entity.ngLife.life <= 0) { | ||
$entity.$add('ngDead'); | ||
$entity.$remove('ngLive'); | ||
} | ||
}); | ||
}] | ||
}); | ||
m.$s('ngDeathIfInsideZone', { | ||
$require: ['ngDeathZone', 'ngInsideZone'], | ||
m.$s('ngDeathIfOutOfLifeZone', { | ||
$require: ['ngLifeZone', 'ngOutOfZone', 'ngLive'], | ||
$addEntity: function($entity) { | ||
$entity.$add('ngDead'); | ||
} | ||
}); | ||
})(darlingjs); | ||
$addEntity: function($entity) { | ||
$entity.$add('ngDead'); | ||
$entity.$remove('ngLive'); | ||
} | ||
}); | ||
m.$s('ngDeathIfInsideZone', { | ||
$require: ['ngDeathZone', 'ngInsideZone'], | ||
$addEntity: function($entity) { | ||
$entity.$add('ngDead'); | ||
} | ||
}); |
@@ -6,262 +6,264 @@ /** | ||
(function(darlingjs) { | ||
'use strict'; | ||
'use strict'; | ||
var m = darlingjs.module('ngPhysics'); | ||
var darlingjs = require('./../../../'); | ||
m.$c('ngPhysic', { | ||
type: 'dynamic', //static | ||
restitution: 0.5, | ||
friction: 0.75, | ||
density: 1.0, | ||
angularDamping: 0.0, | ||
linearDamping: 0.0, | ||
fixedRotation: false, | ||
allowSleep: true | ||
}); | ||
var m = darlingjs.module('ngPhysics'); | ||
m.$c('ngFixedRotation', {}); | ||
m.$c('ngPhysic', { | ||
type: 'dynamic', //static | ||
restitution: 0.5, | ||
friction: 0.75, | ||
density: 1.0, | ||
angularDamping: 0.0, | ||
linearDamping: 0.0, | ||
fixedRotation: false, | ||
allowSleep: true | ||
}); | ||
m.$c('ngAnyJoint', { | ||
joint: null | ||
}); | ||
m.$c('ngFixedRotation', {}); | ||
m.$c('ngPrismaticJoint', { | ||
lowerTransition: 0.0, | ||
upperTranslation: 0.0, | ||
enableLimit: false, | ||
maxMotorForce: 100.0, | ||
motorSpeed: 5.0, | ||
enableMotor: false | ||
}); | ||
m.$c('ngAnyJoint', { | ||
joint: null | ||
}); | ||
m.$c('ngDistanceJoint', { | ||
collideConnected: true, | ||
frequencyHz: 4.0, | ||
dampingRatio: 0.5 | ||
}); | ||
m.$c('ngPrismaticJoint', { | ||
lowerTransition: 0.0, | ||
upperTranslation: 0.0, | ||
enableLimit: false, | ||
maxMotorForce: 100.0, | ||
motorSpeed: 5.0, | ||
enableMotor: false | ||
}); | ||
m.$c('ngPulleyJoint', { | ||
//TODO: | ||
}); | ||
m.$c('ngDistanceJoint', { | ||
collideConnected: true, | ||
frequencyHz: 4.0, | ||
dampingRatio: 0.5 | ||
}); | ||
m.$c('ngRevoluteJoint', { | ||
lowerAngle: Number.NaN, | ||
upperAngle: Number.NaN, | ||
enableLimit: false, | ||
maxMotorTorque: 10.0, | ||
motorSpeed: 0.0, | ||
enableMotor: false, | ||
bodyAName: null, | ||
bodyBName: null | ||
}); | ||
m.$c('ngPulleyJoint', { | ||
//TODO: | ||
}); | ||
m.$c('ngSensor', {}); | ||
m.$c('ngRevoluteJoint', { | ||
lowerAngle: Number.NaN, | ||
upperAngle: Number.NaN, | ||
enableLimit: false, | ||
maxMotorTorque: 10.0, | ||
motorSpeed: 0.0, | ||
enableMotor: false, | ||
bodyAName: null, | ||
bodyBName: null | ||
}); | ||
m.$c('ngMotorSwitcher', { | ||
targetId: null, | ||
targetEntity: null | ||
}); | ||
m.$c('ngSensor', {}); | ||
m.$c('ngEnableMotor', {}); | ||
m.$c('ngMotorSwitcher', { | ||
targetId: null, | ||
targetEntity: null | ||
}); | ||
m.$c('ngEnableMotorReverse', {}); | ||
m.$c('ngEnableMotor', {}); | ||
m.$c('ngEnableMotorOnKeyDown', { | ||
//array of key codes for enabling motor | ||
keyCode: null, | ||
//array of key codes for enabling reverse motor | ||
keyCodeReverse: null | ||
}); | ||
m.$c('ngEnableMotorReverse', {}); | ||
m.$c('ngEnableMotorOnAccelerometer', { | ||
xAxis: false, | ||
yAxis: false, | ||
zAxis: false, | ||
edge: 15.0, | ||
invert: true | ||
}); | ||
m.$c('ngEnableMotorOnKeyDown', { | ||
//array of key codes for enabling motor | ||
keyCode: null, | ||
//array of key codes for enabling reverse motor | ||
keyCodeReverse: null | ||
}); | ||
m.$c('ngMotorWithAcceleration', { | ||
min: 0.0, | ||
max: 10.0, | ||
acceleration: 0.1, | ||
degradation: 0.1 | ||
}); | ||
m.$c('ngEnableMotorOnAccelerometer', { | ||
xAxis: false, | ||
yAxis: false, | ||
zAxis: false, | ||
edge: 15.0, | ||
invert: true | ||
}); | ||
/** | ||
* Just Enable Motor (add component ngEnableMotor/ngEnableMotorReverse) on key down, and stop motor on up. | ||
* | ||
*/ | ||
m.$s('ngEnableMotorOnKeyDown', { | ||
$require: ['ngEnableMotorOnKeyDown', 'ngSelected'], | ||
m.$c('ngMotorWithAcceleration', { | ||
min: 0.0, | ||
max: 10.0, | ||
acceleration: 0.1, | ||
degradation: 0.1 | ||
}); | ||
$addEntity: function($entity) { | ||
var component = $entity.ngEnableMotorOnKeyDown; | ||
/** | ||
* Just Enable Motor (add component ngEnableMotor/ngEnableMotorReverse) on key down, and stop motor on up. | ||
* | ||
*/ | ||
m.$s('ngEnableMotorOnKeyDown', { | ||
$require: ['ngEnableMotorOnKeyDown', 'ngSelected'], | ||
var keyCode = component.keyCode; | ||
var keyCodeReverse = component.keyCodeReverse; | ||
$addEntity: function($entity) { | ||
var component = $entity.ngEnableMotorOnKeyDown; | ||
this._target = document.getElementById(this.domId) || document; | ||
var keyCode = component.keyCode; | ||
var keyCodeReverse = component.keyCodeReverse; | ||
function onKeyDown(e) { | ||
var index; | ||
index = keyCode.indexOf(e.keyCode); | ||
this._target = document.getElementById(this.domId) || document; | ||
function onKeyDown(e) { | ||
var index; | ||
index = keyCode.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
if (!$entity.$has('ngEnableMotor')) { | ||
$entity.$add('ngEnableMotor'); | ||
} | ||
} else { | ||
index = keyCodeReverse.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
if (!$entity.$has('ngEnableMotorReverse')) { | ||
$entity.$add('ngEnableMotorReverse'); | ||
} | ||
if (!$entity.$has('ngEnableMotor')) { | ||
$entity.$add('ngEnableMotor'); | ||
} | ||
} else { | ||
index = keyCodeReverse.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
if (!$entity.$has('ngEnableMotorReverse')) { | ||
$entity.$add('ngEnableMotorReverse'); | ||
} | ||
if (!$entity.$has('ngEnableMotor')) { | ||
$entity.$add('ngEnableMotor'); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
function onKeyUp(e) { | ||
var index = keyCode.indexOf(e.keyCode); | ||
function onKeyUp(e) { | ||
var index = keyCode.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
$entity.$remove('ngEnableMotor'); | ||
} else { | ||
index = keyCodeReverse.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
$entity.$remove('ngEnableMotor'); | ||
} else { | ||
index = keyCodeReverse.indexOf(e.keyCode); | ||
if (index >= 0) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
$entity.$remove('ngEnableMotor'); | ||
} | ||
} | ||
} | ||
component.$$onKeyDown = onKeyDown; | ||
component.$$onKeyUp = onKeyUp; | ||
} | ||
component.$$onKeyDown = onKeyDown; | ||
component.$$onKeyUp = onKeyUp; | ||
this._target.addEventListener('keydown', onKeyDown); | ||
this._target.addEventListener('keyup', onKeyUp); | ||
}, | ||
this._target.addEventListener('keydown', onKeyDown); | ||
this._target.addEventListener('keyup', onKeyUp); | ||
}, | ||
$removeEntity: function($entity) { | ||
var component = $entity.ngEnableMotorOnKeyDown; | ||
this._target.removeEventListener('keydown', component.$$onKeyDown); | ||
this._target.removeEventListener('keyup', component.$$onKeyUp); | ||
} | ||
}); | ||
$removeEntity: function($entity) { | ||
var component = $entity.ngEnableMotorOnKeyDown; | ||
this._target.removeEventListener('keydown', component.$$onKeyDown); | ||
this._target.removeEventListener('keyup', component.$$onKeyUp); | ||
} | ||
}); | ||
m.$s('ngEnableMotorOnAccelerometer', { | ||
$require: ['ngEnableMotorOnAccelerometer', 'ngSelected'], | ||
m.$s('ngEnableMotorOnAccelerometer', { | ||
$require: ['ngEnableMotorOnAccelerometer', 'ngSelected'], | ||
_calibration: {alpha:0.0, beta:0.0, gamma:0.0}, | ||
_acceleration: {x:0.0, y:0.0, z:0.0}, | ||
_handler: null, | ||
_enabled: false, | ||
_calibration: {alpha:0.0, beta:0.0, gamma:0.0}, | ||
_acceleration: {x:0.0, y:0.0, z:0.0}, | ||
_handler: null, | ||
_enabled: false, | ||
$added: function() { | ||
var self = this; | ||
window.addEventListener('deviceorientation', this._handler = function(e) { | ||
self._acceleration.x = e.alpha - self._calibration.alpha; | ||
self._acceleration.y = e.beta - self._calibration.beta; | ||
self._acceleration.z = e.gamma - self._calibration.gamma; | ||
}); | ||
}, | ||
$added: function() { | ||
var self = this; | ||
window.addEventListener('deviceorientation', this._handler = function(e) { | ||
self._acceleration.x = e.alpha - self._calibration.alpha; | ||
self._acceleration.y = e.beta - self._calibration.beta; | ||
self._acceleration.z = e.gamma - self._calibration.gamma; | ||
}); | ||
}, | ||
$remove: function() { | ||
window.removeEventListener('deviceorientation', this._handler); | ||
}, | ||
$remove: function() { | ||
window.removeEventListener('deviceorientation', this._handler); | ||
}, | ||
enableMotorIsInInterval: function($entity, value, edge, invert) { | ||
var enablingMotor = false; | ||
var reverse = false; | ||
if (value > edge) { | ||
enablingMotor = true; | ||
reverse = false; | ||
} else if (value < -edge) { | ||
enablingMotor = true; | ||
reverse = true; | ||
} | ||
enableMotorIsInInterval: function($entity, value, edge, invert) { | ||
var enablingMotor = false; | ||
var reverse = false; | ||
if (value > edge) { | ||
enablingMotor = true; | ||
reverse = false; | ||
} else if (value < -edge) { | ||
enablingMotor = true; | ||
reverse = true; | ||
} | ||
if (invert) { | ||
reverse = !reverse; | ||
if (invert) { | ||
reverse = !reverse; | ||
} | ||
if (enablingMotor) { | ||
this._enabled = true; | ||
if (!$entity.ngEnableMotor) { | ||
$entity.$add('ngEnableMotor'); | ||
} | ||
if (enablingMotor) { | ||
this._enabled = true; | ||
if (!$entity.ngEnableMotor) { | ||
$entity.$add('ngEnableMotor'); | ||
if (reverse) { | ||
if (!$entity.ngEnableMotorReverse) { | ||
$entity.$add('ngEnableMotorReverse'); | ||
} | ||
if (reverse) { | ||
if (!$entity.ngEnableMotorReverse) { | ||
$entity.$add('ngEnableMotorReverse'); | ||
} | ||
} else { | ||
if ($entity.ngEnableMotorReverse) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
} | ||
} | ||
} else { | ||
if (this._enabled) { | ||
this._enabled = false; | ||
if ($entity.ngEnableMotorReverse) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
} | ||
if ($entity.ngEnableMotor) { | ||
$entity.$remove('ngEnableMotor'); | ||
} | ||
if ($entity.ngEnableMotorReverse) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
} | ||
} | ||
} else { | ||
if (this._enabled) { | ||
this._enabled = false; | ||
if ($entity.ngEnableMotorReverse) { | ||
$entity.$remove('ngEnableMotorReverse'); | ||
} | ||
if ($entity.ngEnableMotor) { | ||
$entity.$remove('ngEnableMotor'); | ||
} | ||
} | ||
} | ||
}, | ||
$update: ['$entity', function($entity) { | ||
var component = $entity.ngEnableMotorOnAccelerometer; | ||
if (component.xAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.x, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} else if (component.yAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.y, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} else if (component.zAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.z, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} | ||
}] | ||
}); | ||
m.$c('ngWantsToCollide', { | ||
'with' : [ | ||
{ | ||
'andGet': 'ngCollide' | ||
}, | ||
{ | ||
'any': ['ngBonus'], | ||
'andGet': 'ngGetBonus' | ||
}, | ||
{ | ||
'any': ['ngSounding'], | ||
'andGet': 'ngPlaySoundOf' | ||
} | ||
] | ||
}); | ||
$update: ['$entity', function($entity) { | ||
var component = $entity.ngEnableMotorOnAccelerometer; | ||
if (component.xAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.x, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} else if (component.yAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.y, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} else if (component.zAxis) { | ||
this.enableMotorIsInInterval($entity, this._acceleration.z, $entity.ngEnableMotorOnAccelerometer.edge, component.invert); | ||
} | ||
}] | ||
}); | ||
m.$c('ngCollide'); | ||
m.$c('ngWantsToCollide', { | ||
'with' : [ | ||
{ | ||
'andGet': 'ngCollide' | ||
}, | ||
{ | ||
'any': ['ngBonus'], | ||
'andGet': 'ngGetBonus' | ||
}, | ||
{ | ||
'any': ['ngSounding'], | ||
'andGet': 'ngPlaySoundOf' | ||
} | ||
] | ||
}); | ||
m.$c('ngCollisionGroup', { | ||
'alwaysWith': null, | ||
'neverWith': null | ||
}); | ||
m.$c('ngCollide'); | ||
m.$c('ngCollisionCategory', { | ||
'is': ['rock'], | ||
'collideWith': ['vehicle'] | ||
}); | ||
m.$c('ngCollisionGroup', { | ||
'alwaysWith': null, | ||
'neverWith': null | ||
}); | ||
/** | ||
* Component for applying impulse to physic body | ||
*/ | ||
m.$c('ngApplyImpulse', { | ||
x: 0.0, | ||
y: 0.0, | ||
center: true, | ||
dx: 0.0, | ||
dy: 0.0, | ||
autoRemove: true | ||
}); | ||
m.$c('ngCollisionCategory', { | ||
'is': ['rock'], | ||
'collideWith': ['vehicle'] | ||
}); | ||
/** | ||
* Component for applying impulse to physic body | ||
*/ | ||
m.$c('ngApplyImpulse', { | ||
x: 0.0, | ||
y: 0.0, | ||
center: true, | ||
dx: 0.0, | ||
dy: 0.0, | ||
autoRemove: true | ||
}) | ||
})(darlingjs); | ||
module.exports = m; |
@@ -10,70 +10,72 @@ /** | ||
(function(darlingjs, darlingutil) { | ||
'use strict'; | ||
'use strict'; | ||
var m = darlingjs.module('ngFlatland'); | ||
var darlingjs = require('./../../../'); | ||
/** | ||
* Component describe position in 2D environment | ||
*/ | ||
m.$c('ng2D', { | ||
x: 0.0, | ||
y: 0.0 | ||
}); | ||
var m = darlingjs.module('ngFlatland'); | ||
m.$c('ng2DSize', { | ||
width: 10.0, | ||
height: 10.0 | ||
}); | ||
/** | ||
* Component describe position in 2D environment | ||
*/ | ||
m.$c('ng2D', { | ||
x: 0.0, | ||
y: 0.0 | ||
}); | ||
m.$c('ng2DRotation', { | ||
rotation: 0.0 | ||
}); | ||
m.$c('ng2DSize', { | ||
width: 10.0, | ||
height: 10.0 | ||
}); | ||
m.$c('ng2DCircle', { | ||
radius: 10.0 | ||
}); | ||
m.$c('ng2DRotation', { | ||
rotation: 0.0 | ||
}); | ||
m.$c('ng2DPolygon', { | ||
line: null | ||
}); | ||
m.$c('ng2DCircle', { | ||
radius: 10.0 | ||
}); | ||
//Markers or State in FiniteStateMachine | ||
m.$c('ngGoingLeft', {}); | ||
m.$c('ngGoingRight', {}); | ||
m.$c('ng2DPolygon', { | ||
line: null | ||
}); | ||
//Service | ||
m.$s('ng2DViewPort', { | ||
lookAt: {x:0.0, y:0.0}, | ||
width: 640, | ||
height: 480 | ||
}); | ||
//Markers or State in FiniteStateMachine | ||
m.$c('ngGoingLeft', {}); | ||
m.$c('ngGoingRight', {}); | ||
/** | ||
* Marker for elements that doesn't influenced by the viewPort | ||
*/ | ||
m.$c('ngLockViewPort', { | ||
lockX: true, | ||
lockY: true | ||
}); | ||
//Service | ||
m.$s('ng2DViewPort', { | ||
lookAt: {x:0.0, y:0.0}, | ||
width: 640, | ||
height: 480 | ||
}); | ||
/** | ||
* Component of moving entity. Can be used in any dimension. | ||
*/ | ||
m.$c('ngShiftMove', { | ||
}); | ||
/** | ||
* Marker for elements that doesn't influenced by the viewPort | ||
*/ | ||
m.$c('ngLockViewPort', { | ||
lockX: true, | ||
lockY: true | ||
}); | ||
m.$s('ng2DShiftMovingSystem', { | ||
$require: ['ng2D', 'ngShiftMove'], | ||
/** | ||
* Component of moving entity. Can be used in any dimension. | ||
*/ | ||
m.$c('ngShiftMove', { | ||
}); | ||
$addEntity : function($entity) { | ||
$entity.ngShiftMove.dx = $entity.ngShiftMove.dx || 0.0; | ||
$entity.ngShiftMove.dy = $entity.ngShiftMove.dy || 0.0; | ||
}, | ||
m.$s('ng2DShiftMovingSystem', { | ||
$require: ['ng2D', 'ngShiftMove'], | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ng2D.x += 0.001 * $entity.ngShiftMove.dx * $time; | ||
$entity.ng2D.y += 0.001 * $entity.ngShiftMove.dy * $time; | ||
}] | ||
}); | ||
})(darlingjs, darlingutil); | ||
$addEntity : function($entity) { | ||
$entity.ngShiftMove.dx = $entity.ngShiftMove.dx || 0.0; | ||
$entity.ngShiftMove.dy = $entity.ngShiftMove.dy || 0.0; | ||
}, | ||
$update: ['$entity', '$time', function($entity, $time) { | ||
$entity.ng2D.x += 0.001 * $entity.ngShiftMove.dx * $time; | ||
$entity.ng2D.y += 0.001 * $entity.ngShiftMove.dy * $time; | ||
}] | ||
}); | ||
module.exports = m; |
@@ -8,13 +8,2 @@ /** | ||
/** | ||
* Get From AngularJS Project with little changes based on JSHint. | ||
*/ | ||
/** | ||
* @ignore | ||
* @type {*} | ||
* @private | ||
*/ | ||
var _darlingutil = window.darlingutil; | ||
/** | ||
* The static facade of darlingjs utils | ||
@@ -25,25 +14,7 @@ * | ||
*/ | ||
var darlingutil = window.darlingutil = window.darlingutil||{}; | ||
var darlingutil = {}; | ||
darlingutil.version = '0.0.0'; | ||
/** | ||
* @ngdoc function | ||
* @name darlingutil.noConflict | ||
* @function | ||
* | ||
* @description | ||
* Restores the previous global value of darlingutil and returns the current instance. Other libraries may already use the | ||
* darlingutil namespace. Or a previous version of darlingutil is already loaded on the page. In these cases you may want to | ||
* restore the previous namespace and keep a reference to darlingutil. | ||
* | ||
* @return {Object} The current darlingutil namespace | ||
*/ | ||
darlingutil.noConflict = function() { | ||
var a = window.darlingutil; | ||
window.darlingutil = _darlingutil; | ||
return a; | ||
}; | ||
(function() { | ||
(function(context) { | ||
'use strict'; | ||
@@ -104,4 +75,4 @@ | ||
var rv = -1; // Return value assumes failure. | ||
if (navigator.appName == 'Microsoft Internet Explorer') { | ||
var ua = navigator.userAgent; | ||
if (context.navigator && context.navigator.appName == 'Microsoft Internet Explorer') { | ||
var ua = context.navigator.userAgent; | ||
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); | ||
@@ -116,4 +87,4 @@ if (re.exec(ua) != null) | ||
var rv = 0; // Default value | ||
if (window.opera) { | ||
var sver = window.opera.version(); | ||
if (context && context.opera) { | ||
var sver = context.opera.version(); | ||
rv = parseFloat(sver); | ||
@@ -124,4 +95,4 @@ } | ||
var __userAgent = navigator.userAgent; | ||
var __isIE = navigator.appVersion.match(/MSIE/) != null; | ||
var __userAgent = context.navigator && context.navigator.userAgent || ''; | ||
var __isIE = context.navigator && context.navigator.appVersion.match(/MSIE/) != null; | ||
var __IEVersion = __getIEVersion(); | ||
@@ -136,5 +107,5 @@ var __isIENew = __isIE && __IEVersion >= 8; | ||
var __isWebKit = navigator.appVersion.match(/WebKit/) != null; | ||
var __isChrome = navigator.appVersion.match(/Chrome/) != null; | ||
var __isOpera = window.opera != null; | ||
var __isWebKit = context.navigator && context.navigator.appVersion.match(/WebKit/) != null; | ||
var __isChrome = context.navigator && context.navigator.appVersion.match(/Chrome/) != null; | ||
var __isOpera = context.window && context.window.opera != null; | ||
var __operaVersion = __getOperaVersion(); | ||
@@ -267,3 +238,3 @@ var __isOperaOld = __isOpera && (__operaVersion < 10); | ||
// })(window); | ||
})(); | ||
})(this); | ||
@@ -375,2 +346,3 @@ var toString = Object.prototype.toString; | ||
*/ | ||
darlingutil.copy = copy; | ||
function copy(source, destination, deleteAllDestinationProperties){ | ||
@@ -579,2 +551,3 @@ if (isWindow(source)) { | ||
darlingutil.annotate = annotate; | ||
function annotate(fn) { | ||
@@ -644,2 +617,4 @@ var $inject, | ||
*/ | ||
darlingutil.noop = noop; | ||
function noop(){} | ||
@@ -652,152 +627,3 @@ | ||
// Create local references to array methods we'll want to use later. | ||
var array = []; | ||
var slice = array.slice; | ||
// Backbone.Events | ||
// --------------- | ||
// A module that can be mixed in to *any object* in order to provide it with | ||
// custom events. You may bind with `on` or remove with `off` callback | ||
// functions to an event; `trigger`-ing an event fires all callbacks in | ||
// succession. | ||
// | ||
// var object = {}; | ||
// _.extend(object, Backbone.Events); | ||
// object.on('expand', function(){ alert('expanded'); }); | ||
// object.trigger('expand'); | ||
// | ||
var Events = { | ||
// Bind an event to a `callback` function. Passing `"all"` will bind | ||
// the callback to all events fired. | ||
on: function(name, callback, context) { | ||
if (!eventsApi(this, 'on', name, [callback, context]) || !callback) return this; | ||
this._events || (this._events = {}); | ||
var events = this._events[name] || (this._events[name] = []); | ||
events.push({callback: callback, context: context, ctx: context || this}); | ||
return this; | ||
}, | ||
// Bind an event to only be triggered a single time. After the first time | ||
// the callback is invoked, it will be removed. | ||
once: function(name, callback, context) { | ||
if (!eventsApi(this, 'once', name, [callback, context]) || !callback) return this; | ||
var self = this; | ||
var once = _.once(function() { | ||
self.off(name, once); | ||
callback.apply(this, arguments); | ||
}); | ||
once._callback = callback; | ||
return this.on(name, once, context); | ||
}, | ||
// Remove one or many callbacks. If `context` is null, removes all | ||
// callbacks with that function. If `callback` is null, removes all | ||
// callbacks for the event. If `name` is null, removes all bound | ||
// callbacks for all events. | ||
off: function(name, callback, context) { | ||
var retain, ev, events, names, i, l, j, k; | ||
if (!this._events || !eventsApi(this, 'off', name, [callback, context])) return this; | ||
if (!name && !callback && !context) { | ||
this._events = {}; | ||
return this; | ||
} | ||
names = name ? [name] : _.keys(this._events); | ||
for (i = 0, l = names.length; i < l; i++) { | ||
name = names[i]; | ||
if (events = this._events[name]) { | ||
this._events[name] = retain = []; | ||
if (callback || context) { | ||
for (j = 0, k = events.length; j < k; j++) { | ||
ev = events[j]; | ||
if ((callback && callback !== ev.callback && callback !== ev.callback._callback) || | ||
(context && context !== ev.context)) { | ||
retain.push(ev); | ||
} | ||
} | ||
} | ||
if (!retain.length) delete this._events[name]; | ||
} | ||
} | ||
return this; | ||
}, | ||
// Trigger one or many events, firing all bound callbacks. Callbacks are | ||
// passed the same arguments as `trigger` is, apart from the event name | ||
// (unless you're listening on `"all"`, which will cause your callback to | ||
// receive the true name of the event as the first argument). | ||
trigger: function(name) { | ||
if (!this._events) return this; | ||
var args = slice.call(arguments, 1); | ||
if (!eventsApi(this, 'trigger', name, args)) return this; | ||
var events = this._events[name]; | ||
var allEvents = this._events.all; | ||
if (events) triggerEvents(events, args); | ||
if (allEvents) triggerEvents(allEvents, arguments); | ||
return this; | ||
}, | ||
// Tell this object to stop listening to either specific events ... or | ||
// to every object it's currently listening to. | ||
stopListening: function(obj, name, callback) { | ||
var listeners = this._listeners; | ||
if (!listeners) return this; | ||
var deleteListener = !name && !callback; | ||
if (typeof name === 'object') callback = this; | ||
if (obj) (listeners = {})[obj._listenerId] = obj; | ||
for (var id in listeners) { | ||
listeners[id].off(name, callback, this); | ||
if (deleteListener) delete this._listeners[id]; | ||
} | ||
return this; | ||
} | ||
}; | ||
// Regular expression used to split event strings. | ||
var eventSplitter = /\s+/; | ||
// Implement fancy features of the Events API such as multiple event | ||
// names `"change blur"` and jQuery-style event maps `{change: action}` | ||
// in terms of the existing API. | ||
var eventsApi = function(obj, action, name, rest) { | ||
if (!name) return true; | ||
// Handle event maps. | ||
if (typeof name === 'object') { | ||
for (var key in name) { | ||
obj[action].apply(obj, [key, name[key]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
// Handle space separated event names. | ||
if (eventSplitter.test(name)) { | ||
var names = name.split(eventSplitter); | ||
for (var i = 0, l = names.length; i < l; i++) { | ||
obj[action].apply(obj, [names[i]].concat(rest)); | ||
} | ||
return false; | ||
} | ||
return true; | ||
}; | ||
// A difficult-to-believe, but optimized internal dispatch function for | ||
// triggering events. Tries to keep the usual cases speedy (most internal | ||
// Backbone events have 3 arguments). | ||
var triggerEvents = function(events, args) { | ||
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2]; | ||
switch (args.length) { | ||
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return; | ||
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return; | ||
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return; | ||
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return; | ||
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); | ||
} | ||
}; | ||
// Helper function to correctly set up the prototype chain, for subclasses. | ||
@@ -839,2 +665,3 @@ // Similar to `goog.inherits`, but uses a hash of prototype properties and | ||
darlingutil.swallowCopy = swallowCopy; | ||
function swallowCopy(original, extended) { | ||
@@ -861,2 +688,3 @@ if (extended === null || original === null) { | ||
*/ | ||
darlingutil.mixin = mixin; | ||
function mixin(original, extended) { | ||
@@ -880,30 +708,3 @@ if (extended === null) { | ||
/** | ||
* Optimization based on | ||
* http://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/5 | ||
* | ||
* Priority of function calls: | ||
* 1. direct: that.f(); | ||
* 2. by string: that['f'](); | ||
* 3. by call: f.call(that); | ||
* 4. by apply: f.apply(that); | ||
* | ||
* So where it's possbile we use call by string | ||
* | ||
* @ignore | ||
* @param fn | ||
* @param context | ||
* @param args | ||
* @param methodName | ||
* @return {*} | ||
*/ | ||
function factoryOfFastFunction(fn, context, args, methodName) { | ||
if (context.hasOwnProperty(methodName)) { | ||
return factoryOfFastFunctionAsAMember(fn, context, args, methodName); | ||
} else { | ||
return factoryOfFastFunctionAsCallOrApply(fn, context, args); | ||
} | ||
} | ||
darlingutil.factoryOfFastFunctionAsCallOrApply = factoryOfFastFunctionAsCallOrApply; | ||
function factoryOfFastFunctionAsCallOrApply(fn, context, args) { | ||
@@ -932,2 +733,3 @@ switch(args.length) { | ||
darlingutil.factoryOfFastFunctionAsCallOrApply = factoryOfFastFunctionAsAMember; | ||
function factoryOfFastFunctionAsAMember(fn, context, args, methodName) { | ||
@@ -957,4 +759,13 @@ switch(args.length) { | ||
/** | ||
* Create function with custom matcher | ||
* Optimization based on | ||
* http://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/5 | ||
* | ||
* Priority of function calls: | ||
* 1. direct: that.f(); | ||
* 2. by string: that['f'](); | ||
* 3. by call: f.call(that); | ||
* 4. by apply: f.apply(that); | ||
* | ||
* So where it's possbile we use call by string | ||
* | ||
* @ignore | ||
@@ -964,11 +775,12 @@ * @param fn | ||
* @param args | ||
* @param argsMatcher | ||
* @param methodName | ||
* @return {Function} | ||
* @return {*} | ||
*/ | ||
function factoryOfFastFunctionWithMatcher(fn, context, args, argsMatcher, methodName) { | ||
darlingutil.factoryOfFastFunction = factoryOfFastFunction; | ||
function factoryOfFastFunction(fn, context, args, methodName) { | ||
if (context.hasOwnProperty(methodName)) { | ||
return factoryOfFastFunctionWithMatcherAsAMember(fn, context, args, argsMatcher, methodName); | ||
return factoryOfFastFunctionAsAMember(fn, context, args, methodName); | ||
} else { | ||
return factoryOfFastFunctionWithMatcherAsCallOrApply(fn, context, args, argsMatcher); | ||
return factoryOfFastFunctionAsCallOrApply(fn, context, args); | ||
} | ||
@@ -985,2 +797,3 @@ } | ||
*/ | ||
darlingutil.factoryOfFastFunctionWithMatcherAsCallOrApply = factoryOfFastFunctionWithMatcherAsCallOrApply; | ||
function factoryOfFastFunctionWithMatcherAsCallOrApply(fn, context, args, argsMatcher) { | ||
@@ -1010,2 +823,3 @@ switch(args.length) { | ||
darlingutil.factoryOfFastFunctionWithMatcherAsAMember = factoryOfFastFunctionWithMatcherAsAMember; | ||
function factoryOfFastFunctionWithMatcherAsAMember(fn, context, args, argsMatcher, methodName) { | ||
@@ -1039,4 +853,24 @@ switch(args.length) { | ||
/** | ||
* Create function with custom matcher | ||
* | ||
* @ignore | ||
* @param fn | ||
* @param context | ||
* @param args | ||
* @param argsMatcher | ||
* @param methodName | ||
* @return {Function} | ||
*/ | ||
darlingutil.factoryOfFastFunctionWithMatcher = factoryOfFastFunctionWithMatcher; | ||
function factoryOfFastFunctionWithMatcher(fn, context, args, argsMatcher, methodName) { | ||
if (context.hasOwnProperty(methodName)) { | ||
return factoryOfFastFunctionWithMatcherAsAMember(fn, context, args, argsMatcher, methodName); | ||
} else { | ||
return factoryOfFastFunctionWithMatcherAsCallOrApply(fn, context, args, argsMatcher); | ||
} | ||
} | ||
// remove all own properties on obj, effectively reverting it to a new object | ||
darlingutil.wipe = function (obj) { | ||
darlingutil.wipe = function(obj) { | ||
for (var p in obj) { | ||
@@ -1119,2 +953,4 @@ if (obj.hasOwnProperty(p)) | ||
} | ||
} | ||
}; | ||
module.exports = darlingutil; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
98
1
2
5170713
15
118677