updateDamageTint(tick) { if (tick.lastDamagedTick && (tick.lastDamagedTick !==…
- User
updateDamageTint(tick) { if (tick.lastDamagedTick && (tick.lastDamagedTick !== this.lastDamagedTick || !this.lastDamagedAnimationDone)) { this.lastDamagedTick = tick.lastDamagedTick; this.lastDamagedAnimationDone = false; var msSinceFiring = game.world.getReplicator().getMsSinceTick(tick.lastDamagedTick); var flashDurationMs = 100; var flashPercent = Math.min(msSinceFiring / flashDurationMs, 1.0); var flashMultiplier = Math.sin(flashPercent * Math.PI); var tint = (255 << 16) | ((255 - 255 * flashMultiplier / 4) << 8) | ((255 - 255 * flashMultiplier / 4) << 0); if (flashPercent === 1) { tint = 0xFFFFFF; this.lastDamagedAnimationDone = true; } this.base.setTint(tint); if (this.weapon) { this.weapon.setTint(tint); } } }
cleanup and improve this code
- Assistant
To clean up and improve this code, we should aim for better readability, maintainability, and possibly optimization. Here's the refactored code:
updateDamageTint(tick) { if (!tick.lastDamagedTick || (tick.lastDamagedTick === this.lastDamagedTick && this.lastDamagedAnimationDone)) { return; } this.lastDamagedTick = tick.lastDamagedTick; this.lastDamagedAnimationDone = false; const flashDurationMs = 100; const msSinceFiring = game.world.getReplicator().getMsSinceTick(tick.lastDamagedTick); const flashPercent = Math.min(msSinceFiring / flashDurationMs, 1.0); const flashMultiplier = Math.sin(flashPercent * Math.PI); let tint; if (flashPercent === 1) { tint = 0xFFFFFF; this.lastDamagedAnimationDone = true; } else { const greenBlueValue = 255 - (255 * flashMultiplier / 4); tint = (255 << 16) | (greenBlueValue << 8) | greenBlueValue; } this.base.setTint(tint); if (this.weapon) { this.weapon.setTint(tint); } }