Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var grid = new vg.HexGrid({
rings: 5,
cellSize: 10
});
var board = new vg.Board(grid);
var mouse = new vg.MouseCaster(board.group, scene.camera, canvas);
var input = new Input(board.group, mouse);
var plane = new EditorPlane(board.group, grid, mouse);
nexus.input = input;
nexus.plane = plane;
nexus.board = board;
nexus.grid = grid;
nexus.scene = scene;
nexus.mouse = mouse;
var boardSize = 20; // TODO: get from settings
plane.generatePlane(boardSize * boardSize * 1.8, boardSize * boardSize * 1.8);
plane.addHoverMeshToGroup(scene.container);
board.generateOverlay(boardSize);
tower.tileAction.add(onMapChange, this);
// scene.add(board.group);
scene.focusOn(board.group);
if (map) {
loadMap(map);
}
else {
function update() {
currentGridCell = nexus.grid.pixelToCell(nexus.input.editorWorldPos);
if (nexus.mouse.down && keyboard.shift && nexus.mouse.allHits && nexus.mouse.allHits.length) {
// only check if the user's mouse is over the editor plane
if (!currentGridCell.equals(prevGridCell)) {
addTile(currentGridCell);
}
prevGridCell.copy(currentGridCell);
}
}
function update() {
currentGridCell = nexus.grid.pixelToCell(nexus.input.editorWorldPos);
if (nexus.mouse.down && keyboard.shift && nexus.mouse.allHits && nexus.mouse.allHits.length) {
// only check if the user's mouse is over the editor plane
if (!currentGridCell.equals(prevGridCell)) {
addTile(currentGridCell);
}
prevGridCell.copy(currentGridCell);
}
}
function onUserAction(type, overTile, data) {
var hit = nexus.mouse.allHits[0]
switch (type) {
case vg.MouseCaster.WHEEL:
if (keyboard.shift && overTile) {
if (!overTile.cell) {
overTile.dispose();
return;
}
_cel.copy(overTile.cell);
_cel.tile = null;
var dif = lastHeight - data;
var last = _cel.h;
_cel.h += dif > 0 ? -heightStep : heightStep;
if (_cel.h < 1) _cel.h = 1;
nexus.mouse.wheel = Math.round((_cel.h / heightStep) + (dif > 0 ? -1 : 1));
switch (type) {
case vg.MouseCaster.WHEEL:
if (keyboard.shift && overTile) {
if (!overTile.cell) {
overTile.dispose();
return;
}
_cel.copy(overTile.cell);
_cel.tile = null;
var dif = lastHeight - data;
var last = _cel.h;
_cel.h += dif > 0 ? -heightStep : heightStep;
if (_cel.h < 1) _cel.h = 1;
nexus.mouse.wheel = Math.round((_cel.h / heightStep) + (dif > 0 ? -1 : 1));
lastHeight = nexus.mouse.wheel;
if (last === _cel.h) return;
removeTile(overTile);
var tile = addTile(_cel);
tile.select();
tower.tileAction.dispatch(tower.TILE_CHANGE_HEIGHT, tile);
}
break;
case vg.MouseCaster.OVER:
if (keyboard.shift) {
if (overTile && nexus.mouse.rightDown) {
removeTile(overTile);
switch (type) {
case vg.MouseCaster.WHEEL:
if (keyboard.shift && overTile) {
if (!overTile.cell) {
overTile.dispose();
return;
}
_cel.copy(overTile.cell);
_cel.tile = null;
var dif = lastHeight - data;
var last = _cel.h;
_cel.h += dif > 0 ? -heightStep : heightStep;
if (_cel.h < 1) _cel.h = 1;
nexus.mouse.wheel = Math.round((_cel.h / heightStep) + (dif > 0 ? -1 : 1));
lastHeight = nexus.mouse.wheel;
if (last === _cel.h) return;
removeTile(overTile);
var tile = addTile(_cel);
tile.select();
tower.tileAction.dispatch(tower.TILE_CHANGE_HEIGHT, tile);
}
break;
case vg.MouseCaster.OVER:
if (keyboard.shift) {
if (overTile && nexus.mouse.rightDown) {
removeTile(overTile);
function addTile(cell) {
if (!cell || nexus.board.getTileAtCell(cell)) return;
var newCell = new vg.Cell();
newCell.copy(cell);
newCell.h = Math.abs(nexus.mouse.wheel * heightStep);
var newTile = nexus.grid.generateTile(newCell, 0.95);
nexus.board.addTile(newTile);
tower.tileAction.dispatch(tower.TILE_ADD, newTile);
return newTile;
}
function over(cell, tile, mesh) {
if (cell.tile) return;
if (!tile && nexus.mouse.down) {
addTile(cell);
}
}
function over(cell, tile, mesh) {
if (tile && nexus.mouse.down) {
walkableTile(tile);
}
}