Skip to content

Commit

Permalink
Merge pull request #29 from lukewilliamboswell/formatting
Browse files Browse the repository at this point in the history
Update roc syntax and format all code
  • Loading branch information
lukewilliamboswell authored Jul 20, 2024
2 parents 412e18b + 91fc9a0 commit c8d53c7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ examples/*.wasm
*.png

.envrc
.direnv

generated-docs/

Expand Down
54 changes: 22 additions & 32 deletions examples/basic.roc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
app "basic"
packages {w4: "../platform/main.roc"}
imports [w4.Task.{ Task }, w4.W4]
provides [main, Model] to w4
app [main, Model] { w4: platform "../platform/main.roc" }

import w4.Task exposing [Task]
import w4.W4

Program : {
init : Task Model [],
Expand All @@ -22,30 +22,25 @@ init =
when List.first savedData is
Ok v -> v
Err _ -> 0
W4.trace! "Game has been loaded $(Inspect.toStr saves) times"

W4.trace! "Game has been loaded \(Inspect.toStr saves) times"

# Ignore save failures
_ = W4.saveToDisk [Num.addWrap saves 1] |> Task.result!

setColorPalette!

setDrawColors!

palette = W4.getPalette!

W4.trace! (Inspect.toStr palette)

colors = W4.getDrawColors!

W4.trace! (Inspect.toStr colors)

W4.tone! {
startFreq: 262,
endFreq: 523,
channel: Pulse1 Quarter,
sustainTime: 60,
decayTime: 30
W4.tone! {
startFreq: 262,
endFreq: 523,
channel: Pulse1 Quarter,
sustainTime: 60,
decayTime: 30,
}

update : Model -> Task Model []
Expand All @@ -54,28 +49,23 @@ update = \model ->
# Get inputs
{ button1, button2, left, right, up, down } = W4.getGamepad! Player1
mouse = W4.getMouse!

# Draw the gamepad state
W4.setTextColors! { fg: red, bg: green }
"X: \(Inspect.toStr button1)" |> W4.text! { x: 0, y: 0 }

"X: $(Inspect.toStr button1)" |> W4.text! { x: 0, y: 0 }
W4.setTextColors! { fg: blue, bg: white }
"Z: \(Inspect.toStr button2)" |> W4.text! { x: 0, y: 8 }
"L: \(Inspect.toStr left)" |> W4.text! { x: 0, y: 16 }
"R: \(Inspect.toStr right)" |> W4.text! { x: 0, y: 24 }
"U: \(Inspect.toStr up)" |> W4.text! { x: 0, y: 32 }
"D: \(Inspect.toStr down)" |> W4.text! { x: 0, y: 40 }

"Z: $(Inspect.toStr button2)" |> W4.text! { x: 0, y: 8 }
"L: $(Inspect.toStr left)" |> W4.text! { x: 0, y: 16 }
"R: $(Inspect.toStr right)" |> W4.text! { x: 0, y: 24 }
"U: $(Inspect.toStr up)" |> W4.text! { x: 0, y: 32 }
"D: $(Inspect.toStr down)" |> W4.text! { x: 0, y: 40 }
W4.setTextColors! { fg: None, bg: None }
"THIS IS TRASPARENT" |> W4.text! { x: 0, y: 48 }

W4.setTextColors! { fg: blue, bg: white }
"Mouse X: \(Inspect.toStr mouse.x)" |> W4.text! { x: 0, y: 56 }
"Mouse Y: \(Inspect.toStr mouse.y)" |> W4.text! { x: 0, y: 64 }
"Mouse L: \(Inspect.toStr mouse.left)" |> W4.text! { x: 0, y: 72 }
"Mouse R: \(Inspect.toStr mouse.right)" |> W4.text! { x: 0, y: 80 }
"Mouse M: \(Inspect.toStr mouse.middle)" |> W4.text! { x: 0, y: 88 }

"Mouse X: $(Inspect.toStr mouse.x)" |> W4.text! { x: 0, y: 56 }
"Mouse Y: $(Inspect.toStr mouse.y)" |> W4.text! { x: 0, y: 64 }
"Mouse L: $(Inspect.toStr mouse.left)" |> W4.text! { x: 0, y: 72 }
"Mouse R: $(Inspect.toStr mouse.right)" |> W4.text! { x: 0, y: 80 }
"Mouse M: $(Inspect.toStr mouse.middle)" |> W4.text! { x: 0, y: 88 }
W4.line! { x: 110, y: 10 } { x: 150, y: 50 }
W4.hline! { x: 5, y: 52, len: 150 }
W4.vline! { x: 80, y: 100, len: 10 }
Expand Down
76 changes: 30 additions & 46 deletions examples/rocci-bird.roc
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
app "rocci-bird"
packages {
w4: "../platform/main.roc",
}
imports [
w4.Task.{ Task },
w4.W4,
w4.Sprite.{ Sprite },
]
provides [main, Model] to w4
app [main, Model] {
w4: platform "../platform/main.roc",
}

import w4.Task exposing [Task]
import w4.W4
import w4.Sprite exposing [Sprite]

Program : {
init : Task Model [],
Expand All @@ -32,7 +29,6 @@ init =
color3: 0x42436e,
color4: 0x26013f,
}

W4.setPalette! palette

frameCount = loadRandFromDisk!
Expand Down Expand Up @@ -85,16 +81,13 @@ runTitleScreen = \prev ->
state = { prev &
rocciIdleAnim: updateAnimation prev.frameCount prev.rocciIdleAnim,
}

setTextColors!
W4.text! "Rocci Bird!!!" { x: 32, y: 12 }
W4.text! "Click to start!" { x: 24, y: 72 }

drawGround! groundSprite 0
drawPlants! plantSpriteSheet state.plants

shift = idleShift state.frameCount state.rocciIdleAnim

drawAnimation! state.rocciIdleAnim { x: playerX, y: playerStartY + shift }
gamepad = W4.getGamepad! Player1
mouse = W4.getMouse!
Expand Down Expand Up @@ -179,7 +172,6 @@ runGame = \prev ->
nextAnim: updateAnimation prev.frameCount prev.rocciFlapAnim,
flapSoundTask: Task.ok {},
}

flapSoundTask!
pipe = maybeGeneratePipe prev.lastPipeGenerated prev.frameCount |> Task.result!

Expand Down Expand Up @@ -228,7 +220,6 @@ runGame = \prev ->
W4.tone pointTone
else
Task.ok {}

pointSoundTask!
drawPipes! pipeSprite state.pipes
drawGround! groundSprite state.groundX
Expand All @@ -240,7 +231,6 @@ runGame = \prev ->

collided = playerCollided! yPixel state.rocciFlapAnim.index
drawAnimation! state.rocciFlapAnim { x: playerX, y: yPixel }

drawScore! state.score { x: 68, y: 4 }

if !collided && y < 134 then
Expand Down Expand Up @@ -277,7 +267,6 @@ initGameOver = \{ frameCount, maxScore, score, player, pipes, plants, groundX }
maxScore
else
hs

saveHighScoreToDisk! highScore

GameOver {
Expand Down Expand Up @@ -312,21 +301,18 @@ runGameOver = \prev ->
highScoreAnim,
player: { y, yVel },
}

drawPipes! pipeSprite state.pipes
drawGround! groundSprite state.groundX
drawPlants! plantSpriteSheet state.plants

yPixel = Num.floor state.player.y
drawAnimation! state.rocciFallAnim { x: playerX, y: yPixel }

W4.setShapeColors! { border: Color4, fill: Color1 }
W4.rect! { x: 16, y: 52, width: 136, height: 32 }
setTextColors!
W4.text! "Game Over!" { x: 44, y: 56 }
W4.text! "Right to restart" { x: 20, y: 72 }
W4.text! "Art by Luke DeVault" { x: 4, y: 151 }

W4.setShapeColors! { border: Color4, fill: Color1 }
W4.rect! { x: 66, y: 2, width: 28, height: 12 }
drawScore! state.score { x: 68, y: 4 }
Expand All @@ -336,9 +322,7 @@ runGameOver = \prev ->
drawAnimation state.highScoreAnim { x: 64, y: 0 }
else
Task.ok {}

highScoreTask!

W4.setShapeColors! { border: Color4, fill: Color1 }
W4.rect! { x: 54, y: 18, width: 52, height: 12 }
setTextColors!
Expand Down Expand Up @@ -395,16 +379,16 @@ onScreenCollided = \playerY, animIndex ->
List.walk collisionPoints (Task.ok Bool.false) (walkCollisionPointsHelp playerX playerY)

walkCollisionPointsHelp = \pX, pY -> \collidedTask, { x, y } ->
if collidedTask! then
Task.ok Bool.true
else
point = {
x: Num.toU8 (pX + x),
y: Num.toU8 (pY + y),
}
color = W4.getPixel! point
Task.ok (color != Color1)
if collidedTask! then
Task.ok Bool.true
else
point = {
x: Num.toU8 (pX + x),
y: Num.toU8 (pY + y),
}
color = W4.getPixel! point

Task.ok (color != Color1)

offScreenCollided =
point = {
Expand Down Expand Up @@ -463,21 +447,21 @@ randomPlant = \x ->
# W4.randBetween { start: 0, before: plantTypes }
# Biased but a least working solution:
W4.rand
|> Task.map Num.toU32
|> Task.map! \t -> t % plantTypes
|> Task.map Num.toU32
|> Task.map! \t -> t % plantTypes

Task.ok { x, type }

startingPlants : Task (List Plant) []
startingPlants =
List.range { start: At 0, end: At 14 }
|> List.walk (Task.ok (List.withCapacity 20)) \task, i ->
plant = randomPlant! (i * 12)
current = task!
|> List.walk (Task.ok (List.withCapacity 20)) \task, i ->
plant = randomPlant! (i * 12)
current = task!

current
|> List.append plant
|> Task.ok
current
|> List.append plant
|> Task.ok

updatePlants : List Plant -> List Plant
updatePlants = \plants ->
Expand All @@ -502,7 +486,6 @@ drawPlants = \spriteSheet, plants ->
drawPlant : Sprite, Plant -> Task {} []
drawPlant = \spriteSheet, { x, type } ->
sprite = Sprite.subOrCrash spriteSheet { srcX: type * 12, srcY: 0, width: 12, height: 12 }

setSpriteColors!
Sprite.blit sprite { x, y: plantY }

Expand Down Expand Up @@ -608,8 +591,9 @@ saveHighScoreToDisk = \highScore ->

loadHighScoreFromDisk : Task U8 []
loadHighScoreFromDisk =
data = W4.loadFromDisk
|> Task.onErr! \_ -> Task.ok []
data =
W4.loadFromDisk
|> Task.onErr! \_ -> Task.ok []

when data is
[_, hs, ..] ->
Expand Down Expand Up @@ -637,7 +621,7 @@ updateAnimation = \frameCount, anim ->
frames

Err _ ->
crash "animation cell out of bounds at index: \(anim.index |> Num.toStr)"
crash "animation cell out of bounds at index: $(anim.index |> Num.toStr)"
if frameCount - anim.lastUpdated < framesPerUpdate then
anim
Expand All @@ -664,7 +648,7 @@ drawAnimation = \anim, { x, y, flags ? [] } ->
Sprite.blit sprite { x, y, flags }
Err _ ->
crash "animation cell out of bounds at index: \(anim.index |> Num.toStr)"
crash "animation cell out of bounds at index: $(anim.index |> Num.toStr)"
wrappedInc = \val, count ->
next = val + 1
Expand Down
21 changes: 7 additions & 14 deletions examples/snake.roc
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
app "snake"
packages {
w4: "../platform/main.roc",
}
imports [
w4.Task.{ Task },
w4.W4.{ Gamepad },
w4.Sprite.{ Sprite },
]
provides [main, Model] to w4
app [main, Model] {
w4: platform "../platform/main.roc",
}

import w4.Task exposing [Task]
import w4.W4 exposing [Gamepad]
import w4.Sprite exposing [Sprite]

Program : {
init : Task Model [],
Expand Down Expand Up @@ -78,7 +75,6 @@ runEndScreen : Model -> Task Model []
runEndScreen = \model ->
drawGame! model
W4.setTextColors! { fg: blue, bg: white }

W4.text! "Game Over!" { x: 40, y: 72 }
Task.ok model

Expand All @@ -103,7 +99,6 @@ runGame = \model ->
fruit = fruitTask!

next = { model & snake, fruit }

drawGame! next

# Return model for next frame
Expand All @@ -120,11 +115,9 @@ drawGame = \model ->
}

Sprite.blit! model.fruitSprite { x: model.fruit.x * 8, y: model.fruit.y * 8 }

# Draw snake body
W4.setShapeColors! { border: blue, fill: green }
drawSnakeBody! model.snake

# Draw snake head
W4.setShapeColors! { border: blue, fill: blue }
drawSnakeHead model.snake
Expand Down
Loading

0 comments on commit c8d53c7

Please sign in to comment.