Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active June 26, 2018 20:08
Show Gist options
  • Save rvanzon/ccfb86c2fff3e31899fb to your computer and use it in GitHub Desktop.
Save rvanzon/ccfb86c2fff3e31899fb to your computer and use it in GitHub Desktop.
ES6 setup for MelonJS (using Webpack)
import game from './game';
import PlayerEntity from './entities/player';
import PlayScreen from './screens/play';
import TitleScreen from './screens/title';
class Bootstrap {
constructor() {
// Initialize the video.
if (!me.video.init("screen", me.video.CANVAS, 960, 640, true, 'auto')) {
alert("Your browser does not support HTML5 canvas.");
return;
}
// add "#debug" to the URL to enable the debug Panel
if (document.location.hash === "#debug") {
window.onReady(function () {
me.plugin.register.defer(this, me.debug.Panel, "debug", me.input.KEY.V);
});
}
// Initialize the audio.
me.audio.init("mp3,ogg");
// Set a callback to run when loading is complete.
me.loader.onload = this.loaded.bind(this);
// Load the resources.
me.loader.preload({});
// Initialize melonJS and display a loading screen.
me.state.change(me.state.LOADING);
}
loaded() {
me.state.set(me.state.MENU, new TitleScreen());
me.state.set(me.state.PLAY, new PlayScreen());
// add our player entity in the entity pool
me.pool.register("mainPlayer", PlayerEntity);
// Start the game.
me.state.change(me.state.PLAY);
}
static boot() {
var bootstrap = new Bootstrap();
// Mobile browser hacks
if (me.device.isMobile && !navigator.isCocoonJS) {
// Prevent the webview from moving on a swipe
window.document.addEventListener("touchmove", function (e) {
e.preventDefault();
window.scroll(0, 0);
return false;
}, false);
// Scroll away mobile GUI
(function () {
window.scrollTo(0, 1);
me.video.onresize(null);
}).defer();
me.event.subscribe(me.event.WINDOW_ONRESIZE, function (e) {
window.scrollTo(0, 1);
});
}
return bootstrap;
}
}
window.onReady(function onReady() {
Bootstrap.boot();
});
class Game {
constructor() {
this.data = {
score : 666,
};
}
};
export default new Game();
import game from '../game';
class PlayScreen extends me.ScreenObject {
/**
* action to perform on state change
*/
onResetEvent() {
me.game.world.addChild(new me.ColorLayer("background", "#ff0000", 0), 0);
}
/**
* action to perform when leaving this screen (state change)
*/
onDestroyEvent() {
}
};
export default PlayScreen;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment