Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/helpers/Sound.js
/** | /** | |||||||||||
* Simple wrapper function for playing sounds that are associated with entities | * Simple wrapper function for playing sounds that are associated with entities | |||||||||||
* @param name Typically one of 'walk', 'run', 'attack', 'death', 'build', | * @param name Typically one of 'walk', 'run', 'attack', 'death', 'build', | |||||||||||
* 'gather_fruit', 'gather_grain', 'gather_wood', 'gather_stone', 'gather_metal' | * 'gather_fruit', 'gather_grain', 'gather_wood', 'gather_stone', 'gather_metal' | |||||||||||
*/ | */ | |||||||||||
function PlaySound(name, ent) | function PlaySound(name, ent) | |||||||||||
{ | { | |||||||||||
var cmpSound = Engine.QueryInterface(ent, IID_Sound); | var cmpSound = Engine.QueryInterface(ent, IID_Sound); | |||||||||||
if (cmpSound) | if (cmpSound) | |||||||||||
cmpSound.PlaySoundGroup(name); | cmpSound.PlaySoundGroup(name); | |||||||||||
} | } | |||||||||||
/** | ||||||||||||
* Gets the matching impact sound group depending on the . | ||||||||||||
* @param {string} type - The type of damage. | ||||||||||||
* @param {number} target - The entity id of the target. | ||||||||||||
* @param {number} attacker - The entity id of the attacker. | ||||||||||||
* @return {string} - The impact sound group. | ||||||||||||
*/ | ||||||||||||
function PlayEntityHitSoundGroup(type, target, blocked) | ||||||||||||
{ | ||||||||||||
let cmpSound = Engine.QueryInterface(target, IID_Sound); | ||||||||||||
if (!cmpSound) | ||||||||||||
return; | ||||||||||||
let attackImpactType = "hit_" + type.toLowerCase(); | ||||||||||||
if (blocked) | ||||||||||||
{ | ||||||||||||
cmpSound.PlaySoundGroup(attackImpactType + "_blocked" || attackImpactType); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
let cmpIdentity = Engine.QueryInterface(target, IID_Identity); | ||||||||||||
Silier: if we missed and group does not exist, you end up calling identity and returning material hit… | ||||||||||||
if (!cmpIdentity) | ||||||||||||
{ | ||||||||||||
cmpSound.PlaySoundGroup(attackImpactType); | ||||||||||||
Done Inline Actionsnegate + early return, less nesting is easier to read Silier: negate + early return, less nesting is easier to read | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
let material = cmpIdentity.GetMaterial(); | ||||||||||||
if (!material) | ||||||||||||
{ | ||||||||||||
Done Inline Actions
Silier: | ||||||||||||
cmpSound.PlaySoundGroup(attackImpactType); | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
cmpSound.PlaySoundGroup(attackImpactType + "_" + material || attackImpactType); | ||||||||||||
SilierUnsubmitted Done Inline Actionsthis looks wrong, also above. Silier: this looks wrong, also above.
first part of || will be always true | ||||||||||||
} | ||||||||||||
Engine.RegisterGlobal("PlayEntityHitSoundGroup", PlayEntityHitSoundGroup); | ||||||||||||
Engine.RegisterGlobal("PlaySound", PlaySound); | Engine.RegisterGlobal("PlaySound", PlaySound); |
Wildfire Games · Phabricator
if we missed and group does not exist, you end up calling identity and returning material hit anyway