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 --- src/behaviour_system.cpp | 33 +++++++++++++++++++-------------- src/script_system.cpp | 4 +++- src/sprite.h | 4 +++- 3 files changed, 25 insertions(+), 16 deletions(-) (limited to 'src') 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