denki_guy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ // ---
2
+ // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
3
+ // ---
4
+
5
+ var help={
6
+
7
+ // generates numbers from st to ed, skipping skip
8
+ seq:function(st,ed,skip) {
9
+ var ret=[];
10
+ for (var i=st;i<ed;i+=(skip==null?1:skip)) ret.push(i);
11
+ return ret;
12
+ },
13
+
14
+ // Handle a multiplier like counter. that means, 0=1 / 1=1 / 2=2*mul etc...
15
+ multiplier:function(v,mul) {
16
+ return (!v||(v<2)?1:v*(!mul?1:mul));
17
+ },
18
+
19
+ // pad str with heading pad until is "len" long
20
+ prepad:function(str,len,pad) {
21
+ str+="";
22
+ while (str.length<len) str=pad+str;
23
+ return str;
24
+ },
25
+
26
+ // pad str with tail pad until is "len" long
27
+ postpad:function(str,len,pad) {
28
+ str+="";
29
+ while (str.length<len) str+=pad;
30
+ return str;
31
+ },
32
+
33
+ // true if "th" is squished by "by"
34
+ isSquished:function(th,by) {
35
+ return ((by.accy>0)&&gbox.collides(th,by)&&(Math.abs(th.y-(by.y+by.h))<(th.h/2)))
36
+ },
37
+ // Random number
38
+ random:function(min,range) {
39
+ return min+Math.floor(Math.random()*range);
40
+ },
41
+ // Decides an animation frame.
42
+ // Args: (number,{speed:<animation speed>,frames:<animation sequence>})
43
+ // Outs: the frame
44
+ decideFrame:function(cnt,anim) {
45
+ return anim.frames[Math.floor(cnt/anim.speed)%anim.frames.length];
46
+ },
47
+ // Get the tile value in a map, using pixels as coords
48
+ // Args: (x,y,map,<output if any tile is found>,<index of the map array in the passed map>)
49
+ // Outs: the tile or ifout
50
+ getTileInMap:function(x,y,map,ifout,mapid) {
51
+ if (!mapid) mapid="map";
52
+ var ts=gbox._tiles[map.tileset];
53
+ var tx=Math.floor(x/ts.tilew);
54
+ var ty=Math.floor(y/ts.tileh);
55
+ if ((ty<0)||(ty>=map[mapid].length)) return ifout; else
56
+ if ((tx<0)||(tx>=map[mapid][ty].length)) return ifout; else
57
+ return map[mapid][ty][tx];
58
+ },
59
+ // Convert an ascii art array of string to a map. The first argument is the map, the second one is the translation map.
60
+ // i.e. (["---","- -","---"],[[null," "],[1,"-"]]);
61
+ asciiArtToMap:function(map,tra) {
62
+ var sz=tra[0][1].length;
63
+ var ret=[];
64
+ var xpos;
65
+ var pie;
66
+ for (var y=0;y<map.length;y++) {
67
+ var row=[];
68
+ xpos=0;
69
+ while (xpos<map[y].length) {
70
+ pie=map[y].substr(xpos,sz);
71
+ for (var t=0;t<tra.length;t++)
72
+ if (pie==tra[t][1]) {
73
+ if (t==0) row.push(null); else row.push(tra[t][0]);
74
+ break;
75
+ }
76
+ xpos+=sz;
77
+ }
78
+ ret.push(row);
79
+ }
80
+ return ret;
81
+ },
82
+
83
+
84
+ // Finalize a map definition, setting height and width in pixels etc.
85
+ // Args: (map)
86
+ // Outs: finalized map
87
+ finalizeTilemap:function(map) {
88
+ var ts=gbox._tiles[map.tileset];
89
+ map.h=map.map.length*ts.tileh;
90
+ map.w=map.map[0].length*ts.tilew;
91
+ map.hw=Math.floor(map.w/2);
92
+ map.hh=Math.floor(map.h/2);
93
+ return map;
94
+ },
95
+ // Converts a x-coord pixel in a x-coord tile
96
+ xPixelToTileX:function(map,x,gap) {
97
+ var ts=gbox._tiles[map.tileset];
98
+ return Math.floor(x/ts.tilew);
99
+ },
100
+ // Converts a x-coord pixel in a x-coord tile
101
+ yPixelToTileY:function(map,y,gap) {
102
+ var ts=gbox._tiles[map.tileset];
103
+ return Math.floor(y/ts.tileh);
104
+ },
105
+ // Converts a x-coord in pixel to the x-coord of the corresponding tile column in pixels
106
+ // Args: (map,x,<gap of the position in tiles>)
107
+ // Outs: the x coord in pixel of the tile column
108
+ xPixelToTile:function(map,x,gap) {
109
+ var ts=gbox._tiles[map.tileset];
110
+ return (Math.floor(x/ts.tilew)+(gap?gap:0))*ts.tilew;
111
+ },
112
+ // Converts a y-coord in pixel to the x-coord of the corresponding tile row in pixels
113
+ // Args: (map,y,<gap of the position in tiles>)
114
+ // Outs: the y coord in pixel of the tile row
115
+ yPixelToTile:function(map,y,gap) {
116
+ var ts=gbox._tiles[map.tileset];
117
+ return (Math.floor(y/ts.tileh)+(gap?gap:0))*ts.tileh;
118
+ },
119
+ // Limit a value between 2 numbers
120
+ // Args: (value,<minimal limit>,<maximal limit>)
121
+ // Outs: the value limited
122
+ limit:function(v,min,max) {
123
+ if (v<min) return min; else if (v>max) return max; else return v;
124
+ },
125
+ // Subtracts or adds 1 to a value to reach 0. i.e passing -3, outs -2. Passing 3, outs 2
126
+ // Args: (value)
127
+ // Outs: new value
128
+ goToZero:function(v) { return (v?v-(v/Math.abs(v)):0); },
129
+
130
+ // Copy a model on a data set if a value is not defined
131
+ mergeWithModel:function(data,model) {
132
+ if (data==null) data={};
133
+ if (model!=null)
134
+ for (var i in model)
135
+ if (data[i]==null) data[i]=model[i];
136
+ return data;
137
+ },
138
+
139
+ // Copy a model into data set
140
+ copyModel:function(data,model) {
141
+ if (data==null) data={};
142
+ if (model!=null)
143
+ for (var i in model) data[i]=model[i];
144
+ return data;
145
+ },
146
+
147
+ // Create a model from an object (copies attributes)
148
+ createModel:function(obj,attrs) {
149
+ var ret={};
150
+ for (var i=0;i<attrs.length;i++) ret[attrs[i]]=obj[attrs[i]];
151
+ return ret;
152
+ },
153
+
154
+ // Clones an objecy
155
+ cloneObject:function(model) {
156
+ if (!model) return model;
157
+ var data={};
158
+ for (var i in model) data[i]=model[i];
159
+ return data;
160
+ },
161
+
162
+ // Blit a tile in a map and changes a surface accordingly
163
+ setTileInMap:function(ctx,tilemap,x,y,tile,map) {
164
+ var ts=gbox.getTiles(tilemap.tileset);
165
+ tilemap[(map==null?"map":map)][y][x]=tile;
166
+ if (tile==null)
167
+ gbox.blitClear(ctx,{x:x*ts.tilew,y:y*ts.tilew,h:ts.tileh,w:ts.tilew});
168
+ else
169
+ gbox.blitTile(ctx,{tileset:tilemap.tileset,tile:tile,dx:x*ts.tilew,dy:y*ts.tilew});
170
+ },
171
+
172
+ // Get the item of an array, if available. Else returns the last one
173
+ getArrayCapped:function(a,id) {
174
+ if (id>=a.length) return a[a.length-1]; else return a[id];
175
+ },
176
+
177
+ // Get an item of an array of object, using a field as index. is returned the first entry if the field is not valued.
178
+ getArrayIndexed:function(a,value,field) {
179
+ if (a[0][field]==null) return a[0];
180
+ var i=0;
181
+ while ((value>a[i][field])&&(i!=a.length-1)) i++;
182
+ return a[i];
183
+ },
184
+
185
+
186
+ // Convert frames to minutes, seconds and csecs
187
+ framestotime:function(frames) {
188
+ var csec=Math.ceil(frames/gbox.getFps()*100);
189
+ return this.prepad((Math.floor(csec/6000)%60),2,"0")+":"+this.prepad((Math.floor(csec/100)%60),2,"0")+":"+this.prepad(csec%100,2,"0");
190
+
191
+ },
192
+
193
+ // get an url parameter
194
+ geturlparameter:function( name ) {
195
+ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
196
+ var regexS = "[\\?&]"+name+"=([^&#]*)";
197
+ var regex = new RegExp( regexS );
198
+ var results = regex.exec( window.location.href );
199
+ if( results == null )
200
+ return "";
201
+ else
202
+ return results[1];
203
+ },
204
+
205
+ // transform simple object in string, for debug
206
+ objToStr:function(o) {
207
+ var ret="";
208
+ for (var n in o) ret+=n+":["+o[n]+"] ";
209
+ return ret;
210
+ },
211
+
212
+ // The default Akihabara initialization. Low-res, low-framerate, zoomed view
213
+ akihabaraInit:function(title) {
214
+ var device=iphopad.getDeviceConfig();
215
+
216
+ document.title=(title?title:"Akihabara");
217
+ document.body.style.backgroundColor="#2A2D34";
218
+ gbox.setScreenBorder(false);
219
+ if (help.geturlparameter("statusbar")) gbox.setStatusBar(1);
220
+ if (help.geturlparameter("db")) gbox.setDoubleBuffering(true);
221
+ if (help.geturlparameter("noautoskip")) gbox.setAutoskip(null);
222
+ if (help.geturlparameter("zoom")) gbox.setZoom(help.geturlparameter("zoom")); else
223
+ if (device.zoom) gbox.setZoom(device.zoom); else
224
+ if (device.width) gbox.setZoom(device.width/320);
225
+ if (help.geturlparameter("fps")) gbox.setFps(help.geturlparameter("fps")*1);
226
+ else gbox.setFps(25);
227
+ if (help.geturlparameter("fskip")) gbox.setFrameskip(help.geturlparameter("fskip"));
228
+
229
+
230
+ gbox.initScreen(320,240);
231
+
232
+ if (help.geturlparameter("touch")=="no");
233
+ else if ((help.geturlparameter("touch")=="yes")||device.touch)
234
+ iphopad.initialize({h:100,dpad:"images/game/akihbara/dpad.png",buttons:"images/game/akihabara/buttons.png",bg:"images/game/akihabara/padbg.png"});
235
+
236
+ // Debug fonts
237
+ gbox.addImage("_dbf","images/game/akihabara/debugfont.png");
238
+ gbox.addFont({id:"_dbf",image:"_dbf",firstletter:" ",tileh:5,tilew:4,tilerow:16,gapx:0,gapy:0});
239
+
240
+ }
241
+ }
@@ -0,0 +1,78 @@
1
+ var object_to_add = { id:"player",
2
+ group:"player",
3
+ tileset:"player",
4
+ zindex:0, // Needed for zindexed objects
5
+ isPaused:false, // Pauses the player during dialogues, cutscenes etc.
6
+
7
+ doPause:function(p) {
8
+ this.isPaused=p;
9
+ },
10
+
11
+ initialize:function() {
12
+ toys.topview.initialize(this,{
13
+ haspushing:true,
14
+ shadow:{tileset:"shadows",tile:0},
15
+ frames:{
16
+ standup:{ speed:1, frames:[0] },
17
+ standdown:{ speed:1, frames:[3] },
18
+ standleft:{ speed:1, frames:[6] },
19
+ standright:{ speed:1, frames:[6] },
20
+ movingup:{speed:3,frames:[0,1,0,2] },
21
+ movingdown:{speed:3,frames:[3,4,3,5] },
22
+ movingleft:{speed:3,frames:[6,7] },
23
+ movingright:{speed:3,frames:[6,7] },
24
+ pushingup:{speed:6,frames:[0,1,0,2] },
25
+ pushingdown:{speed:6,frames:[3,4,3,5] },
26
+ pushingleft:{speed:6,frames:[6,7] },
27
+ pushingright:{speed:6,frames:[6,7] }
28
+ }
29
+ });
30
+ },
31
+
32
+
33
+ first:function() {
34
+
35
+ // Counter
36
+ this.counter=(this.counter+1)%60;
37
+
38
+ var pl=gbox.getObject("player","player");
39
+ var ontile=help.getTileInMap(pl.x+pl.colx+pl.colhw,pl.y+pl.coly+pl.colhh,tilemaps[tilemaps.current],tilemaps._defaultblock,"map");
40
+
41
+ //take player to link
42
+ for (var value in tiles_to_leave_from){
43
+ if (ontile==value) {
44
+ leave_page(tiles_to_leave_from[value]);
45
+ }
46
+ }
47
+
48
+ if (this.stilltimer||maingame.gameIsHold()||this.isPaused)
49
+ toys.topview.controlKeys(this,{}); // Stays still. No key is moving! :)
50
+ else
51
+ toys.topview.controlKeys(this,{left:"left",right:"right",up:"up",down:"down"}); // Moves (if not attacking)
52
+ toys.topview.handleAccellerations(this);
53
+ toys.topview.handleGravity(this); // z-gravity
54
+ toys.topview.applyForces(this); // Apply forces
55
+ toys.topview.tileCollision(this,tilemaps[tilemaps.current],"map",tilemaps._defaultblock); // tile collisions
56
+ toys.topview.floorCollision(this); // Collision with the floor (for z-gravity)
57
+ toys.topview.spritewallCollision(this,{group:"walls"}); // Doors and tresaure chests are sprites that acts like a wall.
58
+ toys.topview.adjustZindex(this);
59
+ if (!this.stilltimer) toys.topview.setFrame(this); // set the right animation frame (if not attacking)
60
+
61
+ if (!this.isPaused&&!maingame.gameIsHold())
62
+ if (gbox.keyIsHit("a")) {
63
+ var ahead=toys.topview.getAheadPixel(this,{distance:5});
64
+ ahead.group="walls";
65
+ ahead.call="doPlayerAction";
66
+ if (!toys.topview.callInColliding(this,ahead)) // if any action is done
67
+ maingame.hud.addValue("weapon","value",1);
68
+ }
69
+
70
+ },
71
+ blit:function() {
72
+
73
+ gbox.blitTile(gbox.getBufferContext(),{tileset:this.shadow.tileset,tile:this.shadow.tile,dx:this.x,dy:this.y+this.h-gbox.getTiles(this.shadow.tileset).tileh+4,camera:this.camera});
74
+ // Then the object. Notes that the y is y+z to have the "over the floor" effect.
75
+ gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
76
+ }
77
+
78
+ }
@@ -0,0 +1,120 @@
1
+ // ---
2
+ // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
3
+ // ---
4
+
5
+ var iphopad={
6
+ _buttonsize:50,
7
+ _buttonsize2:100,
8
+ _buttonsize3:150,
9
+ _gapx:0,
10
+ _gapy:0,
11
+ _width:0,
12
+ _height:0,
13
+ _center:{},
14
+ _cross:{up:false,down:false,left:false,right:false},
15
+ _buttons:{a:false,b:false,c:false},
16
+ _transl:(Math.PI*0.125),
17
+ _brad:(Math.PI*0.25),
18
+ _positions:[
19
+ {up:false,down:false,left:false,right:true},
20
+ {up:false,down:true,left:false,right:true},
21
+ {up:false,down:true,left:false,right:false},
22
+ {up:false,down:true,left:true,right:false},
23
+ {up:false,down:false,left:true,right:false},
24
+ {up:true,down:false,left:true,right:false},
25
+ {up:true,down:false,left:false,right:false},
26
+ {up:true,down:false,left:false,right:true}
27
+ ],
28
+ _swap:false,
29
+ _listen:function(e) {
30
+ var nc={up:false,down:false,left:false,right:false};
31
+ var nb={a:false,b:false,c:false};
32
+ for (var i=0;i<e.touches.length;i++) {
33
+ rp={x:e.touches[i].pageX-iphopad._gapx,y:e.touches[i].pageY-iphopad._gapy};
34
+ if (rp.x<iphopad._height)
35
+ nc=iphopad._positions[Math.floor(trigo.getAngle(iphopad._center,rp,iphopad._transl)/iphopad._brad)];
36
+ else if (rp.x>iphopad._width-iphopad._buttonsize)
37
+ nb.a=true;
38
+ else if (rp.x>iphopad._width-iphopad._buttonsize2)
39
+ nb.b=true;
40
+ else if (rp.x>iphopad._width-iphopad._buttonsize3)
41
+ nb.c=true;
42
+
43
+ }
44
+ this._swap=!this._swap;
45
+ for (var i in this._cross) {
46
+ if (nc[i]!=iphopad._cross[i])
47
+ if (nc[i]) gbox._keydown({fake:true,keyCode:gbox._keymap[i]});
48
+ else gbox._keyup({fake:true,keyCode:gbox._keymap[i]});
49
+ }
50
+ for (var i in this._buttons) {
51
+ if (nb[i]!=iphopad._buttons[i])
52
+ if (nb[i]) gbox._keydown({fake:true,keyCode:gbox._keymap[i]});
53
+ else gbox._keyup({fake:true,keyCode:gbox._keymap[i]});
54
+ }
55
+
56
+ iphopad._cross=nc;
57
+ iphopad._buttons=nb;
58
+ },
59
+ _fakelisten:function(e) {
60
+ iphopad._listen({
61
+ touches:[
62
+ {
63
+ pageX:e.clientX,
64
+ pageY:e.clientY
65
+ }
66
+ ]
67
+ });
68
+ },
69
+ getDeviceConfig:function() {
70
+ if (navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/Android/i))
71
+ return {touch:true,width:320};
72
+ else if (navigator.userAgent.match(/iPad/i))
73
+ return {touch:true,width:768};
74
+ else
75
+ return {zoom:2};
76
+ },
77
+ initialize:function(data) {
78
+ var oElement=document.createElement("div");
79
+ oElement.style.margin="auto";
80
+ oElement.style.padding="0px";
81
+ oElement.style.height=data.h+"px";
82
+ oElement.style.width="100%";
83
+ oElement.style.backgroundImage="url("+data.bg+")";
84
+ oElement.style.backgroundRepeat="repeat-x";
85
+
86
+ var tpad=document.createElement("div");
87
+ tpad.style.cssFloat="left";
88
+ tpad.style.padding="0px";
89
+ tpad.style.margin="0px";
90
+ tpad.style.height=data.h+"px";
91
+ tpad.style.width=data.h+"px";
92
+ tpad.style.backgroundImage="url("+data.dpad+")";
93
+ tpad.style.backgroundRepeat="no-repeat";
94
+
95
+ var bpad=document.createElement("div");
96
+ bpad.style.cssFloat="right";
97
+ bpad.style.padding="0px";
98
+ bpad.style.margin="0px";
99
+ bpad.style.height=data.h+"px";
100
+ bpad.style.width=iphopad._buttonsize3+"px";
101
+ bpad.style.backgroundImage="url("+data.buttons+")";
102
+ bpad.style.backgroundRepeat="no-repeat";
103
+
104
+ oElement.appendChild(tpad);
105
+ oElement.appendChild(bpad);
106
+ gbox._box.appendChild(oElement);
107
+
108
+ oElement.ontouchstart=function(evt) { evt.preventDefault();evt.stopPropagation(); iphopad._listen(evt) };
109
+ oElement.ontouchend=function(evt) { evt.preventDefault();evt.stopPropagation();iphopad._listen(evt) };
110
+ oElement.ontouchmove=function(evt) { evt.preventDefault();evt.stopPropagation();iphopad._listen(evt) };
111
+ //oElement.onmousemove=function(evt) { iphopad._fakelisten(evt) };
112
+ var sizes=gbox._domgetabsposition(oElement);
113
+ this._gapx=sizes.x;
114
+ this._gapy=sizes.y;
115
+ this._width=sizes.w;
116
+ this._height=sizes.h;
117
+ this._center={x:Math.floor(this._height/2),y:Math.floor(this._height/2)};
118
+ }
119
+
120
+ }