-
Notifications
You must be signed in to change notification settings - Fork 4
/
engine.lua
179 lines (125 loc) · 2.68 KB
/
engine.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function engine_init()
-- set background
gr.setBackgroundColor(hsl(unpack(colors.sky_night)))
-- set tile size
base_w = 0x20 --* math.sqrt(3)/2
base_h = 0x10
base_z = 0x20
tile_update()
-- load map
map = map_load(require "maps.map_1")
-- get screen size
getW = gr.getWidth()
getH = gr.getHeight()
-- set input keys
input_init()
end
function engine_run()
for gz in ipairs(map) do
--NEW ROTATION
local min_gy = 1
local max_gy = #map[gz]
if map_Y == -1 then
min_gy, max_gy = max_gy, min_gy
end
for gy = min_gy, max_gy, map_Y do
local min_gx = 1
local max_gx = #map[gz][gy]
if map_X == -1 then
min_gx, max_gx = max_gx, min_gx
end
for gx = min_gx, max_gx, map_X do
v = map[gz][gy][gx]
--NEW ROTATION
block_draw(gx, gy, gz, v)
end
end
end
end
-- load default and update tiles geometry
function tile_update()
zoom = zoom or 1
factor_w = factor_w or 1
factor_h = factor_h or 1
factor_z = factor_z or 1
tile_w = base_w * factor_w * zoom
tile_h = base_h * factor_h * zoom
tile_z = base_z * factor_z * zoom
end
-- modify tile geometry factors
function camera_rotate(d, dt)
factor_w = factor_w + d * dt
factor_h = factor_h + d * dt * 4
factor_z = factor_z - d * dt / 4
tile_update()
end
-- modify zoom
function camera_zoom(d, dt)
zoom = zoom * (1 + d * dt)
tile_update()
end
-- calculate every rhombi vectors
function rhombi(x, y)
return
x + tile_w, y + tile_z/2,
x, y + tile_h + tile_z/2,
x - tile_w, y + tile_z/2,
x, y - tile_h + tile_z/2,
x + tile_w, y - tile_z/2,
x, y + tile_h - tile_z/2,
x - tile_w, y - tile_z/2,
x, y - tile_h - tile_z/2
end
function offset(x, y)
-- store player draw coordinates
local px, py = axono(
player.gx,
player.gy,
player.gz
)
return
-- center on player
x + getW/2 - px,
y + getH/2 - py
end
-- converse grid coordinates to axonometric draw
function axono(x, y, z)
return
--NEW ROTATION
(map_Y * x - map_X * y) * tile_w,
(map_Y * y + map_X * x) * tile_h - z * tile_z
--NEW ROTATION
end
-- get draw coordinates
function block_getVertices(x, y, z)
return rhombi(offset(axono(x, y, z)))
end
-- return v if value, else nil
function block_getValue(gx, gy, gz)
return
map[gz] and
map[gz][gy] and
map[gz][gy][gx]
end
-- return alpha if transparent, else nil
function block_getAlpha(v)
return not v or colors[v][4]
end
-- return true if something is on screen
function onScreen(x0, y0, xW, yH)
return
x0 > 0 and xW < getW and
y0 > 0 and yH < getH
end
-- get distance between the player and a block
function getDist(x, y, z)
local dx, dy, dz =
player.gx - x,
player.gy - y,
player.gz - z
return math.sqrt(
dx * dx +
dy * dy +
dz * dz
)
end