Player12
An open source football management simulator
Loading...
Searching...
No Matches
MatchPlayController Class Reference

#include <match_play_controller.h>

Classes

struct  Options
 

Public Member Functions

 MatchPlayController ()=default
 
 ~MatchPlayController ()
 
 MatchPlayController (const MatchPlayController &)=delete
 
MatchPlayController & operator= (const MatchPlayController &)=delete
 
void begin (MatchEngine &engine, bool homeTeam, const Options &options)
 
void end (MatchEngine &engine)
 
bool isActive () const
 
bool controlsHome () const
 
void setOptions (const Options &changed)
 
const Options & options () const
 
bool handleEvent (const SDL_Event &event)
 
void clearPresses ()
 
bool takePauseRequest ()
 
void update (MatchEngine &engine, float simulatedSeconds, const PlayScreenBasis &basis)
 
PlayerID nextSwitch () const
 
float chargeShare (const MatchEngine &engine) const
 
bool isCharging () const
 
bool hasGamepad () const
 
const char * gamepadName () const
 
const MatchPlayerInput & lastInput () const
 

Static Public Attributes

static constexpr float FULL_POWER_SECONDS = 1.0f
 

Private Types

enum  Control : std::uint8_t {
  UP , DOWN , LEFT , RIGHT ,
  PASS , SHOOT , THROUGH , LOB ,
  SWITCH , SPRINT , JOCKEY , PAUSE ,
  CONTROL_COUNT
}
 

Private Member Functions

void press (Control control)
 
void release (Control control)
 
bool held (Control control) const
 
void openGamepad (SDL_JoystickID which)
 
void openFirstGamepad ()
 
void closeGamepad ()
 
void readStick (float &x, float &y) const
 
void switchTo (MatchEngine &engine, PlayerID player)
 
void applySwitchingRules (MatchEngine &engine, PlayerID current, bool idle)
 

Static Private Member Functions

static Control controlForKey (const SDL_KeyboardEvent &key)
 
static Control controlForButton (int button)
 

Private Attributes

bool active = false
 
bool home = true
 
Options settings
 
std::array< std::uint8_t, SDL_SCANCODE_COUNT > keys {}
 
std::array< std::uint8_t, CONTROL_COUNT > keyHolds {}
 
std::array< bool, CONTROL_COUNT > padHolds {}
 
std::array< bool, CONTROL_COUNT > pressed {}
 
std::array< bool, CONTROL_COUNT > released {}
 
bool pause_requested = false
 
SDL_Gamepad * gamepad = nullptr
 
bool gamepad_subsystem = false
 
MatchInputAction charging = MatchInputAction::NONE
 
std::uint64_t charge_step = 0
 
MatchInputAction pending = MatchInputAction::NONE
 
float pending_power = 0.0f
 
MatchPlayerInput submitted
 
bool submitted_any = false
 
PlayerID next_switch = 0
 
double last_switch_seconds = -1e9
 
double last_manual_seconds = -1e9
 
PlayerID manual_skip = 0
 
double idle_since = 0.0
 
bool was_idle = true
 

Detailed Description

The human's side of play mode: keyboard and gamepad state, the action buttons with their power bar, and who the active footballer is.

Devices are read on every frame, but the engine only receives one sample per simulation step (taken on the frame in which the engine is about to step), as a tick-stamped MatchPlayerInput record; switching goes through MatchEngine::setControlledPlayer. Power comes from how many simulation steps a button was held, so a recorded match replays exactly and the frame rate never changes what happens on the pitch.

Member Enumeration Documentation

◆ Control

enum MatchPlayController::Control : std::uint8_t
private

A play control, from either device.

Enumerator
UP 
DOWN 
LEFT 
RIGHT 
PASS 
SHOOT 
THROUGH 
LOB 
SWITCH 
SPRINT 
JOCKEY 
PAUSE 
CONTROL_COUNT 

Constructor & Destructor Documentation

◆ MatchPlayController() [1/2]

MatchPlayController::MatchPlayController ( )
default

◆ ~MatchPlayController()

MatchPlayController::~MatchPlayController ( )

◆ MatchPlayController() [2/2]

MatchPlayController::MatchPlayController ( const MatchPlayController &  )
delete

Member Function Documentation

◆ applySwitchingRules()

void MatchPlayController::applySwitchingRules ( MatchEngine &  engine,
PlayerID  current,
bool  idle 
)
private

◆ begin()

void MatchPlayController::begin ( MatchEngine &  engine,
bool  homeTeam,
const Options &  options 
)

Takes control of homeTeam's best placed footballer.

◆ chargeShare()

float MatchPlayController::chargeShare ( const MatchEngine &  engine) const

Filled share of the power bar while a button is held, else 0.

◆ clearPresses()

void MatchPlayController::clearPresses ( )

Forgets presses and a charging button (after a pause or a dialog).

◆ closeGamepad()

void MatchPlayController::closeGamepad ( )
private

◆ controlForButton()

MatchPlayController::Control MatchPlayController::controlForButton ( int  button)
staticprivate

◆ controlForKey()

MatchPlayController::Control MatchPlayController::controlForKey ( const SDL_KeyboardEvent &  key)
staticprivate

