Skip to content

Commit

Permalink
bug fix and multimap logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dip000 committed Feb 14, 2022
1 parent 6fcb8aa commit 109d33e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 76 deletions.
6 changes: 3 additions & 3 deletions MapBuilderForWeb/BuilderVisuals.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
if(isClicking){
if(isShapeEditorActive)
PlaceDotAtPoint(currentItemPlacingInfo.positionX, currentItemPlacingInfo.positionY);
else
PlaceItemAtCurrentItemInfo();
else{
OnGridClickAndDrag(currentItemPlacingInfo.positionX, currentItemPlacingInfo.positionY);
}

//console.log("isClicking: " + isClicking + "; isShapeEditorActive:" + isShapeEditorActive);
}
Expand Down Expand Up @@ -120,7 +121,6 @@
if(shape==null) return;
for(var i=0; i<shape.x.length; i++){
var cell = table.rows[ shape.x[i] ].cells[ shape.y[i] ];

if(cell == null) continue;

cell.style.backgroundColor = color;
Expand Down
63 changes: 37 additions & 26 deletions MapBuilderForWeb/MapBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,66 @@
var occupancyMap;
const OCCUPIED = true;
const FREE = false;
function GetOccupancyOfCoordenates(coordenates){
const OUT_OF_BOUNDS = 2;
const OBSTRUCTED = 3;
const MAP_CUT = 4;
function GetOccupancyOfPlacingInfo(){
//If at least one coordenate is occupied, then return occupied
let mapLengthX = occupancyMap.length;
let mapLengthY = occupancyMap[0].length;

let x = currentItemPlacingInfo.coordenates.x;
let y = currentItemPlacingInfo.coordenates.y;

try{
for(var i=0; i<coordenates.x.length; i++){
if(occupancyMap[coordenates.x[i]][coordenates.y[i]] == OCCUPIED){
//console.log("Coordenates are occupied:");
//console.log(coordenates);
return OCCUPIED;
let state = ( occupancyMap[ currentItemPlacingInfo.positionX ][ currentItemPlacingInfo.positionY ] );

if( state == null){
return MAP_CUT;
}
if( state == OCCUPIED){
return OCCUPIED;
}

for(var i=0; i<x.length; i++){
state = occupancyMap[ x[i] ][ y[i] ];

if(state == null){
return OUT_OF_BOUNDS;
}

if(state == OCCUPIED){
return OBSTRUCTED;
}
}
} catch {return OCCUPIED;}
//console.log("Coordenates are unoccupied:");
//console.log(coordenates);
} catch {return OUT_OF_BOUNDS;}

return FREE;
}



function UpdateOccupancy(coordenates, OCCUPIED){
for(var i=0; i<coordenates.x.length; i++){
occupancyMap[coordenates.x[i]][coordenates.y[i]] = OCCUPIED;
}

//console.log("UPDATED OCCUPANCY MAP:");
//console.log(occupancyMap);
function UpdateOccupancy(coordenates, state){
for(var i=0; i<coordenates.x.length; i++){
occupancyMap[coordenates.x[i]][coordenates.y[i]] = state;
}
}

var historyOfPlacements = [];
var historyIndex = 0;
function RegisterHistoryOfPlacements(itemPlacingInfo){
itemPlacingInfo.indexInHistory = historyIndex;
historyOfPlacements[historyIndex++] = new ItemPlacingInfo(itemPlacingInfo);
//console.log("Registered in history. History:");
//console.log(historyOfPlacements);
}

function DeleteFromHistoryOfPlacements(itemPlacingInfo){
historyOfPlacements[itemPlacingInfo.indexInHistory].undoFromHistory();
//console.log("Undone from history. History:");
//console.log(historyOfPlacements);
}

function UndoActionFromHistory(itemPlacingInfo){
if(itemPlacingInfo == null) return;

itemPlacingInfo.deleted = ! (itemPlacingInfo.deleted);
//console.log("Undone Last action from history. History:");
//console.log(historyOfPlacements);

if(itemPlacingInfo.deleted == true){
return ActionTypes.deleted;
Expand All @@ -78,19 +90,18 @@
}


function FindHistoryInfoAtCoordenates(coordenate){
function FindHistoryInfoAtCurrentPlacement(){
for(var i=0; i<historyOfPlacements.length; i++){

//Skip the search if it was marked as deleted
if(historyOfPlacements[i].deleted == true){
continue;
}

let historyCoordenates = historyOfPlacements[i].coordenates;

for(var j=0; j<historyCoordenates.x.length; j++){
let sameCoordenateX = (historyCoordenates.x[j] == coordenate.x[0]);
let sameCoordenateY = (historyCoordenates.y[j] == coordenate.y[0]);
let sameCoordenateX = (historyCoordenates.x[j] == currentItemPlacingInfo.positionX);
let sameCoordenateY = (historyCoordenates.y[j] == currentItemPlacingInfo.positionY);

if(sameCoordenateX && sameCoordenateY){
return historyOfPlacements[i];
Expand Down
117 changes: 70 additions & 47 deletions MapBuilderForWeb/MapBuilderForWeb.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<script type="text/javascript" src="BuilderCalculations.js"></script>
<script type="text/javascript" src="BuilderVisuals.js"></script>
<script type="text/javascript" src="ShapeBuilder.js"></script>
<script type="text/javascript" src="MultimapMechanics.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="MapBuilderStyles.css">
Expand Down Expand Up @@ -53,6 +54,7 @@
const clearedGridColor = "white";
const itemSelectedColor = "#B9F51B";
const itemDefaultColor = "#1D8742";
const mapCutColor = "#1D8742";

//List Of Shapes
var listOfShapes = [
Expand Down Expand Up @@ -98,9 +100,11 @@
//GoToShapesEditor();
}



function initializeMapEditorGrid(x, y){
//Creates grid
let placingLcation = document.getElementById("GridGraphics");
let placingLcation = document.getElementById("GridGraphics");
table = generate_table(x, y);
table.id = "grid";
table.className = "gridMap";
Expand All @@ -110,6 +114,7 @@

//Initializes grid logic
occupancyMap = Array(x).fill(null).map(()=>Array(y).fill(false));
cutsMap = Array(x).fill(null).map(()=>Array(y).fill(false));

//Add functionality to the grid
var tablerowscount=table.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
Expand Down Expand Up @@ -245,50 +250,72 @@

occupancyMap = Array(mapLengthX).fill(null).map(()=>Array(mapLengthY).fill(false));

//console.log("DELETED MAP INFO:");
//console.log(formatedCoordenates);
//Reset cuts in map. See MultimapMechanics.js for more info
rows(nrows);
cols(ncols);
}

function HideMapEditor(){
table.style.display = "none";
let lateralButtons = document.getElementsByClassName("control")[0];
lateralButtons.style.display = "none";
}


function OnGridClickAndDrag(x, y){
UpdateItemPlacingInfo(x, y);
state = GetOccupancyOfPlacingInfo();


if (state == FREE){
PlaceCurrentItemInfo();
}
}

function OnGridClick(x, y){
//OCCUPIED happens when you click directly over a shape in the map
//OBSTRUCTED happens when you try to place a shape and indiretctly is being blocked by another
//FREE is space availabe

//Resets Ctrl+Z actions
chainedUndoneIndex = 0;

//Creates an object {[x],[y]} where x and y are integrers
var clickedCoordenate = new Vector2Array(x, y);
UpdateItemPlacingInfo(x, y);
state = GetOccupancyOfPlacingInfo();

if ( GetOccupancyOfCoordenates(clickedCoordenate) == OCCUPIED){
RemoveItemAtCoordenate(clickedCoordenate);
if ( state == OUT_OF_BOUNDS){
//console.log("OUT_OF_BOUNDS");
return;
}
else if (state == OCCUPIED){
//console.log("OCCUPIED");
RemoveCurrentItemInfo();
}
else{
//console.log("Clicked on empty space");
PlaceItemAtCurrentItemInfo();
else if (state == OBSTRUCTED){
//console.log("OBSTRUCTED");
}
else if (state == MAP_CUT){
//console.log("MAP_CUT");
}
else if (state == FREE){
//console.log("FREE");
PlaceCurrentItemInfo();
}
}

function RemoveItemAtCoordenate(clickedCoordenate){
let itemPlacingInfo = FindHistoryInfoAtCoordenates(clickedCoordenate);
//console.log("Clicked on an item. Placing info:");
//console.log(itemPlacingInfo);
DeleteFromHistoryOfPlacements(itemPlacingInfo);
printVisualsOfCoordenates(itemPlacingInfo.coordenates, clearedGridColor);
UpdateOccupancy(itemPlacingInfo.coordenates, FREE);
}

//Sooo. i know is confusing but here goes:
// 1. Average volume of an object is its center position. Not the bounding box center
// 2. Here changes the index-position for the center-volume-position of the object so it visualy rotates from its center
// 3. The (rows, columns) system is different from the (x, y) system by 90 degrees or (-y, x)
// 4. We need to register the index position as it was taken by a (x,y) system, so it changes again the index-position-row-col to index-position-xy
// 5. Summary; internally we're using (row,col) as original detection of coordenates. Volume-center to place the visuals. And standar (x,y) as output
function PlaceItemAtCurrentItemInfo(){
function UpdateItemPlacingInfo(x, y){
//Sooo. i know is confusing but here goes:
// 1. Average volume of an object is its center position. Not the bounding box center
// 2. Here changes the index-position for the center-volume-position of the object so it visualy rotates from its center
// 3. The (rows, columns) system is different from the (x, y) system by 90 degrees or (-y, x)
// 4. We need to register the index position as it was taken by a (x,y) system, so it changes again the index-position-row-col to index-position-xy
// 5. Summary; internally we're using (row,col) as original detection of coordenates. Volume-center to place the visuals. And standar (x,y) as output

//Creates an object {[x],[y]} where x and y are integrers
let clickedCoordenate = new Vector2Array(x, y);
currentItemPlacingInfo.positionX = x;
currentItemPlacingInfo.positionY = y;

//Finds info
let shape = listOfShapes[currentItemPlacingInfo.itemType];
let shapeRotated = RotateCoordenatesByAngle(shape, currentItemPlacingInfo.rotation);

Expand All @@ -300,28 +327,26 @@
//make info
let coordenates = GlobalizeCoordenates(shapeRotated, volumeIndex.x, volumeIndex.y);
currentItemPlacingInfo.coordenates = coordenates;

}

function RemoveCurrentItemInfo(){
let infoToRemove = FindHistoryInfoAtCurrentPlacement();
DeleteFromHistoryOfPlacements(infoToRemove);
printVisualsOfCoordenates(infoToRemove.coordenates, clearedGridColor);
UpdateOccupancy(infoToRemove.coordenates, FREE);
printHoverVisuals();
}

function PlaceCurrentItemInfo(){
//Standar (x,y) system for ouputting
let minValues = GetMinValuesOfCoordenates(coordenates)
let minValues = GetMinValuesOfCoordenates(currentItemPlacingInfo.coordenates)
currentItemPlacingInfo.positionX = minValues.x;
currentItemPlacingInfo.positionY = minValues.y;

//console.log(averageVolume);
//console.log(roundedAverageVolume);
//console.log(volumeIndex);

if(GetOccupancyOfCoordenates(coordenates) == FREE){
//console.log("CorrectPlacement");
printVisualsOfCoordenates(coordenates, listOfShapeColors[currentItemPlacingInfo.itemType]);
UpdateOccupancy(coordenates, OCCUPIED);
RegisterHistoryOfPlacements(currentItemPlacingInfo);
}
else{
//console.log("Incorrect Placement");
}

//console.log("END OF PLACEMENT");
//console.log(coordenates);
printVisualsOfCoordenates(currentItemPlacingInfo.coordenates, listOfShapeColors[currentItemPlacingInfo.itemType]);
UpdateOccupancy(currentItemPlacingInfo.coordenates, OCCUPIED);
RegisterHistoryOfPlacements(currentItemPlacingInfo);

}

var lastShapeElement;
Expand All @@ -330,8 +355,6 @@
if(itemIndex > listOfShapes.length-1)
itemIndex = listOfShapes.length-1;

console.log("new itemIndex:" + itemIndex);
//console.log(shapeElements);
currentItemPlacingInfo.itemType = itemIndex;
itemType.innerHTML = currentItemPlacingInfo.itemType;

Expand Down

0 comments on commit 109d33e

Please sign in to comment.