summaryrefslogtreecommitdiffhomepage
path: root/editor/tests/walk-test.cpp
blob: 47cd753d643a3937e97524b0d47f61c5337e6b28 (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
#include "../tests-private.hpp"
#include "compat/shared-ptr-wrapper.hpp"
#include "editor/app.hpp"
#include "src/critter.hpp"
#include "floormat/main.hpp"
#include "../imgui-raii.hpp"

namespace floormat::tests {

using namespace floormat::imgui;

namespace {

struct pending_s
{
    point dest;
    bool has_value : 1 = false;
};

struct pf_test final : base_test
{
    pending_s current;

    ~pf_test() noexcept override = default;

    bool handle_key(app& a, const key_event& e, bool is_down) override;
    bool handle_mouse_click(app& a, const mouse_button_event& e, bool is_down) override;
    bool handle_mouse_move(app& a, const mouse_move_event& e) override;
    void draw_overlay(app& a) override;
    void draw_ui(app& a, float menu_bar_height) override;
    void update_pre(app& a, const Ns& dt) override;
    void update_post(app&, const Ns&) override {}
};

bool pf_test::handle_key(app& a, const key_event& e, bool is_down)
{
    (void) a; (void)e; (void)is_down;
    return false;
}

bool pf_test::handle_mouse_click(app& a, const mouse_button_event& e, bool is_down)
{
    if (e.button == mouse_button_left && is_down)
    {
        if (auto ptʹ = a.cursor_state().point())
        {
            current = {
                .dest = *ptʹ,
                .has_value = true,
            };
            return true;
        }
    }
    else if (e.button == mouse_button_right && is_down)
        current = {};
    return false;
}

bool pf_test::handle_mouse_move(floormat::app &a, const mouse_move_event& e)
{
    if (e.buttons & mouse_button_left)
        return handle_mouse_click(a, {e.position, e.mods, mouse_button_left, 1}, true);
    else
        return false;
}

void pf_test::draw_overlay(app& a)
{
    (void)a;
}

void pf_test::draw_ui(app& a, float)
{
    (void)a;
}

void pf_test::update_pre(app& a, const Ns& dt)
{
    if (!current.has_value)
        return;

    auto& m = a.main();
    auto& C = *a.ensure_player_character(m.world()).ptr;
    fm_assert(C.is_dynamic());

    if (C.movement.L | C.movement.R | C.movement.U | C.movement.D) [[unlikely]]
    {
        current.has_value = false;
        return;
    }

    auto index = C.index();
    critter::move_result result;

    if (current.dest == C.position() ||
        (void(result = C.move_toward(index, dt, current.dest)), result.blocked) ||
        result.moved && current.dest == C.position())
        current.has_value = false;
}

} // namespace

Pointer<base_test> tests_data::make_test_walk() { return Pointer<pf_test>{InPlaceInit}; }

} // namespace floormat::tests