forked from kkirilov91/hitman-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.js
More file actions
92 lines (74 loc) · 2.31 KB
/
Copy pathPlayer.js
File metadata and controls
92 lines (74 loc) · 2.31 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
function Player(x, y, color, inStepSize, inPlayerSize){
//default values
x = typeof x !== "undefined" ? x : 0;
y = typeof y !== "undefined" ? y : 0;
color = typeof color !== "undefined" ? color : "#00FF00";
inPlayerSize = typeof inPlayerSize !== "undefined" ? inPlayerSize : 20;
inStepSize = typeof inStepSize !== "undefined" ? inStepSize : 20;
var cx = x;
var cy = y;
var playerColor = color;
var playerSize = inPlayerSize;
var stepMove = inStepSize;
var playerLastDirection = 37;
function initParameters() {
cx = x;
cy = y;
playerColor = color;
playerSize = inPlayerSize;
stepMove = inStepSize;
playerLastDirection = 37;
}
function initKeys(){
$(document).bind("keydown", function(e){
switch(e.keyCode) {
//left
case 37:
if (cx-stepMove < 0 || !MoveValidator.isValidLeftMove(cx,cy)) break;
playerLastDirection = e.keyCode;
ctx.fillStyle = playerColor;
moveLeft(cx, cy);
cx -= stepMove;
break;
//up
case 38:
if (cy-stepMove < 0 || !MoveValidator.isValidUpMove(cx,cy)) break;
playerLastDirection = e.keyCode;
ctx.fillStyle = playerColor;
moveUp(cx, cy);
cy -= stepMove;
break;
//right
case 39:
if (cx+stepMove>960 || !MoveValidator.isValidRightMove(cx,cy)) break;
playerLastDirection = e.keyCode;
ctx.fillStyle = playerColor;
moveRight(cx, cy);
cx += stepMove;
break;
//down
case 40:
if (cy+stepMove>450 || !MoveValidator.isValidDownMove(cx,cy)) break;
playerLastDirection = e.keyCode;
ctx.fillStyle = playerColor;
moveDown(cx, cy);
cy += stepMove;
break;
case 32:
if(playerLastDirection === 37) Shooting.shootLeft(cx - soldierSize, cy);
if(playerLastDirection === 38) Shooting.shootUp(cx, cy - soldierSize);
if(playerLastDirection === 39) Shooting.shootRight(cx + 2 * soldierSize, cy);
if(playerLastDirection === 40) Shooting.shootDown(cx, cy + 2 * soldierSize);
break;
}
});
}
this.start = function() {
initParameters();
initKeys();
}
this.restart = function() {
initParameters();
// initKeys();
}
}