I am writing code to check if an object can be placed on a grid.
int[,] floorMap;
public Vector2[] objectMap;
int floorWidth = 30;
int floorHeight = 30;
void GenerateMap() {
for (int x = 0; x < floorWidth; x++) {
for(int y = 0; y < floorHeight; y++) {
floorMap[x,y] = 0;
}
}
}
public bool CheckSafePlace(int offsetX, int offsetY) {
bool allowPlacement = true;
for(int i = 0; i < objectMap.Length; i++) {
// Check each position the object takes up and reference it against the offset to determine if there is a floorMap value of 1 - which would not allow placement. Then exit the loop.
}
return allowPlacement;
}
I can’t work out what to put in the commented out line to get this to work.
int[,] floorMap;
public Vector2 objectMap;
int floorWidth = 30;
int floorHeight = 30;
void GenerateMap() {
for (int x = 0; x < floorWidth; x++) {
for(int y = 0; y < floorHeight; y++) {
floorMap[x,y] = 0;
}
}
}
public bool CheckSafePlace(int offsetX, int offsetY) {
bool allowPlacement = floorMap[offsetX, offsetY] != 1;
return allowPlacement;
}
Try that. Not sure why you are using a for loop.
public Vector2 objectMap;
// This is set on the prefab of the object. I set the values to the offset of each part of it and what tile it will cover. Eg. Vector2(0,0), Vector2(1,0), Vector2(2,0),Vector2(1,1),Vector2(1,2) makes a T shape
public bool CheckPlacement() {
bool result = true;
// roomManager.floorMap is an int[30,30] where 30 is the roomManager.roomSize in tiles
int[,] floorMap = roomManager.floorMap;
foreach(Vector2 objectPoint in objectMap) {
// interactionManager.currentX and interactionManager.currentY are the x and y int that my mouse is pointing to via a raycast
int xPoint = Mathf.FloorToInt(objectPoint.x) + interactionManager.currentX;
int yPoint = Mathf.FloorToInt(objectPoint.y) + interactionManager.currentY;
if((xPoint >= 0 && xPoint < roomManager.roomSize) && (yPoint >= 0 && yPoint < roomManager.roomSize)) {
if(floorMap[xPoint,yPoint] != 0) {
result = false;
}
} else {
result = false;
}
}
return result;
}