From 6d6e03b95b197d3a16b629131d463263b46c1e42 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 6 Feb 2021 13:24:35 -0500 Subject: Added sprite interaction --- res/scripts/script0001.lua | 2 +- src/direction.h | 13 +++++++++++++ src/input_system.cpp | 46 +++++++++++++++++++++++++++++++++------------- src/main.cpp | 1 + src/sprite.h | 3 ++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/res/scripts/script0001.lua b/res/scripts/script0001.lua index a259e47..3a09705 100644 --- a/res/scripts/script0001.lua +++ b/res/scripts/script0001.lua @@ -1,4 +1,4 @@ function script0001() - DisplayMessage("Some people always try to avoid fighting when there are enemies around. You know the type, right? They use the dash ability to zoom right by. I guess you could say they're followers of \"peace at any price\".", "Sparrow", SpeakerType.GIRL) + DisplayMessage("It's almost suppertime.\nHinawa should be back soon.\nWhat? She's not coming back? Why, did you do something to her?\nHuh? I did it to her? Hinawa isn't coming back?\nWho are you? What did you do?\nIt's time for supper. It's time for supper.", "Flint", SpeakerType.MAN) HideCutsceneBars() end diff --git a/src/direction.h b/src/direction.h index a83a6f8..e11b1f3 100644 --- a/src/direction.h +++ b/src/direction.h @@ -51,4 +51,17 @@ inline bool dirHasDir(Direction value, Direction inner) { } } +inline Direction oppositeDirection(Direction value) { + switch (value) { + case Direction::up: return Direction::down; + case Direction::up_right: return Direction::down_left; + case Direction::right: return Direction::left; + case Direction::down_right: return Direction::up_left; + case Direction::down: return Direction::up; + case Direction::down_left: return Direction::up_right; + case Direction::left: return Direction::right; + case Direction::up_left: return Direction::down_right; + } +} + #endif /* end of include guard: DIRECTION_H_AB66A90E */ diff --git a/src/input_system.cpp b/src/input_system.cpp index 6ddb59e..5133e27 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp @@ -3,6 +3,8 @@ #include "character_system.h" #include "message_system.h" #include "script_system.h" +#include "transform_system.h" +#include "animation_system.h" struct Input { bool left = false; @@ -26,20 +28,38 @@ void InputSystem::tick(double dt) { game_.getSystem().beginCrouch(spriteId); } } - } else if (e.key.keysym.sym == SDLK_a) { - // TODO: Remove this, it's just for testing. - /*if (game_.getSystem().getCutsceneBarsProgress() == 1.0) { - game_.getSystem().hideCutsceneBars(); + } else if (e.key.keysym.sym == SDLK_SPACE) { + // If there is text on screen, try to advance it. + if (game_.getSystem().getCutsceneBarsProgress() != 0.0) { + game_.getSystem().advanceText(); } else { - game_.getSystem().displayCutsceneBars(); - }*/ - //game_.getSystem().displayMessage("Some people always try to avoid fighting when there are enemies around. You know the type, right? They use the dash ability to zoom right by. I guess you could say they're followers of \"peace at any price\".", "Sparrow", SpeakerType::Woman); - //game_.getSystem().displayMessage("Lucas. You're awful at hide-and-seek, you know that? Try harder.", "Kumatora", SpeakerType::Woman); - //game_.getSystem().displayMessage("Hi Tooth! I hope you're having a good day.", "Lucas", SpeakerType::Boy); - game_.getSystem().runScript("script0001"); - } else if (e.key.keysym.sym == SDLK_b) { - // TODO: Remove this, it's just for testing. - game_.getSystem().advanceText(); + // Otherwise, check if there is a sprite in front of the player. + for (int spriteId : game_.getSprites()) { + Sprite& sprite = game_.getSprite(spriteId); + if (sprite.controllable) { + vec2i checkLoc = sprite.loc + (unitVecInDirection(sprite.dir) * MOVEMENT_SPEED); + CollisionResult collision = game_.getSystem().checkCollision(spriteId, checkLoc, sprite.dir); + + // If there is a sprite to be interacted with, rotate that sprite so it is facing the player. + // Then, run its interaction script if present. + if (collision.horiz.colliderSprite != -1) { + game_.getSystem().setSpriteDirection(collision.horiz.colliderSprite, oppositeDirection(sprite.dir)); + + Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); + if (collider.interactionScript != "") { + game_.getSystem().runScript(collider.interactionScript); + } + } else if (collision.vert.colliderSprite != -1) { + game_.getSystem().setSpriteDirection(collision.vert.colliderSprite, oppositeDirection(sprite.dir)); + + Sprite& collider = game_.getSprite(collision.vert.colliderSprite); + if (collider.interactionScript != "") { + game_.getSystem().runScript(collider.interactionScript); + } + } + } + } + } } } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { for (int spriteId : game_.getSprites()) { diff --git a/src/main.cpp b/src/main.cpp index 8363995..3ea92ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,6 +51,7 @@ void loop(Renderer& renderer) { game.getSystem().initSprite(flintSprite, {35*16, 19*16}); game.getSystem().setUpCollision(flintSprite, {-8, -8}, {12, 8}); game.getSystem().initSprite(flintSprite, "../res/sprites/flint_anim.txt", renderer); + game.getSprite(flintSprite).interactionScript = "script0001"; game.getSystem().setFollowingSprite(lucasSprite); game.getSystem().unlockCamera(); diff --git a/src/sprite.h b/src/sprite.h index e9c6fd0..b2cca87 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -36,12 +36,13 @@ public: bool collidable = false; vec2i collisionOffset; vec2i collisionSize; + std::string interactionScript; // Animation bool isAnimated = false; int textureId; Direction dir = Direction::down; - std::string animationName; + std::string animationName = "still"; int animationId = 0; int animationFrame = 0; std::vector frames; -- cgit 1.4.1