Skip to content

Commit c6d7b1b

Browse files
committed
feat: add type sprite on kontra engine
1 parent c90c409 commit c6d7b1b

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/scripts/kontra.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { init, GameLoop } from 'kontra';
1+
import { init, GameLoop, Sprite } from 'kontra';
22
import Engine from './engine.js';
33

44
class KontraEngine extends Engine {
@@ -22,6 +22,8 @@ class KontraEngine extends Engine {
2222
this.ctx.strokeStyle = 'black';
2323
this.ctx.fillStyle = 'white';
2424
}
25+
const image = new Image();
26+
image.src = 'sprite.png';
2527

2628
// Particle creation
2729
const particles = new Array(this.count);
@@ -34,7 +36,16 @@ class KontraEngine extends Engine {
3436
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
3537
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
3638
];
37-
particles[i] = { x, y, size: size, dx, dy };
39+
let sprite;
40+
if (this.type === 'sprite') {
41+
sprite = Sprite({
42+
x: x,
43+
y: y,
44+
anchor: { x: 0.5, y: 0.5 },
45+
image: image,
46+
});
47+
}
48+
particles[i] = { x, y, size: size, dx, dy, el: sprite };
3849
}
3950
this.particles = particles;
4051
}
@@ -51,16 +62,24 @@ class KontraEngine extends Engine {
5162
else if (r.y + r.size < 0) r.dy *= -1;
5263
if (r.x > this.width) r.dx *= -1;
5364
else if (r.y > this.height) r.dy *= -1;
65+
if (r.el) {
66+
r.el.x = r.x;
67+
r.el.y = r.y;
68+
}
5469
}
5570
this.fpsmeter.tick();
5671
},
5772
render: () => {
5873
for (let i = 0; i < this.count; i++) {
5974
const r = this.particles[i];
60-
this.ctx.beginPath();
61-
this.ctx.arc(r.x, r.y, r.size, 0, 2 * Math.PI);
62-
if (this.type === 'fill') this.ctx.fill();
63-
this.ctx.stroke();
75+
if (this.type === 'sprite') {
76+
r.el.render();
77+
} else {
78+
this.ctx.beginPath();
79+
this.ctx.arc(r.x, r.y, r.size, 0, 2 * Math.PI);
80+
if (this.type === 'fill') this.ctx.fill();
81+
this.ctx.stroke();
82+
}
6483
}
6584
},
6685
});

0 commit comments

Comments
 (0)