Now that we have all the required tools, coding the endgame will be a piece of cake.
Make the following changes:
Blob.onCollision(blobA: Blob, blobB: Blob) {
// ...
if (blobA.category === "player" && blobB.category === "player") {
onCollisionPlayerVsPlayer(blobA, blobB);
}
}
const onCollisionPlayerVsPlayer = (winnerBlob: Blob, loserBlob: Blob) => {
if (winnerBlob.size < loserBlob.size) {
let tempBlob = winnerBlob
winnerBlob = loserBlob
loserBlob = tempBlob
}
let winner = blobPlayers[winnerBlob.id]
winnerBlob.size = Math.sqrt(Math.pow(winnerBlob.size/2, 2) + Math.pow(loserBlob.size/2, 2)) * 2
winner.score = winner.score + 100
Message.print("super yummy!", { player: winner })
let loser = blobPlayers[loserBlob.id]
loser.kill()
Message.print("you got killed!", { player: loser })
}
Player.onDeath = (player: Player) => {
let blob = playerBlobs[player.id]
blob.destroy()
delete playerBlobs[player.id]
delete blobPlayers[blob.id]
boostingPlayers.delete(player.id)
}
Here’s what’s going on here:
-
We determine winner as a blob with greater size (switching the blobs if needed) and we make his blob absorb the loser blob’s area. Then we increase his score and send a cheerful message his way.
-
We take care of the loser, killing the player and sending the bad news via a message.
-
We handle player’s death in separate
@heyplay/iokit:Player.onDeath
callback that also gets called e.g. when the player disconnects. There, we destroy his blob and cleanup his state.
That’s it! Players may now hit each other in which case the bigger one wins and gets it all.