src/core/WeaponSystem.js
/**
	* Determines the most appropriate weapon to use given the current game state.
	*
	* @return {WeaponSystem} A reference to this weapon system.
	*/
Determines the most appropriate weapon to use given the current game state.
@return {WeaponSystem} A reference to this weapon system.
Determines the most appropriate weapon to use given the current game state . @return { WeaponSystem } A reference to this weapon system .
selectBestWeapon() {
		const owner = this.owner;
		const target = owner.targetSystem.getTarget();
		if ( target ) {
			let highestDesirability = 0;
			let bestWeaponType = WEAPON_TYPES_BLASTER;
			const distanceToTarget = this.owner.position.distanceTo( target.position );
			for ( let i = 0, l = this.weapons.length; i < l; i ++ ) {
				const weapon = this.weapons[ i ];
				let desirability = ( weapon.roundsLeft === 0 ) ? 0 : weapon.getDesirability( distanceToTarget );
				if ( this.currentWeapon !== weapon ) desirability -= CONFIG.BOT.WEAPON.CHANGE_COST;
				if ( desirability > highestDesirability ) {
					highestDesirability = desirability;
					bestWeaponType = weapon.type;
				}
			}
			 select the best weapon
			this.setNextWeapon( bestWeaponType );
		}
		return this;
	}
selectBestWeapon ( ) { const owner = this . owner ; const target = owner . targetSystem . getTarget ( ) ; if ( target ) { let highestDesirability = 0 ; let bestWeaponType = WEAPON_TYPES_BLASTER ; const distanceToTarget = this . owner . position . distanceTo ( target . position ) ; for ( let i = 0 , l = this . weapons . length ; i < l ; i ++ ) { const weapon = this . weapons [ i ] ; let desirability = ( weapon . roundsLeft === 0 ) ? 0 : weapon . getDesirability ( distanceToTarget ) ; if ( this . currentWeapon !== weapon ) desirability -= CONFIG . BOT . WEAPON . CHANGE_COST ; if ( desirability > highestDesirability ) { highestDesirability = desirability ; bestWeaponType = weapon . type ; } } this . setNextWeapon ( bestWeaponType ) ; } return this ; }
Determines the most appropriate weapon to use given the current game state.
Determines the most appropriate weapon to use given the current game state .
// calculate the distance to the target // for each weapon in the inventory calculate its desirability given the // current situation. The most desirable weapon is selected // if weapon is different than currentWeapon, decrease the desirability in order to respect the // cost of changing a weapon // // select the best weapon