R6 class to create and manage a Phaser game within a Shiny application. Provides methods for adding sprites, animations, images, sounds, backgrounds, controls, and collision handling.
Public fields
idCharacter. ID of the Game container. Used as the HTML element ID where the game canvas will be rendered.
Methods
Method new()
Create a PhaserGame object with the given configuration.
Usage
PhaserGame$new(
id = "phaser_game",
width = 800,
height = 600,
gravity_x = 0,
gravity_y = 0
)Arguments
idCharacter. ID of the Game container (defaults to "phaser_game").
widthNumeric. Width of the Phaser canvas in pixels (defaults to 800).
heightNumeric. Height of the Phaser canvas in pixels (defaults to 600).
gravity_xNumeric. Horizontal Arcade Physics world gravity (defaults to 0).
gravity_yNumeric. Vertical Arcade Physics world gravity (defaults to 0).
Examples
game <- PhaserGame$new(id = "my_game", width = 1024, height = 768)Method set_shiny_session()
Set the Shiny session used to send Phaser custom messages.
Usage
PhaserGame$set_shiny_session(session = shiny::getDefaultReactiveDomain())Method save_game()
Save game data and Phaser object state to a JSON file on the server. Positions are read directly from Phaser immediately before the save request, avoiding stale Shiny coordinates.
Usage
PhaserGame$save_game(
name,
state = list(),
objects = NULL,
snapshot = NULL,
directory = NULL
)Arguments
nameCharacter. Human-readable name of the save.
stateNamed list. Additional JSON-serializable application state.
objectsCharacter vector. Named Phaser scene objects to capture. By default all named scene objects are captured.
snapshotNamed list. Optional Phaser object snapshot already captured in the browser. Supplying it writes the save synchronously.
directoryCharacter. Server-side directory. Defaults to a game-specific folder below
tempdir().
Method load_game()
Load a saved game from server disk.
Method add_text()
Add a text object to the Phaser scene.
Usage
PhaserGame$add_text(
text,
id,
x,
y,
style = list(font_size = "22px"),
visible = TRUE
)Method add_rectangle()
Add a rectangle object to the Phaser scene.
Usage
PhaserGame$add_rectangle(
name,
x,
y,
width,
height,
color,
visible = TRUE,
clickable = FALSE
)Arguments
nameCharacter. Unique name for the rectangle.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
widthNumeric. Rectangle width in pixels.
heightNumeric. Rectangle height in pixels.
colorCharacter. Fill color in Phaser-compatible format.
visibleLogical. Whether rectangle is initially visible.
clickableLogical. Whether rectangle emits click events.
Method add_image()
Adds a static image to the Phaser scene.
Arguments
nameCharacter. Unique key to reference this image.
urlCharacter. URL or path to the image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
visibleLogical. Whether the image is initially visible (default: TRUE).
clickableLogical. Whether the image should emit click events (default: FALSE).
Method add_map()
Add a background (tilemap) layer from Tiled JSON + tileset image(s).
Arguments
map_keyCharacter. Key for the tilemap JSON.
map_urlCharacter. URL of the Tiled JSON file (relative to www/assets/).
tileset_urlsCharacter vector. URLs of tileset image files.
tileset_namesCharacter vector. Names of tilesets as defined in Tiled.
layer_nameCharacter. Name of the layer to render from Tiled.
Method activate_map()
Activate a tilemap previously loaded with add_map().
Arguments
map_keyCharacter. Key of the tilemap to activate.
player_nameCharacter. Optional player sprite to reposition.
xNumeric. Optional player x-coordinate.
yNumeric. Optional player y-coordinate.
visible_objectsCharacter vector. Scene objects to show and enable.
hidden_objectsCharacter vector. Scene objects to hide and disable.
Method add_proximity_trigger()
Add a trigger that monitors when a scene object is near a point.
Usage
PhaserGame$add_proximity_trigger(
id,
object_name,
x,
y,
radius = 180,
element_id = NULL,
context = NULL,
input_id = NULL
)Arguments
idCharacter. Unique trigger identifier.
object_nameCharacter. Scene object whose position is monitored.
xNumeric. Target x-coordinate.
yNumeric. Target y-coordinate.
radiusNumeric. Maximum distance at which the trigger is active.
element_idCharacter or
NULL. ID of an HTML element to show while the trigger is active.contextCharacter or
NULL. Optional map key that must be active for the trigger to be evaluated.input_idCharacter or
NULL. Optional Shiny input that receives a payload whenever the trigger is entered or exited.
Method add_sprite()
Load a base spritesheet and create an "idle" animation.
Usage
PhaserGame$add_sprite(
name,
url,
x,
y,
frame_width,
frame_height,
frame_count = 1,
frame_rate = 1
)Arguments
nameCharacter. Unique key for the sprite and its idle animation.
urlCharacter. URL or path to the spritesheet image.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
frame_widthNumeric. Width of each frame.
frame_heightNumeric. Height of each frame.
frame_countNumeric. Number of frames in the spritesheet.
frame_rateNumeric. Frames per second for the idle animation.
Method add_collider()
Adds a collider between two game objects.
Usage
PhaserGame$add_collider(
object_one,
object_two = NULL,
group = NULL,
browser_action = browser_actions(),
input = NULL,
server_action = NULL
)Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group to compare against.
browser_actionActions created with
browser_actions()that run immediately in the browser.inputShiny input list.
server_actionFunction called in R with the collision event.
Method add_overlap()
Adds a collider between two game objects.
Usage
PhaserGame$add_overlap(
object_one,
object_two = NULL,
group = NULL,
browser_action = browser_actions(),
input = NULL,
server_action = NULL,
mode = c("enter", "stay"),
interval = 0
)Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group.
browser_actionActions created with
browser_actions()that run immediately in the browser.inputShiny input list.
server_actionFunction called in R with the overlap event.
modeCharacter.
"enter"(default) runs once per contact;"stay"repeats while overlapping.intervalNumeric. Minimum milliseconds between
"stay"actions.
Method add_overlap_end()
Register a callback fired when overlap between objects ends.
Usage
PhaserGame$add_overlap_end(
object_one,
object_two = NULL,
group = NULL,
browser_action = browser_actions(),
input = NULL,
server_action = NULL,
session = shiny::getDefaultReactiveDomain()
)Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group to compare against.
browser_actionActions created with
browser_actions()that run immediately in the browser.inputShiny input list.
server_actionFunction called in R with the overlap-end event.
sessionShiny session object.
Method add_control()
Register a callback fired when a specific key is pressed.
Usage
PhaserGame$add_control(
key,
browser_action = browser_actions(),
input = NULL,
server_action = NULL
)Arguments
keyA character, accepts Javascript key events (they need to align with event.code).
browser_actionActions created with
browser_actions()that run immediately in the browser.inputShiny input list.
server_actionFunction called in R with the keyboard event.
Examples
## ------------------------------------------------
## Method `PhaserGame$new`
## ------------------------------------------------
game <- PhaserGame$new(id = "my_game", width = 1024, height = 768)
## ------------------------------------------------
## Method `PhaserGame$use_phaser`
## ------------------------------------------------
game$use_phaser()
#> <div id="my_game" style="width:100vw; height:100vh;"></div>
#> <script>initPhaserGame('my_game', {"width":1024,"height":768,"gravity_x":0,"gravity_y":0});</script>