Advertisement
KingAesthetic

Example 2

May 12th, 2024 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.06 KB | Source Code | 0 0
  1. // function that simulates loot drops in RPG games:
  2. function getRndLoot() {
  3.     const lootTable = [
  4.         {item: "health potion", chance: 0.2},
  5.         {item: "speed potion", chance: 1.2},
  6.         {item: "rare coin", chance: 1.1},
  7.     ];
  8.     const randomNum = Math.random();
  9.     var selectedItem = null;
  10.     for(const loot of lootTable) {
  11.         if(randomNum < loot.chance) {
  12.             selectedItem = loot.item;
  13.             break;
  14.         }
  15.     }
  16.     return selectedItem || `no loot`;
  17. }
  18. const droppedItem = getRndLoot();
  19. console.log(droppedItem)
  20.  
  21. // function that tracks quests completed by players:
  22. const completedQuests = [];
  23. function completeQuest(questName) {
  24.     if(!completedQuests.includes(questName)) {
  25.         completedQuests.push(questName);
  26.     }
  27. }
  28. completeQuest("Find Darkost in the Armageddon");
  29. completeQuest("Retrieve the crown from the forgotten dungeon");
  30. console.log(`Completed quests: ${completedQuests.join(', ')}`);
  31.  
  32. // function that simulates combat system:
  33. function simulateCombat(playerHP, enemyHP) {
  34.     while (playerHP > 0 && enemyHP > 0) {
  35.         const playerDamage = Math.floor(Math.random() * 20) + 1;
  36.         enemyHP -= playerDamage;
  37.         const enemyDamage =
  38.         Math.floor(Math.random() * 8) + 1;
  39.         playerHP -= enemyDamage;
  40.     }
  41.     if(playerHP <= 0) {
  42.         console.log("You DIED");
  43.     } else {
  44.         console.log("You have WON");
  45.     }
  46. }
  47. const playerHealth = 100;
  48. const enemyHealth = 80;
  49. simulateCombat(playerHealth, enemyHealth);
  50.  
  51. //function that generates random loot from treasure chests:
  52. function openChest() {
  53.     const possibleLoot = ["gold", "medkit", "magic wand", "gemstone"];
  54.     const randomLoot = Math.floor(Math.random() * possibleLoot.length);
  55.     return possibleLoot[randomLoot];
  56. }
  57. const foundLoot = openChest();
  58. console.log(foundLoot);
  59.  
  60. // function that handles skill leveling
  61. const playerSkills = {
  62.     attack: 1,
  63.     defense: 1,
  64.     magic: 1,
  65. };
  66. function levelUpSkill(skill) {
  67.     playerSkills[skill] += 1
  68. }
  69. levelUpSkill("attack");
  70. console.log(playerSkills.attack);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement