Skip to content

Commit 75b8c98

Browse files
committed
feat: Add heart pickups
1 parent 1cf79b0 commit 75b8c98

5 files changed

Lines changed: 89 additions & 7 deletions

File tree

src/Goal.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export class Goal {
6161
{
6262
x: this.x,
6363
y: this.y,
64-
scaleX: this.scale,
65-
scaleY: this.scale,
6664
width: this.width,
6765
height: this.height,
6866
},

src/Heart.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import heart from 'data-url:./assets/img/heart.png';
2+
3+
import { emit, Sprite } from 'kontra';
4+
import { HEART_PICKUP } from './gameEvents';
5+
import { isBoxCollision } from './utils';
6+
7+
export class Heart {
8+
scale = 4;
9+
width = 8;
10+
height = 8;
11+
x;
12+
y;
13+
sprite;
14+
15+
constructor(x, y, { level }) {
16+
this.level = level;
17+
this.x = x;
18+
this.y = y;
19+
this.createSprite();
20+
}
21+
update() {
22+
if (!this.sprite) return;
23+
this.sprite.update();
24+
this.checkCollision();
25+
}
26+
27+
render(_ctx) {
28+
if (!this.sprite) return;
29+
this.sprite.render();
30+
}
31+
32+
createSprite() {
33+
const image = new Image();
34+
image.src = heart;
35+
image.onerror = function (err) {
36+
console.log(err);
37+
};
38+
image.onload = () => {
39+
this.sprite = Sprite({
40+
x: this.x,
41+
y: this.y,
42+
anchor: { x: 0.5, y: 0.5 },
43+
width: 8,
44+
height: 8,
45+
image: image,
46+
scaleX: this.scale,
47+
scaleY: this.scale,
48+
});
49+
};
50+
}
51+
52+
checkCollision() {
53+
if (
54+
isBoxCollision(
55+
{
56+
x: this.x - (this.width * this.scale) / 2,
57+
y: this.y - (this.height * this.scale) / 2,
58+
width: this.width * this.scale,
59+
height: this.height * this.scale,
60+
},
61+
this.level.player.sprite
62+
)
63+
) {
64+
this.sprite = null;
65+
emit(HEART_PICKUP, { heart: this });
66+
}
67+
}
68+
}

src/Level.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { emit, on, off } from 'kontra';
2-
3-
import { GOAL_COLLISION, LEVEL_COMPLETE } from './gameEvents';
41
import { Goal } from './Goal';
2+
import { Heart } from './Heart';
53
import { Player } from './Player';
64
import { Saw } from './Saw';
75
import { BACK_FORTH } from './sawBehavior';
@@ -10,6 +8,7 @@ export class Level {
108
player;
119
saws = [];
1210
goals = [];
11+
hearts = [];
1312
isLevelLoaded = false;
1413
levelId = -1;
1514
constructor(levelId, { game }) {
@@ -19,6 +18,7 @@ export class Level {
1918
this.createPlayer(levelData);
2019
this.createSaws(levelData);
2120
this.createGoals(levelData);
21+
this.createHearts(levelData);
2222
this.isLevelLoaded = true;
2323
});
2424
}
@@ -38,10 +38,15 @@ export class Level {
3838

3939
render(ctx) {
4040
if (!this.isLevelLoaded) return;
41-
this.player.render(ctx);
41+
42+
// TODO (johnedvard) Add to same array if pressing for space
4243
this.saws.forEach((saw) => {
4344
saw.render(ctx);
4445
});
46+
this.hearts.forEach((heart) => {
47+
heart.render(ctx);
48+
});
49+
this.player.render(ctx);
4550
this.goals.forEach((goal) => {
4651
goal.render(ctx);
4752
});
@@ -57,6 +62,9 @@ export class Level {
5762
this.goals.forEach((goal) => {
5863
goal.update();
5964
});
65+
this.hearts.forEach((heart) => {
66+
heart.update();
67+
});
6068
}
6169
createGoals(levelData) {
6270
levelData.g.forEach((g) => {
@@ -71,9 +79,15 @@ export class Level {
7179
}
7280
createSaws(levelData) {
7381
levelData.s.forEach((saw) => {
82+
// TODO (johnedvard) Add actual saw behaviour
7483
this.saws.push(new Saw(saw.x, saw.y, { behavior: BACK_FORTH }));
7584
});
7685
}
86+
createHearts(levelData) {
87+
levelData.h.forEach((heart) => {
88+
this.hearts.push(new Heart(heart.x, heart.y, { level: this }));
89+
});
90+
}
7791

7892
// TODO (johnedvard) Move collisions to own file?
7993
checkCollisions() {

src/gameEvents.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const GOAL_COLLISION = 'gc';
22
export const ARCADIAN_ADDED = 'aa';
33
export const LEVEL_COMPLETE = 'lc';
4+
export const HEART_PICKUP = 'hp';

src/level/level1.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"r": 7,
44
"s": [],
55
"b": [{ "x": 400, "y": 400 }],
6-
"g": [{ "x": 368, "y": 700 }]
6+
"g": [{ "x": 368, "y": 700 }],
7+
"h": [{ "x": 550, "y": 200 }]
78
}

0 commit comments

Comments
 (0)