I am messing around with this shiny app to try and make a clicker game, like cookie clicker for example. I have the initial clicker working correctly, but the growth over time is giving me trouble. What I attempted is below.
My issue starts below # * * * 2.1.6 Passive1 gain ----, the rest of the app works as intended.
How can I correctly make the counter increase over time based on the amount of passive1 owned? Maybe a tick every x seconds.
library(shiny)
# * 1.0 UI Setup ----
ui <- fluidPage(
navbarPage("Clicker",
tabPanel("Game",
titlePanel("Points"),
sidebarLayout(
sidebarPanel(
textOutput("Points")
),
mainPanel(
h5("Click"),
h5("1 point per click"),
actionButton("click","Click"),
p(),
actionButton("passive1","Passive 1"),
textOutput("Passive1Cost"),
textOutput("Passive1Level"),
textOutput("Passive1Gain")
)))))
server <- function(input, output, session) {
counter <- reactiveValues(countervalue = 0)
# * 2.0 Main button ----
observeEvent(input$click, {
counter$countervalue <- counter$countervalue + 1
})
# * * 2.1 Passive1 Stats ----
Passive1Level <- reactiveValues(countervalue = 0)
Passive1Gain <- reactiveValues(countervalue = 0)
Passive1Cost <- reactiveValues(countervalue = 10)
# * * * 2.1.1 Level up passive1 ----
observeEvent(input$passive1, {
if(Passive1Cost$countervalue <= counter$countervalue)
{Passive1Level$countervalue <- Passive1Level$countervalue + 1}
})
# * * * 2.1.2 Spent points for Passive1 -----
observeEvent(input$passive1, {
if(Passive1Cost$countervalue <= counter$countervalue)
{counter$countervalue <- counter$countervalue - Passive1Cost$countervalue}
})
# * * * 2.1.3 Increase cost of Passive1 ----
observeEvent(input$passive1, {
{Passive1Cost$countervalue <- 10 * Passive1Level$countervalue + 10}
})
# * * * 2.1.4 Increasing Passive1 gain ----
observeEvent(input$passive1, {
Passive1Gain$countervalue <- 5 * Passive1Level$countervalue
})
# * * * 2.1.5 Output Passive1 stats ----
output$Passive1Cost <- renderText({
paste("Cost:",Passive1Cost$countervalue)
})
output$Passive1Level <- renderText({
paste("Quantity:",Passive1Level$countervalue)
})
output$Passive1Gain <- renderText({
paste("Points per sec:", Passive1Gain$countervalue)
})
# * * * 2.1.6 Passive1 gain ----
#fails and crashes everything
# CurrentTime <- Sys.time()
# observe({
# if(CurrentTime + 1 > Sys.time()){counter$countervalue <- counter$countervalue + Passive1Gain$countervalue}
# })
# does not even run
# CurrentTime <- reactiveValues(Sys.time())
# observe({
# if(CurrentTime + 1){counter$countervalue <- counter$countervalue + Passive1Gain$countervalue}
# })
#Also fails everytime
# vals <- reactiveValues(countervalue = 0)
#
# observe({
# invalidateLater(1000 / (counter$countervalue + Passive1Gain$countervalue), session)
# # vals$countervalue <- isolate(vals$countervalue) + Passive1Gain$countervalue
# # counter$countervalue <- counter$countervalue + vals$countervalue
# })
#Also crashes
# observe({
# invalidateLater(1000, session)
# counter$countervalue <- counter$countervalue + Passive1Gain$countervalue
# })
# * 2.2 Point display ----
output$Points <- renderText({
paste("Points:",counter$countervalue)
})
}
shinyApp(ui = ui, server = server)