Giter Site home page Giter Site logo

Avoid callbacks about samples-pixi HOT 8 OPEN

fable-compiler avatar fable-compiler commented on June 18, 2024
Avoid callbacks

from samples-pixi.

Comments (8)

alfonsogarciacaro avatar alfonsogarciacaro commented on June 18, 2024 2

What about using Elmish with Pixi? I had a look at Pixi.ticker and if I understand it correctly this is the place to put the view/render function. Besides that, we would have the classic update function to renew the Model (aka context) and when creating a sprite we would pass a dispatch function so the sprites can send messages to Elmish when an event is triggered (same way as React components do). In pseudo-code:

type Model =
  { Sprites = Sprite[] } // Using array for performance

type Msg =
  | Create of sprites: Sprite[]
  | MoveTo of name: string * x: float * y: float
  | Animate of whatever

let createSprite dispatch =
  let sprite = Sprite()
  sprite.onClick(fun ev -> Animate "foo" |> dispatch)
  sprite

let init dispatch =
   [| for i=0 to 10 do yield createSprite dispatch |]
   |> Create |> dispatch

let update model msg =
  match msg with
  | Create sprites ->
     { model with sprites = Array.append model.sprites sprites }, []
  | Animate _ -> failwith "TODO: Animate"
  | _ -> failwith "TODO: etc, etc"

let ticker = PIXI.ticker.Ticker()
ticker.stop()
ticker.add(fun deltaTime ->
  // Here comes the tricky part: we probably need to update the model
  // again using the deltaTime and there're several options for that.
  // We can send a message to update (though we have to make sure it
  // runs synchronously) or we can a specific updatePhysics (or similar) version.   
  renderer.render(stage))
ticker.start()

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024 2

elmishpixi

from samples-pixi.

MangelMaxime avatar MangelMaxime commented on June 18, 2024 1

Not to sure to understand everything yet because I didn't dig into the code but when I was developing a game in JS i was using custom event instead of callbacks.

Thanks to F# we can have strongly typed event and perhaps we can even create some local state machine to handle them as we love them :)

By F#, i am speaking of:

// Describe our event
// Here I am using a DU, we can pass what use any type/structure
type PlayerEvent =
  | MoveTo of float * float
  | TakeDamage of float

// Instantiate the event delegate
let playerEvent = Event<PlayerEvent>()

// Register, our handler/state machine :)
playerEvent.Publish.Add(fun msg ->
  math msg with
  | MoveTo (x, y) -> // do something
  | TakeDamage value -> // do something
)

// Trigger the event
playerEvent.Trigger(TakeDamage 10.)

So we are not using callbacks anymore, or at least not directly because we would still need them to catch pixi event. But where before, we could have used XXX callback we can use one event listener and probably have a better abstraction layer ?

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024 1

Ah indeed that's what I'm using in Suave. Didn't think about it for pixi. I need to try this.
Thanks @MangelMaxime 👏

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024 1

ok. I need to do a few tests. I'll update the sample I'm currently working on. Thanks guys!

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024

Hi @alfonsogarciacaro

Here comes the tricky part: we probably need to update the model
again using the deltaTime and there're several options for that.
We can send a message to update (though we have to make sure it
runs synchronously) or we can a specific updatePhysics (or similar) version.

I'm currently trying to play with the tricky part ;)
How can I dispatch a message to the update function from the ticker loop?

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024

Ok I have something working with subscriptions. I'll post a sample tomorrow.

from samples-pixi.

whitetigle avatar whitetigle commented on June 18, 2024

BTW, it seems the elmish doc is not exactly up to date. (but that's another subject for another repo 😉 )

from samples-pixi.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.