Skip to content

Commit 279a42c

Browse files
committed
Added plant spreading
1 parent e05b0f1 commit 279a42c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

game/classes/objects.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ class Plant extends Placeable{
442442
}
443443
if(this.growthTimer > this.growthRate){
444444
if(this.hp >= this.mhp){
445+
this.growthTimer = 0;
445446
if(this.stage < (objImgs[this.imgNum].length-1)){
446447
this.stage ++;
447-
this.growthTimer = 0;
448448

449449
let chunkPos = testMap.globalToChunk(this.pos.x,this.pos.y);
450450
socket.emit("update_obj", {
@@ -456,6 +456,24 @@ class Plant extends Placeable{
456456
update_value: this.stage
457457
});
458458
}
459+
else{
460+
//try to spread
461+
if(random() > 0.5){
462+
let spreadPos = createVector(this.pos.x + random(-100, 100), this.pos.y + random(-100, 100));
463+
let chunkPos = testMap.globalToChunk(spreadPos.x,spreadPos.y);
464+
if(testMap.chunks[chunkPos.x+","+chunkPos.y] != undefined){
465+
if(testMap.chunks[chunkPos.x+","+chunkPos.y].data[testMap.globalToChunk(spreadPos.x,spreadPos.y).x + (testMap.globalToChunk(spreadPos.x,spreadPos.y).y / CHUNKSIZE)] == 0){
466+
let newPlant = createObject(this.objName, spreadPos.x, spreadPos.y, 0, this.color, this.id, this.ownerName);
467+
testMap.chunks[chunkPos.x+","+chunkPos.y].objects.push(newPlant);
468+
socket.emit("new_object", {
469+
cx: chunkPos.x,
470+
cy: chunkPos.y,
471+
obj: newPlant
472+
});
473+
}
474+
}
475+
}
476+
}
459477
}
460478
}
461479
this.growthTimer += 1/frameRate(); //growth timer goes up by 1 every second

0 commit comments

Comments
 (0)