-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.hs
145 lines (122 loc) · 4.29 KB
/
Block.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
module Block where
import Data.Tuple
import Graphics.Gloss
data Position =
Position { x_pos :: Int
, y_pos :: Int }
data Block =
Block { blockWidth :: Int
, blockHeight :: Int
, blockColour :: Color
, blockPosition :: Position }
botLeft :: Block -> Position
botLeft = blockPosition
botRight :: Block -> Position
botRight b = let bl = botLeft b in bl { x_pos = x_pos bl + blockWidth b }
topLeft :: Block -> Position
topLeft b = let bl = botLeft b in bl { y_pos = y_pos bl + blockHeight b }
topRight :: Block -> Position
topRight b = let br = botRight b in br { y_pos = y_pos br + blockHeight b }
corners :: Block -> [Position]
corners b = fmap ($b) [ botLeft, botRight, topLeft, topRight ]
move_x :: Block -> Int -> Block
move_x b v = b { blockPosition = pos { x_pos = x_pos pos + v } }
where pos = blockPosition b
move_y :: Block -> Int -> Block
move_y b v = b { blockPosition = pos { y_pos = y_pos pos + v } }
where pos = blockPosition b
data Side = North | West | South | East
deriving (Eq, Enum)
data Door =
Door { doorSize :: Int
, doorDistance :: Int
, doorSide :: Side }
data Board =
Board { boardWidth :: Int
, boardHeight :: Int
, door :: Door
, content :: [Block]
}
displayBlock :: Int -> Block -> Picture
displayBlock k Block{..} =
color blockColour $ polygon $
[ step 0 0
, step 0 blockHeight
, step blockWidth blockHeight
, step blockWidth 0
]
where
step x y = (stepx x, stepy y)
stepx x = fromIntegral $ k * (x_pos blockPosition + x)
stepy y = fromIntegral $ k * (y_pos blockPosition + y)
displayDoor :: Int -> Door -> Picture
displayDoor k Door{..} =
(if doorSide `elem` [North, South]
then translate (fromIntegral $ k * (doorDistance + 1)) 0
else translate 0 (fromIntegral $ k * (doorDistance + 1)))
$ displayBlock k
$ (if doorSide `elem` [North, South]
then Block doorSize 1
else Block 1 doorSize) black
$ Position 0 0
displayFocus :: Int -> Block -> Picture
displayFocus k Block{..} =
color (dark $ dark blockColour) $ Pictures $
[ polygon
[ (stepx 0, stepy 0)
, (stepx 0, stepy (k * blockHeight))
, (stepx factor, stepy (k * blockHeight))
, (stepx factor, stepy 0) ]
, polygon
[ (stepx 0, stepy 0)
, (stepx (k * blockWidth), stepy 0)
, (stepx (k * blockWidth), stepy factor)
, (stepx 0, stepy factor) ]
, polygon
[ (stepx (k * blockWidth), stepy 0)
, (stepx (k * blockWidth), stepy (k * blockHeight))
, (stepx (k * blockWidth - factor), stepy (k * blockHeight))
, (stepx (k * blockWidth - factor), stepy 0) ]
, polygon
[ (stepx 0, stepy (k * blockHeight))
, (stepx (k * blockWidth), stepy (k * blockHeight))
, (stepx (k * blockWidth), stepy (k * blockHeight - factor))
, (stepx 0, stepy (k * blockHeight - factor)) ]
]
where
factor = 10
stepx x = fromIntegral $ k * x_pos blockPosition + x
stepy y = fromIntegral $ k * y_pos blockPosition + y
displayWall :: Int -> Board -> Picture
displayWall k Board{..} =
Pictures $ fmap translateWall $
[ (North, wallH), (South, wallH), (East, wallV), (West, wallV)
, (doorSide door, displayDoor k door) ]
where
w = fromIntegral (k * boardWidth)
h = fromIntegral (k * boardHeight)
mk = fromIntegral (-k)
wallH = displayBlock k $ Block (2 + boardWidth) 1 (greyN 0.2) $ Position 0 0
wallV = displayBlock k $ Block 1 (2 + boardHeight) (greyN 0.2) $ Position 0 0
translateWall (side, r) = translate x y r
where
x = if side == East then w else mk
y = if side == North then h else mk
displayBoard :: Int -> Board -> Picture
displayBoard k b@Board{..} =
translate (-w/2) (-h/2)
$ Pictures
$ displayWall k b
: tiles : fmap (displayBlock k) content
++ [displayFocus k (head content)]
where
w = fromIntegral (k * boardWidth)
h = fromIntegral (k * boardHeight)
tiles =
Color (greyN 0.2) $ Pictures $
fmap line (lines boardWidth boardHeight) ++
fmap (line . fmap swap) (lines boardHeight boardWidth)
lines n l = [ [ (x, 0), (x, fromIntegral $ k * l) ]
| x <- fmap fromIntegral [0,k..(k*n)] ]