12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- function Background(camera) {
- this.position = {x: 0, y: 0};
- this.width = 768;
- this.height = 768;
- this.spriteCanvas = null;
- this.spriteContext = null;
- this.canvasWidth = 768;
- this.canvasHeight = 768;
- this.init = function() {
- this.preRender();
- }
- this.update = function(canvas) {
- this.canvasWidth = canvas.width;
- this.canvasHeight = canvas.height;
- var truePos = {x: 0, y: 0};
- truePos.x = camera.position.x + this.position.x;
- truePos.y = camera.position.y + this.position.y;
- if(truePos.x < -this.width) {
- this.position.x += this.width;
- }
- if(truePos.x > 0) {
- this.position.x -= this.width;
- }
- if(truePos.y < -this.height) {
- this.position.y += this.height;
- }
- if(truePos.y > 0) {
- this.position.y -= this.height;
- }
- }
- this.oldPreRender = function() {
- if(this.spriteContext == null) {
- this.spriteCanvas = document.createElement("canvas");
- this.spriteCanvas.width = this.width + 1;
- this.spriteCanvas.height = this.height + 1;
- this.spriteContext = this.spriteCanvas.getContext("2d");
- this.spriteContext.translate(0.5, 0.5);
- this.spriteContext.strokeStyle = "gold";
-
- /*var columns = parseInt(Math.sqrt(this.width));
- var rows = parseInt(Math.sqrt(this.height));*/
- var columns = parseInt(this.width / 128);
- var rows = parseInt(this.height / 128);
- for(var i = 0; i < columns; i++) {
- this.spriteContext.beginPath();
- this.spriteContext.moveTo(i * (this.width / columns), 1);
- this.spriteContext.lineTo(i * (this.width / columns), this.height);
- this.spriteContext.stroke();
- }
- for(var i = 0; i < rows; i++) {
- this.spriteContext.beginPath();
- this.spriteContext.moveTo(1, i * (this.height / rows));
- this.spriteContext.lineTo(this.width, i * (this.height / rows));
- this.spriteContext.stroke();
- }
- }
- }
- this.preRender = function() {
- if(this.spriteContext == null) {
- this.spriteCanvas = document.createElement("canvas");
- this.spriteCanvas.width = this.width + 1;
- this.spriteCanvas.height = this.height + 1;
- this.spriteContext = this.spriteCanvas.getContext("2d");
- var columns = parseInt(this.width / 32);
- var rows = parseInt(this.height / 32);
- for(var x = 0; x < columns; x++) {
- for(var y = 0; y < rows; y++) {
- this.spriteContext.drawImage(system.assets.getSprite("grass" + parseInt(3 * Math.random() + 1)), 0, 0, 16, 16, x * 32, y * 32, 32, 32);
- }
- }
- }
- }
- this.draw = function(context) {
- for(var x = 0; x < this.canvasWidth / this.width + 1; x++) {
- for(var y = 0; y < this.canvasHeight / this.height + 1; y++) {
- context.drawImage(this.spriteCanvas, this.position.x + (x * this.width), this.position.y + (y * this.height));
- }
- }
- }
- };
|