So lets say you have a player class, and each player can only have 1 weapon.
So maybe our player class looks something like this:
class Player
int health
Weapon equipedWeapon
set_weapon(Weapon new_weapon) {
equipedWeapon = new_weapon
}
fire_weapon {
equipedweapon.fire()
}
and our weapon class might look something like this:
Class Pistol inherits Weapon
Player player
setPlayer(player newPlayer) {
player = newPlayer
}
fire() {
// lets pretend player instance had a function addBullet to some array
player.addBullet(new Bullet(player.x + 1, player.y))
}
My design issue is that I want the Player class to keep an instance of the weapon the player is currently using. But I also want the weapon to have access to the player so it can fire the bullet relative to the players position.
It seems like I'm forced to give the weapon class a reference to the player and the player instance a reference of the weapon. Basically a bidirectional relationship. But this seems like such an awkward design to me?
Can someone recommend a better way to design something like this?
Aucun commentaire:
Enregistrer un commentaire