aboutsummaryrefslogtreecommitdiff
path: root/src/games/snake/Snake.hpp
blob: 81fd30f7bb99fd7dddef7c162b3d792b0ab60f5a (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
#pragma once

#include "games/Game.hpp"
#include "common/math.hpp"

#include <random>


class Snake : public Game {
public:
    enum Direction : int32_t {
        up,
        down,
        left,
        right,
    };


public:
    Snake();


private:
    void Start() override;
    void ProcessEvent(SDL_Event& event) override;
    void FinishUpdate(float dt) override;
    void Draw() override;

    void MaybeMoveSnake(float dt_in_seconds);
    void SpawnFood();



private:
    static constexpr int32_t max_map_width = 16;
    static constexpr int32_t max_map_height = 16;
    static constexpr float tiles_per_second = 4.0f;

    static std::mt19937 s_rng;
    std::uniform_int_distribution<int32_t> m_dist;

    int32_t m_starting_map_width = 16;
    int32_t m_starting_map_height = 16;

    Direction m_direction;
    Direction m_last_advanced_direction;

    int32_t m_map_width;
    int32_t m_map_height;
    int32_t m_tail {0};
    int32_t m_head {1};
    uint64_t m_body_bitmap[max_map_height];
    V2I32 m_body_positions[max_map_width * max_map_height];
    V2I32 m_food_position;
};