forked from AUGDOGyt/Stories-From-Under-The-Bed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (54 loc) · 1.37 KB
/
main.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
import 'main.css';
import { Resources } from "resources.js";
const canvas = document.querySelector("game-canvas");
const ctx = canvas.getContext("2d");
const skySprite = new Sprite({
resource: Resources.images.sky,
frameSize: new Vector2(320, 180)
});
const groundsprite = new Sprite({
resource: Resources.images.ground,
frameSize: new Vector2(320, 180)
});
const Hero = new Sprite({
resource: Resources.images.hero,
frameSize: new Vector2(32, 32),
hFrames: 3,
vFrames: 8,
frame: 1,
});
const Shadow = new Sprite({
resource: Resources.images.shadow,
frameSize: new Vector2(32, 32)
});
const heroPos = new Vector2(16*5, 16*5);
const input = new Input();
const update = () => {
if (input.direction === DOWN) {
heroPos.y += 1;
hero.frame = 0;
}
if (input.direction === UP) {
heroPos.y -= 1;
hero.frame = 6;
}
if (input.direction === LEFT) {
heroPos.x -= 1;
hero.frame = 9;
}
if (input.direction === RIGHT) {
heroPos.x += 1;
hero.frame = 3;
}
};
const draw = () => {
skySprite.drawImage(ctx, 0, 0);
groundSprite.drawImage(ctx, 0, 0);
const herOffset = new Vector2(-8, -21);
const heroPosX = heroPos.x + heroOffset.x;
const heroPosY = heroPos.y + heroOffset.y;
shadow.dramImage(ctx, heroPosX, heroPosY);
hero.drawImage(ctx, heroPosX, heroPosY);
};
const gameLoop = new gameLoop(update, draw);
gameLoop.start();