Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/entities/player.js
1036 views
1
/*jshint bitwise: false*/
2
/*globals define*/
3
define([
4
'box2d',
5
'entities/physics-entity',
6
'config/colors',
7
'config/material',
8
'config/settings',
9
'utils'
10
], function( Box2D, PhysicsEntity, Colors, Material, Settings, Utils ) {
11
'use strict';
12
13
var PI2 = Utils.PI2;
14
15
var Emotion = {
16
NORMAL: 0,
17
HIT: 1
18
};
19
20
function Player( x, y ) {
21
PhysicsEntity.call( this, {
22
shape: 'circle',
23
radius: 3,
24
fixture: {
25
density: 0.25,
26
friction: 0.5,
27
restitution: 0.2,
28
filter: {
29
categoryBits: Material.MATTER
30
}
31
},
32
body: {
33
position: {
34
x: x,
35
y: y
36
},
37
linearDamping: 2,
38
angularDamping: 0.1,
39
type: 'dynamic'
40
}
41
});
42
43
this.emotion = Emotion.NORMAL;
44
this.emotionTimeout = null;
45
}
46
47
Player.Emotion = Emotion;
48
49
Player.prototype = new PhysicsEntity();
50
Player.prototype.constructor = Player;
51
52
Player.prototype.draw = function( ctx ) {
53
// 0.4 compensates for the difference between the drawn and fixture radii.
54
// The drawn ring is at a radius of 0.35 with a maximum lineWidth of 0.18.
55
// This results in a total relative width of (0.35 + 0.09) * 2 = 0.88.
56
// 0.88 * 3 (physical radius) = 2.64 (draw radius).
57
// Whereas:
58
// 0.88 * (3 + 0.4) = 2.992, which gives some spacing.
59
var radius = this.fixture.GetShape().GetRadius() + 0.4;
60
61
var width = 2 * radius,
62
height = 2 * radius;
63
64
ctx.save();
65
ctx.translate( this.x, this.y );
66
if ( this.angle ) {
67
ctx.rotate( this.angle );
68
}
69
70
// Draw casing.
71
ctx.lineWidth = 0.05 * width;
72
73
// Top left.
74
ctx.beginPath();
75
ctx.arc( 0, 0, 0.22 * width, -Math.PI, -0.5 * Math.PI );
76
ctx.strokeStyle = '#fef';
77
ctx.stroke();
78
79
// Top right.
80
ctx.beginPath();
81
ctx.arc( 0, 0, 0.22 * width, -0.5 * Math.PI, 0 );
82
ctx.strokeStyle = '#cbe';
83
ctx.stroke();
84
85
// Bottom right.
86
ctx.beginPath();
87
ctx.arc( 0, 0, 0.22 * width, 0, 0.5 * Math.PI );
88
ctx.strokeStyle = '#98b';
89
ctx.stroke();
90
91
// Bottom left.
92
ctx.beginPath();
93
ctx.arc( 0, 0, 0.22 * width, 0.5 * Math.PI, Math.PI );
94
ctx.strokeStyle = '#658';
95
ctx.stroke();
96
97
// Draw main body.
98
ctx.beginPath();
99
ctx.arc( 0, 0, 0.2 * width, 0, PI2 );
100
ctx.fillStyle = '#ecf';
101
ctx.fill();
102
103
// Strokes.
104
ctx.lineWidth = 0.02 * width;
105
ctx.strokeStyle = 'rgba(0, 0, 0, 0.5)';
106
ctx.stroke();
107
108
ctx.beginPath();
109
ctx.arc( 0, 0, 0.18 * width, 0, PI2 );
110
ctx.strokeStyle = '#fff';
111
ctx.stroke();
112
113
if ( this.angle ) {
114
ctx.save();
115
ctx.rotate( -this.angle - this.game.camera.angle );
116
}
117
118
this.drawFace( ctx, width, height );
119
120
if ( this.angle ) {
121
ctx.restore();
122
}
123
124
this.drawRing( ctx, width );
125
};
126
127
Player.prototype.drawFace = function( ctx, width, height ) {
128
var faceColor = Colors.Face;
129
130
// Determine direction of movement.
131
var dx = Utils.inverseLerp( Math.abs( this.vx ), 0, 20 ),
132
dy = Utils.inverseLerp( Math.abs( this.vy ), 0, 20 );
133
134
dx = Utils.clamp( dx, 0, 1 );
135
dy = Utils.clamp( dy, 0, 1 );
136
137
dx *= this.vx < 0 ? -1 : 1;
138
dy *= this.vy < 0 ? -1 : 1;
139
140
dx *= 0.05 * width;
141
dy *= 0.05 * width;
142
143
dx += this.game.camera.x;
144
dy += this.game.camera.y;
145
146
var d = this.game.camera.toLocal( dx, dy );
147
dx = d.x;
148
dy = d.y;
149
150
if ( this.emotion === Emotion.NORMAL ) {
151
// Draw eyes.
152
ctx.beginPath();
153
154
ctx.save();
155
ctx.scale( 0.6, 1 );
156
157
// // Draw left eye.
158
ctx.arc( -0.12 * width + dx, dy, 0.07 * width, 0, PI2 );
159
// // Draw right eye.
160
ctx.arc( 0.12 * width + dx, dy, 0.07 * width, 0, PI2 );
161
162
ctx.fillStyle = faceColor;
163
ctx.fill();
164
ctx.restore();
165
} else if ( this.emotion === Emotion.HIT ) {
166
// Draw X.
167
ctx.beginPath();
168
ctx.lineCap = 'round';
169
170
// Smaller eye movements.
171
dx *= 0.5;
172
dy *= 0.5;
173
174
// Left diagonal.
175
ctx.moveTo( -0.08 * width + dx, -0.07 * height + dy );
176
ctx.lineTo( 0.08 * width + dx, 0.07 * height + dy );
177
// Right diagonal.
178
ctx.moveTo( -0.08 * width + dx, 0.07 * height + dy );
179
ctx.lineTo( 0.08 * width + dx, -0.07 * height + dy );
180
181
ctx.lineWidth = 0.04 * width;
182
ctx.strokeStyle = faceColor;
183
ctx.stroke();
184
185
ctx.lineCap = 'butt';
186
}
187
};
188
189
Player.prototype.drawRing = function( ctx, width ) {
190
if ( Settings.glow ) {
191
ctx.globalCompositeOperation = 'lighter';
192
}
193
194
ctx.beginPath();
195
ctx.arc( 0, 0, 0.35 * width, 0, PI2 );
196
197
var glowColor = Colors.Glow[ Material.type( this.material ) ];
198
199
if ( glowColor ) {
200
ctx.lineWidth = ( 0.1 + Math.random() * 0.08 ) * width;
201
ctx.strokeStyle = glowColor;
202
ctx.stroke();
203
}
204
205
ctx.lineWidth = 0.07 * width;
206
ctx.strokeStyle = '#fff';
207
ctx.stroke();
208
209
if ( Settings.glow ) {
210
ctx.globalCompositeOperation = 'source-over';
211
}
212
213
ctx.restore();
214
};
215
216
Player.prototype.toggleMaterial = function() {
217
this.material = this.material ^ ( Material.MATTER | Material.ANTIMATTER );
218
};
219
220
return Player;
221
});
222
223