Improve edit screen

This commit is contained in:
asraelite 2018-03-05 22:29:35 +00:00
parent 52770d7eb2
commit 826986cdbf
12 changed files with 235 additions and 40 deletions

View file

@ -12,68 +12,107 @@ export let width = 0;
export let height = 0;
export let position = [0, 0];
export let bounds = [0, 0, 0, 0];
export let message = '';
export function init() {
let ship = world.playerShip;
let modules = ship.modules;
let margin = consts.EDIT_MARGIN;
tiles.clear();
modules.forEach(m => {
let pos = [m.x, m.y];
tiles.set(posId(...pos), new Tile(...pos, m));
});
message = '';
adjustSize();
let neededZoom = graphics.canvas.width / (Math.max(width, height) + 10);
graphics.changePerspective('planet', 0, -5);
graphics.setZoom(neededZoom);
}
function adjustSize() {
let margin = consts.EDIT_MARGIN;
let [sx, ex, sy, ey] = getBoundaries();
[width, height] = [ex - sx + margin * 2 + 1, ey - sy + margin * 2 + 1];
position = [sx - margin, sy - margin];
let neededZoom = graphics.canvas.width / (Math.max(width, height) + 10);
graphics.changePerspective('planet', 0, -5);
graphics.setZoom(neededZoom);
}
export function end() {
let result = validate();
return {
result = {
valid: result === false,
reason: result
};
if (result.valid) {
let ship = world.playerShip;
let [ox, oy] = ship.com;
ship.clearModules();
tiles.forEach(t => {
if (t.type === null) return;
ship.addModule(t.x, t.y, modules[t.type][t.id]);
});
let [nx, ny] = ship.com;
let [dx, dy] = [nx - ox, ny - oy];
ship.x -= dx;
ship.y -= dy;
}
return result;
}
function validate() {
let capsulesFound = 0;
let thrustersFound = 0;
let fuelFound = 0;
let unvisited = new Set();
tiles.forEach(t => {
if (t.type !== null) unvisited.add(t)
});
let reason = '';
if (unvisited.size == 0) {
return 'no capsule';
reason = 'no capsule';
}
let visit = (tile) => {
unvisited.delete(tile);
if (tile.type == 'capsule') capsulesFound++;
if (tile.type == 'thruster') thrustersFound++;
if (tile.type == 'fuel') fuelFound++;
tile.neighbours.forEach(n => {
if (unvisited.has(n)) visit(n);
});
};
visit(unvisited.values().next().value);
if (unvisited.size > 0)
visit(unvisited.values().next().value);
if (unvisited.size > 0) {
return 'not connected'
reason = 'not connected'
} else if (capsulesFound === 0) {
return 'no capsule'
reason = 'no capsule'
} else if (thrustersFound === 0) {
return 'no thruster'
reason = 'no thruster'
} else if (fuelFound === 0) {
reason = 'no fuel tank'
} else {
return false;
reason = false;
}
if (reason === false) {
message = '';
} else {
message = reason;
}
return reason;
}
function positionAdjust(x, y) {
@ -95,6 +134,8 @@ export function clickTile(x, y) {
let id = posId(...pos);
tiles.set(id, new Tile(...pos, inventory.currentItem));
inventory.removeItem(...inventory.currentItem.ident);
adjustSize();
validate();
}
export function rightClickTile(x, y) {
@ -104,6 +145,8 @@ export function rightClickTile(x, y) {
let id = posId(tx, ty);
tiles.set(id, new Tile(tx, ty, null));
inventory.addItem(current.type, current.id);
adjustSize();
validate();
}
export function getTile(x, y) {
@ -124,7 +167,15 @@ function posId(x, y) {
}
function getBoundaries() {
return [0, 0, 0, 3];
let sx = sy = ex = ey = null
tiles.forEach(t => {
if (t.type === null) return;
if (sx === null || t.x < sx) sx = t.x;
if (ex === null || t.x > ex) ex = t.x;
if (sy === null || t.y < sy) sy = t.y;
if (ey === null || t.y > ey) ey = t.y;
});
return [sx, ex, sy, ey];
}
class Tile {

View file

@ -21,7 +21,11 @@ export function launchShip() {
game.state.landed = false;
}
export function editShip() {
export function toggleEdit() {
if (game.state.editing) {
endEditing();
return;
}
game.state.editing = true;
game.state.inventory = true;
edit.init();
@ -34,8 +38,6 @@ export function endEditing() {
graphics.changePerspective('universe');
game.state.editing = false;
game.state.inventory = false;
} else {
console.log(reason);
}
}

View file

@ -6,8 +6,6 @@ export let currentItem = null;
export function init() {
items.clear();
addItem('capsule', 'small');
addItem('thruster', 'light');
}
export function getTiles() {