Spaces:
Running
Running
Update static/tribalStage.js
Browse files- static/tribalStage.js +34 -22
static/tribalStage.js
CHANGED
@@ -1,38 +1,50 @@
|
|
1 |
class TribalStage extends GameStage {
|
2 |
constructor(canvas) {
|
3 |
-
super();
|
4 |
this.canvas = canvas;
|
5 |
-
this.ctx = canvas.getContext('2d');
|
6 |
this.creatures = [];
|
7 |
this.huts = [];
|
8 |
this.rivalTribes = [];
|
9 |
-
this.
|
|
|
10 |
this.loadPatterns();
|
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 |
init() {
|
37 |
this.spawnCreature(400, 300); // Player's creature
|
38 |
this.spawnRivalTribe(600, 100); // Rival tribe
|
|
|
1 |
class TribalStage extends GameStage {
|
2 |
constructor(canvas) {
|
3 |
+
super(canvas);
|
4 |
this.canvas = canvas;
|
5 |
+
this.ctx = canvas.getContext('2d');
|
6 |
this.creatures = [];
|
7 |
this.huts = [];
|
8 |
this.rivalTribes = [];
|
9 |
+
this.images = {}; // Store loaded images
|
10 |
+
this.patterns = {}; // Store created patterns
|
11 |
this.loadPatterns();
|
12 |
}
|
13 |
|
14 |
+
// Function to load an image and return a promise
|
15 |
+
loadImage(url) {
|
16 |
+
return new Promise((resolve, reject) => {
|
17 |
+
const img = new Image();
|
18 |
+
img.crossOrigin = "anonymous";
|
19 |
+
img.onload = () => resolve(img);
|
20 |
+
img.onerror = () => reject(new Error(`Failed to load image: ${url}`));
|
21 |
+
img.src = url;
|
22 |
+
});
|
23 |
}
|
24 |
|
25 |
+
async loadPatterns() {
|
26 |
+
try {
|
27 |
+
// Load all images
|
28 |
+
this.images.background = await this.loadImage("https://huggingface.co/spaces/broadfield-dev/spore_grok/resolve/main/static/images/beige-paper.png");
|
29 |
+
this.images.creature = await this.loadImage("https://huggingface.co/spaces/broadfield-dev/spore_grok/resolve/main/static/images/dark-wood.png");
|
30 |
+
this.images.hut = await this.loadImage("https://huggingface.co/spaces/broadfield-dev/spore_grok/resolve/main/static/images/wood-pattern.png");
|
31 |
+
this.images.rival = await this.loadImage("https://huggingface.co/spaces/broadfield-dev/spore_grok/resolve/main/static/images/dark-mosaic.png");
|
32 |
+
this.images.food = await this.loadImage("https://huggingface.co/spaces/broadfield-dev/spore_grok/resolve/main/static/images/beige-paper.png");
|
33 |
+
|
34 |
+
// Create patterns once images are loaded
|
35 |
+
this.patterns.background = this.ctx.createPattern(this.images.background, "repeat");
|
36 |
+
this.patterns.creature = this.ctx.createPattern(this.images.creature, "repeat");
|
37 |
+
this.patterns.hut = this.ctx.createPattern(this.images.hut, "repeat");
|
38 |
+
this.patterns.rival = this.ctx.createPattern(this.images.rival, "repeat");
|
39 |
+
this.patterns.food = this.ctx.createPattern(this.images.food, "repeat");
|
40 |
+
|
41 |
+
console.log("All patterns loaded successfully.");
|
42 |
+
} catch (error) {
|
43 |
+
console.error("Error loading patterns:", error);
|
44 |
+
}
|
45 |
}
|
46 |
|
47 |
+
|
48 |
init() {
|
49 |
this.spawnCreature(400, 300); // Player's creature
|
50 |
this.spawnRivalTribe(600, 100); // Rival tribe
|