Skip to main content

Loot

Loot makes gold and items pop out of a point of the map. Each pickup arcs out, bounces on the ground, then is collected: it flies to the player who comes near it, the player walks on it, or the player presses the action key next to it. Use it for a tree that is cut, a chest, a barrel, a defeated enemy or any scripted reward. It is built in: there is nothing to install. The playground game playground/games/action-loot shows it in action.

Drop loot from an event

Call dropLoot() on an event (or a player) on the server. The pickups pop out of its center:
The first argument is the reward table:
  • gold: split into coins (one coin per 5 gold, 6 coins at most);
  • items: item ids or { itemId, amount, chance }, where chance is a percentage (100 by default);
  • showNotification: show the inventory notification when an item is collected.
To drop loot from any point, use the map:
Both return the ids of the spawned pickups.

Collect modes

The magnet radius can depend on the player, for example with a magnet amulet:

Options

Items and their loot config

A pickup is linked to its item in the database:
  • it shows the item icon (a spritesheet id); items without an icon show a gem;
  • collecting it calls player.addItem(item, amount), so the inventory, the menu and the hotbar update as usual;
  • gold is not an item: it shows a spinning coin and adds to player.gold.
Declare how an item behaves on the ground with its loot field:
The item option of dropLoot() takes precedence over the item config.

Client presentation

The engine always animates the motion: the arc, the bounce, the idle bob, the magnet and the flight to the collector. The loot client hook changes what is drawn and heard:
  • components: components drawing a pickup, addressed by the item loot.visual.
  • sounds.land: played when a pickup lands; sounds.collect: played to the collector when the item has no sound.
  • collectText: shows +5 gold or +2 Wood above the collector. The texts use the rpg.loot.gold and rpg.loot.item translation keys.
A custom component only draws the pickup. It receives kind, amount, icon, name, color, rarity, state (flying, idle, collecting, expiring) and progress (0 to 1 within the state):

Action Battle enemies

With @rpgjs/action-battle, add drop to the enemy rewards. The experience is granted on defeat, the gold and the items pop as loot owned by the attacker:
drop: true uses the default settings.

Client, server and multiplayer

  • Server: rolls the drops, places the pickups, checks the distance to the players and grants the gold and items. Pickups are dynamic events with a small hitbox and through, so they never block movement or attacks.
  • Synchronized: each pickup is an event carrying a componentsCenter component with its kind, icon, name, color, rarity, sound and arc settings.
  • Client: the built-in rpg:loot sprite component animates the pickup and plays the sounds and the collect text for the collector. A player who joins long after the drop sees the pickups already on the ground.
  • Standalone RPG and MMORPG: both work the same. With ownership: "player", pickups are scenario events that only their owner receives.