-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (81 loc) · 1.89 KB
/
script.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
let playerState = 'fall';
const dropdown = document.getElementById('animations');
dropdown.addEventListener('change', function(e){
playerState = e.target.value;
})
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
const CANVAS_WIDTH = canvas.width = 600;
const CANVAS_HEIGHT = canvas.height = 600;
const playerImage = new Image();
playerImage.src = 'shadow_dog.png';
const spriteWidth = 575;
const spriteHeight = 523;
let gameFrame = 0;
const staggerFrames = 5;
const spriteAnimations = [];
const animationStates = [
{
name: 'idle',
frames: 7,
},
{
name: 'jump',
frames: 7,
},
{
name: 'fall',
frames: 7,
},
{
name: 'run',
frames: 9,
},
{
name: 'dizzy',
frames: 11,
},
{
name: 'sit',
frames: 5,
},
{
name: 'roll',
frames: 7,
},
{
name: 'bite',
frames: 7,
},
{
name: 'KO',
frames: 12,
},
{
name: 'gethit',
frames: 4,
}
];
animationStates.forEach((state, index) => {
let frames = {
loc: [],
}
for (let j = 0; j < state.frames; j++) {
let positionX = j * spriteWidth;
let positionY = index * spriteHeight;
frames.loc.push({x: positionX, y: positionY});
}
spriteAnimations[state.name] = frames;
});
console.log(spriteAnimations);
function animate(){
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
let position = Math.floor(gameFrame/staggerFrames) % spriteAnimations[playerState].loc.length;
let frameX = spriteWidth * position;
let frameY = spriteAnimations[playerState].loc[position].y;
ctx.drawImage(playerImage, frameX, frameY,spriteWidth, spriteHeight, 0, 0,
spriteWidth, spriteHeight);
gameFrame++;
requestAnimationFrame(animate);
};
animate();