From 74178731dbc62fa340ae7bf503ccf205047ff943 Mon Sep 17 00:00:00 2001 From: David Neilsen Date: Fri, 4 Sep 2020 17:11:29 +1200 Subject: [PATCH] Add hazards --- src/lib/weight.ts | 8 ++++++++ src/types/BTData.ts | 2 +- src/web/grid.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/weight.ts b/src/lib/weight.ts index 8d0674a..bbabd7b 100644 --- a/src/lib/weight.ts +++ b/src/lib/weight.ts @@ -108,6 +108,14 @@ export function weight(data: BTData, x: number, y: number, blockHeads = true) { } function computeWeight(data: BTData, x: number, y: number, blockHeads = true) { + if (data.board.hazards) { + for (const hazard of data.board.hazards) { + if (hazard.x == x && hazard.y == y) { + return 5; + } + } + } + let result = 100; for (const snake of data.board.snakes) { const body = snake.body; diff --git a/src/types/BTData.ts b/src/types/BTData.ts index a714674..5d7a911 100644 --- a/src/types/BTData.ts +++ b/src/types/BTData.ts @@ -15,7 +15,7 @@ export interface BTBoard { width: number, height: number, food: BTXY[], - hazards: BTXY[], + hazards?: BTXY[], snakes: BTSnake[], } diff --git a/src/web/grid.ts b/src/web/grid.ts index 9f4686d..4333db3 100644 --- a/src/web/grid.ts +++ b/src/web/grid.ts @@ -57,6 +57,14 @@ export function loadGrid(moveJson) { const col = getCol(food.x, food.y); $('
').addClass('food').appendTo(col); } + if (moveJson.body.board.hazards) { + for (const hazard of moveJson.body.board.hazards) { + const col = getCol(hazard.x, hazard.y); + col.css({ + borderColor: '#ff0000', + }); + } + } for (const snake of moveJson.body.board.snakes) { let color = '#e74c3c'; if (snake.id == moveJson.body.you.id) {