Пример #1
0
define(function(require) {
	'use strict';

	var Base = require('core/base');

	var Clock = Base.extend({

		get tick() {
			return this._tick;
		},

		init: function() {
			this.time = 0;
			this.framerate = 60;
			this._tick = 0;
			this.lastChange = 0;
		},

		add: function(seconds) {
			this.lastChange = Date.now();
			this._tick = seconds;
			this.time += seconds;
		},

		real: function() {
			return (Date.now() - this.lastChange) / 1000;
		}
	});

	return Clock;

});
Пример #2
0
define(function(require) {
	'use strict';

	var Base = require('core/base');

	return Base.extend({

		init: function(tilesize, data) {
			this.tilesize = tilesize;
			this.data = data;

			this.height = data.length;
			this.width = data[0] ? data[0].length : 0;
		},

		getTile: function(x, y) {
			var row = Math.floor(y / this.tilesize);
			var col = Math.floor(x / this.tilesize);
			return this.data[row][col];
		},

		setTile: function(x, y, tile) {
			var row = Math.floor(y / this.tilesize);
			var col = Math.floor(x / this.tilesize);
			this.data[row][col] = tile;
		}
	});
});
Пример #3
0
define(function(require) {
	'use strict';

	var Base = require('core/base');

	var Timer = Base.extend({

		init: function() {
			this.base();

		}
	});

	return Timer;

});
Пример #4
0
define(function(require) {
	'use strict';

	var Base = require('core/base');


	function mouse() {

	}

	function key(code) {
		return function(signals, el, signal) {
			el.addEventListener('keydown', function(event) {
				if (event.which === code) signals[signal] = true;
			}, true);
			el.addEventListener('keyup', function(event) {
				if (event.which === code) signals[signal] = false;
			}, true);
		};
	}


	return Base.extend({

		init: function(element) {
			this.signals = {};
			this.$el = element || document.body;
		},

		bind: function(type, signal) {
			type(this.signals, this.$el, signal);
		},

		get: function(signal) {
			return !!this.signals[signal];
		},

		KEY: {
			MOUSE1: mouse('MOUSE1'),
			MOUSE2: mouse('MOUSE2'),
			MWHEEL_UP: mouse('MWHEEL_UP'),
			MWHEEL_DOWN: mouse('MWHEEL_DOWN'),

			0: key(48),
			1: key(49),
			2: key(50),
			3: key(51),
			4: key(52),
			5: key(53),
			6: key(54),
			7: key(55),
			8: key(56),
			9: key(57),
			A: key(65),
			B: key(66),
			C: key(67),
			D: key(68),
			E: key(69),
			F: key(70),
			G: key(71),
			H: key(72),
			I: key(73),
			J: key(74),
			K: key(75),
			L: key(76),
			M: key(77),
			N: key(78),
			O: key(79),
			P: key(80),
			Q: key(81),
			R: key(82),
			S: key(83),
			T: key(84),
			U: key(85),
			V: key(86),
			W: key(87),
			X: key(88),
			Y: key(89),
			Z: key(90),

			LEFT: key(37),
			UP: key(38),
			RIGHT: key(39),
			DOWN: key(40),
			ENTER: key(13),
			SPACE: key(32),
			ESC: key(27),
			TAB: key(9),
			SHIFT: key(16),
			CTRL: key(17),
			COMMAND: key(91),
			ALT: key(18),
			BACKSPACE: key(8),
			PAUSE: key('PAUSE'),
			CAPS: key(20),
			PAGE_UP: key('PAGE_UP'),
			PAGE_DOWN: key('PAGE_DOWN'),
			END: key('END'),
			HOME: key('HOME'),
			INSERT: key('INSERT'),
			DELETE: key('DELETE'),
			F1: key(112),
			F2: key(113),
			F3: key(114),
			F4: key(115),
			F5: key(116),
			F6: key(117),
			F7: key(118),
			F8: key(119),
			F9: key('F9'),
			F10: key('F10'),
			F11: key('F11'),
			F12: key('F12'),
			NUMPAD_0: key('NUMPAD_0'),
			NUMPAD_1: key('NUMPAD_1'),
			NUMPAD_2: key('NUMPAD_2'),
			NUMPAD_3: key('NUMPAD_3'),
			NUMPAD_4: key('NUMPAD_4'),
			NUMPAD_5: key('NUMPAD_5'),
			NUMPAD_6: key('NUMPAD_6'),
			NUMPAD_7: key('NUMPAD_7'),
			NUMPAD_8: key('NUMPAD_8'),
			NUMPAD_9: key('NUMPAD_9'),
			MULTIPLY: key(221),
			ADD: key('ADD'),
			SUBSTRACT: key('SUBSTRACT'),
			DECIMAL: key('DECIMAL'),
			DIVIDE: key(191),
			PLUS: key(187),
			COMMA: key(188),
			MINUS: key(189),
			PERIOD: key(190),
		}
	});
});
Пример #5
0
define(function(require) {
	'use strict';

	var Base = require('core/base');

	var Loader = Base.extend({

		init: function() {
			this.loaded = true;
			this.images = [];
			this._callbacks = [];
		},

		addImage: function(url) {
			if (Loader.cache[url])
				return Loader.cache[url];

			var el = new Image();
			el.src = url;
			Loader.cache[url] = el;
			this.images.push(el);
			this.loaded = false;

			this.run();
			return el;
		},

		run: function() {
			var end = function() {
				this.loaded = true;
				this._callbacks.forEach(function(a) { a() });
				this._callbacks = [];
			}.bind(this);

			var loaded = [];
			this.images.filter(function(element) {
				return !element.complete;
			}).forEach(function(element, index) {
				loaded[index] = false;
				element.onload = function() {
					console.log('received');
					loaded[index] = true;
					if (loaded.every(Boolean)) end();
				};
			});

			if (!loaded.length)
				end();
		},

		onLoad: function(listener) {
			if (typeof listener !== 'function')
				return;

			if (this.loaded)
				setTimeout(listener, 0);
			else
				this._callbacks.push(listener);
		}

	}, {

		cache: {},

		get instance() {
			if (!this._instance)
				this._instance = new this();

			return this._instance;
		}

	});

	return Loader;

});
Пример #6
0
define(function(require) {
	"use strict";

	var tools = require('core/tools');
	var Lang = require('core/lang');
	var Base = require('core/base');
	var Error = require('core/error');
	var Schedule = require('core/schedule');
	var Emitter = require('core/emitter');

	var funct = tools.funct;
	var schedule = Schedule.create();

	var PromiseError = Error.extend({
		name: 'PromiseError',

		init: function(error, index) {
			this.base(error && error.message);
			this.original = error;
			this.index = index;
		}
	});

	var Future = Base.extend({

		name: 'Future',

		init: function(deps) {
			this.base(deps);
			this._emitter = Emitter.create();
			this._completed = false;
			this._failed = false;
			this._error = null;

			this._callbacks = {
				'complete': [],
				'failed': [],
				'finally': []
			};
		},

		dispose: function() {
			this._emitter.dispose();
			this._callbacks['complete'].length = 0;
			this._callbacks['failed'].length = 0;
			this._callbacks['finally'].length = 0;
			this._callbacks = null;
			this.base();
		},

		isCompleted: function() {
			return this._completed;
		},

		hasFailed: function() {
			return this._failed;
		},

		hasSucceed: function() {
			return this._completed && !this._failed;
		},

		then: function(success, fail, after) {
			this.onComplete(success)
			this.onFail(fail)
			this.onFinally(after);
		},

		onComplete: function(callback, scope) {
			this._addCallback('complete', callback, scope);
			return this;
		},

		onFail: function(callback, scope) {
			this._addCallback('failed', callback, scope);
			return this;
		},

		onFinally: function(callback, scope) {
			this._addCallback('finally', callback, scope);
			return this;
		},

		_addCallback: function(type, callback, scope) {
			if (!Lang.is(callback, Function))
				return;

			if (this.isCompleted()) {
				this._emitter.emit('after-' + type, callback, scope);
			} else {
				this._callbacks[type].push({
					handler: callback,
					scope: scope
				});
			}
		},

		_getCallbacks: function(type) {
			return this._callbacks[type];
		}
	});

	var Promise = Base.extend({

		name: 'Promise',

		init: function(deps) {
			this.base(deps);
			this._schedule = (deps && deps.schedule) || schedule;
			this._future = Future.create(this._schedule);
		},

		dispose: function() {
			this._future.dispose(); this._future = null;
			this._schedule = null;
			this.base();
		},

		getFuture: function() {
			return this._future;
		},

		fulfill: function() {
			if (this._future.isCompleted())
				throw new Error('Promise already fulfilled');
			this._schedule(this._fulfill, this, [tools.args(arguments)]);
		},

		_fulfill: function(values) {
			this._complete(true, values);
		},

		fail: function(error) {
			if (this._future.isCompleted())
				throw new Error('Promise already fulfilled');
			this._schedule(this._fail, this, [error]);
		},

		_fail: function(error) {
			this._future._failed = true;
			this._future._error = error;
			this._complete(false, [error]);
		},

		_complete: function(success, args) {
			var future = this._future;
			var type = success ? 'complete' : 'failed';
			var finallyArgs = [ success, success ? args : args[0] ];
			future._completed = true;

			future._getCallbacks(type).forEach(function(i) {
				i.handler.apply(i.scope, args);
			});

			future._getCallbacks('finally').forEach(function(i) {
				i.handler.apply(i.scope, finallyArgs);
			});

			future._emitter.on('after-' + type, function(callback, scope) {
				this._schedule(callback, scope, args);
			}, this);

			future._emitter.on('after-finally', function(callback, scope) {
				this._schedule(callback, scope, finallyArgs);
			}, this);
		}
	});

	Promise.parallel = function() {
		return Promise.all(tools.args(arguments));
	};

	Promise.all = function(futures) {
		var promise = Promise.create();
		var values = [];

		futures.forEach(function(future, index) {
			future.then(function() {
				values[index] = tools.args(arguments);
				if (futures.every(funct('hasSucceed')))
					promise.fulfill.apply(promise, values);
			}, function(error) {
				// TODO: create promise error
				promise.fail(PromiseError.create(error, index));
			});
		});

		return promise.getFuture();
	};

	Promise.serial = function(callbacks, scope) {
		if (!callbacks || callbacks.length === 0) {
			var tmp = Promise.create();
			tmp.fulfill();
			return tmp;
		}

		var promise = Promise.create();
		schedule(next, null, [ callbacks, scope, 0, promise, callbacks[0].call(scope) ]);
		return promise.getFuture();
	};

	function next(stack, scope, index, promise, value) {
		index++;

		if (index >= stack.length)
			return promise.fulfill(value);

		if (!Lang.is(value, Future))
			return next(stack, scope, index, promise, stack[index].call(scope, value));

		value.then(function() {
			next(stack, scope, index, promise, stack[index].apply(scope, arguments));
		}, function(error) {
			promise.fail(PromiseError.create(error, index));
		});
	}

	return Promise;
});