Building Engine for Unity3D - Coding conventions

Create a map

Engine.Map = Engine.designer.CreateMap([positionX = 0f, positionZ = 0f]);

Add a building to the map

Building building = Engine.designer.AddBuilding(Engine.Map, [positionX = 0f, positionZ = 0f, elevation = 0f]);

Or add it with a story

Building building = Engine.designer.AddBuildingWithStory(Engine.Map, storyWidth, storyHeight, floorHeight, [positionX = 0f, positionZ = 0f, elevation = 0f, roofStyleName = ""]);

Remove a building from the map

Engine.designer.RemoveBuilding(building);

Add a story to a building

Story story = Engine.designer.AddStory(building, width, height, floorHeight, [positionX = 0f, positionY = 0f, positionZ = 0f, roofStyleName = ""]);

Remove a story from a building

Engine.designer.RemoveStory(story);

Add a room to a story

Room room = Engine.designer.AddRoom(story, width, height, [positionX = 0f, positionZ = 0f, wallMaterial = null, floorMaterial = null, ceilMaterial = null, wallU = 0f, wallV = 0f, floorU = 0f, floorV = 0f, ceilU = 0f, ceilV = 0f]);

Remove a wall in a room

This method automatically remove the corresponding wall in adjacent room. To get the nowall value to supply : if removing wall at NORTH, add 1. If removing wall at EAST, add 2, if removing wall at SOUTH, add 4, if removing wall at WEST, add 8. A room with all four walls have a nowall value of 0. A room without walls have a nowall value of 15 (1 + 2 + 4 + 8).

Engine.designer.ChangeWallsInRoom(room, nowall);

Add a wall in a room (after removing)

Same as above, but sustract the value to nowall instead of adding it.

Remove a room from a story

Engine.designer.RemoveRoom(room);

Add a portal between two rooms

