summaryrefslogtreecommitdiff
path: root/Userland/Games/FlappyBug/Game.h
blob: 0b285462f116d793c2b07498ad93713e2c0ecd3a (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
155
/*
 * Copyright (c) 2021, Mim Hufford <mim@hotmail.co.uk>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Optional.h>
#include <AK/Random.h>
#include <AK/Vector.h>
#include <LibGUI/Application.h>
#include <LibGUI/Frame.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/StandardCursor.h>

namespace FlappyBug {

class Game final : public GUI::Frame {
    C_OBJECT(Game);

public:
    static const int game_width = 560;
    static const int game_height = 480;

    Function<u32(u32)> on_game_end;

private:
    Game();

    virtual void paint_event(GUI::PaintEvent&) override;
    virtual void keydown_event(GUI::KeyEvent&) override;
    virtual void timer_event(Core::TimerEvent&) override;

    void tick();
    void reset();
    void game_over();
    bool ready_to_start() const;

    struct Bug {
        const float x { 50 };
        const float radius { 16 };
        const float starting_y { 200 };
        const RefPtr<Gfx::Bitmap> falling_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/falling.png") };
        const RefPtr<Gfx::Bitmap> flapping_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/flapping.png") };
        float y {};
        float velocity {};

        void reset()
        {
            y = starting_y;
        }

        RefPtr<Gfx::Bitmap> current_bitmap() const
        {
            return velocity < 0 ? falling_bitmap : flapping_bitmap;
        }

        Gfx::FloatRect rect() const
        {
            return { x - radius, y - radius, radius * 2, radius * 2 };
        }

        void flap()
        {
            const float flap_strength = 15.0f;
            velocity = -flap_strength;
        }

        void fall()
        {
            const float gravity = 1.0f;
            velocity += gravity;
        }

        void apply_velocity()
        {
            y += velocity;
        }
    };

    struct Obstacle {
        const float width { 20 };
        Color color { Color::DarkGray };
        float x {};
        float gap_top_y { 200 };
        float gap_height { 175 };

        void reset()
        {
            x = game_width + width;
            gap_top_y = get_random_uniform(game_height - gap_height);
        }

        Gfx::FloatRect top_rect() const
        {
            return { x - width, 0, width, gap_top_y };
        }

        Gfx::FloatRect bottom_rect() const
        {
            return { x - width, gap_top_y + gap_height, width, game_height - gap_top_y - gap_height };
        }
    };

    struct Cloud {
        const Vector<RefPtr<Gfx::Bitmap>> cloud_bitmaps {
            Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_0.png"),
            Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_1.png"),
            Gfx::Bitmap::load_from_file("/res/icons/flappybug/cloud_2.png"),
        };
        float x {};
        float y {};
        int bitmap_id {};

        Cloud()
        {
            reset();
            x = get_random_uniform(game_width);
        }

        void reset()
        {
            bitmap_id = get_random_uniform(cloud_bitmaps.size());
            x = game_width + bitmap()->width();
            y = get_random_uniform(game_height / 2) + bitmap()->height();
        }

        RefPtr<Gfx::Bitmap> bitmap() const
        {
            return cloud_bitmaps[bitmap_id];
        }

        Gfx::IntRect rect() const
        {
            return { (int)x - bitmap()->width(), (int)y - bitmap()->height(), bitmap()->width(), bitmap()->height() };
        }
    };

    Bug m_bug;
    Obstacle m_obstacle;
    Cloud m_cloud;
    bool m_active;
    Optional<float> m_high_score {};
    float m_last_score {};
    float m_difficulty {};
    float m_restart_cooldown {};
    const RefPtr<Gfx::Bitmap> m_background_bitmap { Gfx::Bitmap::load_from_file("/res/icons/flappybug/background.png") };
    const Gfx::IntRect m_score_rect { 10, 10, 20, 20 };
    const Gfx::IntRect m_text_rect { game_width / 2 - 80, game_height / 2 - 40, 160, 80 };
};

}