summaryrefslogtreecommitdiffstats
path: root/src/game/shared/world/item.hh
blob: bcec37a8b58fcd25acc0d22d277b17cfb4d23697 (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 "core/resource/resource.hh"

#include "shared/types.hh"

// This resource is only defined client-side and
// resource_ptr<TextureGUI> should remain set to null
// anywhere else in the shared and server code
struct TextureGUI;

class Voxel;

class Item {
public:
    Item(void) = default;
    explicit Item(const Item& source, item_id id) noexcept;

    constexpr std::string_view get_name(void) const noexcept;
    constexpr item_id get_id(void) const noexcept;

    constexpr std::string_view get_texture(void) const noexcept;
    constexpr const Voxel* get_place_voxel(void) const noexcept;

    constexpr resource_ptr<TextureGUI>& get_cached_texture(void) const noexcept;
    void set_cached_texture(resource_ptr<TextureGUI> texture) const noexcept;

    std::uint64_t get_checksum(std::uint64_t combine = 0U) const;

protected:
    std::string m_name;
    item_id m_id { NULL_ITEM_ID };

    std::string m_texture;
    const Voxel* m_place_voxel { nullptr };

    mutable resource_ptr<TextureGUI> m_cached_texture; // Client-side only
};

class ItemBuilder final : public Item {
public:
    explicit ItemBuilder(std::string_view name);

    void set_name(std::string_view name);

    void set_texture(std::string_view texture);
    void set_place_voxel(const Voxel* place_voxel);

    std::unique_ptr<Item> build(item_id id) const;
};

constexpr std::string_view Item::get_name(void) const noexcept
{
    return m_name;
}

constexpr item_id Item::get_id(void) const noexcept
{
    return m_id;
}

constexpr std::string_view Item::get_texture(void) const noexcept
{
    return m_texture;
}

constexpr const Voxel* Item::get_place_voxel(void) const noexcept
{
    return m_place_voxel;
}

constexpr resource_ptr<TextureGUI>& Item::get_cached_texture(void) const noexcept
{
    return m_cached_texture;
}