elye-loot-table
v0.2.1
Published
A loot drop table implementation in JavaScript forked for use in elye
Downloads
2
Readme
LootTable.js
A loot drop table implementation in JavaScript.
LootTable is used to make a random choice among a weighted list of alternatives for item drops, map generation, and many other processes. There's a good overview of loot tables on Lost Garden.
Example
var loot = new LootTable();
loot.add('sword', 20);
loot.add('shield', 5);
loot.add('gold', 5);
loot.add(null, 1);
var item = loot.choose(); // most likely a sword, sometimes null
Weights are arbitrary, not percentages, and don't need to add up to 100.
If one item has a weight of 2 and another has a weight of 1, the first item
is twice as likely to be chosen. If quantity is given, then calls to choose()
will only return that item while some are available. Each choose()
that
selects that item will reduce its quantity by 1.
Items can be anything, not just strings. They can be arrays, numbers, JSON data, null, functions... even another LootTable!
Building
npm install
npm run build
Global script example
include a <script src="LootTable.js"></script>
tag on your page and you can use loot table as a global variable.
ES modules example
import LootTable from 'loot-table';
CommonJS (node) example
var LootTable = require('loot-table');