Пример #1
0
    void CmdSpawnBuildingPart(string objectString, int objectID, Vector3 position, Quaternion rotation, int objectIndex, int inTeam,
                              int parentMountPoint, int parentMountBool)
    {
        Debug.Log("Placing " + objectString + "With Object ID:" + currentObject + " at X:" + position.x + "; Y:" + position.y + "; Z:" + position.z);
        GameObject      tempObj = objects[objectID];
        BuildIdentifier tempID  = tempObj.GetComponent <BuildIdentifier>();

        if (tempObj.transform.childCount > 0)
        {
            for (int counter = 0; counter < tempObj.transform.childCount; counter++)
            {
                tempObj.transform.GetChild(counter).gameObject.GetComponent <BoxCollider>().enabled = true;
            }
        }
        else
        {
            tempObj.GetComponent <BoxCollider>().enabled = true;
        }
        tempID.team             = inTeam;
        tempID.id               = objectIndex;
        tempID.parentMountPoint = parentMountPoint;
        tempID.parentMountBool  = parentMountBool;
        GameObject instance = Instantiate(tempObj, position, rotation);

        NetworkServer.Spawn(instance);
    }
Пример #2
0
    void SetpreviewObjectObject(int id)
    {
        BuildIdentifier bid = objects[id].GetComponent <BuildIdentifier>();
        ResourceBank    rba = team.baseObject.GetComponent <ResourceBank>();

        if (bid.woodCost <= rba.wood && bid.stoneCost <= rba.stone && bid.metalCost <= rba.metal)
        {
            if (previewObject != null)
            {
                Destroy(previewObject);
            }
            currentObject = id;
            previewObject = Instantiate(objects[currentObject], baseParent);

            if (previewObject.transform.childCount > 0)
            {
                for (int counter = 0; counter < previewObject.transform.childCount; counter++)
                {
                    previewObject.transform.GetChild(counter).gameObject.GetComponent <BoxCollider>().enabled = false;
                }
            }
            else
            {
                previewObject.GetComponent <BoxCollider>().enabled = false;
            }
            meshRend = previewObject.GetComponent <MeshRenderer>();
            SaveMaterials();
            SetMaterial(invalidMaterial);
            previewObject.layer = 2;
            previewBuildPoints  = previewObject.GetComponent <BuildPoints>();
            previewObject.transform.localEulerAngles = new Vector3(0, 0, 0);
        }
    }
Пример #3
0
 void PlaceObject()
 {
     if (previewBuildPoints.valid)
     {
         //previewObject.name = (previewBuildPoints.type.ToString() + " Placed");
         BuildPoints bp         = previewBuildPoints;
         int         mountIndex = baseBuildings.mountPoints.Count;
         bp.mounting.SetTeam(team.team);
         bp.mounting.SetObjectID(mountIndex);
         CmdAddMountPoint(bp.mounting, team.team);
         SyncStructBool tempBools;
         tempBools.boolList = new bool[0];
         foreach (Vector3 v3 in bp.mounting.points)
         {
             bool used = false;
             tempBools.Add(used);
         }
         CmdAddPointUsed(tempBools, team.team);
         ResetMaterials();
         CmdAddPlacedObject(previewObject.transform.position - previewBuildPoints.offset, team.team);
         previewObject.layer = 0;
         Vector3    poPosition = previewObject.transform.position;
         Quaternion poRotation = previewObject.transform.rotation;
         Destroy(previewObject);
         CmdSpawnBuildingPart(objects[currentObject].ToString(), currentObject, poPosition,
                              poRotation, mountIndex, team.team, snapMountIndex, snapBoolIndex);
         BuildIdentifier bid = objects[currentObject].GetComponent <BuildIdentifier>();
         CmdChangeResources(-bid.woodCost, -bid.stoneCost, -bid.metalCost, team.team);
         previewObject = null;
         meshRend      = null;
     }
 }
Пример #4
0
    void SnapPreview()
    {
        bool snapped   = false;
        int  mpCounter = 0;

        foreach (MountPoint mp in baseBuildings.mountPoints)
        {
            GameObject[] tempObjs = GameObject.FindGameObjectsWithTag("Building");
            GameObject   tempObj  = null;
            foreach (GameObject currentObj in tempObjs)
            {
                BuildIdentifier tempBuild = currentObj.GetComponent <BuildIdentifier>();
                if (tempBuild.id == mp.objectID && tempBuild.team == mp.team)
                {
                    tempObj = currentObj;
                    break;
                }
            }
            for (int j = 0; j < mp.pointType.Length; j++)
            {
                if (mp.pointType[j] == previewBuildPoints.type)
                {
                    Vector3 v3 = mp.points[j];
                    if (!(baseBuildings.pointUsed[mpCounter].boolList[j]))
                    {
                        Vector3 newVec3 = tempObj.transform.TransformPoint(new Vector3(v3.x / (tempObj.transform.localScale.x), v3.y / (tempObj.transform.localScale.y), v3.z / (tempObj.transform.localScale.z)));
                        //Matrix4x4 m = Matrix4x4.TRS(mp.parentPosition, mp.parentRotation, mp.parentScale);
                        //Vector3 newVec3 = m.MultiplyPoint3x4(new Vector3(v3.x / mp.parentScale.x, v3.y / mp.parentScale.y, v3.z / mp.parentScale.z));
                        Debug.DrawRay(newVec3, Vector3.up, Color.red);

                        bool colliding = false;
                        if (previewBuildPoints.type == BuildPoints.MountType.Floor1 || previewBuildPoints.type == BuildPoints.MountType.Ceiling1)
                        {
                            Vector3 radius = newVec3 - tempObj.transform.position;
                            radius.x = radius.x / 2f;
                            radius.z = radius.z / 2f;
                            Vector3 perp1   = new Vector3(radius.z * .95f, 2, -radius.x * .95f);
                            Vector3 perp2   = -perp1;
                            Vector3 corner1 = new Vector3(tempObj.transform.position.x + radius.x * 1.05f + perp1.x, 2,
                                                          tempObj.transform.position.z + radius.z * 1.05f + perp1.z);
                            Vector3 corner2 = new Vector3(tempObj.transform.position.x + radius.x * 1.05f + perp2.x, 2,
                                                          tempObj.transform.position.z + radius.z * 1.05f + perp2.z);
                            Vector3 corner3 = new Vector3(corner2.x + radius.x * 2 * .95f, 2, corner2.z + radius.z * 2 * .95f);
                            Vector3 corner4 = new Vector3(corner1.x + radius.x * 2 * .95f, 2, corner1.z + radius.z * 2 * .95f);
                            Debug.DrawRay(corner1, Vector3.up, Color.cyan);
                            Debug.DrawRay(corner2, Vector3.up, Color.cyan);
                            Debug.DrawRay(corner3, Vector3.up, Color.cyan);
                            Debug.DrawRay(corner4, Vector3.up, Color.cyan);
                            colliding = DetermineCollision(corner1, corner2) ||
                                        DetermineCollision(corner2, corner3) ||
                                        DetermineCollision(corner3, corner4) ||
                                        DetermineCollision(corner4, corner1);
                        }
                        if (!colliding)
                        {
                            if (Vector3.Distance(previewObject.transform.position, newVec3) < 2)
                            {
                                bool near = false;
                                foreach (Vector3 nearV3 in baseBuildings.placedObjects)
                                {
                                    if (Vector3.Distance(nearV3, newVec3) < 0.1f)
                                    {
                                        near = true;
                                    }
                                }
                                if (!near)
                                {
                                    snapMountIndex = mpCounter;
                                    snapBoolIndex  = j;
                                    snapped        = true;
                                    previewObject.transform.position = newVec3 + previewBuildPoints.offset;
                                    previewObject.transform.LookAt(tempObj.transform);
                                    previewObject.transform.localEulerAngles = new Vector3(0, previewObject.transform.localEulerAngles.y - 90, 0);
                                    if (previewBuildPoints.type == mp.parentMountType)
                                    {
                                        previewObject.transform.localEulerAngles = tempObj.transform.localEulerAngles;
                                    }
                                    if (previewBuildPoints.type == BuildPoints.MountType.Stair1)
                                    {
                                        Matrix4x4 tempMatrix = Matrix4x4.identity;
                                        tempMatrix[0, 0] = Mathf.Cos(-tempObj.transform.localEulerAngles.y * Mathf.Deg2Rad);
                                        tempMatrix[0, 2] = -Mathf.Sin(-tempObj.transform.localEulerAngles.y * Mathf.Deg2Rad);
                                        tempMatrix[2, 0] = Mathf.Sin(-tempObj.transform.localEulerAngles.y * Mathf.Deg2Rad);
                                        tempMatrix[2, 2] = Mathf.Cos(-tempObj.transform.localEulerAngles.y * Mathf.Deg2Rad);
                                        Vector3 localCorner1 = new Vector3(tempObj.transform.localScale.x / 2, 0, tempObj.transform.localScale.z / 2);
                                        Vector3 localCorner2 = new Vector3(tempObj.transform.localScale.x / 2, 0, -tempObj.transform.localScale.z / 2);
                                        Vector3 localCorner3 = new Vector3(-tempObj.transform.localScale.x / 2, 0, -tempObj.transform.localScale.z / 2);
                                        Vector3 localCorner4 = new Vector3(-tempObj.transform.localScale.x / 2, 0, tempObj.transform.localScale.z / 2);
                                        localCorner1 = tempMatrix * localCorner1;
                                        localCorner2 = tempMatrix * localCorner2;
                                        localCorner3 = tempMatrix * localCorner3;
                                        localCorner4 = tempMatrix * localCorner4;
                                        Vector3 worldCorner1 = tempObj.transform.position + localCorner1;
                                        Vector3 worldCorner2 = tempObj.transform.position + localCorner2;
                                        Vector3 worldCorner3 = tempObj.transform.position + localCorner3;
                                        Vector3 worldCorner4 = tempObj.transform.position + localCorner4;
                                        float   distCorner1  = (worldCorner1 - gameObject.transform.position).magnitude;
                                        float   distCorner2  = (worldCorner2 - gameObject.transform.position).magnitude;
                                        float   distCorner3  = (worldCorner3 - gameObject.transform.position).magnitude;
                                        float   distCorner4  = (worldCorner4 - gameObject.transform.position).magnitude;
                                        if (distCorner1 < distCorner3 && distCorner1 < distCorner4 && distCorner2 < distCorner3 && distCorner2 < distCorner4)
                                        {
                                            previewObject.transform.localEulerAngles = new Vector3(0, tempObj.transform.localEulerAngles.y + 90, 0);
                                        }
                                        else if (distCorner2 < distCorner4 && distCorner2 < distCorner1 && distCorner3 < distCorner4 && distCorner3 < distCorner1)
                                        {
                                            previewObject.transform.localEulerAngles = new Vector3(0, tempObj.transform.localEulerAngles.y + 180, 0);
                                        }
                                        else if (distCorner3 < distCorner1 && distCorner3 < distCorner2 && distCorner4 < distCorner1 && distCorner4 < distCorner2)
                                        {
                                            previewObject.transform.localEulerAngles = new Vector3(0, tempObj.transform.localEulerAngles.y + 270, 0);
                                        }
                                        else
                                        {
                                            previewObject.transform.localEulerAngles = new Vector3(0, tempObj.transform.localEulerAngles.y, 0);
                                        }
                                    }
                                    if (previewBuildPoints.type == BuildPoints.MountType.Wall &&
                                        mp.parentMountType == BuildPoints.MountType.Door1)
                                    {
                                        previewObject.transform.localEulerAngles = tempObj.transform.localEulerAngles;
                                    }
                                    if (previewBuildPoints.type == BuildPoints.MountType.Door1 &&
                                        mp.parentMountType == BuildPoints.MountType.Wall)
                                    {
                                        previewObject.transform.localEulerAngles = tempObj.transform.localEulerAngles;
                                    }
                                    SetMaterial(validMaterial);
                                    previewBuildPoints.valid = true;
                                }
                            }
                        }
                    }
                }
            }
            mpCounter++;
        }
        if (!snapped && snapMountIndex > -1)
        {
            snapMountIndex = -1;
            snapBoolIndex  = -1;
        }
    }