A portal is described by its direction (the wall of the room where it's located), its position along the wall and its selected shape (ie door, window, hole, doorX2...). The room where it is created is the 'entry room'.

//First, calculate the position of the exit in Building Engine coordinates :
Vector3 exit = Engine.utils.GetPositionFromPortalExit(direction, position, entryRoom);
//Next, find the room at exit position :
Story exitStory = Engine.utils.FindStoryAtPosition(entryRoom.story.building.map, exit);
if (exitStory != null){
 Room exitRoom = Engine.utils.FindRoomAtPosition(entryRoom.story.building.map, exit);
 if(exitRoom !=null)
 {
  //Calculate the position of the portal in the exitRoom
  float exitPosition = Engine.utils.GetPositionInExitRoom(direction, position, entryRoom, exitRoom);
  //Verify that the portal is not too wide for the exitRoom
  if (Engine.utils.IsPortalNotTooWideForExitRoom(direction, exitPosition, exitRoom, Engine.builder.architect.portalsInfos[selectedShape].width))
  {
   //create the portal
   Portal portal = Engine.designer.AddPortalBetweenRooms(entryRoom.story.building, entryRoom, direction, position, Engine.builder.architect.portalsInfos[selectedShape], exitRoom, exitPosition);
  }
 }
}

If the design is safe, you can add the portal without verifications. No need to calculate the exit position nor the exitRoom as the method do it for you :

Portal portal = Engine.designer.AddPortalBetweenRooms(entryRoom.story.building, entryRoom, direction, position, Engine.builder.architect.portalsInfos[selectedShape], [exitRoom = null, exitPosition = -1]);

Add a portal to the outside

//First, calculate the position of the exit in Building Engine coordinates :
Vector3 exit = Engine.utils.GetPositionFromPortalExit(direction, position, entryRoom);
Story exitStory = Engine.utils.FindStoryAtPosition(entryRoom.story.building.map, exit);
if(exitStory == null)
{
 //can the portal be drawn on a facade (ie none other facade block it)
 if (Engine.utils.CanPortalBeDrawnOnFacade(entryRoom.story.building, entryRoom, direction, position, Engine.builder.architect.portalsInfos[selectedShape]))
 {
  //add the portal
  Portal portal = Engine.designer.AddPortalToOutdoor(entryRoom.story.building, entryRoom, direction, position, Engine.builder.architect.portalsInfos[selectedShape]);
 }
}

If the design is safe, you can add the portal without verifications :

Portal portal = Engine.designer.AddPortalToOutdoor(entryRoom.story.building, entryRoom, direction, position, Engine.builder.architect.portalsInfos[selectedShape]);

Remove a portal

Engine.designer.RemovePortal(portal);

Add a vertical portal

The entryRoom is the lower room. The exitRoom is the upper room.

//First we calculate the exit position
Vector3 exit = Engine.utils.GetPositionFromVerticalPortalExit(entryRoom);
//Getting the exitRoom
Story exitStory = Engine.utils.FindStoryAtPosition(entryRoom.story.building.map, exit);
if(exitStory != null)
{
 Room exitRoom = Engine.utils.FindRoomAtPosition(exitStory, exit);
 if(entryRoom != null)
 {
  //check if the portal can be drawn in the exit room floor
  Rect plane = new Rect();
  plane.x = exitRoom.positionX + exitStory.positionX;
  plane.width = exitRoom.width;
  plane.y = exitRoom.positionZ + exitStory.positionZ;
  plane.height = exitRoom.height;
  Rect hole = new Rect();
  hole.x = room.positionX + room.story.positionX;
  hole.width = room.width;
  hole.y = room.positionZ + room.story.positionZ;
  hole.height = room.height;
  if (Engine.utils.IsRect1ContainsRect2(plane, hole))
  {
   //adding the vertical portal
   VerticalPortal verticalPortal = Engine.designer.AddVerticalPortal(entryRoom.story.building, entryRoom, Engine.builder.architect.GetStoryHeight(entryRoom.story), exitRoom);
  }
 }
}

If the design is safe, you can add the vertical portal without verifications. No need to find the exitRoom as the method find it for you :

VerticalPortal verticalPortal = Engine.designer.AddVerticalPortal(entryRoom.story.building, entryRoom, Engine.builder.architect.GetStoryHeight(entryRoom.story), [exitRoom = null]);

Remove a vertical portal

Engine.designer.RemoveVerticalPortal(verticalPortal);

Add a prop to the map

Prop prop = Engine.designer.AddPropToMap(Engine.Map, propName, positionX, positionY, positionZ, rotation);

Remove a prop from the map

Engine.designer.RemovePropFromMap(Engine.Map, prop);

Add a prop to a building

Prop prop = Engine.designer.AddPropToBuilding(building, propName, positionX, positionY, positionZ, rotation);

Remove a prop from a building

Engine.designer.RemovePropFromBuilding(building, prop);

Add a prop to a story

Prop prop = Engine.designer.AddPropToStory(story, propName, positionX, positionY, positionZ, rotation);

Remove a prop from a story

Engine.designer.RemovePropFromStory(story, prop);

Add a prop to a room

Prop prop = Engine.designer.AddPropToRoom(room, propName, positionX, positionY, positionZ, rotation);

Remove a prop from a room

Engine.designer.RemovePropFromRoom(room, prop);

Add a prop to a portal

Prop prop = Engine.designer.AddPropToPortal(portal, propName, positionX, positionY, positionZ, rotation);

Remove a prop from a portal

Engine.designer.RemovePropFromPortal(portal, prop);

Add a prop to a vertical portal

Prop prop = Engine.designer.AddPropToVerticalPortal(verticalPortal, propName, positionX, positionY, positionZ, rotation);

Save map in XML format

string s = Engine.encoder.EncodeMapToXML(Engine.Map).OuterXml;
if (!AssetDatabase.IsValidFolder("Assets/saves")) AssetDatabase.CreateFolder("Assets", "saves");
File.WriteAllText(Application.dataPath + "/saves/" + assetName + ".xml", s);
AssetDatabase.Refresh();

Load map from XML save

string s = File.ReadAllText("Assets/saves/" + assetPath + ".xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlElement mapXML = doc.DocumentElement;
Engine.Map = Engine.decoder.DecodeToMap(mapXML);
//recalculate buildings, stories, rooms, poratls and vertical portals indexes
Engine.designer.SetHigherIndexes(Engine.Map);

Build map

Engine.Build(Engine.Map);

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