Player12
An open source football management simulator
Loading...
Searching...
No Matches
match_play_controller.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <SDL3/SDL.h>
12
13#include <array>
14#include <cstdint>
15
16#include "model/match_engine.h"
17
19enum class PlayAutoSwitch : std::uint8_t
20{
22 OFF,
27 AUTO
28};
29
36{
37 float lengthX = 1.0f;
38 float lengthY = 0.0f;
39 float widthX = 0.0f;
40 float widthY = 1.0f;
41};
42
55{
56 public:
58 struct Options
59 {
62 std::uint8_t passAssist = 1;
64 float deadZone = 0.2f;
65 };
66
68 static constexpr float FULL_POWER_SECONDS = 1.0f;
69
74
76 void begin(MatchEngine& engine, bool homeTeam, const Options& options);
78 void end(MatchEngine& engine);
79 [[nodiscard]] bool isActive() const { return active; }
80 [[nodiscard]] bool controlsHome() const { return home; }
81 void setOptions(const Options& changed) { settings = changed; }
82 [[nodiscard]] const Options& options() const { return settings; }
83
89 bool handleEvent(const SDL_Event& event);
91 void clearPresses();
93 bool takePauseRequest();
94
100 void update(MatchEngine& engine, float simulatedSeconds,
101 const PlayScreenBasis& basis);
102
104 [[nodiscard]] PlayerID nextSwitch() const { return next_switch; }
106 [[nodiscard]] float chargeShare(const MatchEngine& engine) const;
107 [[nodiscard]] bool isCharging() const
108 {
110 }
112 [[nodiscard]] bool hasGamepad() const { return gamepad != nullptr; }
114 [[nodiscard]] const char* gamepadName() const;
116 [[nodiscard]] const MatchPlayerInput& lastInput() const { return submitted; }
117
118 private:
136
139 static Control controlForKey(const SDL_KeyboardEvent& key);
140 static Control controlForButton(int button);
141 void press(Control control);
142 void release(Control control);
143 [[nodiscard]] bool held(Control control) const;
144 void openGamepad(SDL_JoystickID which);
145 void openFirstGamepad();
146 void closeGamepad();
148 void readStick(float& x, float& y) const;
150 void switchTo(MatchEngine& engine, PlayerID player);
151 void applySwitchingRules(MatchEngine& engine, PlayerID current, bool idle);
152
153 bool active = false;
154 bool home = true;
156
159 std::array<std::uint8_t, SDL_SCANCODE_COUNT> keys{};
162 std::array<std::uint8_t, CONTROL_COUNT> keyHolds{};
163 std::array<bool, CONTROL_COUNT> padHolds{};
164 std::array<bool, CONTROL_COUNT> pressed{};
165 std::array<bool, CONTROL_COUNT> released{};
166 bool pause_requested = false;
167
168 SDL_Gamepad* gamepad = nullptr;
169 bool gamepad_subsystem = false;
170
173 std::uint64_t charge_step = 0;
176 float pending_power = 0.0f;
177
179 bool submitted_any = false;
183 double last_switch_seconds = -1e9;
184 double last_manual_seconds = -1e9;
187 double idle_since = 0.0;
188 bool was_idle = true;
189};
Definition match_engine.h:517
Definition match_play_controller.h:55
bool controlsHome() const
Definition match_play_controller.h:80
float chargeShare(const MatchEngine &engine) const
Definition match_play_controller.cpp:549
bool isCharging() const
Definition match_play_controller.h:107
PlayerID manual_skip
Definition match_play_controller.h:185
void applySwitchingRules(MatchEngine &engine, PlayerID current, bool idle)
Definition match_play_controller.cpp:341
void update(MatchEngine &engine, float simulatedSeconds, const PlayScreenBasis &basis)
Definition match_play_controller.cpp:384
SDL_Gamepad * gamepad
Definition match_play_controller.h:168
void readStick(float &x, float &y) const
Definition match_play_controller.cpp:280
bool pause_requested
Definition match_play_controller.h:166
std::uint64_t charge_step
Definition match_play_controller.h:173
std::array< std::uint8_t, SDL_SCANCODE_COUNT > keys
Definition match_play_controller.h:159
static Control controlForButton(int button)
Definition match_play_controller.cpp:121
MatchPlayController & operator=(const MatchPlayController &)=delete
bool handleEvent(const SDL_Event &event)
Definition match_play_controller.cpp:145
std::array< bool, CONTROL_COUNT > pressed
Definition match_play_controller.h:164
~MatchPlayController()
Definition match_play_controller.cpp:56
void setOptions(const Options &changed)
Definition match_play_controller.h:81
PlayerID next_switch
Definition match_play_controller.h:180
MatchInputAction charging
Definition match_play_controller.h:172
MatchInputAction pending
Definition match_play_controller.h:175
bool takePauseRequest()
Definition match_play_controller.cpp:240
MatchPlayController(const MatchPlayController &)=delete
void begin(MatchEngine &engine, bool homeTeam, const Options &options)
Definition match_play_controller.cpp:62
void release(Control control)
Definition match_play_controller.cpp:220
const MatchPlayerInput & lastInput() const
Definition match_play_controller.h:116
bool submitted_any
Definition match_play_controller.h:179
PlayerID nextSwitch() const
Definition match_play_controller.h:104
double idle_since
Definition match_play_controller.h:187
void switchTo(MatchEngine &engine, PlayerID player)
Definition match_play_controller.cpp:323
bool was_idle
Definition match_play_controller.h:188
bool home
Definition match_play_controller.h:154
void clearPresses()
Definition match_play_controller.cpp:230
float pending_power
Definition match_play_controller.h:176
static Control controlForKey(const SDL_KeyboardEvent &key)
Definition match_play_controller.cpp:96
void openGamepad(SDL_JoystickID which)
Definition match_play_controller.cpp:247
std::array< bool, CONTROL_COUNT > padHolds
Definition match_play_controller.h:163
void closeGamepad()
Definition match_play_controller.cpp:266
void press(Control control)
Definition match_play_controller.cpp:210
void end(MatchEngine &engine)
Definition match_play_controller.cpp:88
bool held(Control control) const
Definition match_play_controller.cpp:225
bool gamepad_subsystem
Definition match_play_controller.h:169
std::array< std::uint8_t, CONTROL_COUNT > keyHolds
Definition match_play_controller.h:162
bool active
Definition match_play_controller.h:153
bool isActive() const
Definition match_play_controller.h:79
double last_switch_seconds
Definition match_play_controller.h:183
double last_manual_seconds
Definition match_play_controller.h:184
bool hasGamepad() const
Definition match_play_controller.h:112
Control
Definition match_play_controller.h:121
@ PASS
Definition match_play_controller.h:126
@ DOWN
Definition match_play_controller.h:123
@ RIGHT
Definition match_play_controller.h:125
@ PAUSE
Definition match_play_controller.h:133
@ LEFT
Definition match_play_controller.h:124
@ SHOOT
Definition match_play_controller.h:127
@ UP
Definition match_play_controller.h:122
@ SWITCH
Definition match_play_controller.h:130
@ THROUGH
Definition match_play_controller.h:128
@ JOCKEY
Definition match_play_controller.h:132
@ CONTROL_COUNT
Definition match_play_controller.h:134
@ SPRINT
Definition match_play_controller.h:131
@ LOB
Definition match_play_controller.h:129
static constexpr float FULL_POWER_SECONDS
Definition match_play_controller.h:68
const Options & options() const
Definition match_play_controller.h:82
Options settings
Definition match_play_controller.h:155
const char * gamepadName() const
Definition match_play_controller.cpp:274
MatchPlayController()=default
MatchPlayerInput submitted
Definition match_play_controller.h:178
void openFirstGamepad()
Definition match_play_controller.cpp:255
std::array< bool, CONTROL_COUNT > released
Definition match_play_controller.h:165
ImGuiKey key
Definition input_actions.cpp:25
MatchInputAction
Definition match_engine.h:387
PlayAutoSwitch
Definition match_play_controller.h:20
Definition match_play_controller.h:59
float deadZone
Definition match_play_controller.h:64
PlayAutoSwitch autoSwitch
Definition match_play_controller.h:60
std::uint8_t passAssist
Definition match_play_controller.h:62
Definition match_engine.h:410
Definition match_play_controller.h:36
float lengthY
Definition match_play_controller.h:38
float widthY
Definition match_play_controller.h:40
float lengthX
Definition match_play_controller.h:37
float widthX
Definition match_play_controller.h:39
uint32_t PlayerID
Type alias for Player identifiers.
Definition types.h:21