summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Components/CCustomWall.hpp
blob: 58a5cd4eaeff0294faf132121331c6c8a021fcfa (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#pragma once

#include "SSVOpenHexagon/Components/CCustomWallHandle.hpp"

#include "SSVOpenHexagon/Utils/PointInPolygon.hpp"
#include "SSVOpenHexagon/Utils/FastVertexVector.hpp"

#include <SSVUtils/Core/Common/Frametime.hpp>

#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Color.hpp>

#include <array>
#include <bitset>
#include <cstdint>
#include <utility>

namespace hg {

class CCustomWall
{
public:
    using Handle = int;

private:
    std::array<sf::Vector2f, 4> _vertexPositions;
    std::array<sf::Vector2f, 4> _oldVertexPositions;
    std::array<sf::Color, 4> _vertexColors;
    std::uint8_t _killingSide{0u};

    enum CWFlags : unsigned int
    {
        NoCollision,
        Deadly,

        CWFlagsCount
    };

    std::bitset<CWFlags::CWFlagsCount> _flags; // Default: collides, not deadly

public:
    [[gnu::always_inline]] void reset()
    {
        _killingSide = 0u;
        _flags.reset();
    }

    [[gnu::always_inline]] void draw(Utils::FastVertexVectorTris& wallQuads)
    {
        wallQuads.unsafe_emplace_back_quad(        //
            _vertexPositions[0], _vertexColors[0], //
            _vertexPositions[1], _vertexColors[1], //
            _vertexPositions[2], _vertexColors[2], //
            _vertexPositions[3], _vertexColors[3]);
    }

    [[nodiscard, gnu::always_inline]] bool isOverlapping(
        const sf::Vector2f& point) const noexcept
    {
        return Utils::pointInPolygon(_vertexPositions, point.x, point.y);
    }

    [[gnu::always_inline]] void setVertexPos(
        const int vertexIndex, const sf::Vector2f& pos) noexcept
    {
        _oldVertexPositions[vertexIndex] =
            std::exchange(_vertexPositions[vertexIndex], pos);
    }

    [[gnu::always_inline]] void moveVertexPos(
        const int vertexIndex, const sf::Vector2f& offset) noexcept
    {
        _oldVertexPositions[vertexIndex] = _vertexPositions[vertexIndex];
        _vertexPositions[vertexIndex] += offset;
    }

    [[gnu::always_inline]] void moveVertexPos4Same(
        const sf::Vector2f& offset) noexcept
    {
        _oldVertexPositions = _vertexPositions;

        for(sf::Vector2f& v : _vertexPositions)
        {
            v += offset;
        }
    }

    [[gnu::always_inline]] void setVertexColor(
        const int vertexIndex, const sf::Color& color) noexcept
    {
        _vertexColors[vertexIndex] = color;
    }

    [[gnu::always_inline]] void setCanCollide(const bool collide) noexcept
    {
        _flags[CWFlags::NoCollision] = !collide;
    }

    [[gnu::always_inline]] void setDeadly(const bool deadly) noexcept
    {
        _flags[CWFlags::Deadly] = deadly;
    }

    [[nodiscard, gnu::always_inline]] const sf::Vector2f& getVertexPos(
        const int vertexIndex) const noexcept
    {
        return _vertexPositions[vertexIndex];
    }

    [[nodiscard, gnu::always_inline]] const std::array<sf::Vector2f, 4>&
    getVertexPositions() const noexcept
    {
        return _vertexPositions;
    }

    [[nodiscard, gnu::always_inline]] const std::array<sf::Vector2f, 4>&
    getOldVertexPositions() const noexcept
    {
        return _oldVertexPositions;
    }

    [[nodiscard, gnu::always_inline]] bool getCanCollide() const noexcept
    {
        return !_flags[CWFlags::NoCollision];
    }

    [[nodiscard, gnu::always_inline]] bool getDeadly() const noexcept
    {
        return _flags[CWFlags::Deadly];
    }

    [[nodiscard, gnu::always_inline]] constexpr bool
    isCustomWall() const noexcept
    {
        return true;
    }

    [[gnu::always_inline]] void setKillingSide(const std::uint8_t side) noexcept
    {
        _killingSide = side;
    }

    [[nodiscard, gnu::always_inline]] std::uint8_t
    getKillingSide() const noexcept
    {
        return _killingSide;
    }
};

} // namespace hg