Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: phaserR
Title: An R Interface to the Phaser.js Game Framework
Version: 0.0.0.9012
Version: 0.0.0.9013
Authors@R:
person(
given = "Maciej",
Expand Down
4 changes: 2 additions & 2 deletions R/PhaserGame.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ PhaserGame <- R6::R6Class(
#' @param x Numeric. X-coordinate in pixels.
#' @param y Numeric. Y-coordinate in pixels.
#' @return Invisible; sends a custom message to the client.
add_image = function(name, url, x, y, visible = TRUE) {
return(Image$new(name, url, x, y, visible))
add_image = function(name, url, x, y, visible = TRUE, clickable = FALSE) {
return(Image$new(name, url, x, y, visible, clickable))
},

#' @description Add a background (tilemap) layer from Tiled JSON + tileset image(s).
Expand Down
15 changes: 13 additions & 2 deletions R/images.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Image <- R6::R6Class(
classname = "Image",
public = list(
initialize = function(name, url, x, y, visible,
initialize = function(name, url, x, y, visible, clickable,
session = getDefaultReactiveDomain()) {
private$session <- session
private$name <- name
js <- sprintf("addImage('%s', '%s', %d, %d, %s);", name, url, x, y, tolower(visible))
js <- sprintf("addImage('%s', '%s', %d, %d, %s, %s);",
name, url, x, y, tolower(visible), tolower(clickable))
send_js(private, js)
},
show = function() {
Expand All @@ -15,6 +16,16 @@ Image <- R6::R6Class(
hide = function() {
js <- sprintf("hideImage('%s');", private$name)
send_js(private, js)
},
#' @param event_fun A function.
click = function(event_fun, input) {
js <- sprintf("clickImage('%s');", private$name)
send_js(private, js)
observe_id <- paste0(private$name, "_click")
shiny::observeEvent(input[[observe_id]], {
evt <- input[[observe_id]]
event_fun(evt)
}, ignoreNULL = TRUE)
}
),
private = list(
Expand Down
9 changes: 8 additions & 1 deletion inst/examples/hero_game.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ server <- function(input, output, session) {
url = "assets/hero_game/buttons/talk.png",
y = 600,
x = 600,
visible = FALSE
visible = FALSE,
clickable = TRUE
)
game$add_overlap(
object_one = "hero",
Expand All @@ -87,6 +88,12 @@ server <- function(input, output, session) {
},
input = input
)
talk_btn$click(
event_fun = function(evt) {
browser()
},
input = input
)
}

shinyApp(ui, server)
18 changes: 0 additions & 18 deletions inst/www/phaser-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ function addPlayerControls(name, directions, speed) {
GameBridge.playerControls[name] = { speed, directions };
};

function addImage(imageName, imageUrl, x = null, y = null, visible = true) {
scene.load.image(imageName, imageUrl);

scene.load.once('complete', () => {
const px = x !== null
? x
: scene.cameras.main.width / 2;
const py = y !== null
? y
: scene.cameras.main.height / 2;

scene[imageName] = scene.add.image(px, py, imageName);
scene[imageName].setVisible(visible);
});

scene.load.start();
}

function addMap(mapKey, mapUrl, tilesetUrls, tilesetNames, layerName) {
scene.load.tilemapTiledJSON(mapKey, mapUrl);
for (let i = 0; i < tilesetNames.length; i++) {
Expand Down
31 changes: 31 additions & 0 deletions inst/www/phaser-image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
function addImage(imageName, imageUrl, x = null, y = null, visible = true, clickable = true) {
scene.load.image(imageName, imageUrl);

scene.load.once('complete', () => {
const px = x !== null
? x
: scene.cameras.main.width / 2;
const py = y !== null
? y
: scene.cameras.main.height / 2;

scene[imageName] = scene.add.image(px, py, imageName);
if (clickable) {
scene[imageName].setInteractive();
}
scene[imageName].setVisible(visible);
});

scene.load.start();
}

function showImage(imageName) {
scene[imageName].setVisible(true)
}

function hideImage(imageName) {
scene[imageName].setVisible(false)
}

function clickImage(imageName) {
scene[imageName].on('pointerdown', () => {
console.log(imageName + ' clicked!');
Shiny.setInputValue(
imageName + '_click',
true
)
});
}
Loading