summaryrefslogtreecommitdiffhomepage
path: root/src/entity.hpp
blob: 2ede577b5316a6f7d4e066d8d00fd2f2785279b1 (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
#pragma once
#include "compat/defs.hpp"
#include "src/global-coords.hpp"
#include "src/rotation.hpp"
#include "src/pass-mode.hpp"
#include "src/entity-type.hpp"
#include "src/object-id.hpp"
#include <memory>
#include <vector>

namespace floormat {

template<typename T> struct entity_type_;
struct anim_atlas;
struct world;
struct chunk;

struct entity_proto
{
    std::shared_ptr<anim_atlas> atlas;
    Vector2b offset, bbox_offset;
    Vector2ub bbox_size = Vector2ub(iTILE_SIZE2);
    uint16_t delta = 0, frame = 0;
    entity_type type : 3              = entity_type::none;
    rotation r       : rotation_BITS  = rotation::N;
    pass_mode pass   : pass_mode_BITS = pass_mode::see_through;

    float ordinal(local_coords coord) const;
    entity_proto& operator=(const entity_proto&);
    entity_proto();
    entity_proto(const entity_proto&);

    virtual bool operator==(const entity_proto&) const;
    virtual ~entity_proto() noexcept;
};

struct entity
{
    fm_DECLARE_DELETED_COPY_ASSIGNMENT(entity);

    const object_id id = 0;
    struct chunk* const c;
    std::shared_ptr<anim_atlas> atlas;
    global_coords coord;
    const Vector2b offset, bbox_offset;
    const Vector2ub bbox_size;
    uint16_t delta = 0, frame = 0;
    const entity_type type;
    const rotation r = rotation::N;
    const pass_mode pass = pass_mode::see_through;

    virtual ~entity() noexcept;

    static Vector2b ordinal_offset_for_type(entity_type type, Vector2b offset);
    float ordinal() const;
    static float ordinal(local_coords xy, Vector2b offset, entity_type type);
    struct chunk& chunk() const;
    size_t index() const;

    explicit operator entity_proto() const;

    virtual bool can_activate(size_t i) const;
    virtual bool activate(size_t i);
    virtual bool update(size_t i, float dt) = 0;
    virtual void rotate(size_t i, rotation r);

    static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta);
    [[nodiscard]] virtual bool can_move_to(Vector2i delta);
    size_t move(size_t i, Vector2i delta, rotation new_r);
    virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);
    bool is_dynamic() const;

    friend struct world;

protected:
    entity(object_id id, struct chunk& c, entity_type type, const entity_proto& proto) noexcept;
    void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);
};

} // namespace floormat