/* * 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 "Skins/SnakeSkin.h" #include #include #include namespace Snake { class Game : public GUI::Frame , public Config::Listener { C_OBJECT_ABSTRACT(Game); public: static ErrorOr> try_create(); virtual ~Game() override = default; bool is_paused() const { return !has_timer(); } void start(); void pause(); void reset(); Function on_score_update; void set_skin_color(Color); void set_skin_name(DeprecatedString); void set_skin(NonnullOwnPtr skin); private: explicit Game(Vector> food_bitmaps, Color snake_color, DeprecatedString snake_skin_name, NonnullOwnPtr skin); virtual void paint_event(GUI::PaintEvent&) override; virtual void keydown_event(GUI::KeyEvent&) override; virtual void timer_event(Core::TimerEvent&) override; virtual void config_string_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value) override; void config_u32_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, u32 value) override; void game_over(); void spawn_fruit(); bool is_available(Coordinate const&); void queue_velocity(int v, int h); Velocity const& last_velocity() const; Gfx::IntRect cell_rect(Coordinate const&) const; Direction direction_to_position(Coordinate const& from, Coordinate const& to) const; int m_rows { 20 }; int m_columns { 20 }; Velocity m_velocity { 0, 1 }; Velocity m_last_velocity { 0, 1 }; CircularQueue m_velocity_queue; Coordinate m_head; Vector m_tail; Coordinate m_fruit; int m_fruit_type { 0 }; size_t m_length { 0 }; unsigned m_score { 0 }; bool m_is_new_high_score { false }; Vector> m_food_bitmaps; Color m_snake_color; DeprecatedString m_snake_skin_name; NonnullOwnPtr m_snake_skin; }; }