Building Engine for Unity3D - Using data for game logic

Relation between Unity coordinates and Building Engine coordinates

Z axis of the Building Engine cordinates system is inverse of the Unity coordinates. So a point located at (x, y, z) in Unity will have (x, y, -z) in the Building Engine coordinates.

You can obtain the in-map coordinates (in the Building Engine system) with this function :

Engine.utils.GetInMapCoordinates(Vector3 unityCoordinates);

Where is my avatar ?

Finding the in-map position of an avatar (or the gameObject you want) can be determined like that. First get the in-map coordinates of the avatar :

Vector3 position = Engine.utils.GetInMapCoordinates(Vector3 avatar.transform.position);

Next we can locate it in a story :

Story occupiedStory = Engine.utils.FindStoryAtPosition(Engine.Map, position);

If occupiedStory is null, then the avatar is in the outdoor of the map.

We can locate it in a specific room :

Room occupiedRoom = Engine.utils.FindRoomAtPosition(Engine.Map, position);

If occupiedRoom is null, then the avatar is not in a room (but can be in a story or in outdoor of the map).

Get Unity coordinates of Building Engine data

If we want to get the Unity coordinates of the center of a room, we need first th calculate the center of the room in Building Engine coordinates system :

Vector3 roomCenter = new Vector3(room.positionX + room.story.positionX + room.story.building.positionX + room.story.building.map.positionX + room.width*0.5f, room.story.positionY + room.story.building.elevation + room.story.floorHeight*0.5f, room.positionZ + room.story.positionZ + room.story.building.positionZ + room.story.building.map.positionZ + room.height*0.5f);

So the corresponding Unity coordinates is :

Vector3 roomCenterInUnityCoordinates = new Vector3(roomCenter.x, roomCenter.y, -roomCenter.z);

In order to get the center of a portal in the unity coordinates, proceed like that :

//get the entry room
Room room = Engine.utils.GetRoom(portal);
//get the position of the entry room
Vector3 position = new Vector3(room.positionX + room.story.positionX + room.story.building.positionX + room.story.building.map.positionX, room.story.positionY + room.story.building.elevation + room.story.floorHeight*0.5f, room.positionZ + room.story.positionZ + room.story.building.positionZ + room.story.building.map.positionZ);
//adjust the position according to the direction and the position of the portal
switch(portal.direction){
case 0: //NORTH
position.x += portal.position + portal.width*0.5f;
break;
case 1: //EAST
position.x += room.width;
position.z += portal.position + portal.width*0.5f;
break;
case 2: //SOUTH
position.x += portal.position + portal.width*0.5f;;
position.z += room.height;
break;
case 4: //WEST
position.z += portal.position + portal.width*0.5f;
break;
default:
break;
}
//so the center of the portal in unity coordinates is
Vector3 portalInUnityCoordinates = new Vector3(position.x, position.y, -position.z);

Create a mini-map from data

private void BuildMiniMap()
{
GameObject miniMap = new GameObject("miniMap");
miniMap.transform.position = new Vector3(Engine.Map.positionX * miniMapScale, 0f, -Engine.Map.positionZ * miniMapScale);
Dictionary<Rect, List<Vector2>> storiesAreas;
Rect storyArea;
foreach (Building b in Engine.Map.buildings)
{
storiesAreas = new Dictionary<Rect, List<Vector2>>();
foreach (Story s in b.stories)
{
storyArea = new Rect(s.positionX, s.positionZ, s.width, s.height);
if (storiesAreas.ContainsKey(storyArea))
{
storiesAreas[storyArea].Add(new Vector2(s.positionY, s.floorHeight));
}
else
{
List<Vector2> verticalPosition = new List<Vector2>();
verticalPosition.Add(new Vector2(s.positionY, s.floorHeight));
storiesAreas.Add(storyArea, verticalPosition);
}
}
foreach (KeyValuePair<Rect, List<Vector2>> kvp in storiesAreas)
{
bool nextStoryFound = false;
while (kvp.Value.Count > 0)
{
nextStoryFound = false;
float index = float.MinValue;
if (!nextStoryFound)
{
float minPosition = float.MaxValue;
//finding the lower position
foreach (Vector2 verticalPosition in kvp.Value)
{
if (verticalPosition[0] < minPosition) minPosition = verticalPosition[0];
}
Debug.Log("The lower position = " + minPosition);
//removing the lower story
for (int cnt = kvp.Value.Count - 1; cnt >= 0; cnt--)
{
if (kvp.Value[cnt][0] == minPosition)
{
index = kvp.Value[cnt][0] + kvp.Value[cnt][1];
kvp.Value.RemoveAt(cnt);
nextStoryFound = true;
break;
}
}
//finding the next story and removing it
while (nextStoryFound && kvp.Value.Count > 0)
{
for (int cnt = kvp.Value.Count - 1; cnt >= 0; cnt--)
{
if (Mathf.Approximately(kvp.Value[cnt][0], index))
{
index = kvp.Value[cnt][0] + kvp.Value[cnt][1];
kvp.Value.RemoveAt(cnt);
nextStoryFound = true;
break;
}
if (cnt == 0)
{
nextStoryFound = false;
}
}
}
//Create the cube for this group of stories
GameObject storiesGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
storiesGO.transform.localScale = new Vector3(kvp.Key.width * miniMapScale, (index - minPosition) * miniMapScale, kvp.Key.height * miniMapScale);
storiesGO.transform.position = new Vector3((Engine.Map.positionX + b.positionX + kvp.Key.x) * miniMapScale + storiesGO.transform.localScale.x * 0.5f,
(b.elevation + minPosition) * miniMapScale + storiesGO.transform.localScale.y * 0.5f,
-(Engine.Map.positionZ + b.positionZ + kvp.Key.y) * miniMapScale - storiesGO.transform.localScale.z * 0.5f);
storiesGO.transform.parent = miniMap.transform;
}
}
}
}
}

HoleX1 in a room

© 2017 Magyc Pixel. All rights reserved | Design by W3layouts