This repository has been archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyarukozone_mapcol.js
185 lines (180 loc) · 6.57 KB
/
nyarukozone_mapcol.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
//nyarukozone.js
//場景控制器
var nyarukozone_mapbackgrounds = [];
var nyarukozone_mapprospects = [];
var nyarukozone_collider = [];
var nyarukozone_mapimageall = 0;
var nyarukozone_mapimagearrlen = [0,0];
var nyarukozone_mapimagecache = [];
var nyarukozone_mapinfo = null;
var nyarukozone_mapsize = [];
//清除當前場景
function nyarukozone_resetmap() {
$(".nyarukozone_map").remove();
nyarukozone_mapbackgrounds = [];
nyarukozone_mapprospects = [];
nyarukozone_collider = [];
nyarukozone_mapimageall = 0;
nyarukozone_mapimagearrlen = [0,0];
nyarukozone_mapimagecache = [];
nyarukozone_mapinfo = null;
nyarukozone_mapsize = [];
}
//裝入一個場景
function nyarukozone_loadMap(url) {
nyarukozone_resetmap();
var jsonurl = url + "/index.json";
YSLog("Loading: "+jsonurl);
try {
$.getJSON(jsonurl,function(responseTxt,statusTxt,xhr,data){
if(statusTxt == "error") {
YSLog("E: Download configuration failed. "+xhr.status+": "+xhr.statusText);
}
if(statusTxt == "success") {
nyarukozone_mapinfo = xhr.responseJSON;
var info = nyarukozone_mapinfo["info"];
if (info["filevar"] != 1) {
YSLog("E: mapfilevar");
return
}
nyarukozone_loadMapImage(url,nyarukozone_mapinfo["background"],nyarukozone_mapinfo["prospect"],nyarukozone_mapinfo["collider"]);
YSLog("Load Map Config: " + info["name"]);
}
});
} catch (ex) {
YSLog("E: Data loading failed: "+ex);
}
}
//裝入場景圖片設定
function nyarukozone_loadMapImage(url,backgrounds,prospects,collider) {
nyarukozone_mapimagearrlen = [backgrounds.length,prospects.length];
nyarukozone_mapimageall = nyarukozone_mapimagearrlen[0] + nyarukozone_mapimagearrlen[1] + 1;
$.each(backgrounds, function(i, item) {
nyarukozone_loadMapImage2(1,url,item);
});
$.each(prospects, function(i, item) {
nyarukozone_loadMapImage2(2,url,item);
});
nyarukozone_loadMapImage2(3,url,collider);
}
//下載場景圖片
function nyarukozone_loadMapImage2(type,url,item) {
var nowurl = url + "/" + item;
YSLog("Loading: "+nowurl);
var nowimg = new Image();
nowimg.src = nowurl;
nowimg.onload = function() {
nyarukozone_mapimagecache[nyarukozone_mapimagecache.length] = [type,$(this)];
if (nyarukozone_mapimagecache.length >= nyarukozone_mapimageall) {
nyarukozone_loadMapImage3();
}
};
}
//設定圖片z軸
function nyarukozone_loadMapImage3() {
// var nyarukozone_mapbackgrounds = [];
// var nyarukozone_mapprospects = [];
var nowcollider = null;
$.each(nyarukozone_mapimagecache, function(i, nowimginfo) {
if (nowimginfo[0] == 1) {
nyarukozone_mapbackgrounds[nyarukozone_mapbackgrounds.length] = nowimginfo[1];
} else if (nowimginfo[0] == 2) {
nyarukozone_mapprospects[nyarukozone_mapprospects.length] = nowimginfo[1];
} else if (nowimginfo[0] == 3) {
nowcollider = nowimginfo[1];
}
});
$.each(nyarukozone_mapbackgrounds, function(i, nimg) {
nyarukozone_loadMapImage4(100001+i,nimg);
});
$.each(nyarukozone_mapprospects, function(i, nimg) {
nyarukozone_loadMapImage4(600000+i,nimg);
});
nyarukozone_loadEcollider(nowcollider[0]);
YSLog("Load Map OK.");
}
//轉換碰撞圖片
function nyarukozone_loadEcollider(nowcolliderimg) {
nyarukozone_div.append('<canvas class="nyarukozone_ecollider" id="nyarukozone_ecollider" width="'+nyarukozone_mapsize[0]+'px" height="'+nyarukozone_mapsize[1]+'px" style="z-index:100000">ERROR</canvas>');
var ctxt = nyarukozone_ecollider.getContext('2d');
var icollider = new Image;
icollider.onload = function(){
ctxt.drawImage(icollider, 0, 0);
var icolliderwidth = icollider.width;
var icolliderheight = icollider.height;
var data = ctxt.getImageData(0, 0, icolliderwidth, icolliderheight).data;
nyarukozone_collider.splice(0,nyarukozone_collider.length);
var widthi = 0;
var heighti = 0;
var widths = [];
var truei = 0;
var falsei = 0;
for(var i =0,len = data.length; i<len;i+=4){
//var red = data[i], green = data[i+1], blue = data[i+2], alpha = data[i+3];
var nc = false;
if (data[i] > 0) {
nc = true;
truei++;
} else {
falsei++;
}
widths[widthi] = nc;
widthi++;
if (widthi >= icolliderwidth) {
widthi = 0;
nyarukozone_collider[heighti] = widths.slice(0,widths.length);
widths.splice(0,widths.length);
heighti++;
}
}
YSLog("Load Ecollider OK. PASS="+falsei+", NO="+truei);
$(".nyarukozone_ecollider").remove();
icollider = null;
ctxt = null;
}
icollider.src = nowcolliderimg.src;
}
//呈現場景
function nyarukozone_loadMapImage4(z,img) {
if (nyarukozone_mapsize.length == 0) {
nyarukozone_mapsize = [img[0].width,img[0].height];
YSLog("Map Size: "+nyarukozone_mapsize[0]+" x "+nyarukozone_mapsize[1]+" = "+(nyarukozone_mapsize[0]*nyarukozone_mapsize[1]));
}
img.addClass("nyarukozone_spirit");
img.addClass("nyarukozone_map");
img.css({"z-index":z,"top":"0px","left":"0px"});
nyarukozone_div.append(img);
}
//演算地圖移動位置,防止超出。返回負數用角色移動補償。
function nyarukozone_mapmove(x,y) {
var windoww = nyarukozone_div.width() / 2;
var windowh = nyarukozone_div.height() / 2;
var mx = windoww - x;
var mxx = nyarukozone_div.width() - nyarukozone_mapsize[0];
var my = windowh - y;
var myy = nyarukozone_div.height() - nyarukozone_mapsize[1];
var rolex = windoww;
var roley = windowh;
if (mx > 0) {
mx = 0;
rolex = x;
} else if (mx < mxx) {
mx = mxx;
rolex = nyarukozone_div.width() - (nyarukozone_mapsize[0] - x);
}
if (my > 0) {
my = 0;
roley = y;
} else if (my < myy) {
my = myy;
roley = nyarukozone_div.height() - (nyarukozone_mapsize[1] - y);
}
$.each(nyarukozone_mapbackgrounds, function(i) {
nyarukozone_mapbackgrounds[i].css({"top":my+"px","left":mx+"px"});
});
$.each(nyarukozone_mapprospects, function(i) {
nyarukozone_mapprospects[i].css({"top":my+"px","left":mx+"px"});
});
var nmbool = [rolex,roley];
return nmbool;
}