Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/object2d.js
1036 views
1
/*globals define*/
2
define([
3
'base-object',
4
'color'
5
], function( BaseObject, Color ) {
6
'use strict';
7
8
function Object2D( x, y ) {
9
BaseObject.call( this );
10
11
this.x = x || 0;
12
this.y = y || 0;
13
14
this.angle = 0;
15
16
this.fill = new Color();
17
this.stroke = new Color();
18
19
this.lineWidth = 0;
20
}
21
22
Object2D.prototype = new BaseObject();
23
Object2D.prototype.constructor = Object2D;
24
25
Object2D.prototype.update = function() {};
26
Object2D.prototype.drawPath = function() {};
27
28
Object2D.prototype.draw = function( ctx ) {
29
ctx.save();
30
31
ctx.translate( this.x, this.y );
32
ctx.rotate( -this.angle );
33
34
this.drawPath( ctx );
35
36
ctx.restore();
37
38
if ( this.fill.alpha ) {
39
ctx.fillStyle = this.fill.rgba();
40
ctx.fill();
41
}
42
43
if ( this.lineWidth && this.stroke.alpha ) {
44
ctx.lineWidth = this.lineWidth;
45
ctx.strokeStyle = this.stroke.rgba();
46
ctx.stroke();
47
}
48
};
49
50
Object2D.prototype.toWorld = function( x, y ) {
51
var cos, sin;
52
var rx, ry;
53
54
if ( this.angle ) {
55
cos = Math.cos( -this.angle );
56
sin = Math.sin( -this.angle );
57
58
rx = cos * x - sin * y;
59
ry = sin * x + cos * y;
60
61
x = rx;
62
y = ry;
63
}
64
65
return {
66
x: x + this.x,
67
y: y + this.y
68
};
69
};
70
71
Object2D.prototype.toLocal = function( x, y ) {
72
x -= this.x;
73
y -= this.y;
74
75
var cos, sin;
76
var rx, ry;
77
78
if ( this.angle ) {
79
cos = Math.cos( this.angle );
80
sin = Math.sin( this.angle );
81
82
rx = cos * x - sin * y;
83
ry = sin * x + cos * y;
84
85
x = rx;
86
y = ry;
87
}
88
89
return {
90
x: x,
91
y: y
92
};
93
};
94
95
Object2D.prototype.aabb = function() {
96
return {
97
xmin: this.x,
98
ymin: this.y,
99
xmax: this.x,
100
ymax: this.y
101
};
102
};
103
104
return Object2D;
105
});
106
107