/
screen.lua
117 lines (86 loc) · 3.67 KB
/
screen.lua
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
-- dir.
-- A simple, minimalistic puzzle game.
require 'game/constant'
require 'game/util'
-- Since all the graphics are vector-based and the animations work with percentages
-- the game is resolution independent. The Screen object keeps track of all
-- the elements size and position.
-- This is simpler than each component having a .resize method, because the element
-- positions are dependent on each other.
-- Create a new screen object:
function Screen (game)
local self = {}
-- initialization:
self.init = function (game)
self.game = game
self.tile_size = 0
self.tile_size_small = 0
self.hud_font_size = 0
self.hud_width = 0
self.hud_height = 0
self.hud_x = 0
self.hud_y = 0
self.grid_width = 0
self.grid_height = 0
self.grid_x = 0
self.grid_y = 0
self.menu_font_size = 0
self.menu_width = 0
self.menu_height = 0
self.menu_x = 0
self.menu_y = 0
end
-- determine if a point is inside the hud:
self.point_inside_hud = function (x, y)
return point_inside_rect(x, y, self.hud_x, self.hud_y, self.hud_width, self.hud_height)
end
-- determine if a point is inside the grid:
self.point_inside_grid = function (x, y)
return point_inside_rect(x, y, self.grid_x, self.grid_y, self.grid_width, self.grid_height)
end
-- determine if a point is inside the menu:
self.point_inside_menu = function (x, y)
return point_inside_rect(x, y, self.menu_x, self.menu_y, self.menu_width, self.menu_height)
end
-- recalculate sizes and positions on a window resize:
self.resize = function (window_width, window_height)
-- element sizes (in tiles):
local hud_tile_height = 1
local menu_tile_height = 0.5
local grid_tile_width = self.game.grid_width
local grid_tile_height = self.game.grid_height
-- margin on each side of the grid (in tiles):
local grid_margin_left = 0.10
local grid_margin_right = 0.10
local grid_margin_top = hud_tile_height
local grid_margin_bottom = menu_tile_height
local grid_width_margin = grid_margin_left + grid_margin_right
local grid_height_margin = grid_margin_top + grid_margin_bottom
-- calculate the minimum tile size (in pixels) that can fill the window
-- width or height including the margin on each side:
local width_max_tile = window_width / (grid_tile_width + grid_width_margin)
local height_max_tile = window_height / (grid_tile_height + grid_height_margin)
self.tile_size = math.min(width_max_tile, height_max_tile)
self.tile_size_small = self.tile_size * (TILE_SIZE_SMALL_PERCENTAGE / 100)
-- element sizes (in pixels):
self.hud_font_size = self.tile_size
self.hud_width = window_width
self.hud_height = self.tile_size * hud_tile_height
self.grid_width = self.tile_size * grid_tile_width
self.grid_height = self.tile_size * grid_tile_height
self.menu_font_size = self.tile_size * menu_tile_height
self.menu_width = window_width
self.menu_height = self.tile_size * menu_tile_height
-- element positions (in pixels):
self.hud_x = 0
self.hud_y = 0
local grid_center_x = window_width / 2
local grid_center_y = self.hud_height + ((window_height - self.hud_height - self.menu_height) / 2)
self.grid_x = grid_center_x - (self.grid_width / 2)
self.grid_y = grid_center_y - (self.grid_height / 2)
self.menu_x = 0
self.menu_y = window_height - self.menu_height
end
self.init(game)
return self
end