-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhunchback.js
More file actions
227 lines (183 loc) · 5.87 KB
/
hunchback.js
File metadata and controls
227 lines (183 loc) · 5.87 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Project: hunchback
* User: james
* Date: 30/08/13
* Time: 15:03
*/
"use strict";
var TILE_SHEET;
var TOTAL_SCROLL;
var TILES;
var screen;
var robopunk;
var BG;
var done = 0;
var i = 0;
var POS = 0 | 0; // the position of robopunk
var BACK_POS = 0 | 0; // the position of the tile layer
/* --------------- FUNCTIONS / HANDLERS ETC ---------------- */
/**
* kick things off.
* need this hoisted I think
*/
function ready()
{
/* Fire up PIX. */
if (PIX.SURF_Init(document.getElementById('canvas'),640,480) < 0) {
throw "Unable to initialize PIX";
}
/* Save the screen pointer for later use. */
screen = PIX.SURF_GetMainSurface();
/* Set the color key on the ship animation strip to black.
Enable RLE acceleration for a performance boost. */
// PIX.SURF_SetColorKey(0x000000);
/* Load the game's data into globals. */
LoadGameData(); //hmmmm tail call.....
}
/**
* Loads the game's resources. Exits the program on failure.
*/
var LoadGameData = function() {
TILE_SHEET = PIX.SURF_NewSurface();
BG = PIX.SURF_NewSurface();
PIX.IMG_QueueImage(TILE_SHEET,"ROBOPUNK.png");
PIX.IMG_QueueImage(BG,"background.png");
PIX.IMG_LoadImages(onImagesLoaded);
};
var onImagesLoaded = function() {
initBackground();
initPlayer();
//InitOpponent();
PIX.SURF_FreeSurface(TILE_SHEET); // free up this memory
mainLoop();
};
var initPlayer = function() {
// create a sprite for robopunk
robopunk = PIX.SPR_NewSprite(0,395,32,32,0,0,0,0,0); // sprite object
// extract animation cells for robopunk
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,0,3,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,1,5,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,2,4,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,3,5,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,4,6,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,5,1,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,6,2,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,7,1,0);
PIX.SPR_GrabBitmap(TILE_SHEET,robopunk,8,0,0);
};
var initBackground = function() {
var index;
TILES = PIX.TIL_NewTile(32,32,1,6); // tile object
// extract background cells
for (index=0; index<8; index++)
{
PIX.TIL_GrabBitmap(TILE_SHEET,TILES,index,index,1); // TILE_SHEET is a surface
} // end for index
PIX.TILESCR_Init(TILES,32,32,3,132); // initialise the tile scroller
TOTAL_SCROLL = PIX.TILESCR_GetTotalScroll();
};
var OLDmainLoop = function() {
PIX.LAY_DrawLayers(i); // draw parallax layer(s) in RGB_VIEW
PIX.SURF_Flip();
i++;
/* end main loop body */
if(i<200)
{requestAnimFrame(mainLoop);}
else
{/* exit loop */ console.log("done");}
};
/**
* this is the game loop
*/
var mainLoop = function() {
// erase robopunk
PIX.SPR_EraseSprite(robopunk);
/* Grab a snapshot of the keyboard. */
var pressed = PIX.KEY_GetKeyState();
// has the user pressed a key?
if(Object.keys(pressed).length)
{
// test what key was pressed
if(pressed[39]){
//checkCollisions((POS+32)>>5); // this expression yeilds x component for tilemap look up
//TILE_MAP[((POS+32) >>5 )] != 4
(POS += 8) | 0;
if(POS > TOTAL_SCROLL)
{
POS = TOTAL_SCROLL;
} else {
BACK_POS -= 1; // scroll BACK_POS left
// one pixel
if(BACK_POS < 1) // did we read the end??
BACK_POS += PIX.mainBufferWidth; // yes, wrap around
}
// advance the animation frame and move player
// test if player is moving left, if so
// show player turning before moving
if (robopunk.curr_frame > 4)
{
robopunk.curr_frame = 0;
} // end if player going right
else
if (robopunk.curr_frame == 0 )
robopunk.curr_frame =1;
else
{
// player is already in rightward motion so continue
if (++robopunk.curr_frame > 4)
robopunk.curr_frame = 1;
} // end else
}
if(pressed[37]) {
(POS -= 8) | 0;
if(POS < 0)
{
POS = 0;
} else {
BACK_POS += 1; // scroll BACK_POS right
// one pixel
if(BACK_POS > PIX._mainBufferWidth - 1) // reach end??
BACK_POS -= PIX._mainBufferWidth; // yes, wrap around
}
// advance the animation frame and move player
// test if player is moving right, if so
// show player turning before moving
if (robopunk.curr_frame > 0 && robopunk.curr_frame < 5)
{
robopunk.curr_frame = 0;
}
else if (robopunk.curr_frame == 0 )
{
robopunk.curr_frame = 5;
}
else
{
// player is already in leftward motion so continue
if (++robopunk.curr_frame > 8)
{
robopunk.curr_frame = 5;
}
} // end else
//console.log( TILE_MAP[((POS+8) >>5 )] );
}
if(pressed[81]) {
// the user is exiting
console.log('quitting');
done = 1;
}
}
PIX.LAY_DrawLayers(BACK_POS,POS); // draw parallax layer(s) in RGB_VIEW
// scan background under robopunk
PIX.SPR_BehindSprite(robopunk);
// draw him
PIX.SPR_DrawSprite(robopunk);
// update canvas buffer and write to screen
// IMAGE_DATA.data.set(CANVAS_VIEW);
// CTX.putImageData(IMAGE_DATA, 0, 0);
PIX.SURF_Flip();
/* end main loop body */
if(!done)
{requestAnimFrame(mainLoop);}
else
{/* exit loop */ console.log("done");}
};