UNPKG

featuresgenerator-lyanxyz

Version:

This project generates random game features for a JavaScript game.

180 lines (149 loc) 4.72 kB
class Character { constructor(name, type) { this.name = name; this.type = type; this.health = 100; this.energy = 100; this.attack = 10; this.defense = 5; } attackEnemy(enemy) { const damage = this.attack - enemy.defense; enemy.takeDamage(damage); } takeDamage(damage) { this.health -= damage; if (this.health <= 0) { this.health = 0; } } recoverEnergy() { this.energy += 10; if (this.energy > 100) { this.energy = 100; } } } class Player extends Character { constructor(name, type) { super(name, type); this.level = 1; this.experience = 0; } levelUp() { this.level++; this.attack += 5; this.defense += 3; this.health = 100; this.energy = 100; } gainExperience(exp) { this.experience += exp; if (this.experience >= 100) { this.levelUp(); this.experience -= 100; } } } class Monster extends Character { constructor(name, type) { super(name, type); this.level = Math.floor(Math.random() * 5) + 1; this.attack *= this.level; this.defense *= this.level; this.health *= this.level; this.energy *= this.level; } dropItem() { const items = ['Health Potion', 'Energy Potion', 'Gold Coin']; const itemIndex = Math.floor(Math.random() * items.length); return items[itemIndex]; } } const player = new Player('Player1', 'Warrior'); const monster = new Monster('Goblin', 'Goblin'); while (player.health > 0 && monster.health > 0) { player.attackEnemy(monster); monster.attackEnemy(player); player.recoverEnergy(); } if (player.health <= 0) { console.log('Game over, player defeated!'); } else if (monster.health <= 0) { console.log('Game over, player wins!'); } console.log(`Player level: ${player.level}`); console.log(`Player experience: ${player.experience}`); class Item { constructor(name, type) { this.name = name; this.type = type; } use() { if (this.type === 'Health Potion') { player.health += 20; if (player.health > 100) { player.health = 100; } } else if (this.type === 'Energy Potion') { player.energy += 20; if (player.energy > 100) { player.energy = 100; } } else if (this.type === 'Gold Coin') { const goldAmount = Math.floor(Math.random() * 50) + 10; player.gold += goldAmount; } } } function triggerRandomEvent() { const events = ['itemDrop', 'levelUp', 'monsterEncounter', 'findTreasure', 'allyEncounter']; const eventIndex = Math.floor(Math.random() * events.length); const event = events[eventIndex]; switch (event) { case 'itemDrop': const item = new Item('Random Item', ['Health Potion', 'Energy Potion', 'Gold Coin'][Math.floor(Math.random() * 3)]); item.use(); break; case 'levelUp': player.gainExperience(50); break; case 'monsterEncounter': const newMonster = new Monster('Random Monster', 'Monster'); newMonster.attackEnemy(player); break; case 'findTreasure': player.gold += Math.floor(Math.random() * 100) + 50; break; case 'allyEncounter': player.health += 30; player.energy += 30; break; default: break; } } triggerRandomEvent(); class Quest { constructor(name, description, reward) { this.name = name; this.description = description; this.reward = reward; } complete() { player.experience += this.reward; if (player.experience >= 100) { player.levelUp(); player.experience -= 100; } } } function triggerRandomQuest() { const questNames = ['Defeat the Dragon', 'Retrieve the Lost Artifact', 'Rescue the Princess', 'Explore the Forbidden Forest', 'Clear the Bandit Camp']; const questDescriptions = ['The dragon has been terrorizing the village. Defeat it to bring peace back to the land.', 'An ancient artifact has been lost for centuries. Retrieve it to unlock its secrets.', 'The princess has been captured by an evil sorcerer. Rescue her and earn the kingdom\'s gratitude.', 'The Forbidden Forest holds many secrets and dangers. Explore it to uncover its mysteries.', 'Bandits have been plaguing the countryside. Clear out their camp to restore safety to the roads.']; const rewards = [50, 75, 100, 125, 150]; const questIndex = Math.floor(Math.random() * questNames.length); const quest = new Quest(questNames[questIndex], questDescriptions[questIndex], rewards[questIndex]); quest.complete(); } triggerRandomQuest();