Skip to content

Instantly share code, notes, and snippets.

View raduGaspar's full-sized avatar

Radu B. Gaspar raduGaspar

View GitHub Profile
export default class KeyboardEvents {
static get KEY_UP() {
return 'keyup'; // actual event name, don't change
}
static get KEY_DOWN() {
return 'keydown'; // actual event name, don't change
}
};
import { KeyboardEvents, EventDispatcher } from '../'
let keyboardInstance;
let keysMap = {};
class KeyboardSingleton {
constructor() {
if(!keyboardInstance) {
console.log('Keyboard instance created');
keyboardInstance = this;
let pubSubInstance;
const subjects = {};
const hOP = subjects.hasOwnProperty;
class PubSub {
constructor() {
if(!pubSubInstance) {
console.log('EventDispatcher instance created');
pubSubInstance = this;
}
export default class Utils {
constructor() {
let e = new Error('is a static class, no need to instantiate!');
e.name = 'Utils';
throw e.toString();
}
static toCamelCase(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
import Utils from './Utils';
let assetLoaderInstance;
export default class AssetsLoader {
constructor() {
if(!assetLoaderInstance) {
console.log('AssetsLoader instance created');
this.assets = {};
assetLoaderInstance = this;
@raduGaspar
raduGaspar / dylg2-game.js
Created October 24, 2016 17:08
A simple Game class which accepts multiple scene elements and updates them in a game loop
export default class Game {
constructor(scenes) {
// check if the browser supports requestAnimationFrame
if(!(typeof requestAnimationFrame)) {
throw new Error('Your browser doesn\'t support requestAnimationFrame :(');
}
this.scenes = [];
this.addScenes(scenes);
this.play();
let EventDispatcher = (function() {
let instance = null;
class EventDispatcher {
constructor() {
if(!instance) {
instance = this;
}
return instance;
@raduGaspar
raduGaspar / dp-decorator.js
Created May 25, 2016 13:55
Decorator design pattern example
// Design Patterns: Decorator
class Drink {
constructor() {
this.total = function() {
return 0;
}
}
}
// decorator 1
const swimmer = (state) => ({
swim: () => console.log(state.name, 'is swimming')
})
const eater = (state) => ({
eat: () => console.log(state.name, 'is eating')
})
const sleeper = (state) => ({
sleep: () => console.log(state.name, 'is sleeping')
class Animal {
swim() {
console.log('swimming');
}
eat() {
console.log('eating');
}
sleep() {
console.log('sleeping');
}