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/enemy.js
1324 views
1
/*==============================================================================
2
Init
3
==============================================================================*/
4
$.Enemy = function( opt ) {
5
// set always and optional
6
for( var k in opt ) {
7
this[k] = opt[k];
8
}
9
10
// set optional and defaults
11
this.lightness = $.util.isset( this.lightness ) ? this.lightness : 50;
12
this.saturation = $.util.isset( this.saturation ) ? this.saturation : 100;
13
this.setup = this.setup || function(){};
14
this.death = this.death || function(){};
15
16
// set same for all objects
17
this.index = $.indexGlobal++;
18
this.inView = this.hitFlag = this.vx = this.vy = 0;
19
this.lifeMax = opt.life;
20
this.fillStyle ='hsla(' + this.hue + ', ' + this.saturation + '%, ' + this.lightness + '%, 0.1)';
21
this.strokeStyle = 'hsla(' + this.hue + ', ' + this.saturation + '%, ' + this.lightness + '%, 1)';
22
/*==============================================================================
23
Run Setup
24
==============================================================================*/
25
this.setup();
26
27
/*==============================================================================
28
Adjust Level Offset Difficulties
29
==============================================================================*/
30
if( $.levelDiffOffset > 0 ){
31
this.life += $.levelDiffOffset * 0.25;
32
this.lifeMax = this.life;
33
this.speed += Math.min( $.hero.vmax, $.levelDiffOffset * 0.25 );
34
this.value += $.levelDiffOffset * 5;
35
}
36
};
37
38
/*==============================================================================
39
Update
40
==============================================================================*/
41
$.Enemy.prototype.update = function( i ) {
42
/*==============================================================================
43
Apply Behavior
44
==============================================================================*/
45
this.behavior();
46
47
/*==============================================================================
48
Apply Forces
49
==============================================================================*/
50
this.x += this.vx * $.dt;
51
this.y += this.vy * $.dt;
52
53
/*==============================================================================
54
Lock Bounds
55
==============================================================================*/
56
if( this.lockBounds && !$.util.arcInRect( this.x, this.y, this.radius + 10, 0, 0, $.ww, $.wh ) ) {
57
$.enemies.splice( i, 1 );
58
}
59
60
/*==============================================================================
61
Update View
62
==============================================================================*/
63
if( $.util.arcInRect( this.x, this.y, this.radius, -$.screen.x, -$.screen.y, $.cw, $.ch ) ) {
64
this.inView = 1;
65
} else {
66
this.inView = 0;
67
}
68
};
69
70
/*==============================================================================
71
Receive Damage
72
==============================================================================*/
73
$.Enemy.prototype.receiveDamage = function( i, val ) {
74
if( this.inView ) {
75
$.audio.play( 'hit' );
76
}
77
this.life -= val;
78
this.hitFlag = 10;
79
if( this.life <= 0 ) {
80
if( this.inView ) {
81
$.explosions.push( new $.Explosion( {
82
x: this.x,
83
y: this.y,
84
radius: this.radius,
85
hue: this.hue,
86
saturation: this.saturation
87
} ) );
88
$.particleEmitters.push( new $.ParticleEmitter( {
89
x: this.x,
90
y: this.y,
91
count: 10,
92
spawnRange: this.radius,
93
friction: 0.85,
94
minSpeed: 5,
95
maxSpeed: 20,
96
minDirection: 0,
97
maxDirection: $.twopi,
98
hue: this.hue,
99
saturation: this.saturation
100
} ) );
101
$.textPops.push( new $.TextPop( {
102
x: this.x,
103
y: this.y,
104
value: this.value,
105
hue: this.hue,
106
saturation: this.saturation,
107
lightness: 60
108
} ) );
109
$.rumble.level = 6;
110
}
111
this.death();
112
$.spawnPowerup( this.x, this.y );
113
$.score += this.value;
114
$.level.kills++;
115
$.kills++;
116
$.enemies.splice( i, 1 );
117
}
118
};
119
120
/*==============================================================================
121
Render Health
122
==============================================================================*/
123
$.Enemy.prototype.renderHealth = function( i ) {
124
if( this.inView && this.life > 0 && this.life < this.lifeMax ) {
125
$.ctxmg.fillStyle = 'hsla(0, 0%, 0%, 0.75)';
126
$.ctxmg.fillRect( this.x - this.radius, this.y - this.radius - 6, this.radius * 2, 3 );
127
$.ctxmg.fillStyle = 'hsla(' + ( this.life / this.lifeMax ) * 120 + ', 100%, 50%, 0.75)';
128
$.ctxmg.fillRect( this.x - this.radius, this.y - this.radius - 6, ( this.radius * 2 ) * ( this.life / this.lifeMax ), 3 );
129
}
130
};
131
132
/*==============================================================================
133
Render
134
==============================================================================*/
135
$.Enemy.prototype.render = function( i ) {
136
if( this.inView ) {
137
var mod = $.enemyOffsetMod / 6;
138
$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, this.fillStyle );
139
$.util.strokeCircle( $.ctxmg, this.x, this.y, this.radius / 4 + Math.cos( mod ) * this.radius / 4, this.strokeStyle, 1.5 );
140
$.util.strokeCircle( $.ctxmg, this.x, this.y, this.radius - 0.5, this.strokeStyle, 1 );
141
142
$.ctxmg.strokeStyle = this.strokeStyle;
143
$.ctxmg.lineWidth = 4;
144
$.ctxmg.beginPath();
145
$.ctxmg.arc( this.x, this.y, this.radius - 0.5, mod + $.pi, mod + $.pi + $.pi / 2 );
146
$.ctxmg.stroke();
147
$.ctxmg.beginPath();
148
$.ctxmg.arc( this.x, this.y, this.radius - 0.5, mod, mod + $.pi / 2 );
149
$.ctxmg.stroke();
150
151
if( $.slow) {
152
$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, 'hsla(' + $.util.rand( 160, 220 ) + ', 100%, 50%, 0.25)' );
153
}
154
if( this.hitFlag > 0 ) {
155
this.hitFlag -= $.dt;
156
$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, 'hsla(' + this.hue + ', ' + this.saturation + '%, 75%, ' + this.hitFlag / 10 + ')' );
157
$.util.strokeCircle( $.ctxmg, this.x, this.y, this.radius, 'hsla(' + this.hue + ', ' + this.saturation + '%, ' + $.util.rand( 60, 90) + '%, ' + this.hitFlag / 10 + ')', $.util.rand( 1, 10) );
158
}
159
this.renderHealth();
160
}
161
};
162