Add ship validity checking
This commit is contained in:
parent
986c0479f1
commit
52770d7eb2
5 changed files with 72 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ function tickPlaying() {
|
|||
}
|
||||
|
||||
function tickEditing() {
|
||||
if (held[mapping.exitEdit]) {
|
||||
if (pressed[mapping.exitEdit]) {
|
||||
events.endEditing();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -28,10 +28,15 @@ export function editShip() {
|
|||
}
|
||||
|
||||
export function endEditing() {
|
||||
let {valid, reason} = edit.end();
|
||||
|
||||
if (valid) {
|
||||
graphics.changePerspective('universe');
|
||||
game.state.editing = false;
|
||||
game.state.inventory = false;
|
||||
edit.end();
|
||||
} else {
|
||||
console.log(reason);
|
||||
}
|
||||
}
|
||||
|
||||
export function invalidTilePlacement() {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue