-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
156 lines (136 loc) · 6.12 KB
/
game.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
pc.script.attribute('numPipes', 'number', 10, {
displayName: 'Num Pipes'
});
pc.script.create('game', function (context) {
// Creates a new Game instance
var Game = function (entity) {
this.entity = entity;
this.state = 'intro';
this.pipesDelivered = 0;
};
Game.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.setState('intro');
context.mouse.on(pc.input.EVENT_MOUSEDOWN, this.onMouseDown, this);
},
newGame: function () {
this.timer = 0;
context.root.findByName('Sub').script.submarine.reset();
for (var i = 1; i <= 10; i++) {
context.root.findByName('Pipe' + i).script.reset.reset();
}
this.pipesDelivered = 0;
},
updatePipesDelivered: function (delta) {
this.pipesDelivered += delta;
// On a restart, you can get a negative number so let's hack...
if (this.pipesDelivered < 0)
this.pipesDelivered = 0;
var pipesUi = this.entity.findByName('UI').findByName('Game').findByName('Pipes');
pipesUi.script.font_renderer.text = 'Pipes: ' + this.pipesDelivered + '/10';
if (this.pipesDelivered >= 10) {
this.setState('congratulations');
}
},
onMouseDown: function (event) {
if (event.button === pc.input.MOUSEBUTTON_LEFT) {
if (this.state === 'intro') {
this.setState('instructions');
} else if (this.state === 'instructions') {
this.setState('game');
} else if ((this.state === 'gameover') || (this.state === 'congratulations')) {
this.setState('intro');
}
}
},
setState: function (state) {
var ui;
this.state = state;
switch (state) {
case 'intro':
this.entity.findByName('Game Camera').enabled = false;
this.entity.findByName('Intro Camera').enabled = true;
ui = this.entity.findByName('UI');
ui.findByName('Intro').enabled = true;
ui.findByName('Instructions').enabled = false;
ui.findByName('Game').enabled = false;
ui.findByName('Game Over').enabled = false;
ui.findByName('Congratulations').enabled = false;
context.root.findByName('Sub').script.submarine.reset();
break;
case 'instructions':
ui = this.entity.findByName('UI');
ui.findByName('Intro').enabled = false;
ui.findByName('Instructions').enabled = true;
ui.findByName('Game').enabled = false;
break;
case 'game':
this.entity.findByName('Intro Camera').enabled = false;
this.entity.findByName('Game Camera').enabled = true;
ui = this.entity.findByName('UI');
ui.findByName('Intro').enabled = false;
ui.findByName('Instructions').enabled = false;
ui.findByName('Game').enabled = true;
this.newGame();
break;
case 'gameover':
ui = this.entity.findByName('UI');
ui.findByName('Game').enabled = false;
ui.findByName('Game Over').enabled = true;
break;
case 'congratulations':
ui = this.entity.findByName('UI');
ui.findByName('Game').enabled = false;
ui.findByName('Congratulations').enabled = true;
break;
}
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
switch (this.state) {
case 'intro':
if (context.keyboard.wasPressed(pc.input.KEY_SPACE)) {
this.setState('instructions');
}
break;
case 'instructions':
if (context.keyboard.wasPressed(pc.input.KEY_SPACE)) {
this.setState('game');
}
if (context.keyboard.wasPressed(pc.input.KEY_ESCAPE)) {
this.setState('intro');
}
break;
case 'game':
this.timer += dt;
var totalSec = Math.floor(this.timer);
var hours = '' + parseInt( totalSec / 3600 ) % 24;
if (hours.length === 1) {
hours = '0' + hours;
}
var minutes = '' + parseInt( totalSec / 60 ) % 60;
if (minutes.length === 1) {
minutes = '0' + minutes;
}
var seconds = '' + totalSec % 60;
if (seconds.length === 1) {
seconds = '0' + seconds;
}
var timerUi = this.entity.findByName('UI').findByName('Game').findByName('Timer');
timerUi.script.font_renderer.text = 'Time: ' + hours + ':' + minutes + ':' + seconds;
if (context.root.findByName('Sub').script.submarine.fuel <= 0) {
this.setState('gameover');
}
break;
case 'gameover':
case 'congratulations':
if (context.keyboard.wasPressed(pc.input.KEY_SPACE)) {
this.setState('intro');
}
break;
}
}
};
return Game;
});