0

我对 JS 很陌生,但我想制作一个由概率控制的非常基本的 RPG 项目生成器。这就是我想出的。

我对 itemtype 部分有疑问。在为 itemtype 完成概率滚动后,我想在武器、配饰和盔甲下随机生成一组元素。

它应该是诸如稀有剑、稀有法杖、普通手套或普通宝箱之类的东西。

我该怎么做?

var rarityNum = Math.floor( 1 + Math.random() * 100 );
var itemNum = Math.floor( 1 + Math.random() * 100 );

var rarity;
if ( rarityNum > 75 ) { rarity = "rare "; }
else if ( rarityNum > 65 ) { rarity = "uncommon "; }
else { rarity = "common "; }

var weapons =["sword","bow","staff"];
var armors =["chest","leggings","gloves"];

var itemtype;
if ( itemNum > 51 ) { itemtype = "weapons"; }
else if ( itemNum > 40 ) { itemtype = "accessories"; }
else { itemtype = "armors"; }

document.write (rarity);
document.write (itemtype);
4

1 回答 1

1
var item = {
  "Weapon" : ["Sword", "Bow"],
  "Armor"  : ["Chest", "Leggings", "Gloves", "Helmet"],
  "Access" : ["Ring", "Bracelet", "Amulet"]
};

var rareness   = Math.floor( 1 + Math.random() * 100 )
  , item_type  = Math.floor( 1 + Math.random() * 100 )
;//randoms

var rarity =
    rareness > 75? "Rare"     :
    rareness > 65? "Uncommon" :
                   "Common"   ;

var type =
    item_type > 51? "Weapon" :
    item_type > 40? "Access" :
                    "Armor"  ;

var item_list  = item[type]
  , item_index = Math.round( Math.random() * (item_list.length - 1) )
;//items

document.write( rarity + " " + item_list[item_index] );
于 2013-01-23T05:37:35.540 回答