Skip to content

Commit

Permalink
Fully uploadable
Browse files Browse the repository at this point in the history
Has no input to upload
  • Loading branch information
dip000 committed Feb 19, 2022
1 parent 6ea4925 commit 9c92c38
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 26 deletions.
17 changes: 16 additions & 1 deletion MapBuilderForWeb/BuilderCalculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,22 @@

return minValue;
}


function GetMinValues(coordenates){
let minValue = {x:999, y:999};

for( let i=0; i<coordenates.x.length; i++ ){
if(coordenates.x[i] < minValue.x){
minValue.x = coordenates.x[i];
}
if(coordenates.y[i] < minValue.y){
minValue.y = coordenates.y[i];
}
}

return minValue;
}

function GetMaxValuesOfCoordenates(coordenates){
let maxValue = {x:0, y:0};

Expand Down
60 changes: 43 additions & 17 deletions MapBuilderForWeb/MapBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function ItemPlacingInfo(itemType, rotation, positionX, positionY, coordenates,
this.positionX = 0;
this.positionY = 0;
this.coordenates = new Vector2Array();
this.level = {x:0, y:0};
this.level = new Point();

//Internal properties
this.indexInHistory = 0;
Expand Down Expand Up @@ -326,11 +326,10 @@ function OutputData(){
this.mapSizeX = 0;
this.mapSizeY = 0;
this.mapName = "unnamed";
this.parseInfo = {originalSize:null, originalOffset:null, level:null, rows:null, cols:null};

this.generateFromHistory = function( history=historyOfPlacements ){

//if(history == null) history = ;

let numberOfInstructions = history.length;
this.itemTypes = [];
this.itemRotations = [];
Expand Down Expand Up @@ -439,32 +438,59 @@ function Point(x, y){
}

function uploadMaps(mapsString){

let maps = [];
let mapSize;
maps[0] = JSON.parse(mapsString[0]);

// Simplified version with a non cutted map
if(mapsString.length == 1){
maps = JSON.parse(mapsString[0]);
updateMap(maps);
return;
}
//Rebuild from parseInfo data
let newMapSizeX = maps[0].parseInfo.originalSize.x;
let newMapSizeY = maps[0].parseInfo.originalSize.y;
let nrows = maps[0].parseInfo.rows;
let ncols = maps[0].parseInfo.cols;

// First parse all
let nmaps = mapsString.length;
for(let i=0; i<nmaps; i++){
MapRowCols(newMapSizeX, newMapSizeY, nrows, ncols);
updateMap(maps[0]);

//Build each map
for(let i=1; i<mapsString.length; i++){
maps[i] = JSON.parse(mapsString[i]);
updateMap(maps[i]);
}


}

function updateMap(maps){
map(maps.mapSizeX, maps.mapSizeY);
function updateMap(maps){
//Rebuild from parseInfo data
currentItemPlacingInfo.level = maps.parseInfo.level;

let placementOffset = new Point(0,0);
if( maps.parseInfo.originalOffset != null ){
placementOffset.x = maps.parseInfo.originalOffset.minX;
placementOffset.y = maps.parseInfo.originalOffset.minY;
}

for(let i=0; i<maps.positionsX.length; i++){
ChangeItem(maps.itemTypes[i]);

currentItemPlacingInfo.rotation = maps.itemRotations[i];
OnGridClick(maps.positionsX[i], maps.positionsY[i]);
currentItemPlacingInfo.itemType = maps.itemTypes[i];

let itemType = maps.itemTypes[i];
let itemShape = listOfShapes[itemType];
let shapeRotated = RotateCoordenatesByAngle(itemShape, currentItemPlacingInfo.rotation);

//Performs a global rotation. Switches axis and mirrors Y
currentItemPlacingInfo.positionX = (maps.mapSizeY-1) - maps.positionsY[i] + placementOffset.x;
currentItemPlacingInfo.positionY = maps.positionsX[i] + placementOffset.y;

let pivot = GetMinValuesOfCoordenates(shapeRotated);
let coordenates = GlobalizeCoordenates(shapeRotated, currentItemPlacingInfo.positionX - pivot.x, currentItemPlacingInfo.positionY - pivot.y);
currentItemPlacingInfo.coordenates = coordenates;

//console.log( new ItemPlacingInfo(currentItemPlacingInfo) )
printVisualsOfCoordenates(coordenates, listOfShapeColors[ maps.itemTypes[i] ]);
UpdateOccupancy(coordenates, OCCUPIED);
RegisterHistoryOfPlacements(currentItemPlacingInfo);
}
}

Expand Down
5 changes: 3 additions & 2 deletions MapBuilderForWeb/MapBuilderForWeb.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@

if( levels[r] == null ) levels[r] = new Array();
levels[r][c] = level;

}
}

Expand Down Expand Up @@ -276,8 +277,8 @@

function ResetOccupancyMap(){

for (let r=0; r<gridRows; r++) {
for (let c=0; c<gridCols; c++) {
for (let r=0; r<occupancyMaps.length; r++) {
for (let c=0; c<occupancyMaps[0].length; c++) {
let occupancyMap = occupancyMaps[r][c];
let level = levels[r][c];
let formatedCoordenates = OccupancyMapToCoordenates(occupancyMap);
Expand Down
33 changes: 27 additions & 6 deletions MapBuilderForWeb/MultimapMechanics.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@


function resetMap(){
function clearMap(){
if( AskForReset() == DONT_RESET ) return;

ResetMap();
if(gridMap != null){
document.getElementById("GridGraphics").removeChild(gridMap);
gridMap = null;
}
//console.log("clearMap");
}

function map(x, y){
resetMap()
clearMap();

gridSize.x = Math.max(1, x);
gridSize.y = Math.max(1, y);
Expand All @@ -27,10 +29,20 @@
map(gridSize.x, gridSize.y);
}

function MapRowCols(x, y, nrows, ncols){
clearMap();

gridCols = Math.max(1, ncols);
gridRows = Math.max(1, nrows);
gridSize.x = Math.max(1, x);
gridSize.y = Math.max(1, y);
initializeMapEditorGrid( gridSize.x, gridSize.y );
}

const RESET = true;
const DONT_RESET = false;
function AskForReset(message=""){
for(let r=0; r<occupancyMaps.length; r++){
/*for(let r=0; r<occupancyMaps.length; r++){
for(let c=0; c<occupancyMaps[0].length; c++){
let coordenates = OccupancyMapToCoordenates(occupancyMaps[r][c]);
Expand All @@ -41,7 +53,8 @@
return DONT_RESET;
}
}
}
}*/
//console.log("AskForReset");
return RESET;
}

Expand Down Expand Up @@ -106,8 +119,9 @@

//Localized coordenates are just the minimum size in which the whole level fits
let rotatedCoordenates;
let residueData;
if(localize){
let data = outputData[r][c].generateFromMap( occupancyMaps[r][c] );
residueData = outputData[r][c].generateFromMap( occupancyMaps[r][c] );

//Rotates in local space
rotatedCoordenates = LocalizeCoordenates(formatedCoordenates);
Expand All @@ -121,7 +135,14 @@
outputData[r][c].mapSizeX = gridSize.y;
outputData[r][c].mapSizeY = gridSize.x;
}


//Data that can be used to upload and rebuild the session
outputData[r][c].parseInfo.originalSize = gridSize;
outputData[r][c].parseInfo.originalOffset = residueData;
outputData[r][c].parseInfo.level = new Point(r,c);
outputData[r][c].parseInfo.rows = gridRows;
outputData[r][c].parseInfo.cols = gridCols;

//Coordenates are formated and sent back to output data
outputData[r][c].positionsX = rotatedCoordenates.x;
outputData[r][c].positionsY = rotatedCoordenates.y;
Expand Down

0 comments on commit 9c92c38

Please sign in to comment.