Dark Forest plugin to highlight Invaded, capturable and captured planets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
EMPTY_ADDRESS | |
} from 'https://cdn.skypack.dev/@darkforest_eth/constants'; | |
const colors = { | |
invaded: 'yellow', | |
capturable: 'green', | |
captured: 'gray' | |
} | |
export default class HighlightInvadedAndCapturedPlanets { | |
draw(ctx) { | |
const holdBlocksRequired = ui.gameManager.getContractConstants().CAPTURE_ZONE_HOLD_BLOCKS_REQUIRED | |
const currentBlockNumber = df.ethConnection.blockNumber | |
const viewport = ui.getViewport(); | |
const planets = ui.getPlanetsInViewport() | |
.filter(planet => ( | |
planet.planetLevel >= 2 && | |
planet.invadeStartBlock && | |
planet.invader !== EMPTY_ADDRESS | |
) | |
) | |
const colors = { | |
invaded: 'yellow', | |
capturable: 'green', | |
captured: 'black' | |
} | |
for (const planet of planets) { | |
ctx.fillStyle = ctx.strokeStyle = colors.invaded | |
ctx.globalAlpha = 0.85; | |
if (planet.capturer !== EMPTY_ADDRESS) { | |
ctx.fillStyle = ctx.strokeStyle = colors.captured | |
} else if ( | |
Math.min( | |
planet.invadeStartBlock + holdBlocksRequired - currentBlockNumber, | |
holdBlocksRequired | |
) <= 0 | |
) { | |
ctx.fillStyle = ctx.strokeStyle = colors.capturable | |
} | |
let { x, y } = planet.location.coords; | |
if (planet.planetLevel <= 4) { | |
ctx.lineWidth = 1.5; | |
ctx.strokeStyle = "dashed"; | |
ctx.beginPath(); | |
ctx.arc( | |
viewport.worldToCanvasX(x), | |
viewport.worldToCanvasY(y), | |
viewport.worldToCanvasDist(ui.getRadiusOfPlanetLevel(3) * 3), | |
0, | |
2 * Math.PI | |
); | |
ctx.fill(); | |
ctx.stroke(); | |
ctx.closePath(); | |
} | |
ctx.beginPath(); | |
ctx.arc( | |
viewport.worldToCanvasX(x), | |
viewport.worldToCanvasY(y), | |
viewport.worldToCanvasDist( | |
ui.getRadiusOfPlanetLevel(planet.planetLevel) | |
), | |
0, | |
2 * Math.PI | |
); | |
ctx.fill(); | |
ctx.stroke(); | |
ctx.closePath(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment