|
|
|
 |
Reply From: |
crossbito |
Hi!
I would create functions like getAttack() and getDefense() , you would need the defense of the node receiving damage and the attack of the object dealing damage. Here’s an example implementation:
func calculateDamage(attacker, defender):
var attack = attacker.getAttack()
var defense = defender.getDefense()
# Calculate the damage dealt and return it
var damage = attack - defense
return damage
For your bullet (attacker), you can implement a getAttack() function that references the weapon that created the bullet. The weapon, in turn, would have its own getAttack() function and a reference to the player, where you can get the player’s attack value. Here’s an example:
# Bullet
func getAttack():
return self.attack + weapon.getAttack()
# Weapon
func getAttack():
return self.attack + player.getAttack()
# Player
func getAttack():
return self.attack
In this system, the attack value of the bullet is calculated by summing the attack value of the bullet itself and the attack value obtained from the weapon. The weapon’s attack value is determined by adding its own attack value and the attack value of the player. The player, in turn, simply returns its own attack value.
Now, you can apply the same concept to the enemy. If the enemy has armor, you can calculate the defense by combining the defense from the body with the defense from the armor.
With this approach, you can consistently calculate damage by considering all the effects of bullets, armor, weapons, and other factors. By traversing from one node to another and accumulating the effects, you can accurately calculate the final damage or defense.
This is one approach, but you may be able to find something even better for your specific system.
Thank you for your answer.
In your solution, does it mean that the bullet should carry a reference to the Player (and Player’s ClassStat) and a reference to the Weapon (and Weapon’s WeaponStat)?
Something like Bullet.new(Player, Weapon)?
denisgames | 2023-06-10 10:29
No, in my example, the bullet only needs a reference to the weapon, and the weapon has a reference to the player. The idea is to only have a reference to the node that calls us, and from there, you can obtain data. Bullet → weapon → player. This is useful because it allows you to avoid creating references for everything inside everything.
crossbito | 2023-06-10 12:49