-
Notifications
You must be signed in to change notification settings - Fork 15
/
Field.hs
104 lines (85 loc) · 2.15 KB
/
Field.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
-- Field
{-
' ' Space
'*' Invisible wall (Temporal char when player punch a wall)
-}
module Field (
Field,
Cell,
loadField,
fieldRef,
fieldSet,
isBlock,
hardBlock,
renderField,
chr2img
) where
import Graphics.UI.SDL (Surface)
import Const
import Images
import Util
import AppUtil
type Cell = Char
type Field = [[Cell]]
-- Map
-- Load map
loadField :: Int -> IO Field
loadField stage = readFile fn >>= return . lines
where
fn = "data/stage" ++ (show stage) ++ ".map"
chr2img :: Char -> ImageType
chr2img '@' = ImgBlock1
chr2img 'O' = ImgBlock2
chr2img 'X' = ImgBlock3
chr2img '?' = ImgBlock4
chr2img '#' = ImgBlock5
chr2img '_' = ImgMt02
chr2img '/' = ImgMt11
chr2img ',' = ImgMt12
chr2img '\\' = ImgMt13
chr2img '.' = ImgMt22
chr2img '1' = ImgCloud00
chr2img '2' = ImgCloud01
chr2img '3' = ImgCloud02
chr2img '4' = ImgCloud10
chr2img '5' = ImgCloud11
chr2img '6' = ImgCloud12
chr2img '7' = ImgGrass0
chr2img '8' = ImgGrass1
chr2img '9' = ImgGrass2
chr2img '[' = ImgDk00
chr2img ']' = ImgDk01
chr2img 'l' = ImgDk10
chr2img '|' = ImgDk11
chr2img 'o' = ImgPole0
chr2img '!' = ImgPole1
chr2img 'K' = ImgBlock4
chr2img _ = undefined
isBlock :: Cell -> Bool
isBlock = (`elem` "@OX?#[]l|*K")
hardBlock :: Cell -> Bool
hardBlock = (`elem` "#X@*")
inField :: Field -> Int -> Int -> Bool
inField fld x y = 0 <= y && y < length fld && 0 <= x && x < length (fld !! y)
fieldRef :: Field -> Int -> Int -> Cell
fieldRef fld x y
| inField fld x y = fld !! y !! x
| otherwise = ' '
fieldSet :: Field -> Int -> Int -> Cell -> Field
fieldSet fld x y c
| inField fld x y = replace fld y $ replace (fld !! y) x c
| otherwise = fld
renderField :: Surface -> ImageResource -> Int -> Field -> IO ()
renderField sur imgres scrx fld =
sequence_ $ concatMap lineProc $ zip [0..] fld
where
lineProc (y, ln) = map (cellProc y) $ zip [0..] $ window ln
cellProc y (x, c)
| c `elem` " *" = return ()
| otherwise = putchr x y c >> return ()
putchr x y c = putimg sur imgres (chr2img c) (x*chrSize - rx) (y*chrSize - 8)
-- 表示される部分だけ取り出す
window = take w . drop qx
qx = scrx `div` chrSize
rx = scrx `mod` chrSize
w = 256 `div` chrSize + 1