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/bullet.js
1330 views
1
/*==============================================================================
2
Init
3
==============================================================================*/
4
$.Bullet = function( opt ) {
5
for( var k in opt ) {
6
this[k] = opt[k];
7
}
8
this.enemiesHit = [];
9
this.inView = 0;
10
$.particleEmitters.push( new $.ParticleEmitter( {
11
x: this.x,
12
y: this.y,
13
count: 1,
14
spawnRange: 1,
15
friction: 0.75,
16
minSpeed: 2,
17
maxSpeed: 10,
18
minDirection: 0,
19
maxDirection: $.twopi,
20
hue: 0,
21
saturation: 0
22
} ) );
23
};
24
25
/*==============================================================================
26
Update
27
==============================================================================*/
28
$.Bullet.prototype.update = function( i ) {
29
/*==============================================================================
30
Apply Forces
31
==============================================================================*/
32
this.x += Math.cos( this.direction ) * ( this.speed * $.dt );
33
this.y += Math.sin( this.direction ) * ( this.speed * $.dt );
34
this.ex = this.x - Math.cos( this.direction ) * this.size;
35
this.ey = this.y - Math.sin( this.direction ) * this.size;
36
37
/*==============================================================================
38
Check Collisions
39
==============================================================================*/
40
var ei = $.enemies.length;
41
while( ei-- ) {
42
var enemy = $.enemies[ ei ];
43
if( $.util.distance( this.x, this.y, enemy.x, enemy.y ) <= enemy.radius ) {
44
if( this.enemiesHit.indexOf( enemy.index ) == -1 ){
45
$.particleEmitters.push( new $.ParticleEmitter( {
46
x: this.x,
47
y: this.y,
48
count: Math.floor( $.util.rand( 1, 4 ) ),
49
spawnRange: 0,
50
friction: 0.85,
51
minSpeed: 5,
52
maxSpeed: 12,
53
minDirection: ( this.direction - $.pi ) - $.pi / 5,
54
maxDirection: ( this.direction - $.pi ) + $.pi / 5,
55
hue: enemy.hue
56
} ) );
57
58
this.enemiesHit.push( enemy.index );
59
enemy.receiveDamage( ei, this.damage );
60
61
if( this.enemiesHit.length > 3 ) {
62
$.bullets.splice( i, 1 );
63
}
64
}
65
if( !this.piercing ) {
66
$.bullets.splice( i, 1 );
67
}
68
}
69
}
70
71
/*==============================================================================
72
Lock Bounds
73
==============================================================================*/
74
if( !$.util.pointInRect( this.ex, this.ey, 0, 0, $.ww, $.wh ) ) {
75
$.bullets.splice( i, 1 );
76
}
77
78
/*==============================================================================
79
Update View
80
==============================================================================*/
81
if( $.util.pointInRect( this.ex, this.ey, -$.screen.x, -$.screen.y, $.cw, $.ch ) ) {
82
this.inView = 1;
83
} else {
84
this.inView = 0;
85
}
86
};
87
88
/*==============================================================================
89
Render
90
==============================================================================*/
91
$.Bullet.prototype.render = function( i ) {
92
if( this.inView ) {
93
$.ctxmg.beginPath();
94
$.ctxmg.moveTo( this.x, this.y );
95
$.ctxmg.lineTo( this.ex, this.ey );
96
$.ctxmg.lineWidth = this.lineWidth;
97
$.ctxmg.strokeStyle = this.strokeStyle;
98
$.ctxmg.stroke();
99
}
100
};
101