2016-06-07 13:18:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
class Renderer
|
|
|
|
{
|
|
|
|
constructor(canvas)
|
|
|
|
{
|
|
|
|
this.canvas = canvas;
|
|
|
|
this.context = canvas.getContext("2d");
|
|
|
|
|
|
|
|
this.trackWindowSize();
|
|
|
|
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
setup()
|
|
|
|
{
|
2016-06-08 12:52:55 +00:00
|
|
|
this.hillSets = [];
|
|
|
|
this.hillSets.push(new HillSet(new Vector(2048, this.canvas.height * 0.9), 0.4, "rgb(102, 164, 90)"));
|
|
|
|
this.hillSets.push(new HillSet(new Vector(2048, this.canvas.height * 0.6), 0.4, "rgb(43, 131, 35)"));
|
|
|
|
this.hillSets.push(new HillSet(new Vector(2048, this.canvas.height * 0.2), 0.4, "rgb(50, 111, 8)"));
|
|
|
|
this.hillSets[0].pos = new Vector(0, this.canvas.height - this.hillSets[0].size.y);
|
|
|
|
this.hillSets[1].pos = new Vector(0, this.canvas.height - this.hillSets[1].size.y);
|
|
|
|
this.hillSets[2].pos = new Vector(0, this.canvas.height - this.hillSets[2].size.y);
|
|
|
|
console.log(this.hillSets);
|
2016-06-07 13:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start()
|
|
|
|
{
|
|
|
|
this.lastTime = +new Date();
|
|
|
|
this.nextFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
nextFrame()
|
|
|
|
{
|
|
|
|
let startTime = +new Date();
|
|
|
|
this.update((+new Date() - this.lastTime) / 1000);
|
|
|
|
this.render(this.canvas, this.context);
|
|
|
|
|
|
|
|
this.lastTime = startTime;
|
|
|
|
|
|
|
|
requestAnimationFrame(this.nextFrame.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the simluation ready for the next frame.
|
|
|
|
* @param {number} dt The number of seconds since the last frame was rendered.
|
|
|
|
*/
|
|
|
|
update(dt)
|
|
|
|
{
|
2016-06-08 12:52:55 +00:00
|
|
|
for(let hillSet of this.hillSets)
|
|
|
|
{
|
|
|
|
hillSet.update(dt);
|
|
|
|
}
|
2016-06-07 13:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render(canvas, context)
|
|
|
|
{
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
/*context.fillStyle = "red";
|
|
|
|
context.fillRect(10, 10, 100, 100);*/
|
2016-06-08 12:52:55 +00:00
|
|
|
for(let hillSet of this.hillSets)
|
|
|
|
{
|
|
|
|
hillSet.render(context);
|
|
|
|
}
|
2016-06-07 13:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the canvas size to match the current viewport size.
|
|
|
|
*/
|
|
|
|
matchWindowSize() {
|
|
|
|
this.canvas.width = window.innerWidth;
|
|
|
|
this.canvas.height = window.innerHeight;
|
|
|
|
|
|
|
|
//this.render(this.context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the canvas size track the window size.
|
|
|
|
*/
|
|
|
|
trackWindowSize() {
|
|
|
|
this.matchWindowSize();
|
|
|
|
window.addEventListener("resize", this.matchWindowSize.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("load", function (event) {
|
|
|
|
var canvas = document.getElementById("canvas-main"),
|
|
|
|
renderer = new Renderer(canvas);
|
|
|
|
renderer.start();
|
|
|
|
window.renderer = renderer;
|
|
|
|
});
|