-
Notifications
You must be signed in to change notification settings - Fork 4
/
heroes_v1.lua
306 lines (246 loc) · 7.71 KB
/
heroes_v1.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
--[[
Название: Заставка - Герои
Автор: Avondale, дискорд - avonda
Выбор фонов:
- Название фона вписывается в настройку "BackgroundName"
Список фонов:
"minion" - миньон
"creeper" - крипер
"skull" - череп
"amongus" - амогус
"hulk" - халк
"batsign" - лого бэтмена
"marvel" - железный человек и капитан америка
"mario" - марио
]]
math.randomseed(os.time())
local CLog = require("log")
local CInspect = require("inspect")
local CHelp = require("help")
local CJson = require("json")
local CTime = require("time")
local CAudio = require("audio")
local CColors = require("colors")
local tGame = {
Cols = 24,
Rows = 15,
Buttons = {},
}
local tConfig = {}
-- стейты или этапы игры
local GAMESTATE_SETUP = 1
local GAMESTATE_GAME = 2
local GAMESTATE_POSTGAME = 3
local GAMESTATE_FINISH = 4
local bGamePaused = false
local iGameState = GAMESTATE_GAME
local iPrevTickTime = 0
local tGameStats = {
StageLeftDuration = 0,
StageTotalDuration = 0,
CurrentStars = 0,
TotalStars = 0,
CurrentLives = 0,
TotalLives = 0,
Players = { -- максимум 6 игроков
{ Score = 0, Lives = 0, Color = CColors.NONE },
{ Score = 0, Lives = 0, Color = CColors.NONE },
{ Score = 0, Lives = 0, Color = CColors.NONE },
{ Score = 0, Lives = 0, Color = CColors.NONE },
{ Score = 0, Lives = 0, Color = CColors.NONE },
{ Score = 0, Lives = 0, Color = CColors.NONE },
},
TargetScore = 0,
StageNum = 0,
TotalStages = 0,
TargetColor = CColors.NONE,
}
local tGameResults = {
Won = false,
}
local tFloor = {}
local tButtons = {}
local tFloorStruct = {
iColor = CColors.NONE,
iBright = CColors.BRIGHT0,
bClick = false,
bDefect = false,
iWeight = 0,
}
local tButtonStruct = {
bClick = false,
bDefect = false,
}
function StartGame(gameJson, gameConfigJson)
tGame = CJson.decode(gameJson)
tConfig = CJson.decode(gameConfigJson)
for iX = 1, tGame.Cols do
tFloor[iX] = {}
for iY = 1, tGame.Rows do
tFloor[iX][iY] = CHelp.ShallowCopy(tFloorStruct)
end
end
for _, iId in pairs(tGame.Buttons) do
tButtons[iId] = CHelp.ShallowCopy(tButtonStruct)
end
iPrevTickTime = CTime.unix()
CPaint.OffsetTimer()
CPaint.sBackgroundName = tConfig.BackgroundName
CPaint.BackgroundFrameTimerStart()
if CPaint.sBackgroundName ~= "" then
CAudio.PlayBackground("backgrounds/heroes/"..CPaint.sBackgroundName..".mp3")
end
end
function NextTick()
if iGameState == GAMESTATE_GAME then
GameTick()
end
if iGameState == GAMESTATE_FINISH then
return tGameResults
end
CTimer.CountTimers((CTime.unix() - iPrevTickTime) * 1000)
iPrevTickTime = CTime.unix()
end
function GameTick()
SetGlobalColorBright(CColors.NONE, CColors.BRIGHT0)
CPaint.PaintBG()
end
function RangeFloor(setPixel, setButton)
for iX = 1, tGame.Cols do
for iY = 1, tGame.Rows do
setPixel(iX , iY, tFloor[iX][iY].iColor, tFloor[iX][iY].iBright)
end
end
for i, tButton in pairs(tButtons) do
setButton(i, tButton.iColor, tButton.iBright)
end
end
function SwitchStage()
end
--Paint
CPaint = {}
CPaint.sBackgroundName = ""
CPaint.iBackgroundFrameId = 1
CPaint.iTextColor = 1
CPaint.bAnimateText = false
CPaint.iTextAnimationFrame = 1
CPaint.iOffset = 0
CPaint.PaintBG = function()
local tBg = nil
if CPaint.sBackgroundName ~= "" then
tBg = tGame.Backgrounds[CPaint.sBackgroundName][CPaint.iBackgroundFrameId]
end
for iY = 1, tGame.Rows do
for iX = 1, tGame.Cols do
local tBackgroundColor = CPaint.GetBackgroundColor(iX, iY)
if tBackgroundColor ~= nil then
tFloor[iX][iY].iColor = tBackgroundColor.color
tFloor[iX][iY].iBright = tBackgroundColor.bright
end
if tBg ~= nil and tBg[iY][iX] ~= 8 then
tFloor[iX][iY].iColor = tBg[iY][iX]
tFloor[iX][iY].iBright = tConfig.Bright
end
end
end
end
CPaint.BackgroundFrameTimerStart = function()
if CPaint.sBackgroundName == "" then return end
CTimer.New(tConfig.AnimationDelay, function()
CPaint.iBackgroundFrameId = CPaint.iBackgroundFrameId + 1
if CPaint.iBackgroundFrameId > #tGame.Backgrounds[CPaint.sBackgroundName] then
CPaint.iBackgroundFrameId = 1
end
if iGameState == GAMESTATE_GAME then
return tConfig.AnimationDelay
end
return nil
end)
end
CPaint.OffsetTimer = function()
CTimer.New(tConfig.AnimationDelay, function()
CPaint.iOffset = CPaint.iOffset + 1
return tConfig.AnimationDelay
end)
end
CPaint.GetBackgroundColor = function(iX, iY)
local iId = math.floor((iX + iY + CPaint.iOffset) %(#tGame.Colors))
if iId == 0 then iId = 1 end
return tGame.Colors[iId]
end
--//
--TIMER класс отвечает за таймеры, очень полезная штука. можно вернуть время нового таймера с тем же колбеком
CTimer = {}
CTimer.tTimers = {}
CTimer.New = function(iSetTime, fCallback)
CTimer.tTimers[#CTimer.tTimers+1] = {iTime = iSetTime, fCallback = fCallback}
end
-- просчёт таймеров каждый тик
CTimer.CountTimers = function(iTimePassed)
for i = 1, #CTimer.tTimers do
if CTimer.tTimers[i] ~= nil then
CTimer.tTimers[i].iTime = CTimer.tTimers[i].iTime - iTimePassed
if CTimer.tTimers[i].iTime <= 0 then
iNewTime = CTimer.tTimers[i].fCallback()
if iNewTime and iNewTime ~= nil then -- если в return было число то создаём новый таймер с тем же колбеком
iNewTime = iNewTime + CTimer.tTimers[i].iTime
CTimer.New(iNewTime, CTimer.tTimers[i].fCallback)
end
CTimer.tTimers[i] = nil
end
end
end
end
--//
--UTIL прочие утилиты
function SetGlobalColorBright(iColor, iBright)
for iX = 1, tGame.Cols do
for iY = 1, tGame.Rows do
tFloor[iX][iY].iColor = iColor
tFloor[iX][iY].iBright = iBright
end
end
for i, tButton in pairs(tButtons) do
tButtons[i].iColor = iColor
tButtons[i].iBright = iBright
end
end
function SetAllButtonColorBright(iColor, iBright)
for i, tButton in pairs(tButtons) do
if not tButtons[i].bDefect then
tButtons[i].iColor = iColor
tButtons[i].iBright = iBright
end
end
end
--//
--//
function GetStats()
return tGameStats
end
function PauseGame()
bGamePaused = true
end
function ResumeGame()
bGamePaused = false
iPrevTickTime = CTime.unix()
end
function PixelClick(click)
tFloor[click.X][click.Y].bClick = click.Click
tFloor[click.X][click.Y].iWeight = click.Weight
end
function DefectPixel(defect)
tFloor[defect.X][defect.Y].bDefect = defect.Defect
end
function ButtonClick(click)
if tButtons[click.Button] == nil then return end
tButtons[click.Button].bClick = click.Click
end
function DefectButton(defect)
if tButtons[defect.Button] == nil then return end
tButtons[defect.Button].bDefect = defect.Defect
if defect.Defect then
tButtons[defect.Button].iColor = CColors.NONE
tButtons[defect.Button].iBright = CColors.BRIGHT0
end
end