Skip to content

Commit c9d41af

Browse files
authored
Add files via upload
1 parent 7447d1f commit c9d41af

7 files changed

Lines changed: 74 additions & 15 deletions

File tree

src/collisions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/* collision.js */
22

3-
import { distance } from './math.js';
3+
import { pointDistance } from './math.js';
44

55
// circle in circle collision detection
66

77
export const circleInCircle = (x1, y1, r1, x2, y2, r2) => {
88

9-
return distance( x1, y1, x2, y2 ) < r1 + r2;
9+
return pointDistance( x1, y1, x2, y2 ) < r1 + r2;
1010

1111
};
1212

1313
// point in circle collision check
1414

1515
export const pointInCircle = (px, py, cx, cy, cr) => {
1616

17-
return distance( px, py, cx, cy ) <= cr;
17+
return pointDistance( px, py, cx, cy ) <= cr;
1818

1919
};
2020

src/createCanvas.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const createCanvas = (width = 256, height = 256, bg = '#000') => {
2+
3+
const cnv = document.createElement('canvas'),
4+
ctx = cnv.getContext('2d');
5+
6+
cnv.width = width;
7+
8+
cnv.height = height;
9+
10+
cnv.style.background = bg;
11+
12+
return { cnv, ctx };
13+
14+
};

src/device.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/* device.js */
22

3-
export const isTouchscreen = () => 'ontouchstart' in document.documentElement;
3+
export const isTouchscreen = () => 'ontouchstart' in document.documentElement;

src/loader.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,30 @@ export const load_image = src => {
3333
3434
*/
3535

36-
export const load_images = srcs => Promise.all(srcs.map(load_image));
36+
export const load_images = srcs => Promise.all(srcs.map(load_image));
37+
38+
/*
39+
40+
Loads sounds
41+
42+
*/
43+
44+
export const load_sound = src => {
45+
46+
return new Promise((resolve, reject) => {
47+
48+
const sound = new Audio();
49+
50+
sound.canplaythrough = () => resolve(sound);
51+
52+
sound.onerror = reject;
53+
54+
sound.src = src;
55+
56+
});
57+
58+
};
59+
60+
// load multiple sounds
61+
62+
export const load_sounds = srcs => Promise.all(srcs.map(load_sound));

src/math.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* math.js */
22

3-
export const distance = (x1, y1, x2, y2) => {
3+
// measures distance between two points
4+
5+
export const pointDistance = (x1, y1, x2, y2) => {
46

57
const dx = x1 - x2,
68

@@ -9,3 +11,7 @@ export const distance = (x1, y1, x2, y2) => {
911
return Math.sqrt(dx * dx + dy * dy);
1012

1113
};
14+
15+
// converts point to angle
16+
17+
export const pointToAngle = (x, y) => -Math.atan2(-y, x);

src/sound.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const playSound = sound => sound.play();
2+
3+
export const playMusic = sound => !sound.paused && playSound(sound);
4+
5+
export const setVolume = (sound, volume) => sound.volume = volume;
6+
7+
export const stopSound = sound => {
8+
9+
sound.pause();
10+
11+
sound.currentTime = 0;
12+
13+
};

src/sprite.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default (spritesheet, numCol, numRow, delay) => {
66

77
let currentFrame = 0, then;
88

9-
let currentAnimation, prevAnimation;
9+
let currentAnimation = { frameStart: 0 }, prevAnimation;
1010

1111
return {
1212

@@ -24,7 +24,7 @@ export default (spritesheet, numCol, numRow, delay) => {
2424

2525
},
2626

27-
draw(ctx, x, y, w, h, flipped = false) {
27+
draw(ctx, x, y, w = frameWidth, h = frameHeight, flippedX = false, flippedY = false) {
2828

2929
const now = performance.now();
3030

@@ -45,25 +45,25 @@ export default (spritesheet, numCol, numRow, delay) => {
4545
}
4646

4747
let col = currentFrame % numCol, row = Math.floor(currentFrame / numCol);
48-
48+
4949
ctx.save();
5050

51-
if(flipped) { // flips the sprite horizontally
51+
if(flippedX || flippedY) {
5252

5353
ctx.translate(x + w/2, y + w/2);
5454

55-
ctx.scale(-1, 1);
56-
57-
ctx.translate(-(x + w/2), -(y + w/2));
55+
ctx.scale(flippedX ? -1 : 1, flippedY ? -1 : 1);
5856

57+
ctx.translate(-(x + w/2), -(y + w/2));
58+
5959
}
6060

6161
ctx.drawImage(spritesheet, col * frameWidth, row * frameHeight, frameWidth, frameHeight, x, y, w, h);
62-
62+
6363
ctx.restore();
6464

6565
}
6666

6767
}
6868

69-
};
69+
};

0 commit comments

Comments
 (0)