I'm making a program to compare Borderlands 3 weapon damage over time in a graphic. There's still some work to be done, like implementing elemental damage, but I'd like to know if I'm doing a good job for now regarding the main firing logic.
My biggest concerns are:
General readability.
WeaponReload
andWeaponFire
extendWeaponAction
, a class containing the properties.startTime
,.endTime
and.duration
(included after theWeapon
class code) and both are being treated as if they're the same. I think some other languages can do something similar using structs and protocols to check conformity and I'm not sure if this is the right way to do it in JavaScript.I don't know whether I'm doing a good use of the
try {...} catch {...}
statement inWeapon.fullAuto
or if this logic could be improved upon using other methods.
class Weapon {
constructor (damage, fireRate, reloadSpeed, magazineSize, elementalDamage, elementalChance) {
this.damage = damage
this.fireRate = fireRate
this.reloadSpeed = reloadSpeed
this.magazineSize = magazineSize
this.elementalDamage = elementalDamage
this.elementalChance = elementalChance
this.bulletsLeftInMagazine = magazineSize
}
get timePerShot () {
return 1 / this.fireRate
}
fire (startTime = 0) {
if (this.bulletsLeftInMagazine < 1)
throw new Error("Not enough bullets in the magazine")
let whenCanFireAgain = startTime + this.timePerShot
--this.bulletsLeftInMagazine
return new WeaponShot(startTime, whenCanFireAgain, this.damage)
}
reload (startTime = 0) {
let whenCanFireAgain = startTime + this.reloadSpeed
this.bulletsLeftInMagazine = this.magazineSize
return new WeaponReload(startTime, whenCanFireAgain)
}
fullAuto (duration, startTime = 0) {
let localActionStartTime = 0,
globalActionStartTime = startTime,
actions = []
while (duration > localActionStartTime) {
let localActionEndTime,
globalActionEndTime,
action
try {
action = this.fire(globalActionStartTime)
} catch {
action = this.reload(globalActionStartTime)
}
actions.push(action)
localActionStartTime += action.duration
globalActionStartTime += action.duration
}
return actions
}
}
class WeaponAction {
constructor (startTime, endTime, damageDealt) {
this.startTime = startTime
this.endTime = endTime
this.damageDealt = damageDealt
}
get duration () {
return this.endTime - this.startTime
}
}
WeaponAction
I only added that single class code. The other two are the same thing, except for.damageDealt
but that does nothing. \$\endgroup\$