diff --git a/Userland/Games/Snake/Game.cpp b/Userland/Games/Snake/Game.cpp index 782b371983..5442c8c4d9 100644 --- a/Userland/Games/Snake/Game.cpp +++ b/Userland/Games/Snake/Game.cpp @@ -290,7 +290,7 @@ void Game::queue_velocity(int v, int h) m_velocity_queue.enqueue({ v, h }); } -Game::Velocity const& Game::last_velocity() const +Velocity const& Game::last_velocity() const { if (!m_velocity_queue.is_empty()) return m_velocity_queue.last(); diff --git a/Userland/Games/Snake/Game.h b/Userland/Games/Snake/Game.h index a58ecf7b25..da3a57427f 100644 --- a/Userland/Games/Snake/Game.h +++ b/Userland/Games/Snake/Game.h @@ -1,12 +1,14 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include "Geometry.h" #include #include @@ -36,21 +38,6 @@ private: virtual void keydown_event(GUI::KeyEvent&) override; virtual void timer_event(Core::TimerEvent&) override; - struct Coordinate { - int row { 0 }; - int column { 0 }; - - bool operator==(Coordinate const& other) const - { - return row == other.row && column == other.column; - } - }; - - struct Velocity { - int vertical { 0 }; - int horizontal { 0 }; - }; - void game_over(); void spawn_fruit(); bool is_available(Coordinate const&); diff --git a/Userland/Games/Snake/Geometry.h b/Userland/Games/Snake/Geometry.h new file mode 100644 index 0000000000..3308cc9a2b --- /dev/null +++ b/Userland/Games/Snake/Geometry.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Snake { + +enum class Direction { + Up, + Right, + Down, + Left, +}; + +struct Coordinate { + int row { 0 }; + int column { 0 }; + + bool operator==(Coordinate const& other) const + { + return row == other.row && column == other.column; + } +}; + +struct Velocity { + int vertical { 0 }; + int horizontal { 0 }; + + Direction as_direction() const + { + if (vertical > 0) + return Direction::Down; + if (vertical < 0) + return Direction::Up; + if (horizontal > 0) + return Direction::Right; + if (horizontal < 0) + return Direction::Left; + + return Direction::Up; + } +}; + +}