R6 class to create and manage a Phaser game within a Shiny application. Provides methods for adding sprites, animations, images, 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)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).
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 add_text()
Add a text object to the Phaser scene.
Usage
PhaserGame$add_text(text, id, x, y, style = list(font_size = "22px"))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 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.
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,
callback_fun,
input,
session = shiny::getDefaultReactiveDomain()
)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});</script>