Skip to content

Commit

Permalink
feat(effect): Implement map function (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-xr authored Oct 4, 2024
1 parent 03f115a commit 0abfe6e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions examples/hello/hello.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module Model =
}

type Msg =
| MovePaddle1 of float
| MovePaddle2 of float
| MovePaddle1
| MovePaddle2

let game: Game<Model, Msg> = GameBuilder.local Model.initial

Expand Down Expand Up @@ -74,7 +74,7 @@ let tick model (tick: Time.FrameTime) =
|> handleCollisionWithPaddle model.paddle1
|> handleCollisionWithPaddle model.paddle2)

( { model with ball = newBall; counter = model.counter + 3 }, Effect.none () )
( { model with ball = newBall; counter = model.counter + 3 }, Effect.wrapped (MovePaddle2) |> Effect.map(fun _a -> MovePaddle1) )

open Fable.Core.Rust

Expand All @@ -85,6 +85,7 @@ let init (_args: array<string>) =
game
|> GameBuilder.draw3d (fun world frameTime ->

let eff = Effect.wrapped (MovePaddle2) |> Effect.map (fun _a -> MovePaddle1);
let colorMaterial = Material.color(0.0f, 1.0f, 0.0f, 1.0f);
let textureMaterial = Material.texture( Texture.file("vr_glove_color.jpg"));
// let barrelModel = Model.file("ExplodingBarrel.glb");
Expand Down
9 changes: 9 additions & 0 deletions runtime/functor-runtime-common/src/effect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use fable_library_rust::Native_::Func1;

#[derive(Clone)]
pub enum Effect<T: Clone + 'static> {
None,
Expand All @@ -12,4 +14,11 @@ impl<T: Clone + 'static> Effect<T> {
pub fn wrapped(data: T) -> Effect<T> {
Effect::Wrapped(data)
}

pub fn map<U: Clone + 'static>(mapping: Func1<T, U>, source: Effect<T>) -> Effect<U> {
match source {
Effect::None => Effect::None,
Effect::Wrapped(v) => Effect::Wrapped(mapping(v)),
}
}
}
7 changes: 6 additions & 1 deletion src/Functor.Game/Effect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ open Fable.Core

[<Erase; Emit("functor_runtime_common::Effect<$0>")>] type effect<'msg> = | Noop


module Effect =

[<Emit("functor_runtime_common::Effect::none()")>]
let none (): effect<_> = nativeOnly

[<Emit("functor_runtime_common::Effect::wrapped($0)")>]
let wrapped a: 'a -> effect<'a> = nativeOnly
let wrapped (a: 'a) : effect<'a> = nativeOnly

[<Emit("functor_runtime_common::Effect::map($0, $1)")>]
let map (fn: 'a -> 'b) (eff: effect<'a>) : effect<'b> = nativeOnly
8 changes: 8 additions & 0 deletions src/Functor.Game/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ module Runtime
member this.setState(incomingState) =
state <- OpaqueState.unsafe_coerce incomingState
member this.tick(frameTime: Time.FrameTime) =

// Todo: If first frame, run 'init'

// Todo: Run any pending effects

let (newState, effects) = GameRunner.tick myGame state frameTime

// Todo: Run tick effects

state <- newState
()
member this.render(frameTime: Time.FrameTime) =
Expand Down

0 comments on commit 0abfe6e

Please sign in to comment.