Last active
January 9, 2016 23:57
-
-
Save bruno-cadorette/39db5a41dc01211e3425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Keyboard | |
import Mouse exposing (position) | |
import Time exposing (Time, fps) | |
import Debug exposing(..) | |
import Signal | |
import Graphics.Element exposing (show) | |
import Graphics.Collage | |
import Color exposing (red, black) | |
import Window exposing (dimensions) | |
type alias Point = { x : Int, y : Int } | |
type alias Dude = { position : Point, direction : Point, orientation : Point } | |
type Event = Tick Time | Move Point | Orientation Point | |
opPoint : (Int -> Int -> Int) -> Point -> Point -> Point | |
opPoint f p1 p2 = { x = p1.x `f` p2.x, y = p1.y `f` p2.y } | |
getOrientation : Point -> Point -> Float | |
getOrientation p1 p2 = | |
let p1x = toFloat p1.x | |
p2x = toFloat p2.x | |
p1y = toFloat p1.y | |
p2y = toFloat p2.y | |
in | |
watch "orientation" <| (radians <| atan2 (p2y - p1y) (p2x - p1x) ) | |
moveDude : Event -> Dude -> Dude | |
moveDude event dude = | |
let plus = opPoint (+) | |
in | |
case event of | |
Tick time -> { dude | position = (dude.position `plus` dude.direction) } | |
Move p -> { dude | direction = p, position = (dude.position `plus` p) } | |
Orientation p -> { dude | orientation = p } | |
initialDude = { position = {x=0,y=0}, direction = {x=0,y=0}, orientation = {x=0,y=0} } | |
dudeBody : Graphics.Collage.Form | |
dudeBody = Graphics.Collage.group | |
[Graphics.Collage.filled red (Graphics.Collage.circle 10.0), | |
Graphics.Collage.filled black (Graphics.Collage.rect 8 40)] | |
toTuple p = (p.x,p.y) | |
keyboardInput = | |
let wasd = Signal.map Move Keyboard.wasd | |
time = Signal.map Tick <| fps 30 | |
mouse = Signal.map (\(x,y) -> Orientation <| watchSummary "mouse" toTuple { x = x, y = y }) position | |
in Signal.mergeMany [time, wasd, mouse] | |
mapOrientation : Int -> Int -> Point -> Point | |
mapOrientation w h p = watchSummary "mapOrientation" toTuple <| { x = p.x - w // 2, y = h // 2 - p.y } | |
display (w, h) dude = Graphics.Collage.collage w h <| | |
[Graphics.Collage.rotate (getOrientation dude.position <| mapOrientation w h dude.orientation) | |
<| Graphics.Collage.move (watch "position" (toFloat dude.position.x, toFloat dude.position.y)) | |
<| dudeBody] | |
main = Signal.map2 display dimensions <| Signal.foldp moveDude initialDude keyboardInput |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment