-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventHandler.hs
93 lines (61 loc) · 2.38 KB
/
EventHandler.hs
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
{-
File : EventHandler.hs
Copyright : (c) Matt Teichman, 3/15/17
Functions for use in Galaga.hs that manipulate the enemies.
(Note that enemies, bullets, and the player are all of type Sprite.)
-}
module EventHandler where
import GameState
import Graphics.Gloss.Interface.IO.Game
import Choreography
import EnemyMoves
import PlayerMoves
import Sprite
import System.Random
-- The event handler handles events coming from the user
eventHandler :: Event -> GameState -> IO GameState
-- Pattern matches for Key Events in main menu
eventHandler (EventKey (Char key) Up _ _) state@(MainMenu newScore)
= case key of
's' -> return $ initGame (level2Init 1)
'q' -> return Quit
'h' -> return $ HighScores newScore
_ -> return state
-- Pattern matches for Key Events while playing a game
eventHandler (EventKey (Char key) Up _ _) state@(Game _ _ _ _ _ _ _)
= case key of
'q' -> return (GameOver (score state))
'1' -> return $ changeLevel 1 state
'2' -> return $ changeLevel 2 state
'3' -> return $ changeLevel 3 state
'4' -> return $ changeLevel 4 state
'p' -> attackersFire state
_ -> return state
-- Pattern matches for Special Key Events in the game
-- space to fire
-- left to move left
-- right to move right
eventHandler (EventKey (SpecialKey key) Down _ _) state@(Game _ _ _ _ _ _ _)
= case key of
KeyLeft -> return (plyrChgDrState L state)
KeyRight -> return (plyrChgDrState R state)
KeySpace -> return $ pFireBullet state
_ -> return state
eventHandler (EventKey (SpecialKey key) Up _ _) state@(Game _ _ _ _ _ _ _)
= case key of
KeyLeft -> return $ plyrChgDrState NoMove state
KeyRight -> return $ plyrChgDrState NoMove state
_ -> return state
-- Pattern matches for Key Events in the High Scores screen
eventHandler (EventKey _ Up _ _) state@(HighScores newScore)
= return (MainMenu newScore)
-- Pattern matches for Key Events during Game Over screen
-- (transitions to main screen once user presses a key)
eventHandler (EventKey _ Up _ _) state@(GameOver newScore)
= return $ MainMenu newScore
-- Pattern matches for mouse events
eventHandler (EventKey (MouseButton _) Up _ (xPos,yPos)) state = do
putStrLn $ "Mouse Click on (" ++ show xPos ++ ", " ++ show yPos ++ ")"
return state
-- Catch-All pattern
eventHandler _ state = return state