Skip to content

Commit

Permalink
0.11 supported (#16)
Browse files Browse the repository at this point in the history
* update deps and get project building

* kill warnings

* update Makefile

changed psc command syntax

* make all imports explicit
  • Loading branch information
matthewleon authored and michaelficarra committed Apr 13, 2017
1 parent cf154de commit 484d886
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ deps: bower_components
doc: docs/README.md

SRC = $(shell find src -name '*.purs' -type f | sort)
FFI = $(shell find src -name '*.js' -type f | sort)

NPM = $(shell command -v npm || { echo "npm not found."; exit 1; })
PSC = $(shell command -v psc || { echo "PureScript compiler (psc) not found."; exit 1; })
PSCDOCS = $(shell command -v psc-docs)
PSCBUNDLE = $(shell command -v psc-bundle)
PSCDOCS = $(shell command -v psc-docs || { echo "Purescript doc generator (psc-docs) not found."; exit 1; })
PSCBUNDLE = $(shell command -v psc-bundle || { echo "Purescript bundler (psc-bundle) not found."; exit 1; })

NPM_BIN = $(shell npm bin)
BOWER = $(NPM_BIN)/bower
Expand All @@ -25,9 +24,7 @@ dist/$(MODULE).js: bower_components $(SRC) $(FFI)
mkdir -p "$(@D)"
$(PSC) \
'bower_components/purescript-*/src/**/*.purs' \
--ffi 'bower_components/purescript-*/src/**/*.js' \
$(SRC) \
$(FFI:%=--ffi %) \
--verbose-errors
$(PSCBUNDLE) \
'output/**/*.js' \
Expand Down
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"package.json"
],
"dependencies": {
"purescript-dom": "~0.1.2",
"purescript-foldable-traversable": "~0.4.0",
"purescript-math": "~0.2.0",
"purescript-random": "~0.2.0",
"purescript-signal": "michaelficarra/purescript-signal#GH-29"
"purescript-dom": "^4.1.0",
"purescript-foldable-traversable": "^3.0.0",
"purescript-math": "^2.0.0",
"purescript-random": "^3.0.0",
"purescript-signal": "^9.0.0"
},
"devDependencies": {}
}
35 changes: 25 additions & 10 deletions src/Mario.purs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Mario where

import Prelude ((<<<), (*), (+), (-), (==), (<), (>), (<=), (<>), (&&), not)
import Control.Monad.Eff (Eff())
import DOM (DOM(), Node())
import Math (abs, max, min)
import DOM.Node.Types (Node)
import Math (abs)
import Prelude (max, min, negate, not, otherwise, (&&), (*), (+), (-), (<<<), (<=), (<>), (==), (>))

