Path: blob/main/static/src/gs/public/radius-raid/js/enemy.js
1324 views
/*==============================================================================1Init2==============================================================================*/3$.Enemy = function( opt ) {4// set always and optional5for( var k in opt ) {6this[k] = opt[k];7}89// set optional and defaults10this.lightness = $.util.isset( this.lightness ) ? this.lightness : 50;11this.saturation = $.util.isset( this.saturation ) ? this.saturation : 100;12this.setup = this.setup || function(){};13this.death = this.death || function(){};1415// set same for all objects16this.index = $.indexGlobal++;17this.inView = this.hitFlag = this.vx = this.vy = 0;18this.lifeMax = opt.life;19this.fillStyle ='hsla(' + this.hue + ', ' + this.saturation + '%, ' + this.lightness + '%, 0.1)';20this.strokeStyle = 'hsla(' + this.hue + ', ' + this.saturation + '%, ' + this.lightness + '%, 1)';21/*==============================================================================22Run Setup23==============================================================================*/24this.setup();2526/*==============================================================================27Adjust Level Offset Difficulties28==============================================================================*/29if( $.levelDiffOffset > 0 ){30this.life += $.levelDiffOffset * 0.25;31this.lifeMax = this.life;32this.speed += Math.min( $.hero.vmax, $.levelDiffOffset * 0.25 );33this.value += $.levelDiffOffset * 5;34}35};3637/*==============================================================================38Update39==============================================================================*/40$.Enemy.prototype.update = function( i ) {41/*==============================================================================42Apply Behavior43==============================================================================*/44this.behavior();4546/*==============================================================================47Apply Forces48==============================================================================*/49this.x += this.vx * $.dt;50this.y += this.vy * $.dt;5152/*==============================================================================53Lock Bounds54==============================================================================*/55if( this.lockBounds && !$.util.arcInRect( this.x, this.y, this.radius + 10, 0, 0, $.ww, $.wh ) ) {56$.enemies.splice( i, 1 );57}5859/*==============================================================================60Update View61==============================================================================*/62if( $.util.arcInRect( this.x, this.y, this.radius, -$.screen.x, -$.screen.y, $.cw, $.ch ) ) {63this.inView = 1;64} else {65this.inView = 0;66}67};6869/*==============================================================================70Receive Damage71==============================================================================*/72$.Enemy.prototype.receiveDamage = function( i, val ) {73if( this.inView ) {74$.audio.play( 'hit' );75}76this.life -= val;77this.hitFlag = 10;78if( this.life <= 0 ) {79if( this.inView ) {80$.explosions.push( new $.Explosion( {81x: this.x,82y: this.y,83radius: this.radius,84hue: this.hue,85saturation: this.saturation86} ) );87$.particleEmitters.push( new $.ParticleEmitter( {88x: this.x,89y: this.y,90count: 10,91spawnRange: this.radius,92friction: 0.85,93minSpeed: 5,94maxSpeed: 20,95minDirection: 0,96maxDirection: $.twopi,97hue: this.hue,98saturation: this.saturation99} ) );100$.textPops.push( new $.TextPop( {101x: this.x,102y: this.y,103value: this.value,104hue: this.hue,105saturation: this.saturation,106lightness: 60107} ) );108$.rumble.level = 6;109}110this.death();111$.spawnPowerup( this.x, this.y );112$.score += this.value;113$.level.kills++;114$.kills++;115$.enemies.splice( i, 1 );116}117};118119/*==============================================================================120Render Health121==============================================================================*/122$.Enemy.prototype.renderHealth = function( i ) {123if( this.inView && this.life > 0 && this.life < this.lifeMax ) {124$.ctxmg.fillStyle = 'hsla(0, 0%, 0%, 0.75)';125$.ctxmg.fillRect( this.x - this.radius, this.y - this.radius - 6, this.radius * 2, 3 );126$.ctxmg.fillStyle = 'hsla(' + ( this.life / this.lifeMax ) * 120 + ', 100%, 50%, 0.75)';127$.ctxmg.fillRect( this.x - this.radius, this.y - this.radius - 6, ( this.radius * 2 ) * ( this.life / this.lifeMax ), 3 );128}129};130131/*==============================================================================132Render133==============================================================================*/134$.Enemy.prototype.render = function( i ) {135if( this.inView ) {136var mod = $.enemyOffsetMod / 6;137$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, this.fillStyle );138$.util.strokeCircle( $.ctxmg, this.x, this.y, this.radius / 4 + Math.cos( mod ) * this.radius / 4, this.strokeStyle, 1.5 );139$.util.strokeCircle( $.ctxmg, this.x, this.y, this.radius - 0.5, this.strokeStyle, 1 );140141$.ctxmg.strokeStyle = this.strokeStyle;142$.ctxmg.lineWidth = 4;143$.ctxmg.beginPath();144$.ctxmg.arc( this.x, this.y, this.radius - 0.5, mod + $.pi, mod + $.pi + $.pi / 2 );145$.ctxmg.stroke();146$.ctxmg.beginPath();147$.ctxmg.arc( this.x, this.y, this.radius - 0.5, mod, mod + $.pi / 2 );148$.ctxmg.stroke();149150if( $.slow) {151$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, 'hsla(' + $.util.rand( 160, 220 ) + ', 100%, 50%, 0.25)' );152}153if( this.hitFlag > 0 ) {154this.hitFlag -= $.dt;155$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius, 'hsla(' + this.hue + ', ' + this.saturation + '%, 75%, ' + this.hitFlag / 10 + ')' );156$.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) );157}158this.renderHealth();159}160};161162