Add ship validity checking

This commit is contained in:
asraelite 2018-03-05 17:12:00 +00:00
parent 986c0479f1
commit 52770d7eb2
5 changed files with 72 additions and 16 deletions

View file

@ -1,3 +1,5 @@
// Connectivity = [top, right, bottom, left] (same TRouBLe as CSS)
export const modules = { export const modules = {
capsule: { capsule: {
small: { small: {
@ -7,6 +9,8 @@ export const modules = {
type: 'capsule', type: 'capsule',
id: 'small', id: 'small',
mass: 2, mass: 2,
connectivity: [false, false, true, false],
capacity: 3,
rotation: 0.1 rotation: 0.1
} }
}, },
@ -17,7 +21,8 @@ export const modules = {
type: 'fuel', type: 'fuel',
id: 'small', id: 'small',
mass: 1, mass: 1,
capacity: 3 connectivity: [true, false, true, false],
fuelCapacity: 5
} }
}, },
thruster: { thruster: {
@ -28,6 +33,7 @@ export const modules = {
type: 'thruster', type: 'thruster',
id: 'light', id: 'light',
mass: 2, mass: 2,
connectivity: [true, false, false, false],
thrust: 10, thrust: 10,
isp: 200 isp: 200
} }

View file

@ -51,7 +51,7 @@ function tickPlaying() {
} }
function tickEditing() { function tickEditing() {
if (held[mapping.exitEdit]) { if (pressed[mapping.exitEdit]) {
events.endEditing(); events.endEditing();
} }
} }

View file

@ -33,7 +33,47 @@ export function init() {
} }
export function end() { 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) { function positionAdjust(x, y) {
@ -68,10 +108,13 @@ export function rightClickTile(x, y) {
export function getTile(x, y) { export function getTile(x, y) {
let [px, py] = position; let [px, py] = position;
let [tx, ty] = [px + x, py + y]; return getRawTile(px + x, py + y);
let id = posId(tx, ty); }
export function getRawTile(x, y) {
let id = posId(x, y);
if (!tiles.has(id)) if (!tiles.has(id))
tiles.set(id, new Tile(tx, ty, null)); tiles.set(id, new Tile(x, y, null));
return tiles.get(id); return tiles.get(id);
// TODO: Get linked tiles. // TODO: Get linked tiles.
} }
@ -81,7 +124,7 @@ function posId(x, y) {
} }
function getBoundaries() { function getBoundaries() {
return [0, 0, 0, 2]; return [0, 0, 0, 3];
} }
class Tile { class Tile {
@ -105,6 +148,12 @@ class Tile {
return true; 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() { get source() {
return this; return this;
} }

View file

@ -28,10 +28,15 @@ export function editShip() {
} }
export function endEditing() { export function endEditing() {
graphics.changePerspective('universe'); let {valid, reason} = edit.end();
game.state.editing = false;
game.state.inventory = false; if (valid) {
edit.end(); graphics.changePerspective('universe');
game.state.editing = false;
game.state.inventory = false;
} else {
console.log(reason);
}
} }
export function invalidTilePlacement() { export function invalidTilePlacement() {

View file

@ -78,12 +78,8 @@ export default class GuiInventory extends GuiElement {
tileClicked(type, id, button) { tileClicked(type, id, button) {
if (button == 'left') inventory.selectItem(type, id); if (button == 'left') inventory.selectItem(type, id);
if (!state.editing) { if (!state.editing && button == 'right') {
if (button == 'left') { inventory.removeItem(type, id);
inventory.addItem(type, id);
} else if (button == 'right') {
inventory.removeitem(type, id);
}
} }
this.updateTiles(); this.updateTiles();