Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
Add jiggleObject
Browse files Browse the repository at this point in the history
This references #38
  • Loading branch information
scemino committed Feb 15, 2021
1 parent 361ea3e commit b76bdca
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/engge/Entities/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Entity : public ScriptObject, public ngf::Drawable {
void rotateTo(float destination, ngf::TimeSpan time, InterpolationMethod method);
void scaleTo(float destination, ngf::TimeSpan time, InterpolationMethod method);
void shake(float amount);
void jiggle(float amount);

void stopObjectMotors();

Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(SOURCES
Entities/BlinkState.cpp
Entities/Costume.cpp
Entities/LipAnimation.cpp
Entities/JiggleFunction.cpp
Entities/ShakeFunction.cpp
Entities/TalkingState.cpp
Entities/Entity.cpp
Expand Down
15 changes: 14 additions & 1 deletion src/Entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <engge/Audio/SoundTrigger.hpp>
#include <engge/Engine/Camera.hpp>
#include <engge/Engine/Trigger.hpp>
#include "JiggleFunction.hpp"
#include "ShakeFunction.hpp"
#include "TalkingState.hpp"

Expand All @@ -25,11 +26,12 @@ struct Entity::Impl {
std::optional<UseDirection> m_useDir;
glm::vec2 m_offset{0, 0};
glm::vec2 m_shakeOffset{0, 0};
float m_jiggleOffset{0.f};
bool m_isLit{false};
bool m_isVisible{true};
bool m_isTouchable{true};
glm::ivec2 m_renderOffset{0, 0};
Motor m_offsetTo, m_scaleTo, m_rotateTo, m_moveTo, m_alphaTo, m_shake;
Motor m_offsetTo, m_scaleTo, m_rotateTo, m_moveTo, m_alphaTo, m_shake, m_jiggle;
ngf::Color m_color{ngf::Colors::White};
bool m_objectBumperCycle{true};
ngf::Color m_talkColor;
Expand Down Expand Up @@ -75,6 +77,7 @@ void Entity::update(const ngf::TimeSpan &elapsed) {
update(m_pImpl->m_moveTo, elapsed);
update(m_pImpl->m_alphaTo, elapsed);
update(m_pImpl->m_shake, elapsed);
update(m_pImpl->m_jiggle, elapsed);
}

void Entity::update(Motor &motor, const ngf::TimeSpan &elapsed) {
Expand Down Expand Up @@ -167,6 +170,7 @@ float Entity::getScale() const {
ngf::Transform Entity::getTransform() const {
auto transform = m_pImpl->m_transform;
transform.move(getOffset());
transform.rotate(m_pImpl->m_jiggleOffset);
return transform;
}

Expand Down Expand Up @@ -249,6 +253,13 @@ void Entity::shake(float amount) {
m_pImpl->m_shake.isEnabled = true;
}

void Entity::jiggle(float amount) {
auto setJiggle = [this](const auto &offset) { m_pImpl->m_jiggleOffset = offset; };
auto jiggle = std::make_unique<JiggleFunction>(setJiggle, amount);
m_pImpl->m_jiggle.function = std::move(jiggle);
m_pImpl->m_jiggle.isEnabled = true;
}

void Entity::alphaTo(float destination, ngf::TimeSpan time, InterpolationMethod method) {
auto getAlpha = [this] { return m_pImpl->m_color.a; };
auto setAlpha = [this](const float &a) { m_pImpl->m_color.a = a; };
Expand Down Expand Up @@ -307,6 +318,8 @@ void Entity::stopObjectMotors() {
m_pImpl->m_rotateTo.isEnabled = false;
m_pImpl->m_moveTo.isEnabled = false;
m_pImpl->m_alphaTo.isEnabled = false;
m_pImpl->m_shake.isEnabled = false;
m_pImpl->m_jiggle.isEnabled = false;
for (auto &&child : m_pImpl->m_children) {
child->stopObjectMotors();
}
Expand Down
15 changes: 15 additions & 0 deletions src/Entities/JiggleFunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "JiggleFunction.hpp"

namespace ng {
JiggleFunction::JiggleFunction(std::function<void(float)> jiggle, float amount)
: m_jiggle(std::move(jiggle)), m_amount(amount) {}

JiggleFunction::~JiggleFunction() = default;

bool JiggleFunction::isElapsed() { return false; }

void JiggleFunction::operator()(const ngf::TimeSpan &elapsed) {
m_jiggleTime += 20.f * elapsed.getTotalSeconds();
m_jiggle(m_amount * sinf(m_jiggleTime));
}
}
21 changes: 21 additions & 0 deletions src/Entities/JiggleFunction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <functional>
#include <glm/vec2.hpp>
#include <ngf/System/TimeSpan.h>
#include <engge/Engine/Function.hpp>

namespace ng {
class JiggleFunction final : public Function {
public:
JiggleFunction(std::function<void(float)> jiggle, float amount);
~JiggleFunction() final;

bool isElapsed() final;
void operator()(const ngf::TimeSpan &elapsed) final;

private:
std::function<void(float)> m_jiggle;
float m_amount{0.f};
float m_jiggleTime{0.f};
};
}
2 changes: 1 addition & 1 deletion src/Entities/ShakeFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ng {
ShakeFunction::ShakeFunction(std::function<void(const glm::vec2 &)> shake, float amount)
: m_shake(std::move(shake)), m_amount(amount) {}

ShakeFunction::~ShakeFunction() = default;
ShakeFunction::~ShakeFunction() = default;

bool ShakeFunction::isElapsed() { return false; }

Expand Down
10 changes: 6 additions & 4 deletions src/Scripting/ObjectPack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ class ObjectPack final : public Pack {
}

static SQInteger jiggleObject(HSQUIRRELVM v) {
error("TODO: jiggleObject: not implemented");
Object *obj = EntityManager::getObject(v, 2);
if (sq_gettype(v, 2) == OT_NULL)
return 0;

auto *obj = EntityManager::getEntity(v, 2);
if (!obj) {
return sq_throwerror(v, _SC("failed to get object"));
return sq_throwerror(v, _SC("failed to get object/actor"));
}
SQFloat amount = 0;
if (SQ_FAILED(sq_getfloat(v, 3, &amount))) {
return sq_throwerror(v, _SC("failed to get amount"));
}
// obj->jiggle(amount);
obj->jiggle(amount);
return 0;
}

Expand Down

0 comments on commit b76bdca

Please sign in to comment.