A lightweight JavaScript game engine
git clone git://soophie.de/game-engine
3
<head>4
<meta charset="utf-8">5
<meta name="viewport" content="width=device-width, initial-scale=1.0">6
<title>Universe</title>7
<style type="text/css">9
* {
27
const height = window.innerHeight;29
class Particle extends Surface {29
constructor(x, y) {30
super(x, y, new Shape.Circle(0));31
this.origin = {x, y};32
this.init();33
}34
init() {35
this.time = 0;36
this.angle = Math.random() * 2 * Math.PI;37
this.offset = Math.random() * width - width / 2;38
this.x = this.origin.x + Math.cos(this.angle) * this.offset;39
this.y = this.origin.y + Math.sin(this.angle) * this.offset;40
this.shape.radius = this.time;41
}42
update() {43
this.time += 0.01;44
this.x += Math.cos(this.angle) * this.time / this.offset * 50;45
this.y += Math.sin(this.angle) * this.time / this.offset * 50;46
this.shape.radius = this.time / Math.abs(this.offset) * 50;47
if(!(this.x > 0 && this.x < width && this.y > 0 && this.y < height)) {48
this.init();30
constructor(x, y) {31
super(x, y, new Shape.Circle(0));32
this.origin = {x, y};33
this.init();34
}35
init() {36
this.time = 0;37
this.angle = Math.random() * 2 * Math.PI;38
this.offset = Math.random() * width - width / 2;39
this.x = this.origin.x + Math.cos(this.angle) * this.offset;40
this.y = this.origin.y + Math.sin(this.angle) * this.offset;41
this.shape.radius = this.time;42
}43
update() {44
this.time += 0.01;45
this.x += Math.cos(this.angle) * this.time / this.offset * 50;46
this.y += Math.sin(this.angle) * this.time / this.offset * 50;47
this.shape.radius = this.time / Math.abs(this.offset) * 50;48
if(!(this.x > 0 && this.x < width && this.y > 0 && this.y < height)) {49
this.init();50
}51
}52
render(g) {53
const {x, y, shape: {radius}} = this;54
g.fillStyle = 'white';55
g.beginPath();56
g.arc(x, y, radius, 2 * Math.PI, 0);57
g.fill();58
}50
}51
render(g) {52
const {x, y, shape: {radius}} = this;53
g.fillStyle = 'white';54
g.beginPath();55
g.arc(x, y, radius, 2 * Math.PI, 0);56
g.fill();57
}59
}61
class Universe extends Game {61
constructor() {62
super(width, height);63
this.group = new Group();64
for(let i = 0; i < 500; i++) {65
this.group.add(new Particle(width / 2, height / 2));62
constructor() {63
super(width, height);64
this.group = new Group();65
for(let i = 0; i < 500; i++) {66
this.group.add(new Particle(width / 2, height / 2));67
}68
}69
update() {70
this.group.update();71
}72
render(g) {73
super.render(g);74
g.fillStyle = 'black';75
g.fillRect(0, 0, this.width, this.height);76
this.group.render(g);77
}67
}68
update() {69
this.group.update();70
}71
render(g) {72
super.render(g);73
g.fillRect(0, 0, this.width, this.height);74
this.group.render(g);75
}78
}80
const game = new Universe();