Thief: Ability to Steal as Player Character

* The Thief's ability is now available to the player character in levels
  where you'll play as the Thief.
* The Thief is able to steal items from all characters it contacts,
  including Azulians and other Thieves.
* A.I. Thieves will not steal items from each other, to prevent loops.
  Only a Thief controlled by the player will steal items from a Thief.
master
Noah 2021-08-15 17:27:02 -07:00
parent 08ea9efc82
commit 2e8617e8d3
1 changed files with 52 additions and 36 deletions

View File

@ -8,6 +8,9 @@ function main() {
Self.AddAnimation("walk-left", 200, ["stand-left"]); //, "walk-left-1", "walk-left-2", "walk-left-3", "walk-left-2", "walk-left-1"]);
Self.AddAnimation("walk-right", 200, ["stand-right"]); //, "walk-right-1", "walk-right-2", "walk-right-3", "walk-right-2", "walk-right-1"]);
// All thieves can steal items.
stealable();
// Controlled by the player character?
if (Self.IsPlayer()) {
return playable();
@ -15,7 +18,54 @@ function main() {
return ai();
}
// Enemy Doodad AI.
// Common "steal" power between playable and A.I. thieves.
function stealable() {
// Steals your items.
Events.OnCollide(function (e) {
var victim = e.Actor;
if (!e.Settled) {
return;
}
// Thieves don't steal from Thieves (unless controlled by the player).
if (!Self.IsPlayer() && victim.Drawing.Doodad.Filename === "thief.doodad") {
return;
}
// Steal inventory
var stolen = 0;
if (victim.HasInventory()) {
var myInventory = Self.Inventory(),
theirInventory = victim.Inventory();
for (var key in theirInventory) {
if (!theirInventory.hasOwnProperty(key)) {
continue;
}
var value = theirInventory[key];
if (value > 0 || myInventory[key] === undefined) {
victim.RemoveItem(key, value);
Self.AddItem(key, value);
stolen += (value === 0 ? 1 : value);
}
}
// If the player lost their items, notify them.
if (victim.IsPlayer() && stolen > 0) {
Flash("Watch out for thieves! %d item%s stolen!", parseInt(stolen), stolen === 1 ? ' was' : 's were');
}
// If the Thief IS the player, notify your earnings.
if (Self.IsPlayer() && stolen > 0) {
Flash("Awesome! Stole %d item%s from the %s!", parseInt(stolen), stolen === 1 ? '' : 's', e.Actor.Drawing.Doodad.Title);
}
}
});
}
// Enemy Doodad AI: walks back and forth, changing direction
// when it encounters and obstacle.
function ai() {
// Walks back and forth.
var Vx = Vy = 0.0,
@ -23,8 +73,7 @@ function ai() {
direction = "right",
lastSampledX = 0,
sampleTick = 0,
sampleRate = 2,
stolenItems = {}; // map item->qty
sampleRate = 2;
setInterval(function () {
if (sampleTick % sampleRate === 0) {
@ -43,39 +92,6 @@ function ai() {
Self.StopAnimation();
Self.PlayAnimation("walk-" + direction, null);
}, 100);
// Steals your items.
Events.OnCollide(function (e) {
if (!e.Settled) {
return;
}
// Steal inventory
var stolen = 0;
if (e.Actor.HasInventory()) {
var myInventory = Self.Inventory(),
theirInventory = e.Actor.Inventory();
for (var key in theirInventory) {
if (!theirInventory.hasOwnProperty(key)) {
continue;
}
var value = theirInventory[key];
if (value > 0 || myInventory[key] === undefined) {
e.Actor.RemoveItem(key, value);
Self.AddItem(key, value);
stolenItems[key] = value;
stolen += (value === 0 ? 1 : value);
}
}
// Notify the player if it was them.
if (e.Actor.IsPlayer() && stolen > 0) {
Flash("Watch out for thieves! %d item%s stolen!", parseInt(stolen), stolen === 1 ? ' was' : 's were');
}
}
});
}
// If under control of the player character.