Control a key or gamepad button stands for (CONTROL_COUNT: none); keys follow the bindings of Settings > Controls (sprint is Shift).

◆ controlsHome()

bool MatchPlayController::controlsHome ( ) const
inline

◆ end()

void MatchPlayController::end ( MatchEngine &  engine)

Hands every player back to the AI.

◆ gamepadName()

const char * MatchPlayController::gamepadName ( ) const

Name of the connected gamepad (empty when none).

◆ handleEvent()

bool MatchPlayController::handleEvent ( const SDL_Event &  event)

Keyboard and gamepad events (including hot-plug); true when the event was one of the play controls (the caller should not use it for anything else).

◆ hasGamepad()

bool MatchPlayController::hasGamepad ( ) const
inline

True while a gamepad is connected and in use.

◆ held()

bool MatchPlayController::held ( Control  control) const
private

◆ isActive()

bool MatchPlayController::isActive ( ) const
inline

◆ isCharging()

bool MatchPlayController::isCharging ( ) const
inline

◆ lastInput()

const MatchPlayerInput & MatchPlayController::lastInput ( ) const
inline

The last input sent to the engine (tests, presentation).

◆ nextSwitch()

PlayerID MatchPlayController::nextSwitch ( ) const
inline

The footballer a manual switch would pick now (0 when none).

◆ openFirstGamepad()

void MatchPlayController::openFirstGamepad ( )
private

◆ openGamepad()

void MatchPlayController::openGamepad ( SDL_JoystickID  which)
private

◆ operator=()

MatchPlayController & MatchPlayController::operator= ( const MatchPlayController &  )
delete

◆ options()

const Options & MatchPlayController::options ( ) const
inline

◆ press()

void MatchPlayController::press ( Control  control)
private

◆ readStick()

void MatchPlayController::readStick ( float &  x,
float &  y 
) const
private

Stick direction on screen (x right, y down), magnitude 0..1.

◆ release()

void MatchPlayController::release ( Control  control)
private

◆ setOptions()

void MatchPlayController::setOptions ( const Options &  changed)
inline

◆ switchTo()

void MatchPlayController::switchTo ( MatchEngine &  engine,
PlayerID  player 
)
private

Hands control to player now, keeping the device state.

◆ takePauseRequest()

bool MatchPlayController::takePauseRequest ( )

True once per press of the pause control (Esc, P, Start).

◆ update()

void MatchPlayController::update ( MatchEngine &  engine,
float  simulatedSeconds,
const PlayScreenBasis &  basis 
)

Once per frame, before the engine advances by simulatedSeconds: switches the active footballer when the rules say so and, if the engine is about to step, submits the device state as that step's input.

Member Data Documentation

◆ active

bool MatchPlayController::active = false
private

◆ charge_step

std::uint64_t MatchPlayController::charge_step = 0
private

◆ charging

MatchInputAction MatchPlayController::charging = MatchInputAction::NONE
private

Button being held for power, and the step it was pressed at.

◆ FULL_POWER_SECONDS

constexpr float MatchPlayController::FULL_POWER_SECONDS = 1.0f
staticconstexpr

Seconds of simulated time to fill the power bar.

◆ gamepad

SDL_Gamepad* MatchPlayController::gamepad = nullptr
private

◆ gamepad_subsystem

bool MatchPlayController::gamepad_subsystem = false
private

◆ home

bool MatchPlayController::home = true
private

◆ idle_since

double MatchPlayController::idle_since = 0.0
private

Simulated seconds since which the stick has been idle.

◆ keyHolds

std::array<std::uint8_t, CONTROL_COUNT> MatchPlayController::keyHolds {}
private

Held state of each control from buttons (keys or pad) and presses and releases since the last update.

◆ keys

std::array<std::uint8_t, SDL_SCANCODE_COUNT> MatchPlayController::keys {}
private

Control each physical key is holding down (CONTROL_COUNT: none), so its release ends exactly what its press started.

◆ last_manual_seconds

double MatchPlayController::last_manual_seconds = -1e9
private

◆ last_switch_seconds

double MatchPlayController::last_switch_seconds = -1e9
private

Simulated seconds of the last switch, and of the last manual one with the player it left (so repeated presses cycle).

◆ manual_skip

PlayerID MatchPlayController::manual_skip = 0
private

◆ next_switch

PlayerID MatchPlayController::next_switch = 0
private

◆ padHolds

std::array<bool, CONTROL_COUNT> MatchPlayController::padHolds {}
private

◆ pause_requested

bool MatchPlayController::pause_requested = false
private

◆ pending

MatchInputAction MatchPlayController::pending = MatchInputAction::NONE
private

Action (and its power) waiting for the next step's input.

◆ pending_power

float MatchPlayController::pending_power = 0.0f
private

◆ pressed

std::array<bool, CONTROL_COUNT> MatchPlayController::pressed {}
private

◆ released

std::array<bool, CONTROL_COUNT> MatchPlayController::released {}
private

◆ settings

Options MatchPlayController::settings
private

◆ submitted

MatchPlayerInput MatchPlayController::submitted
private

◆ submitted_any

bool MatchPlayController::submitted_any = false
private

◆ was_idle

bool MatchPlayController::was_idle = true
private

The documentation for this class was generated from the following files: