Skip to content

Commit

Permalink
Slight lift effect
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Jan 23, 2024
1 parent 5906ee9 commit 91609c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
37 changes: 24 additions & 13 deletions src/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Pipe::operator Type() const

Cell::Status Cell::update()
{
if (rotate_start) {
if (rotation()) {
const Uint64 diff = SDL_GetTicks64() - rotate_start;
if (diff > rotation_time) {
// rotation completed
Expand All @@ -152,21 +152,32 @@ Cell::Status Cell::update()
return Cell::Unchanged;
}

double Cell::angle() const
double Cell::phase() const
{
double angle = pipe.angle();
double phase = 1.0;

if (rotate_start) {
if (rotation()) {
const Uint64 diff = SDL_GetTicks64() - rotate_start;
if (diff < rotation_time) {
const float diff_angle = static_cast<float>(diff) /
static_cast<float>(rotation_time) * 90.0f;
angle = rotate_pipe.angle();
if (rotate_clockwise) {
angle += diff_angle;
} else {
angle -= diff_angle;
}
phase =
static_cast<double>(diff) / static_cast<double>(rotation_time);
}
}

return phase;
}

double Cell::angle() const
{
double angle = pipe.angle();

if (rotation()) {
const float diff_angle = phase() * 90.0f;
angle = rotate_pipe.angle();
if (rotate_clockwise) {
angle += diff_angle;
} else {
angle -= diff_angle;
}
}

Expand All @@ -175,7 +186,7 @@ double Cell::angle() const

void Cell::rotate(bool clockwise)
{
if (rotate_start) {
if (rotation()) {
// rotation in progress
if (rotate_clockwise == clockwise) {
rotate_twice = true;
Expand Down
12 changes: 12 additions & 0 deletions src/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ struct Cell {
*/
Status update();

/**
* Check if rotation is in progress.
* @return true if rotation in progress
*/
inline bool rotation() const { return rotate_start != 0; }

/**
* Get pipe rotation phase [0.0, 1.0].
* @return pipe rotation phase
*/
double phase() const;

/**
* Get pipe angle.
* @return pipe angle
Expand Down
12 changes: 12 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "buildcfg.h"

#include <cmath>

struct LevelSize {
size_t size;
const char* name;
Expand Down Expand Up @@ -194,6 +196,11 @@ void Game::draw_puzzle()
default:
continue;
}
if (cell.rotation()) {
const double phase = cell.phase();
dst.x += shadow_shift * sin(M_PI * phase);
dst.y += shadow_shift * sin(M_PI * phase);
}
render.draw(tid, dst, cell.angle(), 0.3);
}
}
Expand Down Expand Up @@ -224,6 +231,11 @@ void Game::draw_puzzle()
default:
continue;
}
if (cell.rotation()) {
const double phase = cell.phase();
dst.x -= shadow_shift * sin(M_PI * phase);
dst.y -= shadow_shift * sin(M_PI * phase);
}
render.draw(tid, dst, cell.angle());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void Level::update()
}

// trace from sender
if (get_cell(sender).rotate_start == 0) {
if (!get_cell(sender).rotation()) {
trace_state(sender);
}

Expand Down Expand Up @@ -261,7 +261,7 @@ void Level::trace_state(const Position& pos)
if (cell.pipe.get(side)) {
const Position next_pos = neighbor(pos, side);
Cell& next_cell = get_cell(next_pos);
if (next_cell.rotate_start == 0 && !next_cell.active &&
if (!next_cell.rotation() && !next_cell.active &&
next_cell.pipe.get(side.opposite())) {
next_cell.active = true;
trace_state(next_pos);
Expand Down

0 comments on commit 91609c7

Please sign in to comment.