In my JavaScript class practice, I'm working on creating two users, each equipped with a weapon. My goal is to have a function within the class that allows the players to attack each other and decrease their health based on the damage of their respective weapons. I've split my work into three different files, but I'm struggling to combine my User class with my Gun class to assign a weapon to each user. Here's what I envision:
let John = new User("John", "Sniper");
let Michael = new User("Michael", "Shotgun");
John.attack("Michael");
My plan is to add more functions to my classes for some fun, but first, I need to solve the challenge of incorporating a weapon into the User class.
I've attempted several methods, but nothing seems to work...
main.js
import Gun from '/js/gun.js';
import User from '/js/user.js';
let players = [new User("Mic", "sniper"), new User("AI", "rifle")];
players[0].shootAt(players[1]);
players[1].shootAt(players[0]);
user.js
export default class User{
constructor(userName, gun){
this.name = userName;
this.health = 50;
console.log(this.name + ' was created with ' + this.health + ' health');
}
shootAt(target){
target.health -= gun.damage;
}
}
gun.js
export default class Gun{
constructor(){
this.sniper = {
damage: 150,
magazine: 7,
fireRate: 1.5
};
this.rifle = {
damage: 25,
magazine: 30,
fireRate: 0.05
};
}
}
The current implementation is not functioning as desired:
let players = [new User("Mic", "sniper"), new User("AI", "rifle")];
players[0].shootAt(players[1]);
players[1].shootAt(players[0]);
But this is the outcome I hope to achieve... Any assistance would be greatly appreciated!