inputPacketCreator
The InputPacketCreator class handles the conversion of user input events (keyboard and mouse) into network packets that are sent to the server. It listens to events from the InputManager and communicates with the Network module.
InputPacketCreator public
Bounded to game as game.inputPacketCreator. Alias: game.inputPacketCreatorType
Properties
| Property | Type | Description |
|---|---|---|
sendMouseMoveChance | number | The probability (0.0 to 1.0) of sending a mouseMoved packet to the server. Defaults to 1. |
lastMouseMoveYaw | number | The last yaw angle sent during a mouse move event. Used for deduplication. |
lastMouseDragYaw | number | The last yaw angle sent during a mouse drag event. Used for deduplication. |
lastAnyYaw | number | The most recent yaw angle recorded from any mouse interaction. |
enabled | boolean | Whether the packet creator is currently processing and sending inputs. |
Methods
start()
function start(): voidInitializes the packet creator by binding to key and mouse events from InputManager.
setSendMouseMoveChance()
function setSendMouseMoveChance(chance: number): voidSets the probability of sending mouse move packets.
getLastAnyYaw()
function getLastAnyYaw(): numberReturns the most recent yaw angle recorded.
getEnabled()
function getEnabled(): booleanReturns whether the input packet creator is currently enabled.
setEnabled()
function setEnabled(enabled: boolean): voidSets the enabled state of the packet creator.
bindKeys()
function bindKeys(): voidBinds listeners to keyPress and keyRelease events. It maps WASD and arrow keys to directional input packets (up, down, left, right) and the spacebar to the space input. Input is ignored if an input or textarea element is active.
bindMouse()
function bindMouse(): voidBinds listeners to mouse events (mouseDown, mouseUp, mouseMovedWhileDown, mouseMoved). It calculates the yaw angle and sends corresponding input packets to the server.
screenToYaw()
function screenToYaw(x: number, y: number): numberCalculates the angle (in degrees, 0-359) from the center of the screen to the specified (x, y) coordinates.
screenToWorld()
function screenToWorld(event: MouseEvent): objectConverts screen coordinates to world coordinates { x, y }, rounding values down to the nearest integer.
distanceToCenter()
function distanceToCenter(x: number, y: number): numberCalculates the distance from the screen center to the specified (x, y) coordinates.
Input Data
The following fields are sent to InputPacketSchduler via scheduler.scheduleInput(). Each packet contains one or more of these properties representing the player's current input state.
Movement & Actions
| Field | Type | Keys | Description |
|---|---|---|---|
up | number | W, Up Arrow | 1 to move up, 0 to stop. |
down | number | S, Down Arrow | 1 to move down, 0 to stop. |
left | number | A, Left Arrow | 1 to move left, 0 to stop. |
right | number | D, Right Arrow | 1 to move right, 0 to stop. |
space | number | Space | 1 when the spacebar is pressed, 0 when released. |
Mouse Interaction
| Field | Type | Description |
|---|---|---|
mouseDown | number | Sent when the left mouse button is pressed. Contains the current yaw angle. Must also includes worldX, worldY, and distance in other fields. |
mouseUp | number | Sent as 1 when the left mouse button is released. Must also includes worldX, worldY, and distance in other fields. |
mouseMoved | number | Sent when the mouse moves (subject to sendMouseMoveChance). Contains the current yaw angle. Must also includes worldX, worldY, and distance in other fields. |
mouseMovedWhileDown | number | Sent when the mouse moves while the left button is held. Contains the current yaw angle. Must also includes worldX, worldY, and distance in other fields. |
worldX | number | The X-coordinate of the mouse in the world. |
worldY | number | The Y-coordinate of the mouse in the world. |
distance | number | The distance from the mouse to the center of the screen. |
INFO
The yaw angle is rounded and normalized to a 0-359 range before being sent to the server.