Path: blob/main/static/src/gs/public/radius-raid/js/hero.js
1325 views
/*==============================================================================1Init2==============================================================================*/3$.Hero = function() {4this.x = $.ww / 2;5this.y = $.wh / 2;6this.vx = 0;7this.vy = 0;8this.vmax = 4;9this.vmax = 6;10this.direction = 0;11this.accel = 0.5;12this.radius = 10;13this.life = 1;14this.takingDamage = 0;15this.fillStyle = '#fff';16this.weapon = {17fireRate: 5,18fireRateTick: 5,19spread: 0.3,20count: 1,21bullet: {22size: 15,23lineWidth: 2,24damage: 1,25speed: 10,26piercing: 0,27strokeStyle: '#fff'28},29fireFlag: 030};31};3233/*==============================================================================34Update35==============================================================================*/36$.Hero.prototype.update = function() {37if( this.life > 0 ) {38/*==============================================================================39Apply Forces40==============================================================================*/41if( $.keys.state.up ) {42this.vy -= this.accel * $.dt;43if( this.vy < -this.vmax ) {44this.vy = -this.vmax;45}46} else if( $.keys.state.down ) {47this.vy += this.accel * $.dt;48if( this.vy > this.vmax ) {49this.vy = this.vmax;50}51}52if( $.keys.state.left ) {53this.vx -= this.accel * $.dt;54if( this.vx < -this.vmax ) {55this.vx = -this.vmax;56}57} else if( $.keys.state.right ) {58this.vx += this.accel * $.dt;59if( this.vx > this.vmax ) {60this.vx = this.vmax;61}62}6364this.vy *= 0.9;65this.vx *= 0.9;6667this.x += this.vx * $.dt;68this.y += this.vy * $.dt;6970/*==============================================================================71Lock Bounds72==============================================================================*/73if( this.x >= $.ww - this.radius ) {74this.x = $.ww - this.radius;75}76if( this.x <= this.radius ) {77this.x = this.radius;78}79if( this.y >= $.wh - this.radius ) {80this.y = $.wh - this.radius;81}82if( this.y <= this.radius ) {83this.y = this.radius;84}8586/*==============================================================================87Update Direction88==============================================================================*/89var dx = $.mouse.x - this.x,90dy = $.mouse.y - this.y;91this.direction = Math.atan2( dy, dx );9293/*==============================================================================94Fire Weapon95==============================================================================*/96if( this.weapon.fireRateTick < this.weapon.fireRate ){97this.weapon.fireRateTick += $.dt;98} else {99if( $.autofire || ( !$.autofire && $.mouse.down ) ){100$.audio.play( 'shoot' );101if( $.powerupTimers[ 2 ] > 0 || $.powerupTimers[ 3 ] > 0 || $.powerupTimers[ 4 ] > 0) {102$.audio.play( 'shootAlt' );103}104105this.weapon.fireRateTick = this.weapon.fireRateTick - this.weapon.fireRate;106this.weapon.fireFlag = 6;107108if( this.weapon.count > 1 ) {109var spreadStart = -this.weapon.spread / 2;110var spreadStep = this.weapon.spread / ( this.weapon.count - 1 );111} else {112var spreadStart = 0;113var spreadStep = 0;114}115116var gunX = this.x + Math.cos( this.direction ) * ( this.radius + this.weapon.bullet.size );117var gunY = this.y + Math.sin( this.direction ) * ( this.radius + this.weapon.bullet.size );118119for( var i = 0; i < this.weapon.count; i++ ) {120$.bulletsFired++;121var color = this.weapon.bullet.strokeStyle;122if( $.powerupTimers[ 2 ] > 0 || $.powerupTimers[ 3 ] > 0 || $.powerupTimers[ 4 ] > 0) {123var colors = [];124if( $.powerupTimers[ 2 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 2 ].hue + ', ' + $.definitions.powerups[ 2 ].saturation + '%, ' + $.definitions.powerups[ 2 ].lightness + '%)' ); }125if( $.powerupTimers[ 3 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 3 ].hue + ', ' + $.definitions.powerups[ 3 ].saturation + '%, ' + $.definitions.powerups[ 3 ].lightness + '%)' ); }126if( $.powerupTimers[ 4 ] > 0 ) { colors.push( 'hsl(' + $.definitions.powerups[ 4 ].hue + ', ' + $.definitions.powerups[ 4 ].saturation + '%, ' + $.definitions.powerups[ 4 ].lightness + '%)' ); }127color = colors[ Math.floor( $.util.rand( 0, colors.length ) ) ];128}129$.bullets.push( new $.Bullet( {130x: gunX,131y: gunY,132speed: this.weapon.bullet.speed,133direction: this.direction + spreadStart + i * spreadStep,134damage: this.weapon.bullet.damage,135size: this.weapon.bullet.size,136lineWidth: this.weapon.bullet.lineWidth,137strokeStyle: color,138piercing: this.weapon.bullet.piercing139} ) );140}141}142}143144/*==============================================================================145Check Collisions146==============================================================================*/147this.takingDamage = 0;148var ei = $.enemies.length;149while( ei-- ) {150var enemy = $.enemies[ ei ];151if( enemy.inView && $.util.distance( this.x, this.y, enemy.x, enemy.y ) <= this.radius + enemy.radius ) {152$.particleEmitters.push( new $.ParticleEmitter( {153x: this.x,154y: this.y,155count: 2,156spawnRange: 0,157friction: 0.85,158minSpeed: 2,159maxSpeed: 15,160minDirection: 0,161maxDirection: $.twopi,162hue: 0,163saturation: 0164} ) );165this.takingDamage = 1;166this.life -= 0.0075;167$.rumble.level = 3;168if( Math.floor( $.tick ) % 5 == 0 ){169$.audio.play( 'takingDamage' );170}171}172}173}174};175176/*==============================================================================177Render178==============================================================================*/179$.Hero.prototype.render = function() {180if( this.life > 0 ) {181if( this.takingDamage ) {182var fillStyle = 'hsla(0, 0%, ' + $.util.rand( 0, 100 ) + '%, 1)';183$.ctxmg.fillStyle = 'hsla(0, 0%, ' + $.util.rand( 0, 100 ) + '%, ' + $.util.rand( 0.01, 0.15 ) + ')';184$.ctxmg.fillRect( -$.screen.x, -$.screen.y, $.cw, $.ch );185} else if( this.weapon.fireFlag > 0 ) {186this.weapon.fireFlag -= $.dt;187var fillStyle = 'hsla(' + $.util.rand( 0, 359 ) + ', 100%, ' + $.util.rand( 20, 80 ) + '%, 1)';188} else {189var fillStyle = this.fillStyle;190}191192$.ctxmg.save();193$.ctxmg.translate( this.x, this.y );194$.ctxmg.rotate( this.direction - $.pi / 4 );195$.ctxmg.fillStyle = fillStyle;196$.ctxmg.fillRect( 0, 0, this.radius, this.radius );197$.ctxmg.restore();198199$.ctxmg.save();200$.ctxmg.translate( this.x, this.y );201$.ctxmg.rotate( this.direction - $.pi / 4 + $.twopi / 3 );202$.ctxmg.fillStyle = fillStyle;203$.ctxmg.fillRect( 0, 0, this.radius, this.radius );204$.ctxmg.restore();205206$.ctxmg.save();207$.ctxmg.translate( this.x, this.y );208$.ctxmg.rotate( this.direction - $.pi / 4 - $.twopi / 3 );209$.ctxmg.fillStyle = fillStyle;210$.ctxmg.fillRect( 0, 0, this.radius, this.radius );211$.ctxmg.restore();212213$.util.fillCircle( $.ctxmg, this.x, this.y, this.radius - 3, fillStyle );214}215};216217