-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
627 lines (552 loc) · 15.1 KB
/
main.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
----------
-- v 1.00
-- @author: Sergey Pomorin
-- @skype: j0xflash
-- @project: Hook Head
----------
_W = display.contentWidth;
_H = display.contentHeight;
_WO = 1080;
_HO = 1920;
optionsBuild = "android";-- "ios"/"android"/"windows"/"osx"
options_debug = false;
options_pause = false;
options_save = true;
options_demo = false;
options_controls = "touch"; -- touch, cursor, console
optionsMobile = (optionsBuild == "ios" or optionsBuild == "android");
options_save_fname = "dataUser";
encrypt_password = '12345678pass'
-- groups
mainGroup = display.newGroup();
cursorGroup = display.newGroup();
mainGroup.parent:insert(mainGroup);
cursorGroup.parent:insert(cursorGroup);
-- makeup
scaleGraphics = 1;
guiScale = 1;
mobileScale = 1;
minScale = math.min(_W/_WO, _H/_HO);
maxScale = math.max(_W/_WO, _H/_HO);
print("WxH: ".._W.."x".._H);
-- data user
login_obj = {};
initGC = false;
initAppodeal = false;
-- Init Game Center
globalData = require("src.globalData");
globalData.gpgs = nil
globalData.gameCenter = nil
licensing = nil
appodeal = nil
local platform = system.getInfo( "platform" )
local env = system.getInfo( "environment" )
if ( platform == "android" and env ~= "simulator" ) then
globalData.gpgs = require( "plugin.gpgs" )
licensing = require( "licensing" );
-- appodeal = require( "plugin.appodeal" )
elseif ( platform == "ios" and env ~= "simulator" ) then
globalData.gameCenter = require( "gameNetwork" )
end
---- import source
director = require("director");
json = require("json");
---- framework
movieclip = require("framework.movieclip");
require("framework.lua-enumerable");
require("framework.greenStandart");
greenSounds = require("framework.greenSounds").new();
xml = require("framework.greenXml").newParser();
language = require("framework.greenLang").new();
--spine = require("framework.spine");
greenLS = require 'framework.greenLoadSave';
greenInspect = require 'framework.greenInspect';
greenMsgs = require("framework.greenMsgs").new();
greenMsgs.x = 20;
greenMsgs.y = 20;
greenMsgs.isVisible = options_debug;
---- achievements
-- itemAchievement = require("src.ItemAchievement").new();
---- src
game_art = nil;
-- vars
fontMain = 'FRADM.TTF'; -- 'Franklin Gothic Demi', 'FRADM.TTF'
dataXml = nil;
dataImage = {};
_cursor = nil;
_tooltip = nil;
_bLoadGame = false;
-- set achievements
-- itemAchievement:addItemGCID(1, "hh_ach1", "CgkI0POrzdITEAIQAQ", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(2, "hh_ach1", "CgkI0POrzdITEAIQAg", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(3, "hh_ach1", "CgkI0POrzdITEAIQAw", "hh_ach1"); -- 20 +
-- itemAchievement:addItemGCID(4, "hh_ach1", "CgkI0POrzdITEAIQBA", "hh_ach1"); -- 20 +
-- itemAchievement:addItemGCID(5, "hh_ach1", "CgkI0POrzdITEAIQBQ", "hh_ach1"); -- 20 +
-- itemAchievement:addItemGCID(6, "hh_ach1", "CgkI0POrzdITEAIQBg", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(7, "hh_ach1", "CgkI0POrzdITEAIQBw", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(8, "hh_ach1", "CgkI0POrzdITEAIQCA", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(9, "hh_ach1", "CgkI0POrzdITEAIQCQ", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(10, "hh_ach1", "CgkI0POrzdITEAIQCg", "hh_ach1"); -- 20
-- itemAchievement:addItemGCID(11, "hh_ach1", "CgkI0POrzdITEAIQCw", "hh_ach1"); -- 20
local screenLoader = nil;
function refreshScaleGraphics()
_WO = 1080;
_HO = 1920;
_W = display.contentWidth;
_H = display.contentHeight;
scaleGraphics = 1;
minScale = math.min(_W/_WO, _H/_HO);
maxScale = math.max(_W/_WO, _H/_HO);
guiScale = maxScale;
scaleGraphics = minScale;
if(optionsMobile)then
mobileScale = 2;
end
if(_cursor)then
_cursor.img.xScale = 0.5;
_cursor.img.yScale = _cursor.img.xScale;
end
if(_tooltip)then
createTooltip()
end
if(screenLoader)then
screenLoader.x = _W/2;
screenLoader.y = _H/2;
screenLoader.bg.width = _W/scaleGraphics;
screenLoader.bg.height = _H/scaleGraphics;
screenLoader.xScale = scaleGraphics;
screenLoader.yScale = scaleGraphics;
end
print("minScale:", minScale, "maxScale:", maxScale, "guiScale:", guiScale);
end
refreshScaleGraphics();
function createTooltip()
end
if(optionsBuild == "ios")then
elseif(optionsBuild == "android")then
native.setProperty( "androidSystemUiVisibility", "immersiveSticky" )
elseif(optionsBuild == "windows" or optionsBuild == "osx")then
options_controls = "cursor";
end
-- methods
function getText(value)
return language:get_txt(value);
end
-- standart methods
function getTimer()
return system.getTimer();
end
function sign(x)
return (x<0 and -1) or 1
end
function top_mc_count_one(val, valTf)
local ds = val - math.abs(tonumber(valTf));
local ds_abs = math.abs(ds);
if(ds_abs>2000)then
valTf = valTf + sign(ds)*1000;
elseif(ds_abs>200)then
valTf = valTf + sign(ds)*100;
elseif(ds_abs>20)then
valTf = valTf + sign(ds)*10;
elseif(ds_abs>0)then
valTf = valTf + sign(ds);
end
return valTf;
end
-- save/load data
function clearData()
login_obj = {};
end
function saveData()
if(options_save)then
greenLS.save(options_save_fname..'.dat', login_obj)
end
end
function loadData()
_bLoadGame = true;
login_obj = greenLS.load(options_save_fname..'.dat')
if (login_obj == nil) then
clearData();
-- print("Loading: fail!");
end
end
-- language
language:add_lang_xml('en');
language:loadSettings();
profiling = require("framework.greenProfiling").new();
profiling.x = profiling.width/2;
profiling.y = profiling.height/2;
profiling.isVisible = options_debug;
function createLoader()
if(screenLoader)then
screenLoader:removeSelf();
screenLoader = nil;
end
screenLoader = display.newGroup();
mainGroup:insert(screenLoader);
local screen = display.newRect( 0, 0, _WO, _HO );
screen:setFillColor( 0, 0, 0 );
screenLoader:insert(screen);
local options =
{
parent = screenLoader,
text = "",
font = nil,
fontSize = 40,
width = _W*0.8,
align = "center"
}
local tfLoader = display.newText(getText("loading"), 0, 0, nil, 40);
tfLoader:setTextColor( 1,1,1);
tfLoader.y = 200;
screenLoader:insert(tfLoader);
screenLoader.tf = tfLoader;
local tfDesc = display.newText(options);
tfDesc:setTextColor( 1,1,1);
tfDesc.y = 300;
screenLoader.tfDesc = tfDesc;
screenLoader.bg = screen;
screenLoader.bg.width = _W/scaleGraphics;
screenLoader.bg.height = _H/scaleGraphics;
screenLoader.x = _W/2;
screenLoader.y = _H/2;
screenLoader.xScale = scaleGraphics;
screenLoader.yScale = scaleGraphics;
end
createLoader()
function loaderShow()
if(screenLoader)then
screenLoader.tf.text = getText("loading");
screenLoader.tfDesc.text = "";
screenLoader.isVisible = true;
end
end
function loaderClose()
if(screenLoader)then
screenLoader.isVisible = false;
end
end
function show_msg(txt)
if(options_debug)then
mainGroup:insert(greenMsgs);
greenMsgs:show_msg(txt);
end
end
-- Show scene
function transitionRemoveSelfHandler(obj)
if(obj)then
if(obj.removeSelf)then
obj:removeSelf();
end
end
end
function show_fade_gfx(callback)
local fade_in = display.newRect(mainGroup, _W/2, _H/2, _W, _H);
fade_in:setFillColor(0,0,0);
transition.from(fade_in, {time=400, alpha=0, transition=easing.outQuad, onComplete=function(obj)
callback();
transition.to(obj, {time=100, alpha=1, onComplete=transitionRemoveSelfHandler});
local fade_out = display.newRect(mainGroup, _W/2, _H/2, _W, _H);
fade_out:setFillColor(0,0,0);
transition.to(fade_out, {time=300, alpha=0, transition=easing.inQuad,
onComplete=transitionRemoveSelfHandler});
end
});
end
function showMenu()
show_fade_gfx(function()
director:changeScene('src.ScreenMenu');
end)
end
function showGame()
show_fade_gfx(function()
director:changeScene('src.ScreenGame');
end)
end
-- methods
function addButton(title, tx, ty, fname, onRelease, tfSize, tfColor, ico)
local item_mc = display.newGroup();
item_mc.act = title;
item_mc.onRelease = onRelease;
item_mc.x, item_mc.y = tx,ty;
local body_mc = display.newImage(item_mc, fname);
body_mc.x, body_mc.y = 0,0;
local over_mc = display.newImage(item_mc, fname);
over_mc.blendMode = "add";
over_mc.alpha = 0.0;
over_mc.x, over_mc.y = 0,0;
item_mc.r = body_mc.width*body_mc.xScale/2;
item_mc.rr = item_mc.r*item_mc.r;
item_mc.w = item_mc.width*item_mc.xScale;
item_mc.h = item_mc.height*item_mc.yScale;
item_mc._selected = false;
item_mc._body = body_mc;
item_mc._over = over_mc;
item_mc.enabled = true;
if(tfSize)then
item_mc.tf = display.newText( getText(title), 0, 0, fontMain, tfSize );
if(tfColor)then
item_mc.tf:setFillColor( tfColor[1]/255, tfColor[2]/255, tfColor[3]/255 );
else
item_mc.tf:setFillColor( 1,1,1 );
end
item_mc.tf.x = 0;
item_mc.tf.y = 0;
item_mc:insert(item_mc.tf);
end
if(ico)then
local body_mc = display.newImage(item_mc, "image/buttons/" .. ico .. ".png");
item_mc.ico = body_mc;
end
return item_mc
end
function addButtonTexture(name, title)
if(title == nil)then
title = name;
end
local item_mc = display.newGroup();
item_mc.act = title;
local body_mc = addObj(name);
item_mc:insert(body_mc)
local over_mc = addObj(name);
over_mc.blendMode = "add";
over_mc.alpha = 0.0;
item_mc:insert(over_mc)
item_mc.r = body_mc.width*body_mc.xScale/2;
item_mc.rr = item_mc.r*item_mc.r;
item_mc.w = item_mc.width*item_mc.xScale;
item_mc.h = item_mc.height*item_mc.yScale;
item_mc._selected = false;
item_mc._body = body_mc;
item_mc._over = over_mc;
item_mc.enabled = true;
if(tfSize)then
item_mc.tf = display.newText( getText(title), 0, 0, fontMain, tfSize );
if(tfColor)then
item_mc.tf:setFillColor( tfColor[1]/255, tfColor[2]/255, tfColor[3]/255 );
else
item_mc.tf:setFillColor( 1,1,1 );
end
item_mc.tf.x = 0;
item_mc.tf.y = 0;
item_mc:insert(item_mc.tf);
end
if(ico)then
local body_mc = display.newImage(item_mc, "image/buttons/" .. ico .. ".png");
item_mc.ico = body_mc;
end
return item_mc
end
function createText(text, size, fillColor, embossColor, font)
if(size == nil)then
size = 24*scaleGraphics;
end
if(fillColor == nil)then
fillColor = {1,1,1};
end
if(embossColor == nil)then
embossColor =
{
highlight = { r=0, g=0, b=0 },
shadow = { r=0, g=0, b=0 }
}
end
if(font == nil)then
font = fontMain;
end
local tf = display.newEmbossedText(text, 0, 0, font, size)
tf:setFillColor(fillColor[1], fillColor[2], fillColor[3]);
tf:setEmbossColor(embossColor)
return tf;
end
function scaleObjects(obj, scale)
obj.xScale = scale;
obj.yScale = scale;
if(obj.w)then
obj.w = obj.w*scale;
end
if(obj.h)then
obj.h = obj.h*scale;
end
end
function addItemCount(item_id, val)
if(val and login_obj)then
if(login_obj[item_id])then
login_obj[item_id] = login_obj[item_id] + tonumber(val);
else
login_obj[item_id] = tonumber(val);
end
end
if(getItemCount("countDeath") >= 20)then
-- itemAchievement:createAchievement(8);
end
end
function setItemCount(item_id, val)
if(val and login_obj)then
login_obj[item_id] = tonumber(val);
end
end
function getItemCount(item_id)
local count = 0;
if(login_obj[item_id])then
count = login_obj[item_id];
end
return count;
end
function loadTextureUnit(name)
game_art:loadData(name);
end
function addObj(name)
local data = dataImage[name];
local obj = nil;
if(data)then
obj = display.newImage(data.image, data.sheet);
else
print("addObj: not found", name);
obj = display.newRect(0, 0, 100, 100);
obj:setFillColor(1, 0, 0);
end
return obj;
end
-- music/sound
function musicContinue()
greenSounds:music_continue();
end
function musicStop()
greenSounds:music_stop();
end
function musicPlay(val)
greenSounds:music_play(val);
end
function musicVolume(val)
greenSounds:setVolumeMusic(val);
end
function soundPlay(val)
greenSounds:sound_play(val);
end
function soundVolume(val)
greenSounds:setVolume(val);
end
function setMusic(value)
greenSounds:setMusicBol(value);
end
function setSound(value)
greenSounds:setSoundBol(value);
end
function musicSwith()
greenSounds:switchMusic();
return greenSounds:getMusicBol();
end
suspended_obj = {};
function onSystem(evt)
print("onSystem type:", evt.type);
if evt.type == "applicationStart" then
elseif evt.type == "applicationExit" then
-- saveData();
elseif evt.type == "applicationSuspend" then
if(_bLoadGame)then
saveData();
end
suspended_obj['sound'] = greenSounds:getSoundBol();
suspended_obj['music'] = greenSounds:getMusicBol();
greenSounds:setSoundBol(false);
greenSounds:setMusicBol(false);
elseif evt.type == "applicationResume" then
greenSounds:setSoundBol(suspended_obj['sound']);
greenSounds:setMusicBol(suspended_obj['music']);
end
end
Runtime:addEventListener("system", onSystem);
local function iniSetArt(set_name)
local sheetInfo = require('images.texture.'..set_name);
local data = sheetInfo:getSheet();
local image = graphics.newImageSheet('images/texture/'.. set_name..".png", data);
local sequences = {};
for key,value in pairs(sheetInfo.frameIndex) do
data.frames[value].name = key;
end
for i=1,#data.frames do
local frame_name = data.frames[i].name;
local texture = sheetInfo:getFrameIndex(frame_name);
dataImage[frame_name] = {image=image, sheet=texture};
end
end
local function loadMusic()
greenSounds:add_sound('music', true);
end
local function loadSounds()
greenSounds:add_sound('click_approve');
greenSounds:add_sound('soundDie');
greenSounds:add_sound('soundHook');
greenSounds:add_sound('soundSaw');
greenSounds:add_sound('soundTorch');
end
local function loadTexture()
iniSetArt("back1Texture");
iniSetArt("back2Texture");
iniSetArt("itemsTexture");
end
local function onResize(event)
refreshScaleGraphics();
showMenu();
end
-- init game
local function main()
display.setStatusBar(display.HiddenStatusBar);
local loading_steps = {};
table.insert(loading_steps, function()
mapsXml = xml:loadFile('data/data.xml');
end);
table.insert(loading_steps, function()
loadTexture();
end);
table.insert(loading_steps, function()
loadMusic();
end);
table.insert(loading_steps, function()
loadSounds();
end);
-- table.insert(loading_steps, function()
-- _G.game_art = require("src.ItemAnima").new();
-- end);
table.insert(loading_steps, function()
loadData();
end);
table.insert(loading_steps, function()
musicPlay("music");
end);
local loading_steps_max = #loading_steps+1;
local st=getTimer();
loaderShow();
local function mainHandler(e)
if(#loading_steps>0)then
loading_steps[1]();
table.remove(loading_steps, 1);
if(screenLoader)then
local id = loading_steps_max - #loading_steps-1;
local loading_p = math.floor((loading_steps_max - #loading_steps)*100/loading_steps_max);
screenLoader.tf.text = getText("loading")..': '..loading_p..'%';
-- screenLoader.tfDesc.text = getText("loading_"..id);
end
return
end
local function adListener( event )
if ( event.phase == "init" ) then -- Successful initialization
initAppodeal = true;
end
end
if(appodeal)then
-- Initialize the Appodeal plugin
appodeal.init( adListener, { appKey="bdaed3861536353c253d7874e73b4d93f1d783eacd8442e4" } )
end
loaderClose();
director:changeScene("src.ScreenMenu");
print('mainHandler:game loaded in '..(getTimer()-st)..'ms');
Runtime:removeEventListener("enterFrame", mainHandler);
end
Runtime:addEventListener("enterFrame", mainHandler);
end
main();
if(options_controls == "cursor")then
Runtime:addEventListener( "resize", onResize )
end