Skip to content

Commit cbc7c26

Browse files
authored
BehaviorSuperCircleJs touchups (#223)
- Fewer magic numbers for rendering - Condense canvas contex code - Edit readme
1 parent d096955 commit cbc7c26

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Running a web-compatible recipe:
8787
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/AceEditorHalogenHooks/src/Main.purs) - [fixme](recipes/AceEditorHalogenHooks/tryFixMe.md)) | [AceEditorHalogenHooks](recipes/AceEditorHalogenHooks) | A Halogen Hooks port of the ["Ace Editor" Halogen Example](https://github.com/purescript-halogen/purescript-halogen/tree/master/examples/ace). |
8888
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/AddRemoveEventListenerJs/src/Main.purs) - [fixme](recipes/AddRemoveEventListenerJs/tryFixMe.md)) | [AddRemoveEventListenerJs](recipes/AddRemoveEventListenerJs) | This recipe shows how to add and remove an event listener to an HTML element. |
8989
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BasicHalogenHooks/src/Main.purs)) | [BasicHalogenHooks](recipes/BasicHalogenHooks) | Displays a button that toggles the label to "On" and "Off". |
90-
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BehaviorSuperCircleJs/src/Main.purs)) | [BehaviorSuperCircleJs](recipes/BehaviorSuperCircleJs) | A simplified Super Hexagon clone written in PureScript. There are only two controls: the left and right arrow keys. |
90+
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BehaviorSuperCircleJs/src/Main.purs)) | [BehaviorSuperCircleJs](recipes/BehaviorSuperCircleJs) | A simplified Super Hexagon clone written with [behaviors](https://pursuit.purescript.org/packages/purescript-behaviors). |
9191
| :heavy_check_mark: | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BigIntJs/src/Main.purs)) | [BigIntJs](recipes/BigIntJs) | This recipe shows how to print, create, and use values of the `BigIntJs` type in either the node.js or web browser console. |
9292
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BookHalogenHooks/src/Main.purs)) | [BookHalogenHooks](recipes/BookHalogenHooks) | A Halogen port of the ["HTTP - Book" Elm Example](https://elm-lang.org/examples/book). |
9393
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/BookReactHooks/src/Main.purs)) | [BookReactHooks](recipes/BookReactHooks) | A React port of the ["HTTP - Book" Elm Example](https://elm-lang.org/examples/book). |
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# BehaviorSuperCircleJs
22

3-
A simplified Super Hexagon clone written in PureScript. There are only two controls: the left and right arrow keys.
3+
A simplified Super Hexagon clone written with [behaviors](https://pursuit.purescript.org/packages/purescript-behaviors).
44

55
## Expected Behavior:
66

@@ -10,6 +10,5 @@ Loads the game in a canvas on screen. Use left/right arrow keys to move. Score r
1010

1111
## Resources
1212

13-
[original repository](https://github.com/i-am-tom/purescript-super-circles)
14-
[Presentation (youtube)](https://www.youtube.com/watch?v=DWNnPh0o4NE)
15-
[behaviors](https://pursuit.purescript.org/packages/purescript-behaviors/7.0.0)
13+
- [original repository](https://github.com/i-am-tom/purescript-super-circles)
14+
- [Presentation (youtube)](https://www.youtube.com/watch?v=DWNnPh0o4NE)

recipes/BehaviorSuperCircleJs/src/Main.purs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,22 @@ update (Seconds timestamp) control previous
150150

151151
{- PAINT -}
152152

153+
-- | Constants
154+
size :: Number
155+
size = 400.0
156+
157+
halfSize :: Number
158+
halfSize = size / 2.0
159+
160+
eighthSize :: Number
161+
eighthSize = size / 8.0
162+
153163
-- | Given the timestamp, we need to work out how "wide" we should draw the
154164
-- | obstacle. This function does some ugly maths to tell us just that.
155165

156166
timestampToObstacleRadius :: Number -> Number
157167
timestampToObstacleRadius time
158-
= 50.0 + remainder * 75.0
168+
= eighthSize + remainder * size * 3.0 / 16.0
159169
where
160170
-- 2.0 is "brand new", 0.0 is "just finished"
161171
remainder :: Number
@@ -174,29 +184,29 @@ background
174184
backdrop
175185
= filled
176186
(fillColor white)
177-
(rectangle 0.0 0.0 400.0 400.0)
187+
(rectangle 0.0 0.0 size size)
178188

179189
-- The guideline shows the path of the cursor, but never actually moves.
180190
guideline
181191
= outlined
182192
(outlineColor (graytone 0.8))
183-
(circle 200.0 200.0 50.0)
193+
(circle halfSize halfSize eighthSize)
184194

185195
-- | Given the value of the score, produce the display: simple text in the
186196
-- | top-right of the canvas.
187197

188198
score :: Int -> Drawing
189199
score value
190-
= text (font monospace 12 mempty) 300.0 50.0
200+
= text (font monospace 12 mempty) (size * 0.75) eighthSize
191201
(fillColor black) (show value)
192202

193203
-- | Given the cursor's current rotation, we can work out where to draw it on
194204
-- | the canvas using a little bit of trigonometry.
195205

196206
rotationToCursorPosition :: Number -> { x :: Number, y :: Number }
197207
rotationToCursorPosition rotation
198-
= { x: 200.0 + 50.0 * cos rotation
199-
, y: 200.0 + 50.0 * sin rotation
208+
= { x: halfSize + eighthSize * cos rotation
209+
, y: halfSize + eighthSize * sin rotation
200210
}
201211

202212
-- | Rotation is usually around the origin, which isn't really what we want. To
@@ -205,9 +215,9 @@ rotationToCursorPosition rotation
205215

206216
rotateAroundCentre :: Number -> Drawing -> Drawing
207217
rotateAroundCentre angle
208-
= translate 200.0 200.0
218+
= translate halfSize halfSize
209219
<<< rotate angle
210-
<<< translate (-200.0) (-200.0)
220+
<<< translate (-halfSize) (-halfSize)
211221

212222
-- | Given all the information we calculated in the `update` function, we can
213223
-- | now work out what we want to draw for the next render frame in our game's
@@ -235,7 +245,7 @@ paint state
235245
-- Centre the obstacle and rotate accordingly.
236246
positionObstacle :: Drawing -> Drawing
237247
positionObstacle
238-
= translate 200.0 200.0
248+
= translate halfSize halfSize
239249
<<< rotate (timestampToObstacleRotation obstacleStart)
240250

241251
-- When did this obstacle start?
@@ -362,10 +372,12 @@ renderLoop keyboard
362372

363373
main :: Effect Unit
364374
main = do
375+
-- Grab the keyboard to thread through to 'controls'.
376+
keyboard <- getKeyboard
377+
378+
-- Grab canvas context for rendering.
365379
ctx <- getRenderNode
366380

367-
-- Grab the keybord to thread through to 'controls'.
368-
keyboard <- getKeyboard
369381
-- Animate the canvas!
370382
_ <- animate (renderLoop keyboard) (render ctx)
371383

@@ -382,18 +394,9 @@ getRenderNode :: Effect Context2D
382394
getRenderNode = do
383395
htmlDoc <- document =<< window
384396
body <- maybe (throw "Could not find body element") pure =<< HTMLDocument.body htmlDoc
385-
let
386-
doc = HTMLDocument.toDocument htmlDoc
387-
canvasElem <- createElement "canvas" doc
397+
canvasElem <- createElement "canvas" $ HTMLDocument.toDocument htmlDoc
388398
setId "canvas" canvasElem
389-
let
390-
bodyNode = HTMLElement.toNode body
391-
canvasNode = Element.toNode canvasElem
392-
void $ appendChild canvasNode bodyNode
399+
void $ appendChild (Element.toNode canvasElem) (HTMLElement.toNode body)
393400
canvas <- maybe (throw "Could not find canvas") pure =<< getCanvasElementById "canvas"
394-
let
395-
width = 400.0
396-
height = 400.0
397-
_ <- setCanvasDimensions canvas { height, width }
398-
ctx <- getContext2D canvas
399-
pure ctx
401+
_ <- setCanvasDimensions canvas { height: size, width: size}
402+
getContext2D canvas

0 commit comments

Comments
 (0)