Spaces:
Sleeping
Sleeping
Update static/tribalStage.js
Browse files- static/tribalStage.js +64 -15
static/tribalStage.js
CHANGED
@@ -3,27 +3,32 @@ class TribalStage extends GameStage {
|
|
3 |
super(canvas);
|
4 |
this.creatures = [];
|
5 |
this.huts = [];
|
6 |
-
this.
|
7 |
-
this.
|
8 |
-
this.
|
|
|
|
|
9 |
}
|
10 |
|
11 |
init() {
|
12 |
-
this.spawnCreature(400, 300); //
|
|
|
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 |
-
|
24 |
-
|
25 |
-
this.updateUI();
|
26 |
-
}
|
27 |
}
|
28 |
|
29 |
buildHut() {
|
@@ -31,11 +36,43 @@ class TribalStage extends GameStage {
|
|
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);
|
35 |
}
|
36 |
this.updateUI();
|
37 |
}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
update() {
|
40 |
this.creatures.forEach(creature => {
|
41 |
creature.x += (Math.random() - 0.5) * creature.speed;
|
@@ -51,18 +88,30 @@ class TribalStage extends GameStage {
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
this.creatures.forEach(creature => {
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
});
|
67 |
|
68 |
this.update();
|
@@ -70,7 +119,7 @@ class TribalStage extends GameStage {
|
|
70 |
}
|
71 |
}
|
72 |
|
73 |
-
let game;
|
74 |
document.addEventListener("DOMContentLoaded", () => {
|
75 |
game = new TribalStage(document.getElementById("gameCanvas"));
|
76 |
game.init();
|
|
|
3 |
super(canvas);
|
4 |
this.creatures = [];
|
5 |
this.huts = [];
|
6 |
+
this.rivalTribes = [];
|
7 |
+
this.grassImg = this.loadAsset("https://kenney.nl/assets/topdown-shooter/tiles/grass.png");
|
8 |
+
this.creatureImg = this.loadAsset("https://kenney.nl/assets/rpg-character/character.png");
|
9 |
+
this.hutImg = this.loadAsset("https://opengameart.org/sites/default/files/hut.png");
|
10 |
+
this.rivalImg = this.loadAsset("https://kenney.nl/assets/rpg-enemies/enemy.png");
|
11 |
}
|
12 |
|
13 |
init() {
|
14 |
+
this.spawnCreature(400, 300); // Player's creature
|
15 |
+
this.spawnRivalTribe(600, 100); // Rival tribe
|
16 |
this.render();
|
17 |
}
|
18 |
|
19 |
spawnCreature(x, y) {
|
20 |
+
this.creatures.push({ x, y, speed: 2, color: "#ff0000", size: 32 });
|
21 |
this.population = this.creatures.length;
|
22 |
this.updateUI();
|
23 |
}
|
24 |
|
25 |
+
spawnRivalTribe(x, y) {
|
26 |
+
this.rivalTribes.push({ x, y, members: 3, hostile: true });
|
27 |
+
}
|
28 |
+
|
29 |
gatherFood() {
|
30 |
+
this.resources.food += 5 * this.creatures.length;
|
31 |
+
this.updateUI();
|
|
|
|
|
32 |
}
|
33 |
|
34 |
buildHut() {
|
|
|
36 |
this.resources.food -= 20;
|
37 |
this.huts.push({ x: 350 + this.huts.length * 50, y: 250 });
|
38 |
this.maxPopulation += 2;
|
39 |
+
this.spawnCreature(400, 300);
|
40 |
}
|
41 |
this.updateUI();
|
42 |
}
|
43 |
|
44 |
+
attackTribe() {
|
45 |
+
if (this.rivalTribes.length > 0 && this.creatures.length > 1) {
|
46 |
+
this.rivalTribes[0].members -= 1;
|
47 |
+
if (this.rivalTribes[0].members <= 0) {
|
48 |
+
this.rivalTribes.shift();
|
49 |
+
this.relations = "Victorious";
|
50 |
+
} else {
|
51 |
+
this.relations = "Hostile";
|
52 |
+
}
|
53 |
+
this.updateUI();
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
socializeTribe() {
|
58 |
+
if (this.rivalTribes.length > 0 && this.resources.food >= 10) {
|
59 |
+
this.resources.food -= 10;
|
60 |
+
this.rivalTribes[0].hostile = false;
|
61 |
+
this.relations = "Friendly";
|
62 |
+
this.updateUI();
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
applyCreatureChanges() {
|
67 |
+
const color = document.getElementById("creatureColor").value;
|
68 |
+
const size = parseInt(document.getElementById("creatureSize").value);
|
69 |
+
this.creatures.forEach(c => {
|
70 |
+
c.color = color;
|
71 |
+
c.size = size;
|
72 |
+
});
|
73 |
+
toggleEditor(); // Hide editor after applying
|
74 |
+
}
|
75 |
+
|
76 |
update() {
|
77 |
this.creatures.forEach(creature => {
|
78 |
creature.x += (Math.random() - 0.5) * creature.speed;
|
|
|
88 |
// Draw grass background
|
89 |
for (let x = 0; x < this.canvas.width; x += 32) {
|
90 |
for (let y = 0; y < this.canvas.height; y += 32) {
|
91 |
+
if (this.grassImg.complete) this.ctx.drawImage(this.grassImg, x, y, 32, 32);
|
92 |
}
|
93 |
}
|
94 |
|
95 |
// Draw huts
|
96 |
this.huts.forEach(hut => {
|
97 |
+
if (this.hutImg.complete) this.ctx.drawImage(this.hutImg, hut.x, hut.y, 48, 48);
|
98 |
});
|
99 |
|
100 |
+
// Draw rival tribes
|
101 |
+
this.rivalTribes.forEach(tribe => {
|
102 |
+
if (this.rivalImg.complete) this.ctx.drawImage(this.rivalImg, tribe.x, tribe.y, 32, 32);
|
103 |
+
this.ctx.fillStyle = tribe.hostile ? "red" : "green";
|
104 |
+
this.ctx.fillText(`Members: ${tribe.members}`, tribe.x, tribe.y - 10);
|
105 |
+
});
|
106 |
+
|
107 |
+
// Draw creatures with custom color/size
|
108 |
this.creatures.forEach(creature => {
|
109 |
+
if (this.creatureImg.complete) {
|
110 |
+
this.ctx.drawImage(this.creatureImg, creature.x, creature.y, creature.size, creature.size);
|
111 |
+
} else {
|
112 |
+
this.ctx.fillStyle = creature.color;
|
113 |
+
this.ctx.fillRect(creature.x, creature.y, creature.size, creature.size);
|
114 |
+
}
|
115 |
});
|
116 |
|
117 |
this.update();
|
|
|
119 |
}
|
120 |
}
|
121 |
|
122 |
+
let game;
|
123 |
document.addEventListener("DOMContentLoaded", () => {
|
124 |
game = new TribalStage(document.getElementById("gameCanvas"));
|
125 |
game.init();
|