mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-10 18:08:55 +03:00
Introduce basic TEA for breakout
This commit is contained in:
parent
999dbfd9d1
commit
c2b823ce99
@ -3,54 +3,57 @@ app "breakout"
|
||||
imports []# [ pf.Action.{ Action }, pf.Elem.{ button, text, row, col } ]
|
||||
provides [ program ] to pf
|
||||
|
||||
program = { render }
|
||||
program = { init, update, render }
|
||||
|
||||
render = \event ->
|
||||
init = \_ -> { width: 1900, height: 1000 }
|
||||
|
||||
update = \state ->
|
||||
when event is
|
||||
Resize size ->
|
||||
numRows = 4
|
||||
numCols = 8
|
||||
numBlocks = numRows * numCols
|
||||
Resize size -> size
|
||||
KeyUp keyCode -> { width: 1900, height: 1000 }
|
||||
KeyDown keyCode -> { width: 1900, height: 1000 }
|
||||
|
||||
blocks = List.map (List.range 0 numBlocks) \index ->
|
||||
col =
|
||||
Num.rem index numCols
|
||||
|> Result.withDefault 0
|
||||
|> Num.toF32
|
||||
render = \state ->
|
||||
numRows = 4
|
||||
numCols = 8
|
||||
numBlocks = numRows * numCols
|
||||
|
||||
row =
|
||||
index // numCols
|
||||
|> Result.withDefault 0
|
||||
|> Num.toF32
|
||||
blocks = List.map (List.range 0 numBlocks) \index ->
|
||||
col =
|
||||
Num.rem index numCols
|
||||
|> Result.withDefault 0
|
||||
|> Num.toF32
|
||||
|
||||
red = (col / Num.toF32 numCols) |> Result.withDefault 0
|
||||
green = ((row / Num.toF32 numRows) |> Result.withDefault 0)
|
||||
blue = (Num.toF32 index / Num.toF32 numBlocks) |> Result.withDefault 0
|
||||
row =
|
||||
index // numCols
|
||||
|> Result.withDefault 0
|
||||
|> Num.toF32
|
||||
|
||||
color = { r: red * 0.8, g: 0.2 + green * 0.6, b: 0.2 + blue * 0.8, a: 1 }
|
||||
red = (col / Num.toF32 numCols) |> Result.withDefault 0
|
||||
green = ((row / Num.toF32 numRows) |> Result.withDefault 0)
|
||||
blue = (Num.toF32 index / Num.toF32 numBlocks) |> Result.withDefault 0
|
||||
|
||||
{ row, col, color }
|
||||
color = { r: red * 0.8, g: 0.2 + green * 0.6, b: 0.2 + blue * 0.8, a: 1 }
|
||||
|
||||
blockWidth = size.width / numCols |> Result.withDefault 0
|
||||
blockHeight = 80
|
||||
{ row, col, color }
|
||||
|
||||
rects =
|
||||
List.map blocks \{ row, col, color } ->
|
||||
left = Num.toF32 col * blockWidth
|
||||
top = Num.toF32 (row * blockHeight)
|
||||
blockWidth = state.width / numCols |> Result.withDefault 0
|
||||
blockHeight = 80
|
||||
|
||||
Rect { left, top, width: blockWidth, height: blockHeight, color }
|
||||
rects =
|
||||
List.map blocks \{ row, col, color } ->
|
||||
left = Num.toF32 col * blockWidth
|
||||
top = Num.toF32 (row * blockHeight)
|
||||
|
||||
paddle =
|
||||
color = { r: 0.8, g: 0.8, b: 0.8, a: 1.0 }
|
||||
width = size.width * 0.25
|
||||
height = blockHeight
|
||||
left = (size.width * 0.5) - (width * 0.5)
|
||||
top = size.height - (height * 2)
|
||||
Rect { left, top, width: blockWidth, height: blockHeight, color }
|
||||
|
||||
Rect { left, top, width, height, color }
|
||||
paddle =
|
||||
color = { r: 0.8, g: 0.8, b: 0.8, a: 1.0 }
|
||||
width = state.width * 0.25
|
||||
height = blockHeight
|
||||
left = (state.width * 0.5) - (width * 0.5)
|
||||
top = state.height - (height * 2)
|
||||
|
||||
List.append rects paddle
|
||||
Rect { left, top, width, height, color }
|
||||
|
||||
_ ->
|
||||
[ Text "TODO handle other events than Resize!" ]
|
||||
List.append rects paddle
|
||||
|
@ -1,5 +1,5 @@
|
||||
platform "gui"
|
||||
requires {} { program : Program State }
|
||||
requires { Model } { program : _ }
|
||||
exposes []
|
||||
packages {}
|
||||
imports []
|
||||
@ -12,5 +12,9 @@ Elem : [ Rect { color : Rgba, left : F32, top : F32, width : F32, height : F32 }
|
||||
Event : [ Resize { width : F32, height : F32 }, KeyDown U32, KeyUp U32 ]
|
||||
|
||||
# TODO allow changing the window title - maybe via a Task, since that shouldn't happen all the time
|
||||
programForHost : { render : (Event -> List Elem) as Render }
|
||||
programForHost : {
|
||||
init : ({} -> Model) as Init,
|
||||
update : (Model, Event -> Model) as Update,
|
||||
render : (Model -> List Elem) as Render
|
||||
}
|
||||
programForHost = program
|
||||
|
Loading…
Reference in New Issue
Block a user