forked from ScruffyFurn/HTML5RPG2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
406 lines (365 loc) · 17.4 KB
/
Copy pathplayer.js
File metadata and controls
406 lines (365 loc) · 17.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
Copyright (c) 2014, Mickey "@ScruffyFurn" MacDonald,
Will "@WStieh" Stieh
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var Player = function (game) {
//This is the players unique id for the multiplayer
var id;
var INPUT_TYPES = { NONE: 0, UP: 1, DOWN: 2, LEFT: 3, RIGHT: 4 }
var input = 0;//This input here is for multiplayer, we send the players input along with its position to all other players, that way on their machine
//all of the non-local players animate correctly
var player = new Sprite(game.spriteWidth, game.spriteHeight);//First we set up the player object as a sprite
//A sprite is an aspect of 2D game developement, it represents a piece of a sprite sheet
//It contains properties such as position, scale, frame (current frame of animation to draw), spriteOffset (position on sprite sheet)
//Check out the explanation for sprites and sprite sheets within game.js
//The isLocal argument is used to determine if this is a local player or an online one
var setPlayer = function (isLocal) {
player.spriteOffset = 5;//Sprites offset on the sprite sheet, value of 5 means it is the 6th image on the sprite sheet (remember with computers we start at 0 not 1)
player.startingX = 6;//Starting X for the player
player.startingY = 14;//Starting Y for the player
player.x = player.startingX * game.spriteWidth;//Players x position in the world, we multiply it by the spriteWidth so it is perfectly aligned in the world
player.y = player.startingY * game.spriteHeight;//Players y position in the world, likewise we multiply it by the spriteHeight so it perfectly lines up with the other sprites
player.direction = 0;//Current direction
player.walk = 0;//Current walk, used for animating. We have 2 frames of animation so its either 0 or 1
player.frame = player.spriteOffset + player.direction; //We set the players actual frame to be drawn, looking at the sprite sheet 'sprites.png' you will see that if direction was set to 2, we would get 5+2 = 7 meaning we would see the frame that shows the player walking away from us
player.image = new Surface(game.spriteSheetWidth, game.spriteSheetHeight);//We set the players image as a Surface, think of a surface as a picture that you will be drawing on
player.image.draw(game.assets['sprites.png']);//...drawing on with the 'sprites.png'
//RPG Elemetns
player.name = "Steve";//Give him a name
player.characterClass = "Knight";//Give him a class
player.exp = 0;//Give him experience points
player.level = 1;//Give him a level
player.gp = 100;//Give him some gold
//Set up level stats, when we need your attack or max health we will reference this array and pass in your level which will give us the correct info
player.levelStats = [{}, { attack: 4, maxHp: 10, maxMp: 0, expMax: 10 },//Level 1
{ attack: 6, maxHp: 14, maxMp: 0, expMax: 30 }, //Level 2
{ attack: 7, maxHp: 20, maxMp: 5, expMax: 50 } //Level 3 with MAGIC!!
];
//Our attack value, just returns the attack value of our current level
player.attack = function () {
return player.levelStats[player.level].attack;
};
//We set our hp and mp to our current level's amounts
player.hp = player.levelStats[player.level].maxHp;
player.mp = player.levelStats[player.level].maxMp;
//We setup the on enter frame event here for the player
//We use this to call the players update
//We pass whether he is local or not in as well, which in turn was passed in to us
player.on('enterframe', function () {
update(isLocal);
});
//Finally we set up our statusLabel, but we don't add it to anything...yet
//Later we will use this to show messages and the like
player.statusLabel = new Label("");
player.statusLabel.width = game.width;
player.statusLabel.y = undefined;
player.statusLabel.x = undefined;
player.statusLabel.color = '#fff';
player.statusLabel.backgroundColor = '#000';
};
//Displays the players status...using the statusLabel
var displayStatus = function () {
player.statusLabel.text =
"--" + player.name + " the " + player.characterClass +
"<br />--HP: " + player.hp + "/" + player.levelStats[player.level].maxHp +
"<br />--MP: " + player.mp + "/" + player.levelStats[player.level].maxMp +
"<br />--Exp: " + player.exp +
"<br />--Level: " + player.level +
"<br />--GP: " + player.gp +
"<br /><br />--Inventory:";
player.statusLabel.height = 170;
showInventory(0);
};
//Clears the statusLabel
var clearStatus = function () {
player.statusLabel.text = "";
player.statusLabel.height = 0;
hideInventory();
};
var move = function () {
//Set the players current frame
//Example, moving up the screen, 5 + 1 * 2 + 1 which gives us 8!
//Now looking at our sprite sheet that is the second walking up animation frame
player.frame = player.spriteOffset + player.direction * 2 + player.walk;
if (player.isMoving) {
//If we are moving, lets move the player
player.moveBy(player.xMovement, player.yMovement);
//Then we update its animation
//What we are doing here is using the modulus operator (%) which returns the remainder from division
//For example, 5 % 2 gives you 1 because 2 *2 = 4, and then it doesn't go into the remaining 1, which is returned
//We mod the game.frame (number of frames since we hit start) by 2 because our animations are 2 frames long
//So if we mod game.frame by 2 and it is not 0
//We increase the walk and then mod that by 2 so its always either 0 or 1
if (!(game.frame % 2)) {
player.walk++;
player.walk %= 2;
}
//Now if we are moving along and our character is perfectly aligned with something divisible by 16
//We stop moving, and we set our walk to 1 (second from of animation)
//There to kinda smooth things out
if ((player.xMovement && player.x % 16 === 0) || (player.yMovement && player.y % 16 === 0)) {
player.isMoving = false;
player.walk = 1;
}
} else {
//If we are not moving, we set our x and y movement to 0
player.xMovement = 0;
player.yMovement = 0;
//Check for input
//For local stuff we just care about game.input.whatever
//Once we get/or don't get that input, depending on if there is any
//We also set the input variable, this tells the server what button we pressed
//That way we can properly be updated on each client
if (game.input.up) {
input = INPUT_TYPES.UP;
player.direction = 1;
player.yMovement = -4;
clearStatus();
} else if (game.input.right) {
input = INPUT_TYPES.RIGHT;
player.direction = 2;
player.xMovement = 4;
clearStatus();
} else if (game.input.left) {
input = INPUT_TYPES.LEFT;
player.direction = 3;
player.xMovement = -4;
clearStatus();
} else if (game.input.down) {
input = INPUT_TYPES.DOWN;
player.direction = 0;
player.yMovement = 4;
clearStatus();
}
else {
input = INPUT_TYPES.NONE;
player.isMoving = false;
//Tell the server we are not moving, send in our input and our x/y position
//game.socket.emit("move player", { id: id, input: input, x: player.x, y: player.y });
}
if (player.xMovement || player.yMovement) {
//Below code makes use of a one line if statement
//If the players.xMovement is not 0 then we set it to player.xMovemnt/Mathf.abs(player.xMovement * 16 else we make it 0
//When you abs (absolute) something you get the number of places it is from 0, which in lay mans means the positive version of that number
//Lets say our xMovement is -4 (so we are moving left) then we get -4 / Math.abs(-4) * 16 -> -4/4 which gives us -1 * 16 = -16
//So we move -16 to the left
//This little line of code ensures we are always moving 16 pixels in whatever direction
var x = player.x + (player.xMovement ? player.xMovement / Math.abs(player.xMovement) * 16 : 0);
var y = player.y + (player.yMovement ? player.yMovement / Math.abs(player.yMovement) * 16 : 0);
if (0 <= x && x < game.getMap().width && 0 <= y && y < game.getMap().height && !game.getMap().hitTest(x, y)) {
player.isMoving = true;
//Tell the server we are moving, send in our input and our x/y position
//MULTIPLAYER COMMENTgame.socket.emit("move player", { id: id, input: input, x: player.x, y: player.y });
//Recursively call this function
move();
}
}
}
};
//Returns the square that the player is standing on within the game's map
var square = function () {
return { x: Math.floor(player.x / game.spriteWidth), y: Math.floor(player.y / game.spriteHeight) }
}
//Returns what square we are currently facing
//Returns null if that square is outside of the game world
var getFacingSquare = function () {
var playerSquare = square();
var facingSquare;
if (player.direction === 0) {//Down
facingSquare = { x: playerSquare.x, y: playerSquare.y + 1 }
} else if (player.direction === 1) {//Up
facingSquare = { x: playerSquare.x, y: playerSquare.y - 1 }
} else if (player.direction === 2) {//Right
facingSquare = { x: playerSquare.x + 1, y: playerSquare.y }
} else if (player.direction === 3) {//Left
facingSquare = { x: playerSquare.x - 1, y: playerSquare.y }
}
if ((facingSquare.x < 0 || facingSquare.x >= game.getMap().width / 16) || (facingSquare.y < 0 || facingSquare.y >= game.getMap().height / 16)) {
return null;
} else {
return facingSquare;
}
}
//This function is called by the game to determine if the player is facing an NPC to interact with them
var facing = function () {
var fSquare = getFacingSquare();
if (!fSquare) {
return null;
} else {
return game.foregroundData[fSquare.y][fSquare.x];
}
}
//Inventory
//Items visible (what the player has)
var visibleItems = [];
//Surface for displaying the inventory
var itemSurface = new Surface(game.itemSpriteSheetWidth, game.spriteSheetHeight);
//Inventory array, the numbers equal the ids set up in the game.js file's inventory array
var inventory = [];
//Hides the inventory by removing everything in the visibleItems
var hideInventory = function () {
for (var i = 0; i < visibleItems.length; i++) {
visibleItems[i].remove();
}
visibleItems = [];
};
//Draws the inventory using the sprites from the items.png
var showInventory = function (yOffset) {
if (visibleItems.length === 0) {
itemSurface.draw(game.assets['items.png']);
for (var i = 0; i < inventory.length; i++) {
var item = new Sprite(game.spriteWidth, game.spriteHeight);
item.y = 130 + yOffset;
item.x = 30 + 70 * i;
item.frame = inventory[i];
item.scaleX = 2;
item.scaleY = 2;
item.image = itemSurface;
visibleItems.push(item);
game.currentScene.addChild(item);
}
}
};
//Our mighty update function
var update = function (isLocal) {
//If its a local player, we call the local update
if (isLocal) {
localUpdate();
}//Otherwise we call the server player update
else {
serverPlayerUpdate();
}
};
//The local update
var localUpdate = function () {
move();
if (game.input.a) {
var playerFacing = facing();
if (!playerFacing || !spriteRoles[playerFacing]) {
player.displayStatus();
} else {
spriteRoles[playerFacing].action();
};
};
};
//This function only applies to non-local players in multiplayer
//Its a stripped down version of our move function
//However we remove the emits and instead of using game.input.x, we look at our input variable
//Then we move the player as normal
var serverPlayerUpdate = function () {
//Get the current frame
player.frame = player.spriteOffset + player.direction * 2 + player.walk;
//If input is none we just axe the movement by setting player.isMoving to false
if (input == INPUT_TYPES.NONE)
player.isMoving = false;
if (player.isMoving) {
player.moveBy(player.xMovement, player.yMovement);
if (!(game.frame % 2)) {
player.walk++;
player.walk %= 2;
}
if ((player.xMovement && player.x % 16 === 0) || (player.yMovement && player.y % 16 === 0)) {
player.isMoving = false;
player.walk = 1;
}
}
else {
//Player is not moving
player.xMovement = 0;
player.yMovement = 0;
switch (input) {
case INPUT_TYPES.UP:
player.direction = 1;
player.yMovement = -4;
break;
case INPUT_TYPES.RIGHT:
player.direction = 2;
player.xMovement = 4;
break;
case INPUT_TYPES.LEFT:
player.direction = 3;
player.xMovement = -4;
break;
case INPUT_TYPES.DOWN:
player.direction = 0;
player.yMovement = 4;
break;
case INPUT_TYPES.NONE:
player.isMoving = false;
player.xMovement = 0;
player.yMovement = 0;
break;
}
if (player.xMovement || player.yMovement) {
var x = player.x + (player.xMovement ? player.xMovement / Math.abs(player.xMovement) * 16 : 0);
var y = player.y + (player.yMovement ? player.yMovement / Math.abs(player.yMovement) * 16 : 0);
if (0 <= x && x < game.width && 0 <= y && y < game.height) { //Bounds checking
player.isMoving = true;
//Unlike our normal update we don't need to emit here
}
}
}
}
//Movement getter and setters
var getX = function () {
return player.x;
};
var getY = function () {
return player.y;
};
var setX = function (newX) {
player.x = newX;
};
var setY = function (newY) {
player.y = newY;
};
var getInput = function () {
return input;
}
var setInput = function (i) {
input = i;
}
//Called when the player dies
//Shows the main menu
var playerDeath = function () {
game.showMenu(true);
};
var getStatusLabel = function () {
return player.statusLabel;
}
return {
setPlayer: setPlayer,
object: player,//Returns player as object (Saves confusion)
update: update,
showInventory: showInventory,
hideInventory: hideInventory,
facing: facing,
facingSquare: getFacingSquare,
clearStatus: clearStatus,
displayStatus: displayStatus,
setPlayer: setPlayer,
getStatusLabel : getStatusLabel,
inventory: inventory,
playerDeath: playerDeath,
getX: getX,
getY: getY,
setX: setX,
setY: setY,
id: id,
getInput: getInput,
setInput: setInput
};
}