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

namespace floormat {

class anim_atlas;
class world;
class chunk;
struct Ns;

struct object_proto
{
    std::shared_ptr<anim_atlas> atlas;
    Vector2b offset, bbox_offset;
    Vector2ub bbox_size = Vector2ub(tile_size_xy);
    uint32_t delta = 0;
    uint16_t frame = 0;
    object_type type : 3              = object_type::none;
    rotation r = rotation::N;
    pass_mode pass = pass_mode::see_through; // todo move to struct scenery, add inherit bit

    object_proto() noexcept;
    virtual ~object_proto() noexcept;
    virtual bool operator==(const object_proto&) const;
    bool operator!=(const object_proto& o) const noexcept { return !operator==(o); }
    object_type type_of() const noexcept;

    object_proto& operator=(const object_proto&) noexcept;
    object_proto(const object_proto&) noexcept;
    object_proto& operator=(object_proto&&) noexcept;
    object_proto(object_proto&&) noexcept;
};

struct object
{
    fm_DECLARE_DELETED_COPY_ASSIGNMENT(object);
    fm_DECLARE_DELETED_MOVE_ASSIGNMENT(object);

    const object_id id = 0;
    uint64_t last_frame_no = 0;
    class chunk* const c;
    const std::shared_ptr<anim_atlas> atlas;
    const global_coords coord;
    const std::weak_ptr<object> parent;
    const Vector2b offset, bbox_offset;
    const Vector2ub bbox_size;
    uint32_t delta = 0;
    uint16_t frame = 0;
    const rotation r = rotation::N; // todo remove bitfield?
    const pass_mode pass = pass_mode::see_through;
    bool ephemeral : 1 = false;
    bool gone      : 1 = false;
    //char _pad[4]; // got 4 bytes left

    virtual ~object() noexcept;

    virtual Vector2 ordinal_offset(Vector2b offset) const = 0;
    virtual float depth_offset() const = 0;
    float ordinal() const;
    float ordinal(local_coords xy, Vector2b offset, Vector2s z_offset) const;
    class chunk& chunk() const;
    class world& world() const;
    size_t index() const;
    virtual bool is_virtual() const;
    point position() const;

    explicit operator object_proto() const;

    virtual object_type type() const noexcept = 0;
    virtual bool can_activate(size_t i) const;
    virtual bool activate(size_t i);
    virtual void update(const std::shared_ptr<object>& self, size_t& i, const Ns& dt) = 0;
    void rotate(size_t i, rotation r);
    bool can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size);
    bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize);
    void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);

    object_type type_of() const noexcept;
    static point normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta);
    static point normalize_coords(const point& pt, Vector2i delta);

    virtual bool is_dynamic() const;
    virtual bool updates_passability() const;
    virtual void maybe_mark_neighbor_chunks_modified();

    bool can_rotate(rotation new_r);
    bool can_move_to(Vector2i delta);
    bool move_to(size_t& i, Vector2i delta, rotation new_r);
    bool move_to(Vector2i delta);
    void teleport_to(size_t& i, global_coords coord, Vector2b offset, rotation new_r);
    void teleport_to(size_t& i, point pt, rotation new_r);

    template<typename T>
    requires std::is_unsigned_v<T>
    static uint32_t alloc_frame_time(const Ns& dt, T& accum, uint32_t hz, float speed);

    virtual void init_script(const std::shared_ptr<object>& ptr);
    virtual void destroy_script_pre(const std::shared_ptr<object>& ptr, script_destroy_reason r);
    virtual void destroy_script_post();

protected:
    object(object_id id, class chunk& c, const object_proto& proto);
    void set_bbox_(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass);

    static void check_script_update_1(script_lifecycle state);
    [[nodiscard]] static bool check_script_update_2(script_lifecycle state);
};

} // namespace floormat