-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
158 lines (147 loc) · 5.28 KB
/
controls.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
onload = function() {
sketch({
images: {
spritesheet: 'sheet.png',
ground: 'ground.png'
},
resetState: function() {
this.pos = {x: 0, y: 0};
this.direction = {x: 0, y: 0};
this.facing = 'down';
this.animState = 0;
this.spriter = {
tileWidth: 32,
tileHeight: 32,
width: 3,
height: 4,
goUp: [9, 10, 11, 10],
goRight: [6, 7, 8, 7],
goDown: [0, 1, 2, 1],
goLeft: [3, 4, 5, 4],
idleDown: [1],
idleLeft: [3],
idleUp: [10],
idleRight: [6]
};
this.phase = 0;
},
hit: function() {
this.hitPhase = this.phase;
},
keydown: function(event) {
var moveUnit = 0.2;
if(!event.ctrlKey) {
switch(event.keyCode) {
case 'W'.charCodeAt():
case 'Z'.charCodeAt():
case 38:
event.preventDefault();
this.direction.y = -moveUnit;
this.facing = 'up';
break;
case 'A'.charCodeAt():
case 'Q'.charCodeAt():
case 37:
event.preventDefault();
this.direction.x = -moveUnit;
this.facing = 'left';
break;
case 'S'.charCodeAt():
case 40:
event.preventDefault();
this.direction.y = moveUnit;
this.facing = 'down';
break;
case 'D'.charCodeAt():
case 39:
event.preventDefault();
this.direction.x = moveUnit;
this.facing = 'right';
break;
case 'X'.charCodeAt():
this.hit();
break;
}
}
},
keyup: function(event) {
switch(event.keyCode) {
case 'W'.charCodeAt():
case 'Z'.charCodeAt():
case 'S'.charCodeAt():
case 38:
case 40:
this.direction.y = 0;
break;
case 'A'.charCodeAt():
case 'Q'.charCodeAt():
case 'D'.charCodeAt():
case 37:
case 39:
event.preventDefault();
this.direction.x = 0;
break;
}
},
getTileCoords: function() {
var action = 'idle';
if(this.direction.y < 0) {
action = 'goUp';
}
else if(this.direction.y > 0) {
action = 'goDown';
}
else if(this.direction.x > 0) {
action = 'goRight';
}
else if(this.direction.x < 0) {
action = 'goLeft';
}
else {
switch(this.facing) {
case 'left':
action = 'idleLeft';
break;
case 'right':
action = 'idleRight';
break;
case 'up':
action = 'idleUp';
break;
case 'down':
action = 'idleDown';
break;
}
}
var animArray = this.spriter[action];
this.frameNumber = animArray[Math.floor(this.phase) % animArray.length];
return {
x: Math.floor(this.spriter.tileWidth * (this.frameNumber % this.spriter.width)),
y: Math.floor(this.spriter.tileHeight * Math.floor(this.frameNumber / this.spriter.width))
};
},
update: function(dt) {
var dx = this.direction.x;
var dy = this.direction.y;
if(dx === 1 && dy === 1 ||
dx === -1 && dy === -1) {
dx /= Math.sqrt(2);
dy /= Math.sqrt(2);
}
this.pos.x += dx * dt;
this.pos.y += dy * dt;
this.phase += 0.01 * dt;
},
draw: function(ctx, images, dt) {
var m = 3;
var coords = this.getTileCoords();
var pattern = ctx.createPattern(images.ground, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = 'rgba(0, 0, 0, 0.4)';
ctx.fillRect(this.pos.x+16, this.pos.y+32*m-16, 32 * 2, 32);
ctx.drawImage(images.spritesheet, coords.x, coords.y, this.spriter.tileWidth, this.spriter.tileHeight,
this.pos.x, this.pos.y, this.spriter.tileWidth * m, this.spriter.tileHeight * m);
}
});
}