From 89fc2da3f08fcd751ca069fde0987d193e59b007 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 9 Mar 2021 11:51:34 -0500 Subject: Added "let's switch places!" Claus sprite He will wander randomly until you get close, and will then run at you. Talking to him or bumping into him does nothing currently. If you move out of his range of interest he will go back to wandering at a walking pace. #10 --- res/maps/hallucination_interior.tmx | 33 ++++++++++++++++++++++++- res/scripts/common.lua | 42 +++++++++++++++++++++++++++++++- res/scripts/hallucination_interior.lua | 44 ++++++++++++++++++++++++++++++++++ src/behaviour_system.cpp | 33 ++++++++++++++----------- src/script_system.cpp | 4 +++- src/sprite.h | 4 +++- 6 files changed, 142 insertions(+), 18 deletions(-) diff --git a/res/maps/hallucination_interior.tmx b/res/maps/hallucination_interior.tmx index d42cadc..7e012eb 100644 --- a/res/maps/hallucination_interior.tmx +++ b/res/maps/hallucination_interior.tmx @@ -1,5 +1,5 @@ - + @@ -156,6 +156,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/scripts/common.lua b/res/scripts/common.lua index d0ea6b5..f984667 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -34,7 +34,8 @@ SpriteLayer = { BehaviourType = { NONE = 0, WANDER = 1, - PATH = 2 + PATH = 2, + FOLLOW = 3 } CutsceneOptions = { @@ -440,6 +441,13 @@ function WaitForSpritePath(spriteName) end end +--- Sets a sprite to wander. +function StartWandering(spriteName) + local spriteId = getSpriteByAlias(spriteName) + local sprite = getSprite(spriteId) + sprite.behaviourType = BehaviourType.WANDER +end + --- Turns off the sprite's behaviour. function DisableBehaviour(spriteName) local spriteId = getSpriteByAlias(spriteName) @@ -447,6 +455,23 @@ function DisableBehaviour(spriteName) sprite.behaviourType = BehaviourType.NONE end +--- Directs a sprite to start following a target sprite. +function FollowSprite(spriteName, targetName) + local spriteId = getSpriteByAlias(spriteName) + local targetId = getSpriteByAlias(targetName) + local sprite = getSprite(spriteId) + sprite.followSpriteId = targetId + sprite.behaviourType = BehaviourType.FOLLOW +end + +--- Makes a sprite stop following whatever sprite it was following. +function StopFollowingSprite(spriteName) + local spriteId = getSpriteByAlias(spriteName) + local sprite = getSprite(spriteId) + sprite.followSpriteId = -1 + sprite.behaviourType = BehaviourType.NONE +end + --- Fades out the currently playing music. -- This does not block. If you want it to block, call Delay for the same amount -- of time. @@ -478,6 +503,21 @@ function EnablePlayerControl() playerSprite.controllable = true end +--- Makes the specified sprite face toward the †arget sprite. +-- This version of the function uses any of the eight directions. +-- @param spriteName the name of the sprite to change the direction of +-- @param targetName the name of the sprite to face toward +function FaceTowardSprite(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) + + SetDirection(spriteName, dir) +end + --- Makes the specified sprite face toward the †arget sprite. -- This version of the function uses the closest cardinal direction. -- @param spriteName the name of the sprite to change the direction of diff --git a/res/scripts/hallucination_interior.lua b/res/scripts/hallucination_interior.lua index f009196..0622453 100644 --- a/res/scripts/hallucination_interior.lua +++ b/res/scripts/hallucination_interior.lua @@ -226,3 +226,47 @@ function hallucination_interior.mailbox_time_passage() EnablePlayerControl() end + +function hallucination_interior.switch_claus_attention() + gamestate.switch_claus_lost_interest = false + + Halt("switch_claus") + DisableBehaviour("switch_claus") + ShowExpression("switch_claus", "surprise") + FaceTowardSprite("switch_claus", "lucas") + Delay(1000) + + RemoveExpression("switch_claus") + + if gamestate.switch_claus_lost_interest then + SetMovementSpeed("switch_claus", 1) + StartWandering("switch_claus") + else + SetMovementSpeed("switch_claus", 2) + + if IsSpriteInZone("switch_claus", "switch_claus_hidden") then + MakeSpriteNotSolid("lucas") + DirectSpriteToLocation("switch_claus", "switch_claus_rsvp") + MakeSpriteSolid("lucas") + WaitForSpritePath("switch_claus") + end + + if gamestate.switch_claus_lost_interest then + SetMovementSpeed("switch_claus", 1) + StartWandering("switch_claus") + else + FollowSprite("switch_claus", "lucas") + end + end +end + +function hallucination_interior.switch_claus_lose_interest() + gamestate.switch_claus_lost_interest = true + + SetMovementSpeed("switch_claus", 1) + StartWandering("switch_claus") +end + +function hallucination_interior.lets_switch_places() + -- TODO: let's switch places +end diff --git a/src/behaviour_system.cpp b/src/behaviour_system.cpp index b68cd6d..f4f7546 100644 --- a/src/behaviour_system.cpp +++ b/src/behaviour_system.cpp @@ -19,22 +19,27 @@ void BehaviourSystem::tick(double dt) { while (timer_.step()) { for (int spriteId : game_.getSprites()) { Sprite& sprite = game_.getSprite(spriteId); - if (!sprite.paused && sprite.behaviourType == BehaviourType::Wander) { - // 75% chance of changing what's happening - if (std::bernoulli_distribution(0.75)(game_.getRng())) { - // 50% chance of choosing a direction or stopping - if (std::bernoulli_distribution(0.5)(game_.getRng())) { - Direction dir; - switch (std::uniform_int_distribution(0,3)(game_.getRng())) { - case 0: dir = Direction::left; break; - case 1: dir = Direction::up; break; - case 2: dir = Direction::right; break; - default: dir = Direction::down; break; + if (!sprite.paused) { + if (sprite.behaviourType == BehaviourType::Wander) { + // 75% chance of changing what's happening + if (std::bernoulli_distribution(0.75)(game_.getRng())) { + // 50% chance of choosing a direction or stopping + if (std::bernoulli_distribution(0.5)(game_.getRng())) { + Direction dir; + switch (std::uniform_int_distribution(0,3)(game_.getRng())) { + case 0: dir = Direction::left; break; + case 1: dir = Direction::up; break; + case 2: dir = Direction::right; break; + default: dir = Direction::down; break; + } + game_.getSystem().moveInDirection(spriteId, dir); + } else { + game_.getSystem().stopDirecting(spriteId); } - game_.getSystem().moveInDirection(spriteId, dir); - } else { - game_.getSystem().stopDirecting(spriteId); } + } else if (sprite.behaviourType == BehaviourType::Follow) { + Sprite& target = game_.getSprite(sprite.followSpriteId); + game_.getSystem().moveInDirection(spriteId, directionFacingPoint(target.loc - sprite.loc)); } } } diff --git a/src/script_system.cpp b/src/script_system.cpp index 50ea60a..a50fa7a 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -52,7 +52,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { "enclosureZone", &Sprite::enclosureZone, "movementSpeed", &Sprite::movementSpeed, "solid", &Sprite::solid, - "behaviourType", &Sprite::behaviourType); + "behaviourType", &Sprite::behaviourType, + "followSpriteId", &Sprite::followSpriteId); engine_.new_usertype( "message", @@ -242,6 +243,7 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { loadMapScripts(filename); }); + engine_.set_function("directionFacingPoint", &directionFacingPoint); engine_.set_function("cardinalDirectionFacingPoint", &cardinalDirectionFacingPoint); engine_.script_file("../res/scripts/common.lua"); diff --git a/src/sprite.h b/src/sprite.h index 2ab306d..10dd3a8 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -45,7 +45,8 @@ enum class CharacterMedium { enum class BehaviourType { None, Wander, - Path + Path, + Follow }; enum class MirrorType { @@ -128,6 +129,7 @@ public: vec2i pathfindingDestination; bool cardinalDirectionsOnly = false; std::deque path; + int followSpriteId = -1; // Mirror MirrorType mirrorType = MirrorType::None; -- cgit 1.4.1