-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
193 lines (176 loc) · 6.56 KB
/
main.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
// configuration
var milkcocoa = new MilkCocoa("zooii44z3rf.mlkcca.com");
var bearDataStore = milkcocoa.dataStore("bear");
enchant();
window.onload = function(){
var core = new Core(320, 320);
core.preload('chara1.png', 'pipo-map001.png');
core.fps = 6;
core.onload = function(){
// <--- object configuration -->
// 便利クラス
var util = {
getQueryString: function(){
var result = {};
if( 1 < window.location.search.length )
{
var query = window.location.search.substring( 1 );
var parameters = query.split( '&' );
for( var i = 0; i < parameters.length; i++ )
{
var element = parameters[ i ].split( '=' );
var paramName = decodeURIComponent( element[ 0 ] );
var paramValue = decodeURIComponent( element[ 1 ] );
result[ paramName ] = paramValue;
}
}
return result;
}
}
// bearを管理するクラス
var bearBlockManager = {
bearList: {},
addBearBlock: function(username, bearBlock){
this.bearList[username] = bearBlock;
return bearBlock;
},
getBearBlock: function(key){
return this.bearList[key];
},
}
// bearBlockクラス
var BearBlock = Class.create(Group, {
initialize: function(username, bearObj){
Group.call(this);
this.bear = bearObj;
this.addChild(bearObj);
var nameLabel = new Label();
nameLabel.y = 32;
nameLabel.font = '12px "Arial"';
nameLabel.text = username;
this.addChild(nameLabel);
var commentLabel = new Label();
commentLabel.y = -10;
commentLabel.font = '14px "Arial"';
commentLabel.text = "こんにちは";
this.addChild(commentLabel);
this.commentLabel = commentLabel;
}
});
// 自分のbearのクラス
var MyBear = Class.create(Sprite, {
initialize: function(x, y){
Sprite.call(this, 32, 32);
this.x = x;
this.y = y;
this.image = core.assets['chara1.png'];
},
});
// 他ユーザーのbearのクラス
var OtherBear = Class.create(Sprite, {
initialize: function(x, y){
Sprite.call(this, 32, 32);
this.image = core.assets['chara1.png'];
this.on('enterframe', function(){
this.frame = this.age % 3 + 5;
});
}
});
// sceneのテンプレートクラス
var GameScene = Class.create(Scene, {
initialize: function(string, stringColor, backgroundColor){
Scene.call(this);
this.backgroundColor = backgroundColor;
var label = new Label();
label.x = 80;
label.y = 100;
label.color = stringColor;
label.font = '32px "Arial"';
label.text = string;
this.addChild(label);
},
});
// <-- object configuration /-->
var queryObj = util.getQueryString();
var username = queryObj.username;
var startScene = new GameScene('Game Start!', 'blue', '#EEE');
var gameScene = new GameScene('','','#BBB');
for(var i = 0; i < 10; i++){
for(var j = 0; j < 10; j++){
var bg = new Sprite(32, 32);
bg.image = core.assets['pipo-map001.png'];
bg.frame = 0;
bg.x = 0 + 32*i;
bg.y = 0 + 32*j;
gameScene.addChild(bg);
}
}
var tree = new Sprite(32, 32);
tree.image = core.assets['pipo-map001.png'];
tree.frame = 10;
tree.x = 32*6;
tree.y = 32*2;
gameScene.addChild(tree);
var tree2 = new Sprite(32, 32);
tree2.image = core.assets['pipo-map001.png'];
tree2.frame = 10;
tree2.x = 32*3;
tree2.y = 32*5;
gameScene.addChild(tree2);
var tree3 = new Sprite(32, 32);
tree3.image = core.assets['pipo-map001.png'];
tree3.frame = 10;
tree3.x = 32*1;
tree3.y = 32*3;
gameScene.addChild(tree3);
var input = new Entity();
input.width = 300;
input.height = 20;
input.x = 6;
input.y = 290;
input._element = document.createElement('input');
input._element.setAttribute("name","myText");
input._element.setAttribute("type","text");
input._element.value = "こんにちは";
gameScene.addChild(input);
var myBear = new MyBear(0,0);
var myBearBlock = new BearBlock(username, myBear);
myBearBlock.on('enterframe', function(){
if(core.input.left){this.x -= 5;}
if(core.input.right){this.x += 5;}
if(core.input.up){this.y -= 5;}
if(core.input.down){this.y += 5;}
this.bear.frame = this.age % 3;
this.commentLabel.text = input._element.value;
bearDataStore.set(username,
{
name : username,
comment : input._element.value,
x : this.x,
y : this.y
}
);
});
bearBlockManager.addBearBlock(username, myBearBlock);
gameScene.addChild(myBearBlock);
startScene.on('touchstart', function(){
core.pushScene(gameScene);
});
core.pushScene(startScene);
bearDataStore.on("set",function(data){
bearBlock = bearBlockManager.getBearBlock(data.id);
if(bearBlock != undefined){
bearBlock.x = data.value.x;
bearBlock.y = data.value.y;
bearBlock.commentLabel.text = data.value.comment;
} else {
otherBear = new OtherBear(data.value.x, data.value.y);
otherBearBlock = new BearBlock(data.id, otherBear);
otherBearBlock.commentLabel.text = data.value.comment;
bearBlockManager.addBearBlock(data.id, otherBearBlock);
gameScene.addChild(otherBearBlock);
}
});
}
core.start();
};