Итак, я строю основную игру командной строки, используя readline-sync.
Основная предпосылка - вы сражаетесь с врагом, и если вы убиваете врага, игра продолжается, и вы сражаетесь с другим, если вы умрете, игра заканчивается.
Моя проблема в том, что когда я убиваю врага и игра продолжается, он все еще с тем же врагом и тем же здоровьем противника. Как я могу получить так, чтобы, когда я убивал врага, приходил новый враг со 100 ед. Здоровья, и я сражался с ним?
Мне кажется, я понял, что создал только одного врага из конструктора, но есть ли способ сделать так, чтобы он воссоздает нового врага со 100 ед. Здоровья, когда враг умирает?
Вот мой JavaScript:
var readline = require("readline-sync");
function Player(name, hp, items){
this.name = name;
this.health = hp;
this.items = items;
}
function Enemy(name, hp, items){
this.name = name;
this.health = hp;
this.items = items;
}
//1 in 3 chance of getting attacked
var enemyAttacking = ["safe", "safe", "attack"];
//1 in 2 chance of escaping when running
var escaped = [1, 2];
//aren't getting attacked message
var noAttackMes = ["You didn't run into any monsters. Move along...", "No creatures along the pathway. Continue walking.", "You're all alone. Keep going." , "Enemies are not engaging you. Keep walking.", "The path is clear for you. Continue your journey.", "Enemies are nearby, but are not attacking. Keep walking"];
//randomly chooses a weapon for the enemy to have
var weapon = ["axe", "sword", "bow and arrow"];
var weaponSelected = Math.floor(Math.random() * weapon.length);
//randomly chooses an enemy
var enemies = ["giant", "dark wizard", "wolf"];
var enemyChosen = Math.floor(Math.random() * enemies.length);
var newEnemy = new Enemy(enemies[enemyChosen], 100, weapon[weaponSelected]);
//randomly choose an amount of hp to remove
var hpRemove = [20, 40, 60, 80];
//greeting player
var playerName = readline.question("\nGreetings! Welcome to the Seven Kingdoms! In order to gain passage, you must tell me your name: ");
var newPlayer = new Player(playerName, 100, "none");
console.log(`\nGreetings ${playerName}! You have been granted access, but beware of all the nasty beasts and creatures that lurk in the shadows...`);
//will run walk() until player dies
while(newPlayer.health > 0){
walk();
}
function walk(){
var playerWalk = readline.question("\nPlease press 'w' to walk or type 'print' to view inventory: ");
if(playerWalk === "w"){
if(enemyAttacking[Math.floor(Math.random() * enemyAttacking.length)] === "attack"){
console.log("You're being attacked!");
fight();
} else {
console.log(noAttackMes[Math.floor(Math.random() * noAttackMes.length)]);
}
}else if(playerWalk === "print") {
console.log("Here's what's in your inventory:");
console.log(newPlayer);
}
}
function enemyAttack(){
if(newEnemy.health <= 0){
console.log(`\nYou have killed the ${enemies[enemyChosen]}! He has dropped his ${weapon[weaponSelected]} and you can now reclaim it as your own.`);
}else {
newPlayer.health -= hpRemove[Math.floor(Math.random() * hpRemove.length)];
console.log(`\nThe ${enemies[enemyChosen]} has attacked you back! Your health now stands at ${newPlayer.health}.`);
}
if(newPlayer.health <= 0){
console.log(`\nYou have died the most gruesome of gruesome deaths. Do not be ashamed, as you fought with honor, and will be welcomed to Valhalla with open arms.`);
}
}
function fight(){
var fightOrFlight = ["Fight", "Run"];
var fightChoice = readline.keyInSelect(fightOrFlight, `The ${enemies[enemyChosen]} is attacking you! What do you want to do?!`);
if(fightChoice === 0){
attackEnemy();
}else {
run();
}
while(newEnemy.health > 0 && newPlayer.health > 0){
var fightChoice2 = readline.keyInSelect(fightOrFlight, `You have both taken hits. What will you like to do next?`);
if(fightChoice2 === 0){
attackEnemy();
}else {
run();
}
}
}
function run(){
if(escaped[Math.floor(Math.random() * escaped.length)] === 1){
console.log("\nYou have escaped the creature and may continue walking!");
}else {
console.log("\nYou have not escaped! The creature is attacking!");
attackEnemy();
}
}
function attackEnemy(){
newEnemy.health -= hpRemove[Math.floor(Math.random() * hpRemove.length)];
console.log(`\nYou have attacked the ${enemies[enemyChosen]}! His health now stands at ${newEnemy.health}.`);
if(newEnemy.health <= 0){
newPlayer.items = newEnemy.items;
newPlayer.health += 20;
}
enemyAttack();
}