This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgame_objects.js
109 lines (85 loc) · 1.87 KB
/
game_objects.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
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
/** @constructor */
function Line(x1, y1, x2, y2)
{
this.p1 = { x: x1, y: y1 };
this.p2 = { x: x2, y: y2 };
// it has to include the last point, so + 1
this.width = Math.abs(this.p1.x - this.p2.x) + 1,
this.height = Math.abs(this.p1.y - this.p2.y) + 1;
this.bitmap = null;
//console.log(this.getPixels());
}
Line.prototype.getBitmap = function()
{
if(this.bitmap)
{
return this.bitmap;
}
var
// may be infinity, be careful with it
m = (this.height - 1) / (this.width - 1);
this.bitmap = new Bitmap(this.width, this.height);
// current strategy:
// if slope is > 1, walk from y1 to y2,
// otherwise, walk from x1 to x2
if(m > 1)
{
for(var i = 0; i < this.height; i++)
{
this.bitmap.set(
Math.round(i / m),
i,
1
);
}
}
else
{
for(var i = 0; i < this.width; i++)
{
this.bitmap.set(
i,
Math.round(i * m),
1
);
}
}
return this.bitmap;
};
/** @constructor */
function Rectangle(width, height)
{
this.width = width;
this.height = height;
this.bitmap = null;
}
Rectangle.prototype.getBitmap = function()
{
if(this.bitmap)
{
return this.bitmap;
}
this.bitmap = new Bitmap(this.width, this.height);
for(var i = 0; i < this.bitmap.count; i++)
{
this.bitmap.data[i] = 1;
}
return this.bitmap;
};
/** @constructor */
function AutoShape(image)
{
this.width = image.width;
this.height = image.height;
this.image = image;
this.bitmap = null;
}
AutoShape.prototype.getBitmap = function()
{
if(!this.bitmap)
{
this.bitmap = Bitmap.fromImage(this.image);
}
return this.bitmap;
};