From 324346a87c114f0991a092ac8653afecada364b5 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 5 Jul 2021 11:58:29 -0400 Subject: Added background scripts Background scripts are scripts that are launched when a map is loaded. They differ in use from a map's init script in that they are expected to contain an infinite loop. These scripts are linked to a sprite and will be killed when that sprite is destroyed (usually when the map is unloaded, but if the sprite is made persistent then it may last longer). The thread running the script is given no warning that it is being killed; the coroutine is simply never called again, and the thread is disposed of. Because of this, background scripts MUST ensure the game is in a consistent state before coroutine yielding, because it is not guaranteed that the coroutine will ever be called again. --- res/scripts/common.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'res') diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 2e95f26..35eec22 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -579,6 +579,20 @@ function FaceTowardSpriteCardinally(spriteName, targetName) SetDirection(spriteName, dir) end +--- Checks whether the given sprite is facing another sprite. +-- @param spriteName the name of the sprite whose facing direction is relevant +-- @param targetName the name of the sprite that is or isn't being looked at +function IsFacingTowardSprite(spriteName, targetName) + local spriteId = getSpriteByAlias(spriteName) + local targetId = getSpriteByAlias(targetName) + local sprite = getSprite(spriteId) + local target = getSprite(targetId) + local diff = vec2i.new(target.loc:x() - sprite.loc:x(), target.loc:y() - sprite.loc:y()) + local dir = directionFacingPoint(diff) + + return sprite.dir == dir +end + --- Detaches the sprite's followers and erases their following trails. function BreakUpParty(spriteName) local spriteId = getSpriteByAlias(spriteName) -- cgit 1.4.1