1
- moonshine = require ' lib/moonshine'
1
+ local moonshine = require ' lib/moonshine'
2
+ local utils = require ' utils'
3
+ local handle_input = require ' input_handlers'
2
4
3
5
function love .load ()
6
+ -- Loading shaders
4
7
s_scanlines = moonshine (moonshine .effects .scanlines )
5
8
.chain (moonshine .effects .crt )
6
9
s_scanlines .scanlines .thickness = 0.5
@@ -12,81 +15,84 @@ function love.load()
12
15
s_screen .scanlines .thickness = 0.5
13
16
s_screen .crt .scaleFactor = {1.15 , 1.15 }
14
17
18
+ -- Loading font
15
19
font = love .graphics .newFont (" assets/fonts/Press_Start_2P/PressStart2P-Regular.ttf" , 24 )
20
+ love .graphics .setFont (font )
21
+
22
+ -- Loading images
16
23
frame = love .graphics .newImage (" assets/images/screen-frame.png" )
17
- mouseXY = {0 , 0 }
18
- tileXY = {0 , 0 }
19
- playerXY = {0 , 0 }
24
+
25
+ -- Initialiazing game state.
26
+ game = {
27
+ playerXY = {0 , 0 },
28
+ user_input = {
29
+ keys = {},
30
+ mouseXY = {0 , 0 },
31
+ tileXY = {0 , 0 },
32
+ pressed_key = false
33
+ }
34
+ }
35
+ action = {}
20
36
end
21
37
22
38
function love .update (dt )
39
+ if game .user_input .pressed_key then
40
+ action = handle_input (game .user_input )
41
+ move = action [' move' ]
42
+ exit = action [' exit' ]
43
+ fullscreen = action [' fullscreen' ]
44
+
45
+ if move then
46
+ dx , dy = unpack (move )
47
+ game .playerXY = {game .playerXY [1 ] + dx , game .playerXY [2 ] + dy }
48
+ end
49
+
50
+ if exit then
51
+ love .event .quit ()
52
+ end
53
+
54
+ if fullscreen then
55
+ love .window .setFullscreen (not love .window .getFullscreen (), " exclusive" )
56
+ end
57
+
58
+ game .user_input .pressed_key = false
59
+ end
23
60
end
24
61
25
62
function love .keypressed (key )
26
- if key == " z " then
27
- print ( tileXY [ 1 ] .. " ; " .. tileXY [ 2 ])
28
- end
63
+ game . user_input . keys [ key ] = true
64
+ game . user_input . pressed_key = true
65
+ end
29
66
30
- if key == " up" then
31
- playerXY = {playerXY [1 ], playerXY [2 ] - 1 }
32
- elseif key == " down" then
33
- playerXY = {playerXY [1 ], playerXY [2 ] + 1 }
34
- elseif key == " left" then
35
- playerXY = {playerXY [1 ] - 1 , playerXY [2 ]}
36
- elseif key == " right" then
37
- playerXY = {playerXY [1 ] + 1 , playerXY [2 ]}
38
- end
67
+ function love .keyreleased (key )
68
+ game .user_input .keys [key ] = nil
39
69
end
40
70
41
71
function love .mousemoved (x , y , dx , dy , istouch )
42
72
-- Correcting mouse coordinates.
43
- mouseXY = fisheye ({x , y }, 640 , 496 , 1.15 , 1.06 , 1.065 )
44
- tileXY = {math.floor (mouseXY [1 ] / 24 ), math.floor (mouseXY [2 ] / 24 )}
73
+ game . user_input . mouseXY = utils . fisheye ({x , y }, 640 , 496 , 1.15 , 1.06 , 1.065 )
74
+ game . user_input . tileXY = {math.floor (game . user_input . mouseXY [1 ] / 24 ), math.floor (game . user_input . mouseXY [2 ] / 24 )}
45
75
end
46
76
47
77
function love .draw ()
48
- love .graphics .setFont (font )
49
-
50
78
s_scanlines (function ()
51
79
love .graphics .setColor ({5 , 5 , 5 , 255 })
52
80
love .graphics .rectangle (" fill" , 0 , 0 , 640 , 480 )
53
81
end )
54
82
55
83
s_screen (function ()
56
- drawChar (" @" , playerXY [1 ], playerXY [2 ], {255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 })
57
- if tileXY [1 ] == playerXY [1 ] and tileXY [2 ] == playerXY [2 ] then
58
- drawChar (" @" , playerXY [1 ], playerXY [2 ], {255 , 127 , 127 , 255 }, {127 , 255 , 127 , 255 })
84
+ drawChar (" @" , game . playerXY [1 ], game . playerXY [2 ], {255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 })
85
+ if game . user_input . tileXY [1 ] == game . playerXY [1 ] and game . user_input . tileXY [2 ] == game . playerXY [2 ] then
86
+ drawChar (" @" , game . playerXY [1 ], game . playerXY [2 ], {255 , 127 , 127 , 255 }, {127 , 255 , 127 , 255 })
59
87
end
60
88
-- cursors
61
89
love .graphics .setColor ({255 , 0 , 0 , 255 })
62
- love .graphics .rectangle (" fill" , mouseXY [1 ] - 5 , mouseXY [2 ] - 5 , 10 , 10 )
90
+ love .graphics .rectangle (" fill" , game . user_input . mouseXY [1 ] - 5 , game . user_input . mouseXY [2 ] - 5 , 10 , 10 )
63
91
end )
64
92
65
93
love .graphics .draw (frame , 0 , 0 )
66
94
end
67
95
68
- function readAll (file )
69
- local f = io.open (file , " rb" )
70
- local content = f :read (" *all" )
71
- f :close ()
72
- return content
73
- end
74
-
75
- function inArea (x , y , area_x , area_y , area_w , area_h )
76
- return (((x >= area_x ) and (x <= area_x + area_w )) and ((y >= area_y ) and (y <= area_y + area_h )))
77
- end
78
-
79
- function fisheye (vec , w , h , scale , distortX , distortY )
80
- result = {vec [1 ] / w , vec [2 ] / h }
81
- result = {result [1 ] * 2.0 - 1.0 , result [2 ] * 2.0 - 1.0 }
82
- result = {result [1 ] * scale , result [2 ] * scale }
83
- result = {result [1 ] + (result [2 ] * result [2 ] * result [1 ] * (distortX - 1 )),
84
- result [2 ] + (result [1 ] * result [1 ] * result [2 ] * (distortY - 1 ))}
85
- result = {(result [1 ] + 1.0 ) / 2.0 , (result [2 ] + 1.0 ) / 2.0 }
86
- result = {result [1 ] * w , result [2 ] * h }
87
- return result
88
- end
89
-
90
96
function drawChar (char , x , y , color , bg )
91
97
love .graphics .setColor (bg )
92
98
love .graphics .rectangle (' fill' , x * 24 , y * 24 , 24 , 24 )
0 commit comments