tiny_link/src/components/collider.h

55 lines
1.1 KiB
C
Raw Normal View History

2021-01-03 08:20:01 +08:00
#pragma once
#include <blah.h>
#include <memory>
#include "../world.h"
using namespace Blah;
namespace TL
{
class Collider : public Component
{
public:
enum class Shape
{
None,
Rect,
Grid
};
uint32_t mask;
Collider();
static Collider make_rect(const RectI& rect);
static Collider make_grid(int tile_size, int columns, int rows);
Shape shape() const;
RectI get_rect() const;
void set_rect(const RectI& value);
bool get_cell(int x, int y) const;
void set_cell(int x, int y, bool value);
2021-01-03 09:50:25 +08:00
void set_cells(int x, int y, int w, int h, bool value);
2021-01-03 08:20:01 +08:00
bool check(uint32_t mask, Point offset = Point::zero) const;
bool overlaps(const Collider* other, Point offset = Point::zero) const;
void render(Batch& batch) override;
private:
struct Grid
{
int columns;
int rows;
int tile_size;
std::shared_ptr<bool[]> cells;
};
Shape m_shape = Shape::None;
RectI m_rect;
Grid m_grid;
static bool rect_to_rect(const Collider* a, const Collider* b, Point offset);
static bool rect_to_grid(const Collider* a, const Collider* b, Point offset);
};
}