-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.js
97 lines (91 loc) · 2.98 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var canvas = document.getElementById('player'); /*define canvas*/
var ctx = canvas.getContext('2d'); /*make 2d canvas*/
var fps = 25; /*frames per seconds*/
var ucode; /* Var that will hold users code */
var code;
var errorcode; /* Errorcode reported to the user */
var sprites = {}; /* An Object for all the sprites. Just trust me on the whole object thing please*/
function Sprite(name, x, y, direction) {
this.name = name;
this.x = x;
this.y = y;
this.direction = direction;
this.walk = function(steps) { /*walking in the direction the sprite is facing*/
this.x = (this.x + (Math.sin(this.direction) * steps)); /*x for walking*/
this.y = (this.y + (Math.cos(this.direction) * steps)); /*y for walking*/
return { /*returns the new x and y in a object */
x: this.x,
y: this.y
}
};
this.changeX = function(x) {
this.x = this.x + x; /*Changes x*/
return this.x; /*returns the new x*/
};
this.changeY = function(y) {
this.y = this.y + y; /*Changes y*/
return this.y; /*returns the new y*/
};
this.setX = function(x) {
this.x = x; /*Set x*/
return this.x; /*returns the new x*/
};
this.setY = function(y) {
this.y = y; /*Sets y*/
return this.y; /*returns the new y*/
};
this.turn = function(degrees) {
this.direction = (this.direction + degrees) % 360; /*Turn Sprite in direction*/
if (this.direction < 0) {
this.direction = 0;
}
return this.direction; /*returns the direction*/
};
this.turnTo = function(angle) {
this.direction = angle % 360; /*Turn Sprite towards direction*/
if (this.direction < 0) {
this.direction = 0;
}
return this.direction; /*returns the direction*/
};
this.pointTo = function(x, y) {
this.direction = (Math.atan2(x - this.x, y - this.y)) * (180 /
Math.PI); /* Math to point*/
return this.direction /* return new direction */
};
}
function onload() {
}
function draw() {
}
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function makeSprite(name, x, y, direction) {
this[name] = new Sprite(name, x, y, direction); /* Add spirtes*/
}
function runCode() {
eval(editor.getValue())
onload(); /*run onload(); which will be a user command.*/
code = setInterval(function() { /*Uses var code so we can have a kill button */
try {
clear();
eval(draw()); /*users code*/
} catch (err) {
console.log(err);
errorcode = err; /* Send error to client */
clear();
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText(err,canvas.width/2, canvas.height/2);
clearInterval(code); /* Stop code from running */
}
}, 1000 / fps); /*set framerate*/
}
/*
Disable commands here!
Just do function = {};
*/
document.write = {};
document.cookie = {};