Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Incognito-old
Path: blob/main/static/src/gs/public/radius-raid/js/hero.js
1325 views
1
/*==============================================================================
2
Init
3
==============================================================================*/
4
$.Hero = function() {
5
this.x = $.ww / 2;
6
this.y = $.wh / 2;
7
this.vx = 0;
8
this.vy = 0;
9
this.vmax = 4;
10
this.vmax = 6;
11
this.direction = 0;
12
this.accel = 0.5;
13
this.radius = 10;
14
this.life = 1;
15
this.takingDamage = 0;
16
this.fillStyle = '#fff';
17
this.weapon = {
18
fireRate: 5,
19
fireRateTick: 5,
20
spread: 0.3,
21
count: 1,
22
bullet: {
23
size: 15,
24
lineWidth: 2,
25
damage: 1,
26
speed: 10,
27
piercing: 0,
28
strokeStyle: '#fff'
29
},
30
fireFlag: 0
31
};
32
};
33
34
/*==============================================================================
35
Update
36
==============================================================================*/
37
$.Hero.prototype.update = function() {
38
if( this.life > 0 ) {
39
/*==============================================================================
40
Apply Forces
41
==============================================================================*/
42
if( $.keys.state.up ) {
43
this.vy -= this.accel * $.dt;
44
if( this.vy < -this.vmax ) {
45
this.vy = -this.vmax;
46
}
47
} else if( $.keys.state.down ) {
48
this.vy += this.accel * $.dt;
49
if( this.vy > this.vmax ) {
50
this.vy = this.vmax;
51
}
52
}
53
if( $.keys.state.left ) {
54
this.vx -= this.accel * $.dt;
55
if( this.vx < -this.vmax ) {
56
this.vx = -this.vmax;
57
}
58
} else if( $.keys.state.right ) {
59
this.vx += this.accel * $.dt;
60
if( this.vx > this.vmax ) {
61
this.vx = this.vmax;
62
}
63
}
64
65
this.vy *= 0.9;
66
this.vx *= 0.9;
67
68
this.x += this.vx * $.dt;
69
this.y += this.vy * $.dt;
70
71
/*==============================================================================
72
Lock Bounds
73
==============================================================================*/
74
if( this.x >= $.ww - this.radius ) {
75
this.x = $.ww - this.radius;
76
}
77
if( this.x <= this.radius ) {
78
this.x = this.radius;
79
}
80
if( this.y >= $.wh - this.radius ) {
81
this.y = $.wh - this.radius;
82
}
83
if( this.y <= this.radius ) {
84
this.y = this.radius;
85
}
86
87
/*==============================================================================
88
Update Direction
89
==============================================================================*/
90
var dx = $.mouse.x - this.x,
91
dy = $.mouse.y - this.y;
92
this.direction = Math.atan2( dy, dx );
93
94
/*==============================================================================
95
Fire Weapon
96
==============================================================================*/
97
if( this.weapon.fireRateTick < this.weapon.fireRate ){
98
this.weapon.fireRateTick += $.dt;
99
} else {
100
if( $.autofire || ( !$.autofire && $.mouse.down ) ){
101
$.audio.play( 'shoot' );
102
if( $.powerupTimers[ 2 ] > 0 || $.powerupTimers[ 3 ] > 0 || $.powerupTimers[ 4 ] > 0) {
103
$.audio.play( 'shootAlt' );
104
}
105
106
this.weapon.fireRateTick = this.weapon.fireRateTick - this.weapon.fireRate;
107
this.weapon.fireFlag = 6;
108
109
if( this.weapon.count > 1 ) {
110
var spreadStart = -this.weapon.spread / 2;
111
var spreadStep = this.weapon.spread / ( this.weapon.count - 1 );
112
} else {
113
var spreadStart = 0;
114
var spreadStep = 0;
115
}
116
117
var gunX = this.x + Math.cos( this.direction ) * ( this.radius + this.weapon.bullet.size );
118
var gunY = this.y + Math.sin( this.direction ) * ( this.radius + this.weapon.bullet.size );
119
120
for( var i = 0; i < this.weapon.count; i++ ) {
121
$.bulletsFired++;
122
var color = this.weapon.bullet.strokeStyle;
123
if( $.powerupTimers[ 2 ] > 0 || $.powerupTimers[ 3 ] > 0 || $.powerupTimers[ 4 ] > 0) {
124
var colors = [];
125
if( $.powerupTimers[ 2 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 2 ].hue + ', ' + $.definitions.powerups[ 2 ].saturation + '%, ' + $.definitions.powerups[ 2 ].lightness + '%)' ); }
126
if( $.powerupTimers[ 3 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 3 ].hue + ', ' + $.definitions.powerups[ 3 ].saturation + '%, ' + $.definitions.powerups[ 3 ].lightness + '%)' ); }
127
if( $.powerupTimers[ 4 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 4 ].hue + ', ' + $.definitions.powerups[ 4 ].saturation + '%, ' + $.definitions.powerups[ 4 ].lightness + '%)' ); }
128
color = colors[ Math.floor( $.util.rand( 0, colors.length ) ) ];
129
}
130
$.bullets.push( new $.Bullet( {
131
x: gunX,
132
y: gunY,
133
speed: this.weapon.bullet.speed,
134
direction: this.direction + spreadStart + i * spreadStep,
135
damage: this.weapon.bullet.damage,
136
size: this.weapon.bullet.size,
137
lineWidth: this.weapon.bullet.lineWidth,
138
strokeStyle: color,
139
piercing: this.weapon.bullet.piercing
140
} ) );
141
}
142
}
143
}
144
145
/*==============================================================================
146
Check Collisions
147
==============================================================================*/
148
this.takingDamage = 0;
149
var ei = $.enemies.length;
150
while( ei-- ) {
151
var enemy = $.enemies[ ei ];
152
if( enemy.inView && $.util.distance( this.x, this.y, enemy.x, enemy.y ) <= this.radius + enemy.radius ) {
153
$.particleEmitters.push( new $.ParticleEmitter( {
154
x: this.x,
155
y: this.y,
156
count: 2,
157
spawnRange: 0,
158
friction: 0.85,
159
minSpeed: 2,
160
maxSpeed: 15,
161
minDirection: 0,
162
maxDirection: $.twopi,
163
hue: 0,
164
saturation: 0
165
} ) );
166
this.takingDamage = 1;
167
this.life -= 0.0075;
168
$.rumble.level = 3;
169
if( Math.floor( $.tick ) % 5 == 0 ){
170
$.audio.play( 'takingDamage' );
171
}
172
}
173
}
174
}
175
};
176
177
/*==============================================================================
178
Render
179
==============================================================================*/
180
$.Hero.prototype.render = function() {
181
if( this.life > 0 ) {
182
if( this.takingDamage ) {
183
var fillStyle = 'hsla(0, 0%, ' + $.util.rand( 0, 100 ) + '%, 1)';
184
$.ctxmg.fillStyle = 'hsla(0, 0%, ' + $.util.rand( 0, 100 ) + '%, ' + $.util.rand( 0.01, 0.15 ) + ')';
185
$.ctxmg.fillRect( -$.screen.x, -$.screen.y, $.cw, $.ch );
186
} else if( this.weapon.fireFlag > 0 ) {
187
this.weapon.fireFlag -= $.dt;
188
var fillStyle = 'hsla(' + $.util.rand( 0, 359 ) + ', 100%, ' + $.util.rand( 20, 80 ) + '%, 1)';
189
} else {
190
var fillStyle = this.fillStyle;
191
}
192
193
$.ctxmg.save();
194
$.ctxmg.translate( this.x, this.y );
195
$.ctxmg.rotate( this.direction - $.pi / 4 );
196
$.ctxmg.fillStyle = fillStyle;
197
$.ctxmg.fillRect( 0, 0, this.radius, this.radius );
198
$.ctxmg.restore();
199
200
$.ctxmg.save();
201
$.ctxmg.translate( this.x, this.y );
202
$.ctxmg.rotate( this.direction - $.pi / 4 + $.twopi / 3 );
203
$.ctxmg.fillStyle = fillStyle;
204
$.ctxmg.fillRect( 0, 0, this.radius, this.radius );
205
$.ctxmg.restore();
206
207
$.ctxmg.save();
208
$.ctxmg.translate( this.x, this.y );
209
$.ctxmg.rotate( this.direction - $.pi / 4 - $.twopi / 3 );
210
$.ctxmg.fillStyle = fillStyle;
211
$.ctxmg.fillRect( 0, 0, this.radius, this.radius );
212
$.ctxmg.restore();
213
214
$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius - 3, fillStyle );
215
}
216
};
217