made with requirebin
-
-
Save max-mapper/9557776 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inherits = require('inherits'); | |
var Game = require('crtrdg-gameloop'); | |
var Entity = require('crtrdg-entity'); | |
var Keyboard = require('crtrdg-keyboard'); | |
inherits(Player, Entity); | |
function Player(options){ | |
this.position = { | |
x: options.position.x, | |
y: options.position.y | |
}; | |
this.size = { | |
x: options.size.x, | |
y: options.size.y | |
}; | |
this.velocity = { | |
x: options.velocity.x, | |
y: options.velocity.y | |
}; | |
this.speed = options.speed; | |
this.friction = options.friction; | |
this.color = options.color; | |
} | |
Player.prototype.move = function(velocity){ | |
this.position.x += velocity.x; | |
this.position.y += velocity.y; | |
}; | |
Player.prototype.checkBoundaries = function(){ | |
if (this.position.x <= 0){ | |
this.position.x = 0; | |
} | |
if (this.position.x >= this.game.width - this.size.x){ | |
this.position.x = this.game.width - this.size.x; | |
} | |
if (this.position.y <= 0){ | |
this.position.y = 0; | |
} | |
if (this.position.y >= this.game.height - this.size.y){ | |
this.position.y = this.game.height - this.size.y; | |
} | |
}; | |
Player.prototype.keyboardInput = function(){ | |
if ('A' in keyboard.keysDown){ | |
this.velocity.x = -this.speed; | |
} | |
if ('D' in keyboard.keysDown){ | |
this.velocity.x = this.speed; | |
} | |
if ('W' in keyboard.keysDown){ | |
this.velocity.y = -this.speed; | |
} | |
if ('S' in keyboard.keysDown){ | |
this.velocity.y = this.speed; | |
} | |
}; | |
var game = new Game({ | |
canvasId: 'game', | |
width: window.innerWidth, | |
height: window.innerHeight, | |
backgroundColor: '#E187B8' | |
}); | |
var keyboard = new Keyboard(game); | |
var player = new Player({ | |
position: { x: 10, y: 10 }, | |
size: { x: 10, y: 10 }, | |
velocity: { x: 0, y: 0 }, | |
speed: 3, | |
friction: 0.9, | |
color: '#fff' | |
}); | |
player.addTo(game); | |
player.on('update', function(interval){ | |
this.keyboardInput(keyboard); | |
this.move(this.velocity); | |
this.velocity.x *= this.friction; | |
this.velocity.y *= this.friction; | |
this.checkBoundaries(); | |
}); | |
player.on('draw', function(draw){ | |
draw.fillStyle = this.color; | |
draw.fillRect(this.position.x, this.position.y, this.size.x, this.size.y); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Player(e){this.position={x:e.position.x,y:e.position.y},this.size={x:e.size.x,y:e.size.y},this.velocity={x:e.velocity.x,y:e.velocity.y},this.speed=e.speed,this.friction=e.friction,this.color=e.color}require=function e(t,n,i){function r(o,a){if(!n[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(s)return s(o,!0);throw Error("Cannot find module '"+o+"'")}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return r(n?n:e)},u,u.exports,e,t,n,i)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({"oxw+vU":[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}],inherits:[function(e,t){t.exports=e("oxw+vU")},{}]},{},[]),require=function e(t,n,i){function r(o,a){if(!n[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(s)return s(o,!0);throw Error("Cannot find module '"+o+"'")}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return r(n?n:e)},u,u.exports,e,t,n,i)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({1:[function(e,t){function n(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function i(e){return"function"==typeof e}function r(e){return"number"==typeof e}function s(e){return"object"==typeof e&&null!==e}function o(e){return void 0===e}t.exports=n,n.EventEmitter=n,n.prototype._events=void 0,n.prototype._maxListeners=void 0,n.defaultMaxListeners=10,n.prototype.setMaxListeners=function(e){if(!r(e)||0>e||isNaN(e))throw TypeError("n must be a positive number");return this._maxListeners=e,this},n.prototype.emit=function(e){var t,n,r,a,h,u;if(this._events||(this._events={}),"error"===e&&(!this._events.error||s(this._events.error)&&!this._events.error.length))throw t=arguments[1],t instanceof Error?t:TypeError('Uncaught, unspecified "error" event.');if(n=this._events[e],o(n))return!1;if(i(n))switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];n.apply(this,a)}else if(s(n)){for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];for(u=n.slice(),r=u.length,h=0;r>h;h++)u[h].apply(this,a)}return!0},n.prototype.addListener=function(e,t){var r;if(!i(t))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",e,i(t.listener)?t.listener:t),this._events[e]?s(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,s(this._events[e])&&!this._events[e].warned){var r;r=o(this._maxListeners)?n.defaultMaxListeners:this._maxListeners,r&&r>0&&this._events[e].length>r&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}return this},n.prototype.on=n.prototype.addListener,n.prototype.once=function(e,t){function n(){this.removeListener(e,n),r||(r=!0,t.apply(this,arguments))}if(!i(t))throw TypeError("listener must be a function");var r=!1;return n.listener=t,this.on(e,n),this},n.prototype.removeListener=function(e,t){var n,r,o,a;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;if(n=this._events[e],o=n.length,r=-1,n===t||i(n.listener)&&n.listener===t)delete this._events[e],this._events.removeListener&&this.emit("removeListener",e,t);else if(s(n)){for(a=o;a-->0;)if(n[a]===t||n[a].listener&&n[a].listener===t){r=a;break}if(0>r)return this;1===n.length?(n.length=0,delete this._events[e]):n.splice(r,1),this._events.removeListener&&this.emit("removeListener",e,t)}return this},n.prototype.removeAllListeners=function(e){var t,n;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[e]&&delete this._events[e],this;if(0===arguments.length){for(t in this._events)"removeListener"!==t&&this.removeAllListeners(t);return this.removeAllListeners("removeListener"),this._events={},this}if(n=this._events[e],i(n))this.removeListener(e,n);else for(;n.length;)this.removeListener(e,n[n.length-1]);return delete this._events[e],this},n.prototype.listeners=function(e){var t;return t=this._events&&this._events[e]?i(this._events[e])?[this._events[e]]:this._events[e].slice():[]},n.listenerCount=function(e,t){var n;return n=e._events&&e._events[t]?i(e._events[t])?1:e._events[t].length:0}},{}],BatbyD:[function(e,t){function n(e){var e=e||{};e.canvas?"string"==typeof e.canvas?this.canvas=document.getElementById(e.canvas):"object"==typeof e.canvas&&e.canvas.tagName&&(this.canvas=e.canvas):(this.canvas=document.createElement("canvas"),this.canvas.id="game",document.body.appendChild(this.canvas)),this.context=this.canvas.getContext("2d"),this.width=this.canvas.width=e.width||window.innerWidth,this.height=this.canvas.height=e.height||window.innerHeight,this.backgroundColor=e.backgroundColor||"#E187B8",this.ticker=r(this.canvas),this.paused=!1,e.maxListeners?this.setMaxListeners(e.maxListeners):this.setMaxListeners(0),this.loop()}var i=e("events").EventEmitter,r=e("raf"),s=e("inherits");t.exports=n,s(n,i),n.prototype.loop=function(){var e=this;this.ticker.on("data",function(t){e.update(t),e.draw()})},n.prototype.pause=function(){this.paused=!0,this.ticker.pause(),this.emit("pause")},n.prototype.resume=function(){var e=this;this.paused=!1,this.ticker=r(this.canvas),this.ticker.on("data",function(t){e.update(t),e.draw()}),this.emit("resume")},n.prototype.update=function(e){this.currentScene&&this.sceneManager.update(e),this.emit("update",e)},n.prototype.draw=function(){this.currentScene?(this.context.fillStyle=this.currentScene.backgroundColor,this.sceneManager.draw(this.context)):this.context.fillStyle=this.backgroundColor,this.context.fillRect(0,0,this.width,this.height),this.emit("draw",this.context)}},{events:1,inherits:4,raf:5}],"crtrdg-gameloop":[function(e,t){t.exports=e("BatbyD")},{}],4:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}],5:[function(e,t){function n(e){function t(){var i=n.now(),a=i-r;r=i,s.emit("data",a),s.paused||o(t,e)}var r=n.now(),s=new i;return s.pause=function(){s.paused=!0},s.resume=function(){s.paused=!1},o(t,e),s}t.exports=n;var i=e("events").EventEmitter,r="undefined"==typeof window?this:window,s=r.performance&&r.performance.now?function(){return performance.now()}:Date.now||function(){return+new Date},o=r.requestAnimationFrame||r.webkitRequestAnimationFrame||r.mozRequestAnimationFrame||r.msRequestAnimationFrame||r.oRequestAnimationFrame||(r.setImmediate?function(e){setImmediate(e)}:function(e){setTimeout(e,0)});n.polyfill=o,n.now=s},{events:1}]},{},[]),require=function e(t,n,i){function r(o,a){if(!n[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(s)return s(o,!0);throw Error("Cannot find module '"+o+"'")}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return r(n?n:e)},u,u.exports,e,t,n,i)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({1:[function(e,t){function n(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function i(e){return"function"==typeof e}function r(e){return"number"==typeof e}function s(e){return"object"==typeof e&&null!==e}function o(e){return void 0===e}t.exports=n,n.EventEmitter=n,n.prototype._events=void 0,n.prototype._maxListeners=void 0,n.defaultMaxListeners=10,n.prototype.setMaxListeners=function(e){if(!r(e)||0>e||isNaN(e))throw TypeError("n must be a positive number");return this._maxListeners=e,this},n.prototype.emit=function(e){var t,n,r,a,h,u;if(this._events||(this._events={}),"error"===e&&(!this._events.error||s(this._events.error)&&!this._events.error.length))throw t=arguments[1],t instanceof Error?t:TypeError('Uncaught, unspecified "error" event.');if(n=this._events[e],o(n))return!1;if(i(n))switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];n.apply(this,a)}else if(s(n)){for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];for(u=n.slice(),r=u.length,h=0;r>h;h++)u[h].apply(this,a)}return!0},n.prototype.addListener=function(e,t){var r;if(!i(t))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",e,i(t.listener)?t.listener:t),this._events[e]?s(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,s(this._events[e])&&!this._events[e].warned){var r;r=o(this._maxListeners)?n.defaultMaxListeners:this._maxListeners,r&&r>0&&this._events[e].length>r&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}return this},n.prototype.on=n.prototype.addListener,n.prototype.once=function(e,t){function n(){this.removeListener(e,n),r||(r=!0,t.apply(this,arguments))}if(!i(t))throw TypeError("listener must be a function");var r=!1;return n.listener=t,this.on(e,n),this},n.prototype.removeListener=function(e,t){var n,r,o,a;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;if(n=this._events[e],o=n.length,r=-1,n===t||i(n.listener)&&n.listener===t)delete this._events[e],this._events.removeListener&&this.emit("removeListener",e,t);else if(s(n)){for(a=o;a-->0;)if(n[a]===t||n[a].listener&&n[a].listener===t){r=a;break}if(0>r)return this;1===n.length?(n.length=0,delete this._events[e]):n.splice(r,1),this._events.removeListener&&this.emit("removeListener",e,t)}return this},n.prototype.removeAllListeners=function(e){var t,n;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[e]&&delete this._events[e],this;if(0===arguments.length){for(t in this._events)"removeListener"!==t&&this.removeAllListeners(t);return this.removeAllListeners("removeListener"),this._events={},this}if(n=this._events[e],i(n))this.removeListener(e,n);else for(;n.length;)this.removeListener(e,n[n.length-1]);return delete this._events[e],this},n.prototype.listeners=function(e){var t;return t=this._events&&this._events[e]?i(this._events[e])?[this._events[e]]:this._events[e].slice():[]},n.listenerCount=function(e,t){var n;return n=e._events&&e._events[t]?i(e._events[t])?1:e._events[t].length:0}},{}],UwIddh:[function(e,t){function n(){return this}var i=e("events").EventEmitter,r=e("inherits");t.exports=n,r(n,i),n.prototype.addTo=function(e,t){return this.game=e||{},this.game.entities||(this.game.entities=[]),this.game.entities.push(this),this.game.findEntity=this.findEntity,this.initializeListeners(),this.exists=!0,t&&t(this),this},n.prototype.initializeListeners=function(){var e=this;this.findEntity(this,function(t){t&&(e.game.on("update",function(t){e.emit("update",t)}),e.game.on("draw",function(t){e.emit("draw",t)}))})},n.prototype.remove=function(){this.exists=!1,this.removeAllListeners("update"),this.removeAllListeners("draw"),this.findEntity(this,function(e,t,n){e&&t.splice(n,1)})},n.prototype.findEntity=function(e,t){var n;n=void 0===this.game?this.entities:this.game.entities;for(var i=0;n.length>i;i++)n[i]===e&&t(!0,n,i)}},{events:1,inherits:4}],"crtrdg-entity":[function(e,t){t.exports=e("UwIddh")},{}],4:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}]},{},[]),require=function e(t,n,i){function r(o,a){if(!n[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(s)return s(o,!0);throw Error("Cannot find module '"+o+"'")}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return r(n?n:e)},u,u.exports,e,t,n,i)}return n[o].exports}for(var s="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({1:[function(e,t){function n(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function i(e){return"function"==typeof e}function r(e){return"number"==typeof e}function s(e){return"object"==typeof e&&null!==e}function o(e){return void 0===e}t.exports=n,n.EventEmitter=n,n.prototype._events=void 0,n.prototype._maxListeners=void 0,n.defaultMaxListeners=10,n.prototype.setMaxListeners=function(e){if(!r(e)||0>e||isNaN(e))throw TypeError("n must be a positive number");return this._maxListeners=e,this},n.prototype.emit=function(e){var t,n,r,a,h,u;if(this._events||(this._events={}),"error"===e&&(!this._events.error||s(this._events.error)&&!this._events.error.length))throw t=arguments[1],t instanceof Error?t:TypeError('Uncaught, unspecified "error" event.');if(n=this._events[e],o(n))return!1;if(i(n))switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];n.apply(this,a)}else if(s(n)){for(r=arguments.length,a=Array(r-1),h=1;r>h;h++)a[h-1]=arguments[h];for(u=n.slice(),r=u.length,h=0;r>h;h++)u[h].apply(this,a)}return!0},n.prototype.addListener=function(e,t){var r;if(!i(t))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",e,i(t.listener)?t.listener:t),this._events[e]?s(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,s(this._events[e])&&!this._events[e].warned){var r;r=o(this._maxListeners)?n.defaultMaxListeners:this._maxListeners,r&&r>0&&this._events[e].length>r&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}return this},n.prototype.on=n.prototype.addListener,n.prototype.once=function(e,t){function n(){this.removeListener(e,n),r||(r=!0,t.apply(this,arguments))}if(!i(t))throw TypeError("listener must be a function");var r=!1;return n.listener=t,this.on(e,n),this},n.prototype.removeListener=function(e,t){var n,r,o,a;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;if(n=this._events[e],o=n.length,r=-1,n===t||i(n.listener)&&n.listener===t)delete this._events[e],this._events.removeListener&&this.emit("removeListener",e,t);else if(s(n)){for(a=o;a-->0;)if(n[a]===t||n[a].listener&&n[a].listener===t){r=a;break}if(0>r)return this;1===n.length?(n.length=0,delete this._events[e]):n.splice(r,1),this._events.removeListener&&this.emit("removeListener",e,t)}return this},n.prototype.removeAllListeners=function(e){var t,n;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[e]&&delete this._events[e],this;if(0===arguments.length){for(t in this._events)"removeListener"!==t&&this.removeAllListeners(t);return this.removeAllListeners("removeListener"),this._events={},this}if(n=this._events[e],i(n))this.removeListener(e,n);else for(;n.length;)this.removeListener(e,n[n.length-1]);return delete this._events[e],this},n.prototype.listeners=function(e){var t;return t=this._events&&this._events[e]?i(this._events[e])?[this._events[e]]:this._events[e].slice():[]},n.listenerCount=function(e,t){var n;return n=e._events&&e._events[t]?i(e._events[t])?1:e._events[t].length:0}},{}],PPtGFE:[function(e,t){function n(e){this.game=e||{},this.keysDown={},this.initializeListeners()}var i=e("events").EventEmitter,r=e("inherits"),s=e("vkey");t.exports=n,r(n,i),n.prototype.initializeListeners=function(){var e=this;document.addEventListener("keydown",function(t){e.emit("keydown",s[t.keyCode]),e.keysDown[s[t.keyCode]]=!0,(40===t.keyCode||38===t.keyCode||37===t.keyCode||39===t.keyCode||32===t.keyCode)&&t.preventDefault()},!1),document.addEventListener("keyup",function(t){e.emit("keyup",s[t.keyCode]),delete e.keysDown[s[t.keyCode]]},!1)}},{events:1,inherits:4,vkey:5}],"crtrdg-keyboard":[function(e,t){t.exports=e("PPtGFE")},{}],4:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}],5:[function(e,t){var n,i="undefined"!=typeof window?window.navigator.userAgent:"",r=/OS X/.test(i),s=/Opera/.test(i),o=!/like Gecko/.test(i)&&!s,a=t.exports={0:r?"<menu>":"<UNK>",1:"<mouse 1>",2:"<mouse 2>",3:"<break>",4:"<mouse 3>",5:"<mouse 4>",6:"<mouse 5>",8:"<backspace>",9:"<tab>",12:"<clear>",13:"<enter>",16:"<shift>",17:"<control>",18:"<alt>",19:"<pause>",20:"<caps-lock>",21:"<ime-hangul>",23:"<ime-junja>",24:"<ime-final>",25:"<ime-kanji>",27:"<escape>",28:"<ime-convert>",29:"<ime-nonconvert>",30:"<ime-accept>",31:"<ime-mode-change>",27:"<escape>",32:"<space>",33:"<page-up>",34:"<page-down>",35:"<end>",36:"<home>",37:"<left>",38:"<up>",39:"<right>",40:"<down>",41:"<select>",42:"<print>",43:"<execute>",44:"<snapshot>",45:"<insert>",46:"<delete>",47:"<help>",91:"<meta>",92:"<meta>",93:r?"<meta>":"<menu>",95:"<sleep>",106:"<num-*>",107:"<num-+>",108:"<num-enter>",109:"<num-->",110:"<num-.>",111:"<num-/>",144:"<num-lock>",145:"<scroll-lock>",160:"<shift-left>",161:"<shift-right>",162:"<control-left>",163:"<control-right>",164:"<alt-left>",165:"<alt-right>",166:"<browser-back>",167:"<browser-forward>",168:"<browser-refresh>",169:"<browser-stop>",170:"<browser-search>",171:"<browser-favorites>",172:"<browser-home>",173:r&&o?"-":"<volume-mute>",174:"<volume-down>",175:"<volume-up>",176:"<next-track>",177:"<prev-track>",178:"<stop>",179:"<play-pause>",180:"<launch-mail>",181:"<launch-media-select>",182:"<launch-app 1>",183:"<launch-app 2>",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"<meta>",224:"<meta>",226:"<alt-gr>",229:"<ime-process>",231:s?"`":"<unicode>",246:"<attention>",247:"<crsel>",248:"<exsel>",249:"<erase-eof>",250:"<play>",251:"<zoom>",252:"<no-name>",253:"<pa-1>",254:"<clear>"};for(n=58;65>n;++n)a[n]=String.fromCharCode(n);for(n=48;58>n;++n)a[n]=n-48+"";for(n=65;91>n;++n)a[n]=String.fromCharCode(n);for(n=96;107>n;++n)a[n]="<num-"+(n-96)+">";for(n=112;136>n;++n)a[n]="F"+(n-111)},{}]},{},[]);var inherits=require("inherits"),Game=require("crtrdg-gameloop"),Entity=require("crtrdg-entity"),Keyboard=require("crtrdg-keyboard");inherits(Player,Entity),Player.prototype.move=function(e){this.position.x+=e.x,this.position.y+=e.y},Player.prototype.checkBoundaries=function(){0>=this.position.x&&(this.position.x=0),this.position.x>=this.game.width-this.size.x&&(this.position.x=this.game.width-this.size.x),0>=this.position.y&&(this.position.y=0),this.position.y>=this.game.height-this.size.y&&(this.position.y=this.game.height-this.size.y)},Player.prototype.keyboardInput=function(){"A"in keyboard.keysDown&&(this.velocity.x=-this.speed),"D"in keyboard.keysDown&&(this.velocity.x=this.speed),"W"in keyboard.keysDown&&(this.velocity.y=-this.speed),"S"in keyboard.keysDown&&(this.velocity.y=this.speed)};var game=new Game({canvasId:"game",width:window.innerWidth,height:window.innerHeight,backgroundColor:"#E187B8"}),keyboard=new Keyboard(game),player=new Player({position:{x:10,y:10},size:{x:10,y:10},velocity:{x:0,y:0},speed:3,friction:.9,color:"#fff"});player.addTo(game),player.on("update",function(){this.keyboardInput(keyboard),this.move(this.velocity),this.velocity.x*=this.friction,this.velocity.y*=this.friction,this.checkBoundaries()}),player.on("draw",function(e){e.fillStyle=this.color,e.fillRect(this.position.x,this.position.y,this.size.x,this.size.y)}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"inherits": "2.0.1", | |
"crtrdg-gameloop": "0.0.4", | |
"crtrdg-entity": "0.0.4", | |
"crtrdg-keyboard": "0.0.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment