コード例 #1
0
ファイル: Object.cs プロジェクト: Grumpy-Kat/3DBuildingGame
    /// <summary>
    /// updates the info of the object, and it's surrounding tiles,
    /// and checks to make sure the position that the object is attempting to be placed at is valid
    /// </summary>
    /// <param name="obj">Object that is trying to be moved</param>
    /// <param name="pos">position that the object is trying to be placed at</param>
    /// <param name="rotLevel">rotLevel, which helps to determine the position and rotation of the object</param>
    /// <param name="moveNeighbors">moveNeighbors, defaults to true and determines whether the Object's neighbors should be moved</param>
    public static void MoveObject(Object obj, Vector3 pos, int rotLevel, bool moveNeighbors = true)
    {
        switch (obj.type)
        {
        case ObjType.Wall:
            Wall.MoveObject((Wall)obj, pos, rotLevel, moveNeighbors);
            break;

        case ObjType.Table:
            Table.MoveObject((Table)obj, pos, rotLevel, moveNeighbors);
            break;

        case ObjType.Chair:
            Chair.MoveObject((Chair)obj, pos, rotLevel, moveNeighbors);
            break;

        default:
            BuildModeController bmc = WorldController.Instance.MouseController.bmc;
            Tile prevT = WorldController.Instance.World.GetTileAt(Mathf.RoundToInt(obj.pos.x), Mathf.RoundToInt(obj.pos.z), bmc.ActiveFloor);
            Tile newT  = WorldController.Instance.World.GetTileAt(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.z), bmc.ActiveFloor);
            if (moveNeighbors == true && obj.connectsToNeighbors == true)
            {
                foreach (Object n in obj.neighbors)
                {
                    Vector3 nPos  = pos - (obj.pos - n.pos);
                    Tile    nNewT = WorldController.Instance.World.GetTileAt(Mathf.RoundToInt(nPos.x), Mathf.RoundToInt(nPos.z), bmc.ActiveFloor);
                    foreach (Object otherObj in nNewT.objs)
                    {
                        if (otherObj.pos == nPos && otherObj != n && n.neighbors.Contains(otherObj) == false)
                        {
                            Debug.LogError("MoveObject: Trying to place an Object where there is already one");
                            return;
                        }
                    }
                    if (nNewT.Type == TileType.Empty)
                    {
                        Debug.LogError("MoveObject: Trying to assign an Object to an empty tile!");
                        return;
                    }
                }
            }
            else
            {
                foreach (Object otherObj in newT.objs)
                {
                    if (otherObj.pos == pos && otherObj != obj && obj.neighbors.Contains(otherObj) == false)
                    {
                        Debug.LogError("MoveObject: Trying to place an Object where there is already one");
                        return;
                    }
                }
                if (newT.Type == TileType.Empty)
                {
                    Debug.LogError("MoveObject: Trying to assign an Object to an empty tile!");
                    return;
                }
            }
            if (prevT.objs.Contains(obj) == true)
            {
                prevT.objs.Remove(obj);
            }
            if (obj.tiles.Contains(prevT) == true)
            {
                obj.tiles.Remove(prevT);
            }
            Vector3 diffPos = pos - obj.pos;
            diffPos.y   = 0;
            obj.diffPos = diffPos;
            //obj.pos = new Vector3(newT.x,obj.defaultY + (2.75f * bmc.ActiveFloor),newT.y);
            obj.pos = new Vector3(newT.x, obj.defaultY, newT.y);
            if (obj.connectsToNeighbors == true && Object.moveNeighbors == true && moveNeighbors == true)
            {
                obj.MoveNeighbors(obj);
            }
            Quaternion rot = Quaternion.identity;
            rot.eulerAngles = new Vector3(0f, 90f * rotLevel, 0f);
            obj.rot         = rot;
            newT.objs.Add(obj);
            obj.tiles.Add(newT);
            if (obj.cbObjectChanged != null)
            {
                obj.cbObjectChanged(obj);
            }
            break;
        }
    }