This repository has been archived by the owner on Mar 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.elm
99 lines (81 loc) · 2.77 KB
/
Pong.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{- |
Module : Pong
Description : Implementation of Extreme Pong.
Copyright : (c) Jeff Smits
License : GPL-3.0
Maintainer : [email protected]
Stability : experimental
Portability : portable
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-}
module Pong where
-------------
-- GENERAL --
-------------
{-
Sadly the compiler can't handle a tuple with the default ball
velocity in the definition of defaultBall when these are defined.
type Position = (Float, Float)
type Velocity = (Float, Float)
-}
import Model
import View
import Controller
--------
-- IO --
--------
-- foldps is a signal which is dependant on a past (hidden) state
foldps : (a -> (b,s) -> (b,s)) -> (b,s) -> Signal a -> Signal b
foldps f s sig = lift fst $ foldp f s sig
foldps': (a -> (b,s) -> (b,s)) -> (a -> (b,s)) -> Signal a -> Signal b
foldps' f sf sig = lift fst $ foldp' f sf sig
-- Game pause state signal
-- playing :: Signal Bool
{-playing = foldps'
(\sp (p,lastSp) ->
if sp && not lastSp
then (not p, sp)
else (p, sp) )
(\sp -> (False, False) )
Keyboard.space-}
-- Time signal
-- delta :: Signal Float
delta = inSeconds <~ fps 50
-- Input signal
-- userInput :: Signal UserInput
userInput = UserInput <~ Keyboard.space
~ (lift .y Keyboard.wasd)
~ (lift .y Keyboard.arrows)
{-
Combination of input and time signals
The let expession using foldps makes sure the Keyboard.space signal
turns into a signal which turns true once when the Keyboard.Space
signal goes from false to true. This can only be handled here because
of the sampleOn function used to define sig.
input :: Signal Input
-}
input = let f (Input d (UserInput sp' l r)) (_, sp) =
if not sp then (Input d (UserInput sp' l r), sp')
else (Input d (UserInput False l r), sp')
s = (defaultInput, False)
sig = sampleOn delta $ lift2 Input delta userInput
in foldps f s sig
-- State signal
-- state :: Signal State
state = foldp stepGame defaultGame input
{-
Lift the display of the State over a Signal of
the window dimensions and a Signal of the State.
-}
main = display <~ delta
~ Window.dimensions
~ state