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

View file

@ -37,3 +37,7 @@ export function endEditing() {
export function invalidTilePlacement() { export function invalidTilePlacement() {
// TODO: Play some audio. // 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; if (!items.has(mapId)) return;
let tile = items.get(mapId); let tile = items.get(mapId);
tile.decrease(); tile.decrease();
if (tile.quantity == 0) {
items.delete(mapId);
currentItem = null;
}
} }
export function selectItem(type, id) { export function selectItem(type, id) {

View file

@ -74,6 +74,15 @@ function renderItemButton(element) {
let [dw, dh] = [element.w * (1 - p), element.h * (1 - p)]; let [dw, dh] = [element.w * (1 - p), element.h * (1 - p)];
context.drawImage(element.image, ox, oy, dw, dh); context.drawImage(element.image, ox, oy, dw, dh);
} }
if (element.quantity > 1) { // CHANGE TO 1
context.textAlign = 'right';
context.textBaseline = 'bottom';
context.fillStyle = '#fff';
context.font = 'bold 10pt Consolas';
let [ex, ey] = element.end;
context.fillText('x' + element.quantity, ex - 2, ey - 2);
}
} }
function renderEdit(element) { function renderEdit(element) {

View file

@ -3,6 +3,7 @@ import GuiElement from './element.mjs';
import GuiItemButton from './item.mjs'; import GuiItemButton from './item.mjs';
import {state} from '../game/index.mjs'; import {state} from '../game/index.mjs';
import * as edit from '../game/edit.mjs'; import * as edit from '../game/edit.mjs';
import * as inventory from './inventory.mjs';
export default class GuiEdit extends GuiElement { export default class GuiEdit extends GuiElement {
constructor(x, y, w = 100, h = 30) { constructor(x, y, w = 100, h = 30) {
@ -11,6 +12,7 @@ export default class GuiEdit extends GuiElement {
this.tileWidth = 0; this.tileWidth = 0;
this.tileHeight = 0; this.tileHeight = 0;
this.active = false; this.active = false;
this.guiInventory = null;
} }
updateTiles() { updateTiles() {
@ -71,5 +73,6 @@ export default class GuiEdit extends GuiElement {
} }
this.updateTiles(); this.updateTiles();
this.guiInventory.updateTiles();
} }
} }

View file

@ -56,7 +56,8 @@ export default class GuiInventory extends GuiElement {
let el = new GuiItemButton(tile, onclick, ex, ey, ew, eh, { let el = new GuiItemButton(tile, onclick, ex, ey, ew, eh, {
padding: 0.1, padding: 0.1,
selected: selected selected: selected,
quantity: tile.quantity
}); });
this.append(el); this.append(el);

View file

@ -2,16 +2,18 @@ import * as gui from './index.mjs';
import GuiButton from './button.mjs'; import GuiButton from './button.mjs';
export default class GuiItemButton extends GuiButton { export default class GuiItemButton extends GuiButton {
constructor(tile, onclick, x, y, w = 50, h = 50, { padding, selected } = { constructor(tile, onclick, x, y, w = 50, h = 50, {
padding: 0, padding = 0,
selected: false selected = false,
}) { quantity = 1,
} = {}) {
super(null, onclick, x, y, w, h); super(null, onclick, x, y, w, h);
this.module = tile.module; this.module = tile.module;
this.image = tile.image; this.image = tile.image;
this.type = 'itemButton'; this.type = 'itemButton';
this.padding = padding; this.padding = padding;
this.selected = selected; this.selected = selected;
this.quantity = quantity
} }
click() { click() {

View file

@ -39,6 +39,10 @@ export class Rect {
return [this.x, this.y, this.w, this.h]; return [this.x, this.y, this.w, this.h];
} }
get end() {
return [this.x + this.w, this.y + this.h];
}
get center() { get center() {
return [this.x + this.w / 2, this.y + this.h / 2]; return [this.x + this.w / 2, this.y + this.h / 2];
} }

View file

@ -60,5 +60,7 @@ export function game() {
inventory.x += 10; inventory.x += 10;
inventory.y += 10; inventory.y += 10;
edit.guiInventory = inventory;
return shadow; return shadow;
} }