From 52770d7eb262863f15ccb3d243ab926a2c365527 Mon Sep 17 00:00:00 2001 From: asraelite Date: Mon, 5 Mar 2018 17:12:00 +0000 Subject: [PATCH] Add ship validity checking --- js/data.mjs | 8 ++++++- js/game/control.mjs | 2 +- js/game/edit.mjs | 57 ++++++++++++++++++++++++++++++++++++++++---- js/game/events.mjs | 13 ++++++---- js/gui/inventory.mjs | 8 ++----- 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/js/data.mjs b/js/data.mjs index 734e948..1c36467 100644 --- a/js/data.mjs +++ b/js/data.mjs @@ -1,3 +1,5 @@ +// Connectivity = [top, right, bottom, left] (same TRouBLe as CSS) + export const modules = { capsule: { small: { @@ -7,6 +9,8 @@ export const modules = { type: 'capsule', id: 'small', mass: 2, + connectivity: [false, false, true, false], + capacity: 3, rotation: 0.1 } }, @@ -17,7 +21,8 @@ export const modules = { type: 'fuel', id: 'small', mass: 1, - capacity: 3 + connectivity: [true, false, true, false], + fuelCapacity: 5 } }, thruster: { @@ -28,6 +33,7 @@ export const modules = { type: 'thruster', id: 'light', mass: 2, + connectivity: [true, false, false, false], thrust: 10, isp: 200 } diff --git a/js/game/control.mjs b/js/game/control.mjs index 7aa216e..b787ee3 100644 --- a/js/game/control.mjs +++ b/js/game/control.mjs @@ -51,7 +51,7 @@ function tickPlaying() { } function tickEditing() { - if (held[mapping.exitEdit]) { + if (pressed[mapping.exitEdit]) { events.endEditing(); } } diff --git a/js/game/edit.mjs b/js/game/edit.mjs index 08b2a36..848bc03 100644 --- a/js/game/edit.mjs +++ b/js/game/edit.mjs @@ -33,7 +33,47 @@ export function init() { } export function end() { + let result = validate(); + return { + valid: result === false, + reason: result + } +} + +function validate() { + let capsulesFound = 0; + let thrustersFound = 0; + let unvisited = new Set(); + + tiles.forEach(t => { + if (t.type !== null) unvisited.add(t) + }); + + if (unvisited.size == 0) { + return 'no capsule'; + } + + let visit = (tile) => { + unvisited.delete(tile); + if (tile.type == 'capsule') capsulesFound++; + if (tile.type == 'thruster') thrustersFound++; + tile.neighbours.forEach(n => { + if (unvisited.has(n)) visit(n); + }); + }; + + visit(unvisited.values().next().value); + + if (unvisited.size > 0) { + return 'not connected' + } else if (capsulesFound === 0) { + return 'no capsule' + } else if (thrustersFound === 0) { + return 'no thruster' + } else { + return false; + } } function positionAdjust(x, y) { @@ -68,10 +108,13 @@ export function rightClickTile(x, y) { export function getTile(x, y) { let [px, py] = position; - let [tx, ty] = [px + x, py + y]; - let id = posId(tx, ty); + return getRawTile(px + x, py + y); +} + +export function getRawTile(x, y) { + let id = posId(x, y); if (!tiles.has(id)) - tiles.set(id, new Tile(tx, ty, null)); + tiles.set(id, new Tile(x, y, null)); return tiles.get(id); // TODO: Get linked tiles. } @@ -81,7 +124,7 @@ function posId(x, y) { } function getBoundaries() { - return [0, 0, 0, 2]; + return [0, 0, 0, 3]; } class Tile { @@ -105,6 +148,12 @@ class Tile { return true; } + get neighbours() { + return [[0, -1], [1, 0], [0, 1], [-1, 0]].filter((_, i) => { + return this.module.connectivity[i]; + }).map(([dx, dy]) => getRawTile(this.x + dx, this.y + dy)); + } + get source() { return this; } diff --git a/js/game/events.mjs b/js/game/events.mjs index 038bada..1c7df6d 100644 --- a/js/game/events.mjs +++ b/js/game/events.mjs @@ -28,10 +28,15 @@ export function editShip() { } export function endEditing() { - graphics.changePerspective('universe'); - game.state.editing = false; - game.state.inventory = false; - edit.end(); + let {valid, reason} = edit.end(); + + if (valid) { + graphics.changePerspective('universe'); + game.state.editing = false; + game.state.inventory = false; + } else { + console.log(reason); + } } export function invalidTilePlacement() { diff --git a/js/gui/inventory.mjs b/js/gui/inventory.mjs index 82fa343..815b391 100644 --- a/js/gui/inventory.mjs +++ b/js/gui/inventory.mjs @@ -78,12 +78,8 @@ export default class GuiInventory extends GuiElement { tileClicked(type, id, button) { if (button == 'left') inventory.selectItem(type, id); - if (!state.editing) { - if (button == 'left') { - inventory.addItem(type, id); - } else if (button == 'right') { - inventory.removeitem(type, id); - } + if (!state.editing && button == 'right') { + inventory.removeItem(type, id); } this.updateTiles();