summaryrefslogtreecommitdiff
path: root/Userland/Games/Pong/Game.cpp
blob: 53aad289cbebb768d45b4439ade11ff65469bd02 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Game.h"
#include <AK/Random.h>

namespace Pong {

Game::Game()
{
    start_timer(16);
    reset();
}

Game::~Game()
{
}

void Game::reset_paddles()
{
    if (m_cursor_paddle_target_y.has_value())
        update(cursor_paddle_target_rect());
    m_cursor_paddle_target_y.clear();

    update(enclosing_int_rect(m_player1_paddle.rect));
    m_player1_paddle.moving_up = m_up_key_held;
    m_player1_paddle.moving_down = m_down_key_held;
    m_player1_paddle.rect = { game_width - 12, game_height / 2 - 40, m_player1_paddle.width, m_player1_paddle.height };
    update(enclosing_int_rect(m_player1_paddle.rect));

    update(enclosing_int_rect(m_player2_paddle.rect));
    m_player2_paddle.moving_up = false;
    m_player2_paddle.moving_down = false;
    m_player2_paddle.rect = { 4, game_height / 2 - 40, m_player2_paddle.width, m_player2_paddle.height };
    update(enclosing_int_rect(m_player2_paddle.rect));
}

void Game::reset()
{
    reset_ball(1);
    reset_paddles();
}

void Game::timer_event(Core::TimerEvent&)
{
    tick();
}

void Game::paint_event(GUI::PaintEvent& event)
{
    GUI::Painter painter(*this);
    painter.add_clip_rect(event.rect());

    painter.fill_rect(rect(), Color::Black);
    painter.fill_rect(enclosing_int_rect(m_net.rect()), m_net.color);

    painter.fill_ellipse(enclosing_int_rect(m_ball.rect()), Color::Red);

    painter.fill_rect(enclosing_int_rect(m_player1_paddle.rect), m_player1_paddle.color);
    painter.fill_rect(enclosing_int_rect(m_player2_paddle.rect), m_player2_paddle.color);

    if (m_cursor_paddle_target_y.has_value())
        painter.fill_ellipse(cursor_paddle_target_rect(), Color::Blue);

    painter.draw_text(player_1_score_rect(), String::formatted("{}", m_player_1_score), Gfx::TextAlignment::TopLeft, Color::White);
    painter.draw_text(player_2_score_rect(), String::formatted("{}", m_player_2_score), Gfx::TextAlignment::TopLeft, Color::White);
}

void Game::keyup_event(GUI::KeyEvent& event)
{
    switch (event.key()) {
    case Key_Up:
        m_up_key_held = false;
        m_player1_paddle.moving_up = false;
        break;
    case Key_Down:
        m_down_key_held = false;
        m_player1_paddle.moving_down = false;
        break;
    default:
        break;
    }
}

void Game::keydown_event(GUI::KeyEvent& event)
{
    switch (event.key()) {
    case Key_Escape:
        GUI::Application::the()->quit();
        break;
    case Key_Up:
        m_up_key_held = true;
        m_player1_paddle.moving_up = true;
        m_player1_paddle.moving_down = false;
        m_cursor_paddle_target_y.clear();
        break;
    case Key_Down:
        m_down_key_held = true;
        m_player1_paddle.moving_up = false;
        m_player1_paddle.moving_down = true;
        m_cursor_paddle_target_y.clear();
        break;
    default:
        break;
    }
}

void Game::track_mouse_move(Gfx::IntPoint const& point)
{
    if (m_up_key_held || m_down_key_held) {
        // We're using the keyboard to move the paddle, the cursor is doing something else
        return;
    }

    if (m_cursor_paddle_target_y.has_value())
        update(cursor_paddle_target_rect());

    auto relative_point = point - window()->position();
    m_cursor_paddle_target_y = clamp(relative_point.y() - m_player1_paddle.rect.height() / 2, 0.f, game_height - m_player1_paddle.rect.height());
    if (m_player1_paddle.rect.y() > *m_cursor_paddle_target_y) {
        m_player1_paddle.moving_up = true;
        m_player1_paddle.moving_down = false;
    } else if (m_player1_paddle.rect.y() < *m_cursor_paddle_target_y) {
        m_player1_paddle.moving_up = false;
        m_player1_paddle.moving_down = true;
    }
    update(cursor_paddle_target_rect());
}

void Game::reset_ball(int serve_to_player)
{
    int position_y_min = (game_width / 2) - 50;
    int position_y_max = (game_width / 2) + 50;
    int position_y = get_random<u32>() % (position_y_max - position_y_min + 1) + position_y_min;
    int position_x = (game_height / 2);
    int velocity_y = get_random<u32>() % 3 + 1;
    int velocity_x = 4 + (5 - velocity_y);
    if (get_random<u32>() % 2)
        velocity_y = velocity_y * -1;
    if (serve_to_player == 2)
        velocity_x = velocity_x * -1;

    m_ball = {};
    m_ball.position = { position_x, position_y };
    m_ball.velocity = { velocity_x, velocity_y };
}

void Game::game_over(int winner)
{
    GUI::MessageBox::show(window(), String::formatted("Player {} wins!", winner), "Pong", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OK);
}

void Game::round_over(int winner)
{
    stop_timer();
    if (winner == 1) {
        update(player_1_score_rect());
        m_player_1_score++;
        update(player_1_score_rect());
    }

    if (winner == 2) {
        update(player_2_score_rect());
        m_player_2_score++;
        update(player_2_score_rect());
    }

    if (m_player_1_score == m_score_to_win || m_player_2_score == m_score_to_win) {
        game_over(winner);
        return;
    }

    reset_ball(winner);
    reset_paddles();
    start_timer(16);
}

void Game::calculate_move()
{
    int player_2_paddle_top = m_player2_paddle.rect.top();
    int player_2_paddle_bottom = m_player2_paddle.rect.bottom();

    if (m_ball.velocity.x() > 0 || m_ball.x() > game_width / 2) {
        // The ball is in the opponent's court, relax.
        m_player2_paddle.moving_up = false;
        m_player2_paddle.moving_down = false;
        return;
    }

    int ball_position = m_ball.y() + m_ball.radius;

    // AI paddle begins moving when the ball crosses the begin_trigger,
    // but stops only if it crosses the end_trigger. end_trigger forces
    // overcorrection, so that the paddle moves more smoothly.
    int begin_trigger = m_player2_paddle.rect.height() / 4;
    int end_trigger = m_player2_paddle.rect.height() / 2;

    if (m_player2_paddle.moving_up) {
        if (player_2_paddle_top + end_trigger < ball_position)
            m_player2_paddle.moving_up = false;
    } else {
        if (player_2_paddle_top + begin_trigger > ball_position)
            m_player2_paddle.moving_up = true;
    }

    if (m_player2_paddle.moving_down) {
        if (player_2_paddle_bottom - end_trigger > ball_position)
            m_player2_paddle.moving_down = false;
    } else {
        if (player_2_paddle_bottom - begin_trigger < ball_position)
            m_player2_paddle.moving_down = true;
    }
}

void Game::tick()
{
    auto new_ball = m_ball;
    new_ball.position += new_ball.velocity;

    update(enclosing_int_rect(m_ball.rect()));

    if (new_ball.y() < new_ball.radius || new_ball.y() > game_height - new_ball.radius) {
        new_ball.position.set_y(m_ball.y());
        new_ball.velocity.set_y(new_ball.velocity.y() * -1);
    }

    if (new_ball.x() < new_ball.radius) {
        round_over(1);
        return;
    }

    if (new_ball.x() > (game_width - new_ball.radius)) {
        round_over(2);
        return;
    }

    update(enclosing_int_rect(new_ball.rect()));

    if (new_ball.rect().intersects(m_player1_paddle.rect)) {
        new_ball.position.set_x(m_ball.x());
        new_ball.velocity.set_x(new_ball.velocity.x() * -1);

        float distance_to_middle_of_paddle = new_ball.y() - m_player1_paddle.rect.center().y();
        float relative_impact_point = distance_to_middle_of_paddle / m_player1_paddle.rect.height();
        new_ball.velocity.set_y(relative_impact_point * 7);
    }

    if (new_ball.rect().intersects(m_player2_paddle.rect)) {
        new_ball.position.set_x(m_ball.x());
        new_ball.velocity.set_x(new_ball.velocity.x() * -1);

        float distance_to_middle_of_paddle = new_ball.y() - m_player2_paddle.rect.center().y();
        float relative_impact_point = distance_to_middle_of_paddle / m_player2_paddle.rect.height();
        new_ball.velocity.set_y(relative_impact_point * 7);
    }

    if (m_player1_paddle.moving_up) {
        update(enclosing_int_rect(m_player1_paddle.rect));
        m_player1_paddle.rect.set_y(max(0.0f, m_player1_paddle.rect.y() - m_player1_paddle.speed));
        if (m_cursor_paddle_target_y.has_value() && m_player1_paddle.rect.y() <= *m_cursor_paddle_target_y) {
            m_cursor_paddle_target_y.clear();
            m_player1_paddle.moving_up = false;
        }
        update(enclosing_int_rect(m_player1_paddle.rect));
    }
    if (m_player1_paddle.moving_down) {
        update(enclosing_int_rect(m_player1_paddle.rect));
        m_player1_paddle.rect.set_y(min(game_height - m_player1_paddle.rect.height(), m_player1_paddle.rect.y() + m_player1_paddle.speed));
        if (m_cursor_paddle_target_y.has_value() && m_player1_paddle.rect.y() >= *m_cursor_paddle_target_y) {
            m_cursor_paddle_target_y.clear();
            m_player1_paddle.moving_down = false;
        }
        update(enclosing_int_rect(m_player1_paddle.rect));
    }

    calculate_move();

    if (m_player2_paddle.moving_up) {
        update(enclosing_int_rect(m_player2_paddle.rect));
        m_player2_paddle.rect.set_y(max(0.0f, m_player2_paddle.rect.y() - m_player2_paddle.speed));
        update(enclosing_int_rect(m_player2_paddle.rect));
    }
    if (m_player2_paddle.moving_down) {
        update(enclosing_int_rect(m_player2_paddle.rect));
        m_player2_paddle.rect.set_y(min(game_height - m_player2_paddle.rect.height(), m_player2_paddle.rect.y() + m_player2_paddle.speed));
        update(enclosing_int_rect(m_player2_paddle.rect));
    }

    m_ball = new_ball;
}

}