type Character = {
node :: Node,
Expand All @@ -17,13 +16,28 @@ data Direction = Left | Right
type SpriteDescriptor = String
data Activity = Walking | Standing | Jumping

gravity :: Number
gravity = 0.15 -- px / frame^2

maxMoveSpeed :: Number
maxMoveSpeed = 2.5 -- px / frame

groundAccel :: Number
groundAccel = 0.06 -- px / frame^2

airAccel :: Number
airAccel = 0.04 -- px / frame^2

groundFriction :: Number
groundFriction = 0.1 -- px / frame^2

airFriction :: Number
airFriction = 0.02 -- px / frame^2

jumpCoefficient :: Number
jumpCoefficient = 0.8 -- px / frame^3

minJumpSpeed :: Number
minJumpSpeed = 4.0 -- px / frame

charSpriteDescriptor :: Character -> SpriteDescriptor
Expand All @@ -39,8 +53,8 @@ charSpriteDescriptor c = "character " <> activityDesc (currentActivity c) <> " "
dirDesc Right = "right"

currentActivity :: Character -> Activity
currentActivity c | isAirborne c = Jumping
currentActivity c | c.dx == 0.0 = Standing
currentActivity c' | isAirborne c' = Jumping
currentActivity c' | c'.dx == 0.0 = Standing
currentActivity _ = Walking

isAirborne :: Character -> Boolean
Expand Down Expand Up @@ -69,10 +83,11 @@ walk _ _ c = applyFriction c
where
-- Mario slows down when he is not attempting to move himself
applyFriction :: Character -> Character
applyFriction c | c.dx == 0.0 = c
applyFriction c | abs c.dx <= friction c = c { dx = 0.0 }
applyFriction c | c.dx > 0.0 = c { dx = c.dx - friction c }
applyFriction c | c.dx < 0.0 = c { dx = c.dx + friction c }
applyFriction c'
| c'.dx == 0.0 = c'
| abs c'.dx <= friction c' = c' { dx = 0.0 }
| c'.dx > 0.0 = c' { dx = c'.dx - friction c' }
| otherwise = c' { dx = c'.dx + friction c' }

-- Mario can change his vertical acceleration when he is on the ground, proportional to his current speed
jump :: Boolean -> Character -> Character
Expand Down
12 changes: 7 additions & 5 deletions src/Mario/DOM.purs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
module Mario.DOM where

import Control.Monad.Eff (Eff())
import DOM (DOM(), Node())
import Prelude ((<<<), (+), Unit())
import Control.Monad.Eff (Eff)
import DOM (DOM)
import DOM.Node.Types (Node)
import Prelude ((<<<), (+), Unit)

import Mario (charSpriteDescriptor, Character(), SpriteDescriptor())

groundHeight :: Number
groundHeight = 40.0 --px

foreign import updatePositionP :: forall eff. Character -> Eff (dom :: DOM | eff) Unit

updatePosition :: Character -> Eff _ Unit
updatePosition :: forall eff. Character -> Eff (dom :: DOM | eff) Unit
updatePosition = updatePositionP <<< offsetY groundHeight
where
offsetY :: Number -> Character -> Character
offsetY amount c = c { y = c.y + amount }

foreign import updateSpriteP :: forall eff. Node -> SpriteDescriptor -> Eff (dom :: DOM | eff) Unit

updateSprite :: forall eff. Character -> Eff _ Unit
updateSprite :: forall eff. Character -> Eff (dom :: DOM | eff) Unit
updateSprite c = updateSpriteP c.node (charSpriteDescriptor c)

foreign import onDOMContentLoaded :: forall a eff. Eff (dom :: DOM | eff) a -> Eff (eff) Unit
Expand Down
31 changes: 18 additions & 13 deletions src/Mario/Main.purs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
module Mario.Main (main) where

import Control.Monad.Eff (Eff())
import Prelude ((<$>), (<*>), bind, return, unit, Unit())
import Signal (foldp, runSignal, sampleOn, Signal())
import Signal.DOM (animationFrame, keyPressed)
import Signal.Time (Time())

import Mario (charSpriteDescriptor, marioLogic, Character(), Direction(Left, Right), SpriteDescriptor())
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Timer (TIMER)
import DOM (DOM)
import Mario (marioLogic, Character, Direction(..))
import Mario.DOM (getMarioNode, onDOMContentLoaded, updatePosition, updateSprite)
import Prelude (Unit, bind, discard, negate, pure, (<$>), (<*>))
import Signal (foldp, runSignal, sampleOn)
import Signal.DOM (animationFrame, keyPressed)

type GameState = { mario :: Character }

leftKeyCode :: Int
leftKeyCode = 37

rightKeyCode :: Int
rightKeyCode = 39

jumpKeyCode :: Int
jumpKeyCode = 38

initialState :: Eff _ GameState
initialState :: forall eff. Eff (dom :: DOM | eff) GameState
initialState = do
marioNode <- getMarioNode
return {
pure {
mario: {
node: marioNode,
x: -50.0,
Expand All @@ -29,18 +34,18 @@ initialState = do
}
}

gameLogic :: { left :: Boolean, right :: Boolean, jump :: Boolean } -> Eff _ GameState -> Eff _ GameState
gameLogic :: forall eff. { left :: Boolean, right :: Boolean, jump :: Boolean } -> Eff (dom :: DOM | eff) GameState -> Eff (dom :: DOM | eff) GameState
gameLogic inputs gameState = do
gs <- gameState
return (gs { mario = marioLogic inputs gs.mario })
pure (gs { mario = marioLogic inputs gs.mario })

render :: Eff _ GameState -> Eff _ Unit
render :: forall eff. Eff (dom :: DOM | eff) GameState -> Eff (dom :: DOM | eff) Unit
render gameState = do
gs <- gameState
updatePosition gs.mario
updateSprite gs.mario

main :: Eff _ Unit
main :: forall eff. Eff (timer :: TIMER | eff) Unit
main = onDOMContentLoaded do
frames <- animationFrame
leftInputs <- keyPressed leftKeyCode
Expand Down

0 comments on commit 484d886

Please sign in to comment.