-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
803 lines (653 loc) · 21.8 KB
/
code.js
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
var Layout = require("Layout");
var Storage = require("Storage");
g.setBgColor(1,1,1);
// allowing to print a graphicsbuffer to console
Graphics.prototype.print = function() {
for (var y=0;y<this.getHeight();y++)
console.log(new Uint8Array(this.buffer,this.getWidth()*y,this.getWidth()).toString());
};
// stack of screens used during the game loop
var screenStack = [];
// abstract screen class
class Screen {
constructor() {
if (this.constructor == Screen) {
throw new Error("Abstract classes can't be instantiated.");
}
}
draw(){
throw new Error("Method 'draw()' must be implemented.");
}
update(){
throw new Error("Method 'update()' must be implemented.");
}
touch(button, xy){
throw new Error("Method 'touch(button, xy)' must be implemented.");
}
drag(event){
throw new Error("Method 'drag(event)' must be implemented.");
}
button(){
throw new Error("Method 'button()' must be implemented.");
}
}
// implements the main screen showing four buttons, one for each area of the game
class MainScreen extends Screen {
constructor() {
this.init();
}
init() {
this.farm_unlocked = false;
for (let i = 0; i < 6; i++){
if (amounts[i] > 0) {
this.farm_unlocked = true;
}
}
}
draw(){
g.clear();
//--- draw main screen
// draw images
g.drawImage(Storage.read("farmer.img"), 0, 1); // farmer
g.drawImage(Storage.read("field.img"), 90, 15); // field
g.drawImage(Storage.read("stats2.img"), 49, 95); // stats
if (this.farm_unlocked == false){
g.drawLine(85, 3, 155, 83);
g.drawLine(85, 83, 155, 3);
}
//g.drawImage(Storage.read("farm.img"), 88, 96); // market
// draw labels
g.setColor(0).setFont("6x8").drawString("Contracts", 13, 75);
g.setFont("6x8").drawString("Farm", 110, 75);
g.setFont("6x8").drawString("Stats", 65, 165);
//g.setFont("6x8").drawString("Shop", 110, 165);
// draw rectangles to indicate button areas
g.drawRect(5, 3, 75, 83);
g.drawRect(85, 3, 155, 83);
g.drawRect(45, 93, 115, 173);
//g.drawRect(85, 93, 155, 173);
}
update(){
}
touch(button, xy){
if (xy.x > 5 && xy.x < 75 && xy.y > 3 && xy.y < 83) {
screenStack.push(new ContractsScreen());
}
if (xy.x > 85 && xy.x < 155 && xy.y > 3 && xy.y < 83) {
for (var i = 0; i< 6; i++){
if (amounts[i] > 0) {
for (let j = 0; j < 6; j++){
recentstats[j] = 0;
}
screenStack.push(new FarmScreen());
return;
}
}
}
if (xy.x > 45 && xy.x < 115 && xy.y > 93 && xy.y < 173) {
screenStack.push(new StatsScreenRecent());
}
//if (xy.x > 85 && xy.x < 155 && xy.y > 93 && xy.y < 173) {
// screenStack.push(new WinScreen());
//}
}
drag(event){
}
button(){
//main screen does not pop
//screenStack.pop()
}
}
const contractor_images = ["otto.img", "oldman.img", "cat.img"];
const vegetables = ["onion", "corn", "turnip", "salad", "tomato", "potato", "chilli"];
class Contractor {
constructor(){
this.index = Math.floor(Math.random() * contractor_images.length);
this.img = contractor_images[this.index];
this.selected = false;
this.amounts = [0, 0, 0, 0, 0, 0];
this.amounts[Math.floor(Math.random()*6)] += Math.floor(3+Math.random()*5);
}
}
// store which amounts of vegetables have been selected
var amounts = [0, 0, 0, 0, 0, 0];
var stats = [0, 0, 0, 0, 0, 0];
var recentstats = [0, 0, 0, 0, 0, 0];
var contractor_stats = [];
for (let i = 0; i < contractor_images.length; i++) {
contractor_stats.push(0);
}
// initialize contractors at the beginning of the game, replace them after their contracts have been fulfilled
var contractors = [];
for (let i = 0; i < 5; i++) {
let c = new Contractor();
this.contractors.push(c);
}
// implements the contracts screen showing the different customers and their needs
class ContractsScreen extends Screen {
constructor() {
this.contractarea = Graphics.createArrayBuffer(450, 90, 8);
this.contractarea.fillRect(0, 0, 450, 90);
for (let i = 0; i < 5; i++) {
this.contractarea.drawImage(Storage.read(contractors[i].img), i*90, 0);
//this.contractarea.setColor(0).drawRect(i*90, 0, i*90+69, 65);
if (contractors[i].selected){
this.contractarea.drawImage(Storage.read("checkbox_checked.img"), i*90+70, 0);
}
else {
this.contractarea.drawImage(Storage.read("checkbox.img"), i*90+70, 0);
}
}
this.offset_x = 0;
var gimg = {
width: 450,
height: 90,
bpp: 8,
buffer: this.contractarea.buffer
};
this.contractarea.flip = function(offset_x) {
g.drawImage(gimg, offset_x, 110);
};
g.clear();
this.contractarea.flip();
}
draw(){
g.clear();
this.contractarea.flip(this.offset_x);
g.drawRect(5,5, 163, 100);
g.drawImage(Storage.read("turnip.img"), 8, 10, {scale:0.5}); // turnip
g.setFont("6x8:2x2").drawString("x" + amounts[0], 40, 15);
g.drawImage(Storage.read("tomato.img"), 6, 40, {scale:0.5}); // tomato
g.setFont("6x8:2x2").drawString("x" + amounts[1], 40, 47);
g.drawImage(Storage.read("chilli.img"), 5, 68, {scale:0.5}); // chilli
g.setFont("6x8:2x2").drawString("x" + amounts[2], 40, 76);
g.drawImage(Storage.read("corn.img"), 86, 10, {scale:0.5}); // corn
g.setFont("6x8:2x2").drawString("x" + amounts[3], 116, 15);
g.drawImage(Storage.read("onion.img"), 86, 37, {scale:0.5}); // onion
g.setFont("6x8:2x2").drawString("x" + amounts[4], 116, 47);
g.drawImage(Storage.read("wheat.img"), 86, 72, {scale:0.5}); // wheat
g.setFont("6x8:2x2").drawString("x" + amounts[5], 116, 76);
g.drawLine(84, 5, 84, 100);
}
update(){
}
touch(button, xy){
//otto size = 71x71
for (let i = 0; i < 5; i++) {
if (xy.x > i*90+this.offset_x && xy.x < i*90+69+this.offset_x && xy.y > 110 && xy.y < 175){
//this.contractarea.setColor(0).drawRect(i*90, 0, i*90+69, 69);
contractors[i].selected = !contractors[i].selected;
this.contractarea.setColor("#FFFFFF").fillRect(i*90+70, 0, i*90+70+13, 13);
if (contractors[i].selected){
this.contractarea.setColor(0).drawImage(Storage.read("checkbox_checked.img"), i*90+70, 0);
for (let j = 0; j < 6; j++){
amounts[j] += contractors[i].amounts[j];
}
} else {
this.contractarea.setColor(0).drawImage(Storage.read("checkbox.img"), i*90+70, 0);
for (let j = 0; j < 6; j++){
amounts[j] -= contractors[i].amounts[j];
}
}
}
}
}
drag(event){
this.offset_x += event.dx;
if (this.offset_x > 0){
this.offset_x = 0;
}
if (this.offset_x < -450+172) {
this.offset_x = -450+172;
}
}
button(){
screenStack.pop();
screenStack[screenStack.length - 1].init();
}
}
class Vegetable {
constructor(count) {
}
}
class Chilli extends Vegetable{
constructor(count){
this.count = count;
this.name = "Chilli";
this.anim1 = ["squats1.img", "side_squats1.img", "reverse_lunches1.img", "burpee1.img",];
this.anim2 = ["squats2.img", "side_squats2.img", "reverse_lunches2.img", "burpee2.img",];
this.text = ["SQUAT to SOW!", "SIDE SQUAT to WATER!", "REVERSE LUNCH to GROW", "BURPEE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvestchili1.img", "harvestchili2.img"];
}
}
class Turnip extends Vegetable{
constructor(count){
this.count = count;
this.name = "Turnip";
this.anim1 = ["plank.img", "plank.img", "plank.img", "plank.img",];
this.anim2 = ["plank.img", "plank_side_kicks.img", "planks_kicks2.img", "plank_raise.img",];
this.text = ["PlANK to SOW!", "PLANK SIDE KICKS to WATER!", "PLANK KICKS to GROW", "PLANK RAISE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvestturnip1.img", "harvestturnip2.img"];
}
}
class Onion extends Vegetable{
constructor(count){
this.count = count;
this.name = "Onion";
this.anim1 = ["flutter1.img", "plank.img", "plank.img", "plank.img",];
this.anim2 = ["flutter2.img", "plank_side_kicks.img", "planks_kicks2.img", "plank_raise.img",];
this.text = ["FLUTTER KICKS to SOW!", "PLANK SIDE KICKS to WATER!", "PLANK KICKS to GROW", "PLANK RAISE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvestonion1.img", "harvestonion2.img"];
}
}
class Tomato extends Vegetable{
constructor(count){
this.count = count;
this.name = "Tomato";
this.anim1 = ["plank.img", "plank.img", "plank.img", "plank.img",];
this.anim2 = ["plank.img", "plank_side_kicks.img", "planks_kicks2.img", "plank_raise.img",];
this.text = ["PlANK to SOW!", "PLANK SIDE KICKS to WATER!", "PLANK KICKS to GROW", "PLANK RAISE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvesttomato1.img", "harvesttomato2.img"];
}
}
class Wheat extends Vegetable{
constructor(count){
this.count = count;
this.name = "Wheat";
this.anim1 = ["plank.img", "plank.img", "plank.img", "plank.img",];
this.anim2 = ["plank.img", "plank_side_kicks.img", "planks_kicks2.img", "plank_raise.img",];
this.text = ["PlANK to SOW!", "PLANK SIDE KICKS to WATER!", "PLANK KICKS to GROW", "PLANK RAISE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvestwheat1.img", "harvestwheat2.img"];
}
}
class Corn extends Vegetable{
constructor(count){
this.count = count;
this.name = "Corn";
this.anim1 = ["plank.img", "plank.img", "plank.img", "plank.img",];
this.anim2 = ["plank.img", "plank_side_kicks.img", "planks_kicks2.img", "plank_raise.img",];
this.text = ["PlANK to SOW!", "PLANK SIDE KICKS to WATER!", "PLANK KICKS to GROW", "PLANK RAISE to HARVEST!"];
this.countstyle = [true, true, true, true];
this.harvest = ["harvestcorn1.img", "harvestcorn2.img"];
}
}
var cropPhase1 = ["seeding.img", "watering1.img", "grow1.img"];
var cropPhase2 = ["seeding2.img", "watering2.img", "grow2.img"];
// implements the farming screen showing the different crops and which exercises need to be completed to harvest them
class FarmScreen extends Screen {
constructor() {
console.log("farm screen");
console.log("stacksize: " + screenStack.length);
g.clear();
this.currentPhase = 0;
this.vegetable = undefined;
for (var i = 0; i< 6; i++){
if (amounts[i] > 0 && this.vegetable == undefined) {
switch (i) {
case 0:
this.vegetable = new Turnip(amounts[i]);
break;
case 1:
this.vegetable = new Tomato(amounts[i]);
break;
case 2:
this.vegetable = new Chilli(amounts[i]);
break;
case 3:
this.vegetable = new Corn(amounts[i]);
break;
case 4:
this.vegetable = new Onion(amounts[i]);
break;
case 5:
this.vegetable = new Wheat(amounts[i]);
break;
default:
screenStack.pop(screenStack.length-1);
return;
}
}
}
this.dragged = 0;
this.validdrag = true;
if (this.vegetable.countstyle[0])
{
this.seconds = this.vegetable.count * 10; //max time for timer
this.active = false;
/*
this.timerInterval = setInterval(function(screen){
if (screen.seconds > 0) {
screen.seconds -= 1;
if (screen.seconds == 0){
Bangle.buzz()
}
}
screen.drawTimer();
}, 1000, this); //seconds*/
} else {
this.seconds = -1;
}
this.flippedTrainer = false;
this.trainerInterval = setInterval(function(screen){
screen.flippedTrainer = !screen.flippedTrainer;
screen.drawTrainer();
}, 1000, this); //every two seconds
this.drawTrainer();
this.drawTimer();
this.drawStartButton();
}
draw(){
//g.setFont("6x8").drawString("Farm", 10, 10);
//g.drawImage(Storage.read("oldman.img"), 9, 95); // oldman
}
drawStartButton() {
g.drawRect(6, 3, 165, 40);
g.setFont("6x8:3x3").drawString('Start', 42, 12);
g.drawRect(0,0,172,172);
}
drawTimer(){ //once per second
min = String(Math.floor(this.seconds/60));
if(min.length < 2){
min = '0'+min;
}
sec = String(this.seconds%60);
if(sec.length < 2){
sec = '0'+sec;
}
g.clearRect(1, 42, 171, 65);
g.setFont("6x8:3x3").drawString(''+min+':'+sec, 42, 42);
}
drawTrainer(){
// clear bottom half of screen
g.clearRect(0, 95, 176, 176);
// write exercise description
g.setFont("6x8").setFontAlign(0, 0).drawString(this.vegetable.text[this.currentPhase], 86, 70);
g.setFontAlign(-1, -1);
// two animation steps
if(this.flippedTrainer){
// draw gropPhase, last gropPhase is dependent on the vegetable
if (this.currentPhase == 3){
g.drawImage(Storage.read(this.vegetable.harvest[1]), 100, 105, {scale:0.8});
} else {
g.drawImage(Storage.read(cropPhase1[this.currentPhase]), 100, 105, {scale: this.currentPhase == 0 ? 1 : 0.8});
}
// draw the exercise
g.drawImage(Storage.read(this.vegetable.anim1[this.currentPhase]), 9, 95, {scale:1});
}
else {
// draw gropPhase, last gropPhase is dependent on the vegetable
if (this.currentPhase == 3){
g.drawImage(Storage.read(this.vegetable.harvest[0]), 100, 105, {scale:0.8});
} else {
g.drawImage(Storage.read(cropPhase2[this.currentPhase]), 100, 105, {scale:this.currentPhase == 0 ? 1 : 0.8});
}
// draw the exercise
g.drawImage(Storage.read(this.vegetable.anim2[this.currentPhase]), 9, 95, {scale:1});
}
}
update(){
if (this.active==false){
}
}
touch(button, xy){
if (this.active == false && xy.x > 6 && xy.x < 165 && xy.y > 3 && xy.y < 40){
this.active = true;
g.clearRect(6,3,165,40);
if (this.vegetable.countstyle[this.currentPhase])
{
this.timerInterval = setInterval(function(screen){
if (screen.seconds > 0) {
screen.seconds -= 1;
if (screen.seconds == 0){
Bangle.buzz();
}
}
screen.drawTimer();
}, 1000, this); //seconds*/
}
}
}
drag(event){
this.dragged += event.dx;
if (this.dragged < -50 && this.validdrag == true){
g.clear();
this.currentPhase += 1;
if (this.currentPhase >= 4){
return this.win();
}
this.validdrag = false;
this.dragged = 0;
this.active = false;
// next exercise & timer reset
if (this.timerInterval != undefined){
clearInterval(this.timerInterval);
this.timerInterval = undefined;
}
if (this.vegetable.countstyle[this.currentPhase])
{
this.seconds = this.vegetable.count * 10; //max time for timer
this.active = false;
/*this.timerInterval = setInterval(function(screen){
if (screen.seconds > 0) {
screen.seconds -= 1;
if (screen.seconds == 0){
Bangle.buzz()
}
}
screen.drawTimer();
}, 1000, this); //seconds*/
} else {
this.seconds = -1;
}
this.drawTrainer();
this.drawTimer();
this.drawStartButton();
}
if (event.b == 0) {
this.validdrag = true;
this.dragged = 0;
}
}
win() {
g.clear();
// clear seconds timer interval
if (this.timerInterval != undefined) {
clearInterval(this.timerInterval);
}
this.timerInterval = undefined;
// clear trainer animation interval
clearInterval(this.trainerInterval);
this.trainerInterval = undefined;
screenStack.pop(screenStack.length-1);
for (let i = 0; i< 6; i++){
if (amounts[i] > 0)
{
// find and replace satisfied contractors
for (let j = 0; j < 5; j++){
let c = contractors[j];
if (c.amounts[i] > 0) {
contractors[j] = new Contractor();
contractor_stats[c.index] += 1;
}
}
//add amount of this vegetable to stats
stats[i] += amounts[i];
recentstats[i] = amounts[i];
amounts[i] = 0;
break;
}
}
for (let i = 0; i< 6; i++){
if (amounts[i] > 0){
screenStack.push(new FarmScreen());
return;
}
}
screenStack.push(new WinScreen());
}
button(){
console.log("button farmscreen");
console.log("stacksize: " + screenStack.length);
g.clear();
// clear seconds timer interval
if (this.timerInterval != undefined) {
clearInterval(this.timerInterval);
}
this.timerInterval = undefined;
// clear trainer animation interval
clearInterval(this.trainerInterval);
this.trainerInterval = undefined;
console.log("stacksize: " + screenStack.length);
screenStack.pop(screenStack.length-1);
console.log("stacksize: " + screenStack.length);
screenStack[screenStack.length-1].init();
}
}
// implements the stats screen showing previously completed exercises
class StatsScreenRecent extends Screen {
constructor() {
}
draw(){
g.clear();
//render function for bar plot
function renderBar(xCo, yCo, data) {
require("graph").drawBar(g, data, {
miny: 0,
axes : true,
x:xCo, y:yCo, width:150, height:70
});
g.drawImage(Storage.read("turnip.img"), 30, 145, {scale:0.3}); // turnip
g.drawImage(Storage.read("tomato.img"), 50, 145, {scale:0.3}); // tomato
g.drawImage(Storage.read("chilli.img"), 70, 145, {scale:0.3}); // chilli
g.drawImage(Storage.read("corn.img"), 90, 145, {scale:0.3}); // corn
g.drawImage(Storage.read("onion.img"), 110, 145, {scale:0.3}); // onion
g.drawImage(Storage.read("wheat.img"), 130, 145, {scale:0.3}); // wheat
}
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("Harvested", 86, 10);
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("Veggies", 86, 25);
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("Last Workout", 86, 40);
g.setFontAlign(-1,-1);
renderBar(10,70,recentstats);
}
update(){
}
touch(button, xy){
screenStack.pop(screenStack.length-1);
screenStack.push(new StatsScreenAll());
}
drag(event){
}
button(){
screenStack.pop(screenStack.length-1);
}
}
// implements the stats screen showing previously completed exercises
class StatsScreenAll extends Screen {
constructor() {
}
draw(){
g.clear();
//render function for bar plot
function renderBar(xCo, yCo, data) {
require("graph").drawBar(g, data, {
miny: 0,
axes : true,
x:xCo, y:yCo, width:150, height:70
});
g.drawImage(Storage.read("turnip.img"), 30, 145, {scale:0.3}); // turnip
g.drawImage(Storage.read("tomato.img"), 50, 145, {scale:0.3}); // tomato
g.drawImage(Storage.read("chilli.img"), 70, 145, {scale:0.3}); // chilli
g.drawImage(Storage.read("corn.img"), 90, 145, {scale:0.3}); // corn
g.drawImage(Storage.read("onion.img"), 110, 145, {scale:0.3}); // onion
g.drawImage(Storage.read("wheat.img"), 130, 145, {scale:0.3}); // wheat
}
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("Harvested", 86, 10);
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("Veggies", 86, 25);
g.setFont("6x8:2x2").setFontAlign(0,0).drawString("In Total", 86, 40);
g.setFontAlign(-1,-1);
renderBar(10,70,stats);
}
update(){
}
touch(button, xy){
screenStack.pop(screenStack.length-1);
screenStack.push(new StatsScreenRecent());
}
drag(event){
}
button(){
screenStack.pop(screenStack.length-1);
}
}
// implements the shopping screen allowing to buy cool stuff
class WinScreen extends Screen {
constructor() {
this.flip = false;
this.posx = [0, 50, 100, 0, 50, 100];
this.posy = [5, 5, 5, 100, 100, 100];
this.img = [Storage.read("corn.img"), Storage.read("tomato.img"), Storage.read("chilli.img"), Storage.read("onion.img"), Storage.read("wheat.img"), Storage.read("turnip.img")];
this.img_rot = [Storage.read("corn_rot.img"), Storage.read("tomato_rot.img"), Storage.read("chili_rot.img"), Storage.read("onion_rot.img"), Storage.read("wheat_rot.img"), Storage.read("turnip_rot.img")];
this.drawCall = setInterval(function(screen) {
screen.flip = !screen.flip;
screen.drawRotatedObjects();
}, 1000, this);
}
drawRotatedObjects() {
g.clear();
Bangle.buzz();
let target = 0;
if (this.flip)
target = 1;
g.setFont("6x8:2x3").drawString("You did it!", 28, 75);
for (let i = 0; i < 6; i++){
if (i % 2 == target) { //draw even positions
g.drawImage(this.img[i], this.posx[i]+10, this.posy[i]+10, {scale:0.8});
} else { //draw uneven positions
g.drawImage(this.img_rot[i], this.posx[i]+10, this.posy[i]+10, {scale:0.8});
}
}
}
draw(){
}
update(){
}
touch(button, xy){
}
drag(event){
}
button(){
g.clear();
// clear seconds timer interval
if (this.drawCall != undefined) {
clearInterval(this.drawCall);
}
this.drawCall = undefined;
screenStack.pop(screenStack.length-1);
console.log(screenStack.length);
}
}
// prepare screenStack
screenStack.push(new MainScreen());
// function to be called every tick
var ticktime = 10;
function tick(){
screenStack[screenStack.length-1].update();
screenStack[screenStack.length-1].draw();
}
setInterval(tick, ticktime);
// forward touch events
Bangle.on('touch', function(button, xy) {
screenStack[screenStack.length-1].touch(button, xy);
});
// forward drag events
Bangle.on('drag', function(event) {
screenStack[screenStack.length-1].drag(event);
});
setWatch(function() {
screenStack[screenStack.length-1].button();
}, BTN, {edge:"rising", debounce:50, repeat:true});