-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainMenu.js
189 lines (149 loc) · 7.56 KB
/
MainMenu.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
BasicGame.MainMenu = function (game) {
this.bg;
this.title_image;
this.cameraSpeed = 10;
this.pos = 0;
// environment
this.environment;
this.background;
this.midground;
this.foreground;
this.playButton = null;
this.cursors;
};
BasicGame.MainMenu.prototype = {
init: function(){
var envs = this.game['GameData'].environments,
totalEnvs = envs.length;
var levelSelect = Math.floor(Math.random() * totalEnvs);
//levelSelect=1
this.environment = envs[levelSelect];
this.game['GameData'].startingEnvironment = levelSelect;
this.levelLength = this.game['GameData'].baseLevelLength * (Math.random() + 1);
console.log("Level: "+levelSelect)
// Non real time controls
// ESC pause game
var escapeKey = this.input.keyboard.addKey(Phaser.Keyboard.ENTER);
escapeKey.onDown.add(this.startGame, this);
},
create: function () {
//Audio
this.game['SoundManager'].music = this.add.sound('titleMusic');
this.game['SoundManager'].music.loop = true;
this.game['SoundManager'].music.play();
this.sound_click = this.add.sound('click');
// 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();
var title_image = this.add.sprite(this.game.width/2, this.game.height/2, 'title_image');
title_image.anchor.set(0.5, 0.5);
title_image.alpha = 0.1;
this.add.tween(title_image).to( { alpha: 1 }, 500, "Linear", true );
var playerButtonA = this.add.sprite(this.game.width/2 -7, (this.game.height/2) + 200, 'start_button_spritesheet');
var playerButtonALoop = playerButtonA.animations.add('start_loop');
playerButtonA.animations.play('start_loop', 30, true);
playerButtonA.hitArea = new PIXI.Rectangle(-playerButtonA.width/2, -playerButtonA.height/2, playerButtonA.width, playerButtonA.height);
playerButtonA.inputEnabled = true;
playerButtonA.events.onInputDown.add(this.startGame, this);
playerButtonA.events.onInputOver.add(buttonHighlightOn, this);
playerButtonA.events.onInputOut.add(buttonHighlightOut, this);
playerButtonA.anchor.set(0.5, 0.5);
playerButtonA.alpha = 0.1;
this.add.tween(playerButtonA).to( { alpha: 1 }, 500, "Linear", true );
function buttonHighlightOn(a) {
a.animations.stop('start_loop', false);
a.animations.frame = 14;
}
function buttonHighlightOut(a) {
a.animations.play('start_loop', 30, true);
}
},
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;
}
})
// this.midground.forEach(function (item) {
// if (item.type === 5) { // tileable sprite
// //item.tilePosition += item.velocity;
// 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 = -(camera.x * item.parallax) + item.offset.x;
// item.tilePosition.y = item.offset.y - item.velocity.y;
// }
// } else if (item.type === 0) {
// console.log(camera.x * item.parallax + item.offset.x)
// item.x = -(camera.x * item.parallax) + item.offset.x;
// //item.y = item.offset.y - item.velocity.y;
// }
// })
},
startGame: function (pointer) {
this.game['GameData'].currentLives = this.game['GameData'].lives;
this.game['GameData'].score = 0;
this.game['GameData'].currentStageScore = 0;
// And start the actual game
this.game.state.start('Instructions');
},
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();
}
};