Actions and Events

The oglr project leverages a robust system for handling user actions and the events they trigger. This system ensures a smooth and responsive gameplay experience by seamlessly connecting player input to in-game actions.

Actions

Actions represent the fundamental units of player interaction within the game. They encompass everything from basic movement to complex interactions with objects and the game world.

Each action is defined by:

  • Name: A descriptive label identifying the action (e.g., “MoveLeft”, “Jump”, “FireWeapon”).
  • Binding: The input method that triggers the action (e.g., “LeftArrow”, “Spacebar”, “MouseClick”).
  • State: The current activation state of the action (e.g., “Pressed”, “Released”, “Held”).

Action States

  • Pressed: Indicates that the action has just been activated by the player.
  • Released: Indicates that the action has just been deactivated by the player.
  • Held: Indicates that the action is currently being held down by the player.

Action Examples

// Example action for moving left
          type MoveLeftAction struct {
              Binding string
          }
          
          // Example action for jumping
          type JumpAction struct {
              Binding string
          }
          

Events

Events are triggered by actions and represent specific occurrences within the game. They can be used to update game logic, display visual effects, or trigger other actions.

Event Types

  • KeyDownEvent: Triggered when a key is pressed.
  • KeyUpEvent: Triggered when a key is released.
  • MouseButtonDownEvent: Triggered when a mouse button is pressed.
  • MouseButtonUpEvent: Triggered when a mouse button is released.
  • MouseMoveEvent: Triggered when the mouse cursor moves.
  • CustomEvent: A generic event type used for application-specific events.

Event Handling

Events are handled by event listeners, which are functions that are called when a specific event occurs.

// Example event listener for a key down event
          func handleKeyDownEvent(event KeyEvent) {
              // Handle the key down event
          }
          
          // Example event listener for a mouse button down event
          func handleMouseButtonDownEvent(event MouseButtonEvent) {
              // Handle the mouse button down event
          }
          

Action and Event Mapping

The oglr project utilizes a mapping system to connect actions to events. This mapping enables the game to respond appropriately to player input.

Mapping Example

// Map the MoveLeftAction to the KeyDownEvent for the left arrow key
          actionMap.MapActionToEvent("MoveLeft", KeyDownEvent, "LeftArrow")
          
          // Map the JumpAction to the KeyDownEvent for the spacebar
          actionMap.MapActionToEvent("Jump", KeyDownEvent, "Spacebar")
          

Action and Event System Architecture

The action and event system in oglr follows a well-defined architecture:

  1. Input Manager: Captures player input from the keyboard, mouse, and other input devices.
  2. Action Manager: Responsible for managing actions, including binding them to input devices and updating their state.
  3. Event Manager: Manages events, including registering event listeners and dispatching events to the appropriate listeners.
  4. Game Logic: Contains the game’s logic, which responds to events triggered by actions.

This architecture ensures a robust and flexible system for handling player input and driving game logic.

References: