-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_ingame.js
More file actions
134 lines (110 loc) · 4.47 KB
/
ui_ingame.js
File metadata and controls
134 lines (110 loc) · 4.47 KB
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
pc.script.create('ui_ingame', function (context) {
var Ui_ingame = function (entity) {
this.entity = entity;
};
Ui_ingame.prototype = {
initialize: function () {
this.game = context.root.findByName('Game').script.game;
this.game.on('reset', this.reset, this);
this.game.on('score', this.drawScore, this);
this.game.on('fuel', this.drawFuel, this);
this.game.on('bonusmeter', this.drawBonusMeter, this);
this.game.on('gameover', this.onGameOver, this);
this.game.on('fuellow', this.onFuelLow, this);
this.game.on('fuelout', this.onFuelOut, this);
this.game.on('fuelrecovery', this.onFuelRecovery, this);
this.game.on('streak', this.onStreakChanged, this);
this.pauseButton = this.entity.findByName('SpritePause').script.sprite;
this.scoreText = this.entity.findByName('TextScore').script.font_renderer;
this.scoreTextShadow = this.entity.findByName('TextScoreShadow').script.font_renderer;
this.fuelProgress = this.entity.findByName('ProgressFuel').script.progressbar;
this.bonusProgress = this.entity.findByName('ProgressBonus').script.progressbar;
this.gameoverSprite = this.entity.getParent().findByName('SpriteGameover');
this.lowFuel = this.entity.getParent().findByName('SpriteLowFuel');
this.noFuel = this.entity.getParent().findByName('SpriteNoFuel');
this.bonusStreak = [];
for (var i=0, len=this.game.maxStreak; i<len; i++) {
var sprite = this.entity.getParent().findByName('SpriteStreak' + (i+1).toString());
if (sprite) {
this.bonusStreak.push(sprite);
}
}
this.pauseButton.on('click', this.onPauseClicked, this);
this.reset();
},
reset: function () {
this.streak = 1;
this.hasFuel = true;
this.lowFuel.enabled = false;
this.noFuel.enabled = false;
this.gameoverSprite.enabled = false;
for (var i=0, len=this.bonusStreak.length; i<len; i++) {
this.bonusStreak[i].enabled = false;
}
},
postInitialize: function () {
this.drawScore(0);
this.drawFuel(100);
this.drawBonusMeter(0);
},
onEnable: function () {
this.onStreakChanged(this.streak);
if (!this.hasFuel) {
this.onFuelOut();
}
},
onDisable: function () {
this.lowFuel.enabled = false;
this.gameoverSprite.enabled = false;
this.noFuel.enabled = false;
for (var i=0, len=this.bonusStreak.length; i<len; i++) {
this.bonusStreak[i].enabled = false;
}
},
onPauseClicked: function () {
this.game.audio.playClickSound();
this.game.pause();
},
drawScore: function (score) {
var txt = score.toString();
this.scoreText.text = txt;
this.scoreTextShadow.text = txt;
},
drawFuel: function (fuel) {
this.fuelProgress.setProgress(fuel * 0.01);
},
drawBonusMeter: function (bonus) {
this.bonusProgress.setProgress(bonus * 0.01);
},
onFuelLow: function () {
this.lowFuel.enabled = true;;
},
onFuelOut: function () {
this.hasFuel = false;
this.noFuel.enabled = true;
},
onFuelRecovery: function () {
this.hasFuel = true;
this.noFuel.enabled = false;
},
onStreakChanged: function (streak) {
this.streak = streak;
for (var i=0, len=this.bonusStreak.length; i<len; i++) {
this.bonusStreak[i].enabled = i === streak - 2;
}
},
getUi: function () {
return this.entity.getParent().script.ui;
},
onGameOver: function (score, isHighScore) {
this.gameoverSprite.enabled = true;
this.noFuel.enabled = false;
setTimeout(function () {
this.gameoverSprite.enabled = false;
var gameOverScreen = this.getUi().showScreen('gameover').script.ui_gameover;
gameOverScreen.setScore(score, isHighScore);
}.bind(this), 2000);
}
};
return Ui_ingame;
});