summaryrefslogtreecommitdiffhomepage
path: root/src/hole.hpp
blob: 25db98869d20d204477e25602896ad4372fc5399 (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
#pragma once
#include "object.hpp"
#include <array>
#include <Magnum/DimensionTraits.h>

namespace floormat {

struct hole_proto final : object_proto
{
    ~hole_proto() noexcept override;
    hole_proto();
    hole_proto(const hole_proto&);
    hole_proto& operator=(const hole_proto&);
    hole_proto(hole_proto&&) noexcept;
    hole_proto& operator=(hole_proto&&) noexcept;
    bool operator==(const object_proto& proto) const override;

    struct flags
    {
        bool operator==(const flags&) const;

        bool on_render  : 1 = true;
        bool on_physics : 1 = true;
        bool enabled    : 1 = true;
        bool is_wall    : 1 = false;
    };

    uint8_t height = 0, z_offset = tile_size_z/2;
    struct flags flags;
};

struct hole final : object
{
    const uint8_t height = 0, z_offset = tile_size_z/2;
    const struct hole_proto::flags flags;

    hole(object_id id, class chunk& c, const hole_proto& proto);
    ~hole() noexcept override;

    Vector2 ordinal_offset(Vector2b offset) const override;
    float depth_offset() const override;
    object_type type() const noexcept override;
    void update(const std::shared_ptr<object>& ptr, size_t& i, const Ns& dt) override;
    bool is_dynamic() const override;
    bool updates_passability() const override;
    bool is_virtual() const override;

    void set_height(uint8_t height);
    void set_z_offset(uint8_t z);
    void set_enabled(bool on_render, bool on_physics, bool on_both);

    explicit operator hole_proto() const;

    friend class world;

private:
    void maybe_mark_neighbor_chunks_modified() override;
};

template<typename T>
struct CutResult
{
    using Vec2 = VectorTypeFor<2, T>;
    struct bbox { Vec2 position; Vector2ub bbox_size; };
    struct rect { Vec2 min, max; };

    static CutResult cut(bbox input, bbox hole);
    static CutResult cut(Vec2 r0, Vec2 r1, Vec2 h0, Vec2 h1);

    std::array<rect, 8> array;
    uint8_t size = 0;
    bool found = false;
};

} // namespace floormat