Spaces:
Sleeping
Sleeping
Create static/tribalStage.js
Browse files- static/tribalStage.js +77 -0
static/tribalStage.js
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class TribalStage extends GameStage {
|
2 |
+
constructor(canvas) {
|
3 |
+
super(canvas);
|
4 |
+
this.creatures = [];
|
5 |
+
this.huts = [];
|
6 |
+
this.grassImg = this.loadAsset("/static/assets/grass.png");
|
7 |
+
this.creatureImg = this.loadAsset("/static/assets/creature.png");
|
8 |
+
this.hutImg = this.loadAsset("/static/assets/hut.png");
|
9 |
+
}
|
10 |
+
|
11 |
+
init() {
|
12 |
+
this.spawnCreature(400, 300); // Start with one creature
|
13 |
+
this.render();
|
14 |
+
}
|
15 |
+
|
16 |
+
spawnCreature(x, y) {
|
17 |
+
this.creatures.push({ x, y, speed: 2 });
|
18 |
+
this.population = this.creatures.length;
|
19 |
+
this.updateUI();
|
20 |
+
}
|
21 |
+
|
22 |
+
gatherFood() {
|
23 |
+
if (this.creatures.length > 0) {
|
24 |
+
this.resources.food += 5 * this.creatures.length;
|
25 |
+
this.updateUI();
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
buildHut() {
|
30 |
+
if (this.resources.food >= 20 && this.huts.length < 5) {
|
31 |
+
this.resources.food -= 20;
|
32 |
+
this.huts.push({ x: 350 + this.huts.length * 50, y: 250 });
|
33 |
+
this.maxPopulation += 2;
|
34 |
+
this.spawnCreature(400, 300); // New creature per hut
|
35 |
+
}
|
36 |
+
this.updateUI();
|
37 |
+
}
|
38 |
+
|
39 |
+
update() {
|
40 |
+
this.creatures.forEach(creature => {
|
41 |
+
creature.x += (Math.random() - 0.5) * creature.speed;
|
42 |
+
creature.y += (Math.random() - 0.5) * creature.speed;
|
43 |
+
creature.x = Math.max(0, Math.min(800, creature.x));
|
44 |
+
creature.y = Math.max(0, Math.min(600, creature.y));
|
45 |
+
});
|
46 |
+
}
|
47 |
+
|
48 |
+
render() {
|
49 |
+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
50 |
+
|
51 |
+
// Draw grass background
|
52 |
+
for (let x = 0; x < this.canvas.width; x += 32) {
|
53 |
+
for (let y = 0; y < this.canvas.height; y += 32) {
|
54 |
+
this.ctx.drawImage(this.grassImg, x, y, 32, 32);
|
55 |
+
}
|
56 |
+
}
|
57 |
+
|
58 |
+
// Draw huts
|
59 |
+
this.huts.forEach(hut => {
|
60 |
+
this.ctx.drawImage(this.hutImg, hut.x, hut.y, 48, 48);
|
61 |
+
});
|
62 |
+
|
63 |
+
// Draw creatures
|
64 |
+
this.creatures.forEach(creature => {
|
65 |
+
this.ctx.drawImage(this.creatureImg, creature.x, creature.y, 32, 32);
|
66 |
+
});
|
67 |
+
|
68 |
+
this.update();
|
69 |
+
requestAnimationFrame(() => this.render());
|
70 |
+
}
|
71 |
+
}
|
72 |
+
|
73 |
+
let game; // Global reference for UI functions
|
74 |
+
document.addEventListener("DOMContentLoaded", () => {
|
75 |
+
game = new TribalStage(document.getElementById("gameCanvas"));
|
76 |
+
game.init();
|
77 |
+
});
|