TWP: Add inventory shake

This commit is contained in:
scemino 2024-04-28 21:28:50 +02:00
parent 0bb1cc2d67
commit 8b64e9a61a
2 changed files with 50 additions and 3 deletions

View file

@ -43,6 +43,24 @@ namespace Twp {
#define BACKWIDTH 137.f
#define BACKHEIGHT 75.f
class ShakeInventory : public Motor {
public:
ShakeInventory(Math::Vector2d &shakeOffset, float amount) : _shakeOffset(shakeOffset), _amount(amount) {}
protected:
void onUpdate(float elapsed) override {
_shakeTime += 40.f * elapsed;
_elapsed += elapsed;
_shakeOffset = Math::Vector2d(_amount * cos(_shakeTime + 0.3f), _amount * sin(_shakeTime));
}
private:
Math::Vector2d &_shakeOffset;
const float _amount;
float _shakeTime = 0.f;
float _elapsed = 0.f;
};
static float _getFps(float fps, float animFps) {
if (fps != 0.f)
return fps;
@ -579,6 +597,10 @@ Inventory::Inventory() : Node("Inventory") {
}
_arrowUpRect = Common::Rect(SCREEN_WIDTH / 2.f, ARROWHEIGHT + MARGINBOTTOM + BACKOFFSET, SCREEN_WIDTH / 2.f + ARROWWIDTH, ARROWHEIGHT + MARGINBOTTOM + BACKOFFSET + ARROWHEIGHT);
_arrowDnRect = Common::Rect(SCREEN_WIDTH / 2.f, MARGINBOTTOM, SCREEN_WIDTH / 2.f + ARROWWIDTH, MARGINBOTTOM + ARROWHEIGHT);
for (int i = 0; i < NUMOBJECTS; i++) {
_shakeTime[i] = 0.f;
_inventoryOver[i] = false;
}
}
Math::Vector2d Inventory::getPos(Common::SharedPtr<Object> inv) const {
@ -661,6 +683,8 @@ void Inventory::drawItems(const Math::Matrix4 &trsf) {
}
float s = obj->getScale();
Twp::scale(t, Math::Vector2d(s, s));
Math::Vector2d shakeOffset = _shakeOffset[i];
t.translate(Math::Vector3d(shakeOffset.getX(), shakeOffset.getY(), 0.f));
drawSprite(*itemFrame, texture, Color::withAlpha(Color(), getAlpha()), t);
}
}
@ -714,9 +738,26 @@ void Inventory::update(float elapsed, Common::SharedPtr<Object> actor, const Col
const Common::Rect &item = _itemRects[i];
if (item.contains(scrPos.getX(), scrPos.getY())) {
size_t index = _actor->_inventoryOffset * NUMOBJECTSBYROW + i;
if (index < _actor->_inventory.size())
_obj = _actor->_inventory[index];
break;
if (index < _actor->_inventory.size()) {
_obj = _actor->_inventory[i];
if (!_inventoryOver[i] && (_shakeTime[i] < 0.1f)) {
_shakeTime[i] = 0.25f;
_shake[i] = Common::ScopedPtr<Motor>(new ShakeInventory(_shakeOffset[i], 0.4f));
_inventoryOver[i] = true;
}
}
} else {
_inventoryOver[i] = false;
}
// shake choice when cursor is over
if ((_shakeTime[i] > 0.0f) && _shake[i]) {
_shake[i]->update(elapsed);
_shakeTime[i] -= elapsed;
if (_shakeTime[i] < 0.f) {
_shakeTime[i] = 0.f;
}
}
}

View file

@ -36,6 +36,8 @@
namespace Twp {
class Motor;
// Represents a node in a scene graph.
class Node {
public:
@ -318,6 +320,10 @@ private:
bool _down = false;
Common::SharedPtr<Object> _obj;
Common::Rect _itemRects[NUMOBJECTS];
Common::ScopedPtr<Motor> _shake[NUMOBJECTS];
bool _inventoryOver[NUMOBJECTS];
Math::Vector2d _shakeOffset[NUMOBJECTS];
float _shakeTime[NUMOBJECTS];
Common::Rect _arrowUpRect;
Common::Rect _arrowDnRect;
float _jiggleTime = 0.f;