-
Notifications
You must be signed in to change notification settings - Fork 5
/
Instructions.js
207 lines (161 loc) · 7.97 KB
/
Instructions.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
BasicGame.Instructions = function (game) {
this.bg;
this.title_image;
this.rudEvent_button = null;
this.playAgain_button = null;
this.cameraSpeed = 10;
this.pos = 0;
// environment
this.environment;
this.background;
this.midground;
this.foreground;
this.cursors;
this.score;
};
BasicGame.Instructions.prototype = {
init: function () {
// Only show instructions once.
if (typeof localStorage !== 'undefined') {
if (localStorage['instructions']) return this.startGame();
localStorage['instructions'] = true;
}
var envs = this.game['GameData'].environments,
totalEnvs = envs.length;
var levelSelect = this.game['GameData'].startingEnvironment;
//levelSelect=1
this.environment = envs[levelSelect];
this.stage.backgroundColor = "#000000";
this.score = String( Math.floor(this.game['GameData'].score + this.game['GameData'].currentStageScore) );
var enterKey = this.input.keyboard.addKey(Phaser.Keyboard.ENTER);
enterKey.onDown.add(this.startGame, this);
},
create: function () {
// set world settings and player start position
this.startPos = { "x": 150, "y": (this.world.height / 2) + 47 };
this.stage.backgroundColor = "#0c9fc7";
this.world.setBounds(0, 0, this.levelLength + this.flatStartLength + this.flatEndLength, 500);
this.addBackground();
this.addMidground();
var graphics = this.add.graphics(0, 0);
//this.drawTube(graphics, this.tunnelPhysicsData);
this.addForeground();
// instructions
var instructions = this.add.sprite(this.camera.width/2, this.camera.height/2, 'instructions1')
instructions.anchor.set(0.5, 0.5);
var pod1 = this.add.sprite(this.camera.width/2, this.camera.height/2 - 150, 'pod');
pod1.scale.set(0.25, 0.25);
pod1.x = this.camera.width/2;
var pod1tween = this.add.tween(pod1).to( { x: pod1.x + 100 }, 500, Phaser.Easing.Exponential.In, true, 0, 0);
pod1tween.onComplete.add(pod1Complete, this);
var pod2 = this.add.sprite(this.camera.width/2 - 50, this.camera.height/2 - 110, 'pod');
pod2.scale.set(0.25, 0.25);
pod2.x = this.camera.width/2;
var pod2tween = this.add.tween(pod2).to( { x: pod2.x + 50 }, 500, Phaser.Easing.Exponential.Out, true, 0, 0);
pod2tween.onComplete.add(pod2Complete, this);
var pod3 = this.add.sprite(this.camera.width/2 + 33, this.camera.height/2 - 50, 'pod');
pod3.anchor.set(0.5, 0.5);
pod3.scale.set(0.25, 0.25);
pod3.angle = 0;
var pod3tween = this.add.tween(pod3).to( { angle: 0 }, 0, Phaser.Easing.Exponential.Out, true, 0, 0);
pod3tween.onComplete.add(pod3Complete, this);
// create score board table from external data
this.playAgain_button = this.add.bitmapText(this.camera.width / 2, 440, 'basic_font_white', 'Play now!', 40)
this.playAgain_button.hitArea = new PIXI.Rectangle(-this.playAgain_button.width/2, -this.playAgain_button.height/2, this.playAgain_button.width, this.playAgain_button.height);
this.playAgain_button.anchor.set(0.5, 0.5);
this.playAgain_button.inputEnabled = true;
this.playAgain_button.events.onInputDown.add(this.startGame, this);
this.playAgain_button.events.onInputOver.add(buttonHighlightOn, this);
this.playAgain_button.events.onInputOut.add(buttonHighlightOut, this);
function pod1Complete() {
pod1.position.x = this.camera.width/2;
pod1tween = this.add.tween(pod1).to( { x: pod1.x + 100 }, 0, Phaser.Easing.Exponential.In, true, 0, 0);
pod1tween.onComplete.add(pod1Complete, this);
}
function pod2Complete() {
pod2.position.x = this.camera.width/2 - 50;
pod2tween = this.add.tween(pod2).to( { x: pod2.x + 50 }, 0, Phaser.Easing.Exponential.Out, true, 0, 0);
pod2tween.onComplete.add(pod2Complete, this);
}
var i = 0;
function pod3Complete() {
if (i%2 == 0) {
pod3tween = this.add.tween(pod3).to( { angle: 30 }, 0, Phaser.Easing.Exponential.Out, true, 0, 0);
} else {
pod3tween = this.add.tween(pod3).to( { angle: -30 }, 0, Phaser.Easing.Exponential.Out, true, 0, 0);
}
pod3tween.onComplete.add(pod3Complete, this);
i++;
}
//buttons
function buttonHighlightOn(a) {
a.tint = 0x015975;
}
function buttonHighlightOut(a) {
a.tint = 0xFFFFFF;
}
},
update: function () {
this.pos += this.cameraSpeed;
var pos = this.pos;
this.midground.forEach(function (item) {
if (item.type === 5) { // tileable sprite
// item.tilePosition.x = -(pos * item.parallax) + item.offset.x;
// item.tilePosition.y = item.offset.y;
if (item.velocity.x != 0 || item.velocity.y != 0) {
item.tilePosition.x += item.velocity.x;
item.tilePosition.y += item.velocity.y;
} else {
item.tilePosition.x = -(pos * item.parallax) + item.offset.x;
item.tilePosition.y = item.offset.y - item.velocity.y;
}
}
})
},
addBackground: function () {
var environmentBackground = this.environment['background'];
var backgroundGroup = this.background = this.add.group();
for (var key in environmentBackground) {
if (environmentBackground.hasOwnProperty(key)) {
if (environmentBackground[key].type === "unique") {
var unique = backgroundGroup.create(environmentBackground[key].position.x, environmentBackground[key].position.y, environmentBackground[key].texture);
unique.fixedToCamera = environmentBackground[key].fixedToCamera;
} else if (environmentBackground[key].type === "unique_randomized") {
var textures = environmentBackground[key].textures;
var texture_index = Math.floor(textures.length*Math.random());
var texture_name = textures[texture_index];
var unique = backgroundGroup.create(environmentBackground[key].position.x, environmentBackground[key].position.y, texture_name);
unique.fixedToCamera = environmentBackground[key].fixedToCamera;
}
}
}
},
addMidground: function () {
var environmentMidground = this.environment['midground'];
var midgroundGroup = this.midground = this.add.group();
for (var key in environmentMidground) {
if (environmentMidground.hasOwnProperty(key)) {
if (environmentMidground[key].type === "unique") {
midgroundGroup.create(environmentMidground[key].position.x, environmentMidground[key].position.y, environmentMidground[key].texture);
} else if (environmentMidground[key].type === "repeat") {
var tileable = this.add.tileSprite(environmentMidground[key].position.x, environmentMidground[key].position.y, this.camera.width, this.cache.getImage(environmentMidground[key].texture).height, environmentMidground[key].texture);
tileable.fixedToCamera = true;
tileable.tileScale = { "x": environmentMidground[key].tileScale.x, "y": environmentMidground[key].tileScale.y };
tileable.tilePosition = { "x": environmentMidground[key].tilePosition.x, "y": environmentMidground[key].tilePosition.y };
// need to pass some data to the update function so store it on the object
tileable['parallax'] = environmentMidground[key].parallax;
tileable['offset'] = environmentMidground[key].tilePosition;
tileable['velocity'] = environmentMidground[key].velocity;
midgroundGroup.add(tileable);
}
}
}
},
addForeground: function () {
this.foreground = this.add.group();
},
startGame: function (pointer) {
this.game['SoundManager'].music.stop()
this.game.state.start('Game');
}
};