doodle/dev-assets/doodads/thief/thief.js

104 lines
3.0 KiB
JavaScript
Raw Normal View History

Thief and Inventory APIs This commit adds the Thief character with starter graphics (no animations). The Thief walks back and forth and will steal items from other doodads, including the player. For singleton items that have no quantity, like the Colored Keys, the Thief will only steal one if he does not already have it. Quantitied items like the Small Key are always stolen. Flexibility in the playable character is introduced: Boy, Azulian, Bird, and Thief all respond to playable controls. There is not currently a method to enable these apart from modifying balance.PlayerCharacterDoodad at compile time. New and Changed Doodads * Thief: new doodad that walks back and forth and will steal items from other characters inventory. * Bird: has no inventory and cannot pick up items, unless player controlled. Its hitbox has also been fixed so it collides with floors correctly - not something normally seen in the Bird. * Boy: opts in to have inventory. * Keys (all): only gives themselves to actors having inventories. JavaScript API - New functions available * Self.IsPlayer() - returns if the current actor IS the player. * Self.SetInventory(bool) - doodads must opt-in to having an inventory. Keys should only give themselves to doodads having an inventory. * Self.HasInventory() bool * Self.AddItem(filename, qty) * Self.RemoveItem(filename, qty) * Self.HasItem(filename) * Self.Inventory() - returns map[string]int * Self.ClearInventory() * Self.OnLeave(func(e)) now receives a CollideEvent as parameter instead of the useless actor ID. Notably, e.Actor is the leaving actor and e.Settled is always true. Other Changes * Play Mode: if playing as a character which doesn't obey gravity, such as the bird, antigravity controls are enabled by default. If you `import antigravity` you can turn gravity back on. * Doodad collision scripts are no longer run in parallel goroutines. It made the Thief's job difficult trying to steal items in many threads simultaneously!
2021-08-10 05:42:22 +00:00
// Thief
function main() {
Self.SetMobile(true);
Self.SetGravity(true);
Self.SetInventory(true);
Self.SetHitbox(0, 0, 32, 58);
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"]);
// Controlled by the player character?
if (Self.IsPlayer()) {
return playable();
}
return ai();
}
// Enemy Doodad AI.
function ai() {
// Walks back and forth.
var Vx = Vy = 0.0,
playerSpeed = 4,
direction = "right",
lastSampledX = 0,
sampleTick = 0,
sampleRate = 2,
stolenItems = {}; // map item->qty
setInterval(function () {
if (sampleTick % sampleRate === 0) {
var curX = Self.Position().X,
delta = Math.abs(curX - lastSampledX);
if (delta < 5) {
direction = direction === "right" ? "left" : "right";
}
lastSampledX = curX;
}
sampleTick++;
Vx = parseFloat(playerSpeed * (direction === "left" ? -1 : 1));
Self.SetVelocity(Vector(Vx, Vy));
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.
function playable() {
Events.OnKeypress(function (ev) {
Vx = 0;
Vy = 0;
if (ev.Right) {
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-right", null);
}
Vx = playerSpeed;
} else if (ev.Left) {
if (!Self.IsAnimating()) {
Self.PlayAnimation("walk-left", null);
}
Vx = -playerSpeed;
} else {
Self.StopAnimation();
animating = false;
}
// Self.SetVelocity(Point(Vx, Vy));
})
}