Skip to content
This repository has been archived by the owner on May 15, 2020. It is now read-only.

Commit

Permalink
Converted World.js code to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
joymon committed Sep 17, 2015
1 parent 15af5b9 commit 9a4d0bd
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions Src/Karel/js/world.ts
@@ -1,64 +1,69 @@
//Class World class World {
function World(canvas) { private cols: number = 9;
this.cols = 9; private rows: number = 9;
this.rows = 9; private context: CanvasRenderingContext2D;
this.context = canvas.getContext("2d"); private boardOffset: number;
this.boardOffset = 0; private boardWidth: number;
this.boardWidth; private boardHeight: number;
this.size = {width:9,height:9}; private size: any;
}; constructor(canvas:HTMLCanvasElement) {
World.prototype.getSize = function () { this.context = canvas.getContext("2d");
return this.size; this.boardOffset = 0;
}; this.size = { width: 9, height: 9 };
World.prototype.getScreenDimensions = function () {
var w, h;
if ($(window).width() > $(window).height()) {
w = $(window).width();
h = $(window).height();
} else {
h = $(window).width();
w = $(window).height();
} }
}; getSize() {
World.prototype.getCellSize = function () { return this.size;
return {w:this.boardWidth / this.cols,h:this.boardHeight / this.rows}; }
}; getScreenDimensions() {
World.prototype.getCellCenter = function (row, col) { var w, h;
cellWidth=this.boardWidth / this.cols; if ($(window).width() > $(window).height()) {
cellHeight = this.boardHeight / this.rows; w = $(window).width();
return { x:(col*cellWidth) + (cellWidth/2) , y: (row* cellHeight) + (cellHeight/2) }; h = $(window).height();
}; } else {
World.prototype.draw = function () { h = $(window).width();
this.context.strokeStyle = "black"; w = $(window).height();
this.boardWidth = this.context.canvas.width; }
this.boardHeight = this.context.canvas.height; }
var x = this.boardOffset; getCellSize() {
var y = 0; return { w: this.boardWidth / this.cols, h: this.boardHeight / this.rows };
}
getCellCenter(row, col) {
var cellWidth = this.boardWidth / this.cols;
var cellHeight = this.boardHeight / this.rows;
return { x: (col * cellWidth) + (cellWidth / 2), y: (row * cellHeight) + (cellHeight / 2) };
}
draw() {
this.context.strokeStyle = "black";
this.boardWidth = this.context.canvas.width;
this.boardHeight = this.context.canvas.height;
var x = this.boardOffset;
var y = 0;


// this.context.canvas.width = this.screenWidth; // this.context.canvas.width = this.screenWidth;
// this.context.canvas.height = this.screenHeight; // this.context.canvas.height = this.screenHeight;


this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height); this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
//this.iDrawBoardSquare(x, y); //this.iDrawBoardSquare(x, y);
this.iDrawSquares(x, y); this.iDrawSquares(x, y);


}; }
World.prototype.iDrawBoardSquare = function (x, y) { iDrawBoardSquare(x, y) {
this.context.fillStyle = "black"; this.context.fillStyle = "black";
this.context.lineWidth = 2; this.context.lineWidth = 2;
this.context.strokeStyle = "black"; this.context.strokeStyle = "black";
this.context.strokeRect(x, y, this.boardWidth, this.boardHeight); this.context.strokeRect(x, y, this.boardWidth, this.boardHeight);
}; }
World.prototype.iDrawSquares = function (x, y) { iDrawSquares(x, y) {
this.context.strokeStyle = "grey"; this.context.strokeStyle = "grey";
var cw = this.boardWidth / this.cols; var cw = this.boardWidth / this.cols;
var ch = this.boardHeight / this.rows; var ch = this.boardHeight / this.rows;
// draw all 81 little squares // draw all 81 little squares
this.context.lineWidth = 1; this.context.lineWidth = 1;
for (i = 0; i < this.rows; i++) { for (var i = 0; i < this.rows; i++) {
for (j = 0; j < this.cols; j++) { for (var j = 0; j < this.cols; j++) {
this.context.strokeRect(x + (j * cw), y + (i * ch), cw, ch); this.context.strokeRect(x + (j * cw), y + (i * ch), cw, ch);


}
} }
} }
} }

0 comments on commit 9a4d0bd

Please sign in to comment.