Add interaction between edit grid and inventory

This commit is contained in:
asraelite 2018-03-05 16:11:58 +00:00
parent 469121e18a
commit 986c0479f1
9 changed files with 51 additions and 14 deletions

View file

@ -3,6 +3,7 @@ import * as graphics from '../graphics/index.mjs';
import * as world from '../world/index.mjs';
import * as consts from '../consts.mjs';
import * as events from './events.mjs';
import * as inventory from './inventory.mjs';
import {modules} from '../data.mjs';
import {images as assets} from '../assets.mjs';
@ -11,7 +12,6 @@ export let width = 0;
export let height = 0;
export let position = [0, 0];
export let bounds = [0, 0, 0, 0];
export let currentModule = null;
export function init() {
let ship = world.playerShip;
@ -36,29 +36,34 @@ export function end() {
}
function trueFromVisible(x, y) {
function positionAdjust(x, y) {
let [px, py] = position;
return [x - px, y - py];
return [x + px, y + py];
}
export function clickTile(x, y) {
if (currentModule !== null) return;
if (inventory.currentItem === null) return;
let current = getTile(x, y).source;
if (current.type !== null) {
events.invalidTilePlacement();
return;
} else {
events.tilePlacement();
}
let id = posId(x, y);
tiles.set(id, new Tile(x, y, currentModule));
let pos = positionAdjust(x, y);
let id = posId(...pos);
tiles.set(id, new Tile(...pos, inventory.currentItem));
inventory.removeItem(...inventory.currentItem.ident);
}
export function rightClickTile(x, y) {
let current = getTile(x, y).source;
if (current === null) return;
if (current.type === null) return;
let { x: tx, y: ty } = current;
let id = posId(tx, ty);
tiles.set(id, new Tile(tx, ty, null));
inventory.addItem(current.type, current.id);
}
export function getTile(x, y) {
@ -84,9 +89,12 @@ class Tile {
if (module === null) {
this.module = null;
this.image = null;
this.type = null
this.id = null;
} else {
this.module = modules[module.type][module.id];
this.image = assets.modules[module.type][module.id];
({type: this.type, id: this.id} = module);
this.module = modules[this.type][this.id];
this.image = assets.modules[this.type][this.id];
if (module.type === 'thruster') this.image = this.image.off;
}
this.x = x;

View file

@ -37,3 +37,7 @@ export function endEditing() {
export function invalidTilePlacement() {
// TODO: Play some audio.
}
export function tilePlacement() {
// TODO: Play some audio.
}

View file

@ -28,6 +28,10 @@ export function removeItem(type, id) {
if (!items.has(mapId)) return;
let tile = items.get(mapId);
tile.decrease();
if (tile.quantity == 0) {
items.delete(mapId);
currentItem = null;
}
}
export function selectItem(type, id) {