Skip to content

Commit 7560c8c

Browse files
committed
refactored demo code
1 parent 9eb690c commit 7560c8c

1 file changed

Lines changed: 62 additions & 52 deletions

File tree

README.md

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,46 @@ Check out a live demo on [Replit](https://replit.com/@harshsinghdev/bottlecap-ex
5454

5555
**game.js:**
5656
```javascript
57-
import * as Bottlecap from 'https://unpkg.com/bottlecap@latest';
57+
import * as Bottlecap from 'bottlecap';
58+
59+
class Player {
60+
constructor(ctx, assets) {
61+
this.speed = 100;
62+
this.sprite = new Bottlecap.AnimatedSprite(ctx, assets.image.playerSprite, 6, 1, 0, 0, 64, 64);
63+
this.sprite.addAnimation("default", 0, 5, 80);
64+
this.sprite.play("default");
65+
}
66+
67+
update(dt, direction) {
68+
this.sprite.position.x += direction.x * this.speed * dt;
69+
this.sprite.position.y += direction.y * this.speed * dt;
70+
71+
this.sprite.flipX = direction.x === -1;
72+
this.sprite.update(dt);
73+
}
74+
}
75+
76+
class Coin {
77+
constructor(ctx, assets, x, y) {
78+
this.visible = true;
79+
this.sprite = new Bottlecap.AnimatedSprite(ctx, assets.image.coin, 18, 1, x, y, 16, 16);
80+
this.sprite.addAnimation("spin", 0, 8, 30);
81+
this.sprite.play("spin");
82+
}
83+
84+
update(dt) {
85+
this.sprite.update(dt);
86+
}
87+
}
5888

5989
class MyGame extends Bottlecap.Game {
6090
constructor() {
6191
super();
6292
this.score = 0;
93+
this.loaded = false;
94+
this.coins = [];
95+
this.player = null;
96+
this.assets = null;
6397
}
6498

6599
init() {
@@ -83,7 +117,7 @@ class MyGame extends Bottlecap.Game {
83117
loaderSetup() {
84118
this.loader = new Bottlecap.Loader();
85119
this.loader.on('load', this.onLoadingComplete.bind(this));
86-
this.loader.on('error', console.error);
120+
this.loader.on('error', this.onLoadingError.bind(this));
87121

88122
this.loader
89123
.addImage('coin', './coin.png')
@@ -96,62 +130,44 @@ class MyGame extends Bottlecap.Game {
96130
this.assets = assets;
97131
this.createPlayer();
98132
this.createCoins();
133+
this.loaded = true;
99134
}
100135

101-
createPlayer() {
102-
const playerSprite = new Bottlecap.AnimatedSprite(this.ctx, this.assets.image.playerSprite, 6, 1, 0, 0, 64, 64);
103-
playerSprite.addAnimation("default", 0, 5, 80);
104-
playerSprite.play("default");
136+
onLoadingError(error) {
137+
console.error('Loading error:', error);
138+
this.loaded = false;
139+
}
105140

106-
this.player = {
107-
speed: 100,
108-
sprite: playerSprite
109-
};
141+
createPlayer() {
142+
this.player = new Player(this.ctx, this.assets);
110143
}
111144

112145
createCoins() {
113-
this.coins = [];
114-
115146
for (let i = 0; i < 20; i++) {
116147
const x = Bottlecap.Utils.randomInt(100, this.canvas.width - 100);
117148
const y = Bottlecap.Utils.randomInt(100, this.canvas.height - 100);
118-
const sprite = new Bottlecap.AnimatedSprite(this.ctx, this.assets.image.coin, 18, 1, x, y, 16, 16);
119-
sprite.addAnimation("spin", 0, 8, 30);
120-
sprite.play("spin");
121-
122-
this.coins.push({
123-
sprite,
124-
visible: true
125-
});
149+
const coin = new Coin(this.ctx, this.assets, x, y);
150+
this.coins.push(coin);
126151
}
127152
}
128153

129154
update(dt) {
130-
if (this.loader.loading) return;
155+
if (!this.loaded) return;
131156

132-
this.updatePlayer(dt);
133-
this.updateCoins(dt);
134-
this.updateCamera(dt);
135-
}
136-
137-
updatePlayer(dt) {
138157
const direction = Bottlecap.Keyboard.getDirection();
139-
this.player.sprite.position.x += direction.x * this.player.speed * dt;
140-
this.player.sprite.position.y += direction.y * this.player.speed * dt;
158+
this.player.update(dt, direction);
141159

142-
this.player.sprite.flipX = direction.x === -1;
143-
this.player.sprite.update(dt);
144-
}
145-
146-
updateCoins(dt) {
147160
this.coins.forEach(coin => {
148161
if (coin.visible && this.checkCoinCollision(coin)) {
149162
coin.visible = false;
150163
this.score += 10;
151164
Bottlecap.Sound.play(null, this.assets.sound.coinpickup);
152165
}
153-
coin.sprite.update(dt);
166+
coin.update(dt);
154167
});
168+
169+
this.camera.lookAt(this.player.sprite.position.x, this.player.sprite.position.y);
170+
this.camera.update(dt);
155171
}
156172

157173
checkCoinCollision(coin) {
@@ -161,18 +177,13 @@ class MyGame extends Bottlecap.Game {
161177
);
162178
}
163179

164-
updateCamera(dt) {
165-
this.camera.lookAt(this.player.sprite.position.x, this.player.sprite.position.y);
166-
this.camera.update(dt);
167-
}
168-
169180
render() {
170-
this.clearCanvas();
171-
if (this.loader.loading) {
172-
this.renderLoadingScreen();
181+
if (!this.loaded) {
182+
this.renderLoadingError();
173183
return;
174184
}
175185

186+
this.clearCanvas();
176187
this.camera.attach();
177188
this.renderCoins();
178189
this.player.sprite.render();
@@ -197,10 +208,11 @@ class MyGame extends Bottlecap.Game {
197208
this.ctx.fillText(`Score: ${this.score}`, 32, 32 + 20);
198209
}
199210

200-
renderLoadingScreen() {
211+
renderLoadingError() {
212+
this.clearCanvas();
201213
this.ctx.font = "32px sans-serif";
202-
this.ctx.fillStyle = '#000';
203-
const text = "Loading...";
214+
this.ctx.fillStyle = '#ff0000';
215+
const text = "Error loading assets!";
204216
const textWidth = this.ctx.measureText(text).width;
205217
this.ctx.fillText(
206218
text,
@@ -210,12 +222,10 @@ class MyGame extends Bottlecap.Game {
210222
}
211223
}
212224

213-
const initGame = () => {
214-
const game = new MyGame();
215-
game.run();
216-
};
217-
218-
Bottlecap.DOM.ready(initGame);
225+
Bottlecap.DOM.ready(() => {
226+
const game = new MyGame();
227+
game.run();
228+
});
219229
```
220230

221231
**index.html:**

0 commit comments

Comments
 (0)