-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
174 lines (156 loc) · 4.9 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
$(document).ready(function() {
var menu, front_layer, back_layer, emojiCursor, cameraSound;
var imagedata = JSON.parse(data);
var width = 400;
var height = 600;
var menuHeight = 75;
var game = new Phaser.Game(width, height, Phaser.AUTO, 'game', { preload: preload, create: create, update: update}, true, false);
var npc = {};
var initialScale = 0;
var initialX = 95;
var initialY = -400;
var hover = false;
var writing = false;
var fashionPhrases = ["Tres chic!", "So stylish", "<3 this look", "Cool outfit!", "Very hip"];
// Tabs
$("#toggle_street").click(function() {
$("#blog").hide();
$("#credits").hide();
$("#game").show()
});
$("#toggle_blog").click(function() {
$("#game").hide();
$("#credits").hide();
$("#blog").show()
});
$("#toggle_credits").click(function() {
$("#game").hide();
$("#blog").hide();
$("#credits").show();
});
function createRandomSprite(type){
object = imagedata[type];
var index = Math.floor((Math.random() * object.length));
var frame1 = type + '/' + (index + 1) + '/' + object[index]["1"];
var frame2 = type + '/' + (index + 1) + '/' + object[index]["2"];
var sprite = back_layer.create(initialX, initialY, 'fashion', frame1);
sprite.scale.setTo(initialScale, initialScale);
sprite.animations.add('walk', [frame1, frame2], 2.5, true, false);
sprite.animations.play('walk');
sprite.name = type;
return sprite;
}
function newNPC(){
var npc = {};
var spriteGroup = game.add.group();
back_layer.add(spriteGroup);
spriteGroup.add(createRandomSprite('body'));
spriteGroup.add(createRandomSprite('hair'));
// randomly decide if jumpsuit or pants/top
var random = Math.floor((Math.random() * 2));
if (random == 0){
spriteGroup.add(createRandomSprite('jumpsuits'));
}
else {
spriteGroup.add(createRandomSprite('pants'));
spriteGroup.add(createRandomSprite('tops'));
}
spriteGroup.add(createRandomSprite('shoes'));
spriteGroup.forEach(function(item) {
item.inputEnabled = true;
item.input.useHandCursor = true;
item.events.onInputOver.add(function(){
hover = true;
emojiCursor.visible = true;
}, this);
item.events.onInputOut.add(function(){
hover = false;
emojiCursor.visible = false;
}, this);
item.events.onInputDown.add(clickOnStyle, this);
}, this);
npc.x = initialX;
npc.y = initialY;
npc.scale = initialScale;
npc.sprites = spriteGroup;
npc.blogged = false;
return npc;
}
function clickOnStyle(){
if (npc.blogged || writing){
return;
}
writing = true;
cameraSound.play();
text = new Phaser.BitmapText(game, 10, 50, "superscript", "I love your\noutfit!", 30);
game.time.events.add(1000, text.destroy, text);
front_layer.add(text);
var bitmap = new Phaser.BitmapData(game, "bitmap", 78, 190);
npc.sprites.forEach(function(item) {
item.y = 0;
item.x = 0;
item.scale.setTo(1, 1);
bitmap.draw(item);
item.y = npc.y;
item.x = npc.x;
item.scale.setTo(npc.scale, npc.scale);
}, this);
$('body').append("<div id='writepost'><h2>Write a blog post!</h2>"
+ "<form><textarea rows='5' class='emojis-wysiwyg'></textarea></form>"
+ "<button id='emojibutton'>Emoji</button><button id='post'>Post</button>"
+ "<button id='cancel'>Cancel</button></div>");
$('.emojis-wysiwyg').emojiarea({wysiwyg: true, button: '#emojibutton'});
$('#post').click(function(){
// Post the blog entry
var entry = $('.emoji-wysiwyg-editor').html();
var data = bitmap.baseTexture.source;
var image = data.toDataURL("image/png");
var index = Math.floor((Math.random() * fashionPhrases.length));
$("#blog").append("<div><h1>" + fashionPhrases[index]
+ "</h1><h3>Posted on "
+ new Date($.now()) + "</h3><div class='entry'>" + entry
+ "</div><img class='blogimg' src='"
+ image + "' /></div>");
$('#writepost').remove();
npc.blogged = true;
writing = false;
});
$('#cancel').click(function(){
$('#writepost').remove();
writing = false;
});
}
function preload() {
game.load.atlasJSONHash('fashion', 'images/fashion.png', 'images/fashion.json');
game.load.bitmapFont('superscript', 'font/superscript.png', 'font/superscript.fnt');
game.load.audio('camera', 'audio/166500__thompsonman__camera-shutter.wav');
game.load.image('emojiCursor', 'emojiarea/packs/basic/images/camera.png')
}
function create() {
back_layer = game.add.group();
front_layer = game.add.group();
cameraSound = game.add.audio('camera');
emojiCursor = front_layer.create(0, 0, 'emojiCursor');
emojiCursor.visible = false;
emojiCursor.scale.setTo(0.5, 0.5);
npc= newNPC();
}
function update() {
if (!hover && !writing){
npc.y += 1.5;
npc.scale = (((npc.y)/height)*(1.5))+3;
npc.sprites.forEach(function(item) {
item.y = npc.y;
item.scale.setTo(npc.scale, npc.scale);
}, this);
// new NPC if out of frame
if (npc.y > height){
npc = newNPC();
}
}
else {
emojiCursor.x = game.input.x;
emojiCursor.y = game.input.y;
}
}
});