Oliver Zeigermann / @DJCordhose
Nils Hartmann / @nilshartmann
Online version at: http://djcordhose.github.io/games/slides/jsunconf.html
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
// draw a ball
function draw() {
context.fillStyle = this.color;
context.beginPath();
context.arc(this.position.x, this.position.y, this.r, 0, Math.PI * 2);
context.fill();
context.closePath();
}
function loop() {
if (running) requestAnimationFrame(loop);
var now = now();
var deltaT = now - previousNow;
previousNow = now;
objects.forEach(function (object) {
if (object.update) {
object.update(deltaT);
}
});
context.clearRect(0, 0, canvas.width, canvas.height);
objects.forEach(function (object) {
if (object.draw) {
object.draw();
}
});
}
// highly simplified
var context = new AudioContext();
function createOscillator() {
// Oscillator defaults to sine wave
var oscillator = context.createOscillator();
oscillator.type = oscillator.SQUARE;
oscillator.frequency.value = 440;
// Connect the source to the output.
oscillator.connect(context.destination);
return oscillator;
}
function update(deltaT) {
control();
// change position based on speed
this.position.x += this.velocity.x * deltaT;
this.position.y += this.velocity.y * deltaT;
// gravity
this.velocity.y += this.gravity * deltaT;
}
function ballsCollide(ball1, ball2) {
// a^2 + b^2 = c^2
var a = ball2.position.x - ball1.position.x;
var b = ball2.position.y - ball1.position.y;
var c = ball1.r + ball2.r;
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) < c;
}
var pressed = {};
window.onkeydown = function (e) {
pressed[e.keyCode] = true;
};
window.onkeyup = function (e) {
delete pressed[e.keyCode];
};
function control() {
if (38 in pressed) this.velocity.y -= this.acceleration * deltaT; // up
if (40 in pressed) this.velocity.y += this.acceleration * deltaT; // down
if (37 in pressed) this.velocity.x -= this.acceleration * deltaT; // left
if (39 in pressed) this.velocity.x += this.acceleration * deltaT; // right
}
function GameObject(config) {
this.config = config;
}
function Ball(config) {
GameObject.call(this, config);
this.position = config.position;
this.r = config.r;
this.color = config.color;
}
util._extends(Ball, GameObject);
Ball.prototype.draw = draw;
Ball.prototype.update = update;
function MovingObject(config) {
// ...
}
// What parameters are required for MovingObject ????
var mo = new MovingObject( { ??? } );
Oliver Zeigermann / @DJCordhose
Nils Hartmann / @nilshartmann