summaryrefslogtreecommitdiffhomepage
path: root/editor/character.cpp
blob: 27692fbfcd9832a451a56a69f7ede6a65658f1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "character.hpp"
#include "src/anim-atlas.hpp"
#include "loader/loader.hpp"
#include "src/world.hpp"
#include "src/RTree.hpp"
#include <cmath>

namespace floormat {

namespace {

template <typename T>
constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); }

constexpr int tile_size_1 = iTILE_SIZE2.sum()/2,
              framerate = 96, move_speed = tile_size_1 * 2;
constexpr float frame_time = 1.f/framerate;
constexpr auto inv_tile_size = 1 / TILE_SIZE2;
constexpr Vector2b bbox_size(12);

constexpr auto arrows_to_dir(bool L, bool R, bool U, bool D)
{
    if (L == R)
        L = R = false;
    if (U == D)
        U = D = false;

    using enum rotation;
    struct {
        int lr = 0, ud = 0;
        rotation r = N;
    } dir;

    if (L && U)
        dir = { -1,  0,  W };
    else if (L && D)
        dir = {  0,  1,  S };
    else if (R && U)
        dir = {  0, -1,  N };
    else if (R && D)
        dir = {  1,  0,  E };
    else if (L)
        dir = { -1,  1, SW };
    else if (D)
        dir = {  1,  1, SE };
    else if (R)
        dir = {  1, -1, NE };
    else if (U)
        dir = { -1, -1, NW };

    return dir;
}

} // namespace

character_wip::character_wip() :
    walk_anim{loader.anim_atlas("npc-walk", loader.ANIM_PATH)}
{
}

character_wip::~character_wip() = default;

int character_wip::allocate_frame_time(float dt)
{
    delta += dt;
    auto ret = (int)(delta*framerate);
    delta -= ret;
    delta = std::fmax(0.f, delta);
    return ret;
}

Vector2 character_wip::move_vec(int left_right, int top_bottom)
{
    constexpr auto c = move_speed * frame_time;
    return c * Vector2(sgn(left_right), sgn(top_bottom)).normalized();
}

void character_wip::tick(world& w, float dt, bool L, bool R, bool U, bool D)
{
    auto [lr, ud, rot] = arrows_to_dir(L, R, U, D);

    if (!lr & !ud)
    {
        delta = 0;
        return;
    }

    int nframes = allocate_frame_time(dt);

    if (!nframes)
        return;

    const auto vec = move_vec(lr, ud);
    r = rot;

    for (int i = 0; i < nframes; i++)
    {
        auto pos_ = pos;
        Vector2 offset_ = offset;
        offset_ += vec;
        auto pos_1 = Vector2i(offset_ * inv_tile_size);
        pos_ += pos_1;
        offset_ = Vector2(std::fmod(offset_[0], TILE_SIZE2[0]), std::fmod(offset_[1], TILE_SIZE2[1]));
        auto [c, t] = w[pos_];
        const auto& r = c.rtree();
        auto center = Vector2(pos_.local()) * TILE_SIZE2 + offset_;
        auto half = Vector2(bbox_size)*.5f;
        auto min = center - half;
        auto max = center + half;
        bool is_blocked = false;
        r->Search(min.data(), max.data(), [&](const std::uint64_t data, const auto&) {
            auto cdata = std::bit_cast<collision_data>(data);
            is_blocked |= (pass_mode)cdata.pass != pass_mode::pass;
            return !is_blocked;
        });
        if (is_blocked)
            break;
        pos = pos_;
        offset = offset_;
        ++frame %= walk_anim->info().nframes;
    }
}

} // namespace floormat