Example #1
0
    protected void DrawQuad(MapController mapController, AssetCellData cellData)
    {
        if (isPress)
        {
            Vector3 curMousePos = Input.mousePosition;

            //Get cell index
            int curXIndex = -1;
            int curZIndex = -1;
            MapUtility.CalCellIndexByMousePosition(curMousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref curXIndex, ref curZIndex);

            int lastXIndex = -1;
            int lastZIndex = -1;
            MapUtility.CalCellIndexByMousePosition(mousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref lastXIndex, ref lastZIndex);

            //Get cell position
            Vector3 curPos  = mapController.GetCellPosition(curXIndex, curZIndex);
            Vector3 lastPos = mapController.GetCellPosition(lastXIndex, lastZIndex);

            //Calulate squre ui's position
            Vector3 centerPos = (curPos + lastPos) * 0.5f;
            centerPos = new Vector3(centerPos.x, cellData.Size.y * 0.5f, centerPos.z + 0.1f);
            Vector3 len = (curPos - lastPos).AbsValue() + Vector3.one;
            len = new Vector3(len.x, cellData.Size.y, len.z);

            squareUI.transform.position   = centerPos;
            squareUI.transform.localScale = len;
            squareUI.SetActive(true);
        }
        else
        {
            squareUI.SetActive(false);
        }
    }
Example #2
0
    bool updateGoPosition(GameObject go, bool isForceUpdate)
    {
        int xIndex = -1;
        int zIndex = -1;

        MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ,
                                               rayCastLayer, ref xIndex, ref zIndex);

        if ((xIndex >= 0 && zIndex >= 0 && xIndex < mapController.MapSizeX && zIndex < mapController.MapSizeZ &&
             (isForceUpdate || xIndex != curCellIndexX || zIndex != curCellIndexZ)))
        {
            go.transform.position = MapUtility.CalCellPosition(mapController.MapDataCollection,
                                                               MapUtility.CalTopLeftCellPosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ),
                                                               xIndex, zIndex, curCellData.Size);
            go.transform.position = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, curCellData, go.transform.position);
            curCellIndexX         = xIndex;
            curCellIndexZ         = zIndex;
            return(true);
        }

        return(false);
    }
Example #3
0
    public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List <MapIndex> mapIndexList)
    {
        mapIndexList.Clear();
        base.Action(prefabName, cellGo, cellData, dataMode, isObstacle, mapIndexList);

        if (string.IsNullOrEmpty(prefabName))
        {
            return;
        }

        if (dataMode == DataMode.ERASE)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            isPress  = true;
            mousePos = Input.mousePosition;
        }

        if (isPress && Input.GetMouseButtonUp(0))
        {
            isPress = false;
            Vector3 curMousePos = Input.mousePosition;

            if (curMousePos != mousePos)
            {
                //Get cell index
                int curXIndex = -1;
                int curZIndex = -1;
                MapUtility.CalCellIndexByMousePosition(curMousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastLayer, ref curXIndex, ref curZIndex);

                int lastXIndex = -1;
                int lastZIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastLayer, ref lastXIndex, ref lastZIndex);

                if (curXIndex < lastXIndex)
                {
                    Swap(ref curXIndex, ref lastXIndex);
                }

                if (curZIndex < lastZIndex)
                {
                    Swap(ref curZIndex, ref lastZIndex);
                }

                if (isInTheMap(curXIndex, curZIndex, mapController) && isInTheMap(lastXIndex, lastZIndex, mapController))
                {
                    //calculate the number of the cells that we can build
                    int xCount = (int)((curXIndex - lastXIndex + 1) / cellData.Size.x);
                    int zCount = (int)((curZIndex - lastZIndex + 1) / cellData.Size.z);
                    if (xCount > 0 && zCount > 0)
                    {
                        //because the cell chosen by the mouse does not point to the center of the cell, so we need to calclute the index to the centero of the cell
                        int startXIndex = lastXIndex + ((int)Mathf.Abs(cellData.Size.x - 1));
                        int startZIndex = lastZIndex + ((int)Mathf.Abs(cellData.Size.z - 1));

                        for (int i = 0; i < xCount; i++)
                        {
                            int xIndex = startXIndex + i * (int)cellData.Size.x;
                            for (int j = 0; j < zCount; j++)
                            {
                                int   zIndex = startZIndex + j * (int)cellData.Size.z;
                                float yPos   = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData);

                                if (dataMode == DataMode.ADD)
                                {
                                    if (!string.IsNullOrEmpty(prefabName))
                                    {
                                        if (!isObstacle)
                                        {
                                            mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                         cellData.Size, cellGo.transform.rotation);
                                        }
                                        else
                                        {
                                            mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                         cellData.Size, cellGo.transform.rotation, UnitType.OBSTACLE);
                                        }
                                    }
                                    else
                                    {
                                        patternImporter.BuildPattern(mapController, xIndex, zIndex, yPos, cellGo.transform.rotation, isObstacle);
                                    }

                                    AddMapIndex(xIndex, zIndex, mapIndexList);
                                }
                                else if (dataMode == DataMode.CAN_MOVE || dataMode == DataMode.CAN_NOT_MOVE)
                                {
                                    bool canMove = dataMode == DataMode.CAN_MOVE ? true : false;
                                    if (mapController.SetCellMovable(xIndex, zIndex, canMove))
                                    {
                                        AddMapIndex(xIndex, zIndex, mapIndexList);
                                    }
                                }
                                else if (dataMode == DataMode.ADD_PLAYER || dataMode == DataMode.ERASE_PLAYER || dataMode == DataMode.ADD_ENEMY || dataMode == DataMode.ERASE_ENEMY)
                                {
                                    AddMapIndex(xIndex, zIndex, mapIndexList);
                                }
                            }
                        }
                    }
                }
            }
        }

        DrawQuad(mapController, cellData);
    }
Example #4
0
    //cellGo -> default gameObject
    //prefabName -> Map Connection prefab
    public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List <MapIndex> mapIndexList)
    {
        mapIndexList.Clear();
        base.Action(prefabName, cellGo, cellData, dataMode, isObstacle, mapIndexList);

        if (Input.GetMouseButton(0))
        {
            if (dataMode == DataMode.ADD)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    int id = MapUtility.CoordinateToId(xIndex, zIndex, mapController.MapSizeX);
                    if (!buildMapId.Contains(id))
                    {
                        if (!isPress)
                        {
                            if (totalBuildMapId.ContainsKey(id))
                            {
                                buildHeight = totalBuildMapId[id];
                            }
                            else
                            {
                                buildHeight = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData);
                            }
                        }

                        if (mapController.MapDataCollection.CanBuildOnTheMap(xIndex, zIndex, buildHeight, cellData.Size) || totalBuildMapId.ContainsKey(id))
                        {
                            buildMapId.Add(id);

                            //Draw dummy object
                            GameObject dummy = Object.Instantiate(dummyObject) as GameObject;
                            dummy.transform.parent = dummyParent.transform;
                            Vector3 pos = mapController.GetCellPosition(xIndex, zIndex);
                            dummy.transform.position = new Vector3(pos.x, buildHeight, pos.z);

                            isPress = true;
                        }
                    }
                }
            }
            else if (dataMode == DataMode.ERASE)
            {
                GameObject hitObject = MapUtility.GetRayCastMapObjectByMousePosition(rayCastLayer);
                if (hitObject != null)
                {
                    AssetCellData cd = hitObject.GetComponent <AssetCellData>();
                    mapController.EraseCellData(cd);
                    totalBuildMapId.Remove(cd.MapObj.Id);
                }
            }
        }
        else if (Input.GetMouseButtonUp(0) && isPress)
        {
            if (buildMapId.Count == 0)
            {
                return;
            }

            MapConnection mapConn = (Resources.Load(MapSetting.MAP_CONNECT_TILE_FOLDER_NAME + prefabName.GetPathWidthoutExtension()) as GameObject).GetComponent <MapConnection>();
            checkIfNeedReBuild(mapController);

            List <MapDir> neighBorList = new List <MapDir>();
            foreach (var item in buildMapId)
            {
                int curXIndex = -1;
                int curZIndex = -1;
                MapUtility.IdToCoordinate(item, mapController.MapSizeX, ref curXIndex, ref curZIndex);

                getNeighborDir(curXIndex, curZIndex, mapController, neighBorList);
                Quaternion rot = Quaternion.identity;
                string     p   = getPrefabName(neighBorList, cellGo, mapConn, ref rot);

                if (!isObstacle)
                {
                    mapController.UpdateCellData(p, curXIndex, curZIndex, buildHeight, cellData.Size, rot);
                }
                else
                {
                    mapController.UpdateCellData(p, curXIndex, curZIndex, buildHeight, cellData.Size, rot, UnitType.OBSTACLE);
                }
                AddMapIndex(curXIndex, curZIndex, mapIndexList);

                totalBuildMapId[item] = buildHeight;
            }

            buildMapId.Clear();
            isPress = false;
            for (int i = dummyParent.transform.childCount - 1; i >= 0; i--)
            {
                Object.Destroy(dummyParent.transform.GetChild(i).gameObject);
            }
        }
    }
Example #5
0
    public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List <MapIndex> mapIndexList)
    {
        mapIndexList.Clear();
        base.Action(prefabName, cellGo, cellData, dataMode, isObstacle, mapIndexList);
        if (Input.GetMouseButton(0))
        {
            if (dataMode == DataMode.ADD)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    int id = MapUtility.CoordinateToId(xIndex, zIndex, mapController.MapSizeX);
                    if (!buildMapId.Contains(id))
                    {
                        float yPos = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData);
                        if (mapController.MapDataCollection.CanBuildOnTheMap(xIndex, zIndex, yPos, cellData.Size))
                        {
                            if (!string.IsNullOrEmpty(prefabName))
                            {
                                if (!isObstacle)
                                {
                                    mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                 cellData.Size, cellGo.transform.rotation);
                                }
                                else
                                {
                                    mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                                 cellData.Size, cellGo.transform.rotation, UnitType.OBSTACLE);
                                }
                            }
                            else
                            {
                                patternImporter.BuildPattern(mapController, xIndex, zIndex, yPos, cellGo.transform.rotation, isObstacle);
                            }

                            //Add id that the cell can not be builded
                            int leftX  = xIndex - ((int)Mathf.Abs(cellData.Size.x)) / 2;
                            int rightX = xIndex + ((int)Mathf.Abs(cellData.Size.x - 1)) / 2;
                            int downZ  = zIndex - ((int)Mathf.Abs(cellData.Size.z)) / 2;
                            int upZ    = zIndex + ((int)Mathf.Abs(cellData.Size.z - 1)) / 2;

                            for (int i = leftX; i <= rightX; i++)
                            {
                                for (int j = downZ; j <= upZ; j++)
                                {
                                    buildMapId.Add(MapUtility.CoordinateToId(i, j, mapController.MapSizeX));
                                }
                            }
                        }

                        AddMapIndex(xIndex, zIndex, mapIndexList);
                    }
                }
            }
            else if (dataMode == DataMode.ERASE)
            {
                GameObject hitObject = MapUtility.GetRayCastMapObjectByMousePosition(rayCastLayer);
                if (hitObject != null)
                {
                    mapController.EraseCellData(hitObject.GetComponent <AssetCellData>());
                }
            }
            else if (dataMode == DataMode.CAN_MOVE || (dataMode == DataMode.CAN_NOT_MOVE))
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    if (mapController.SetCellMovable(xIndex, zIndex, dataMode == DataMode.CAN_MOVE ? true : false))
                    {
                        AddMapIndex(xIndex, zIndex, mapIndexList);
                    }
                }
            }
            else if (dataMode == DataMode.ADD_PLAYER || dataMode == DataMode.ERASE_PLAYER || dataMode == DataMode.ADD_ENEMY || dataMode == DataMode.ERASE_ENEMY)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    AddMapIndex(xIndex, zIndex, mapIndexList);
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            buildMapId.Clear();
        }
    }
Example #6
0
 /// <summary>
 /// Raycast a cell and get the cell index
 /// </summary>
 /// <param name="xIndex"></param>
 /// <param name="zIndex"></param>
 /// <param name="layerMask"></param>
 public void GetCellByMousePosition(ref int xIndex, ref int zIndex, int layerMask = 1)
 {
     xIndex = zIndex = -1;
     MapUtility.CalCellIndexByMousePosition(CenterPosition, MapSizeX, MapSizeZ, layerMask, ref xIndex, ref zIndex);
 }
Example #7
0
    public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List<MapIndex> mapIndexList)
    {
        mapIndexList.Clear();

        if (string.IsNullOrEmpty(prefabName))
        {
            return;
        }

        if (dataMode == DataMode.ERASE)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            isPress = true;
            mousePos = Input.mousePosition;
        }

        if (isPress && Input.GetMouseButtonUp(0))
        {
            isPress = false;
            Vector3 curMousePos = Input.mousePosition;

            if (curMousePos != mousePos)
            {
                //Get cell index
                int curXIndex = -1;
                int curZIndex = -1;
                MapUtility.CalCellIndexByMousePosition(curMousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref curXIndex, ref curZIndex);

                int lastXIndex = -1;
                int lastZIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mousePos, mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref lastXIndex, ref lastZIndex);

                if (curXIndex < lastXIndex)
                {
                    Swap(ref curXIndex, ref lastXIndex);
                }

                if (curZIndex < lastZIndex)
                {
                    Swap(ref curZIndex, ref lastZIndex);
                }

                if (isInTheMap(curXIndex, curZIndex, mapController) && isInTheMap(lastXIndex, lastZIndex, mapController))
                {
                    List<GameObject> randomList = (Resources.Load(MapSetting.MAP_RANDOM_TILE_FOLDER_NAME + prefabName.GetPathWidthoutExtension()) as GameObject).GetComponent<MapRandomList>().GoList;
                    int randomCount = randomList.Count;
 
                    //calculate the number of the cells that we can build     
                    int xCount = (int)((curXIndex - lastXIndex + 1) / cellData.Size.x);
                    int zCount = (int)((curZIndex - lastZIndex + 1) / cellData.Size.z);
                    if (xCount > 0 && zCount > 0)
                    {
                        //because the cell chosen by the mouse does not point to the center of the cell, so we need to calclute the index to the centero of the cell 
                        int startXIndex = lastXIndex + ((int)Mathf.Abs(cellData.Size.x - 1));
                        int startZIndex = lastZIndex + ((int)Mathf.Abs(cellData.Size.z - 1));

                        for (int i = 0; i < xCount; i++)
                        {
                            int xIndex = startXIndex + i * (int)cellData.Size.x;
                            for (int j = 0; j < zCount; j++)
                            {
                                int zIndex = startZIndex + j * (int)cellData.Size.z;

                                int goIndex = Random.Range(0, randomCount);
                                AssetCellData cd = randomList[goIndex].GetComponent<AssetCellData>();
                                float yPos = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cd);

                                if (dataMode == DataMode.ADD)
                                {
                                    string p = getAssetPath(randomList[goIndex]);
                                    if (!isObstacle)
                                    {
                                        mapController.UpdateCellData(p, xIndex, zIndex, yPos, cd.Size, Quaternion.Euler(0f, Random.Range(0, 3) * 90f, 0f));
                                    }
                                    else
                                    {
                                        mapController.UpdateCellData(p, xIndex, zIndex, yPos, cd.Size, Quaternion.Euler(0f, Random.Range(0, 3) * 90f, 0f), UnitType.OBSTACLE);
                                    }
                                    AddMapIndex(xIndex, zIndex, mapIndexList);
                                }
                                else if (dataMode == DataMode.CAN_MOVE || dataMode == DataMode.CAN_NOT_MOVE)
                                {
                                    bool canMove = dataMode == DataMode.CAN_MOVE ? true : false;
                                    if (mapController.SetCellMovable(xIndex, zIndex, canMove))
                                    {
                                        AddMapIndex(xIndex, zIndex, mapIndexList);
                                    }
                                }
                                else if (dataMode == DataMode.ADD_PLAYER || dataMode == DataMode.ERASE_PLAYER || dataMode == DataMode.ADD_ENEMY || dataMode == DataMode.ERASE_ENEMY)
                                {
                                    AddMapIndex(xIndex, zIndex, mapIndexList);
                                }
                            }
                        }
                    }
                }
            }
        }

        DrawQuad(mapController, cellData);
    }
Example #8
0
    public override void Action(string prefabName, GameObject cellGo, AssetCellData cellData, DataMode dataMode, bool isObstacle, List <MapIndex> mapIndexList)
    {
        mapIndexList.Clear();
        base.Action(prefabName, cellGo, cellData, dataMode, isObstacle, mapIndexList);
        if (Input.GetMouseButtonDown(0))
        {
            if (dataMode == DataMode.ADD)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    float yPos = MapUtility.CalCellHeightPosition(mapController.MapDataCollection, xIndex, zIndex, cellData);

                    if (!string.IsNullOrEmpty(prefabName))
                    {
                        if (!isObstacle)
                        {
                            mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                         cellData.Size, cellGo.transform.rotation);
                        }
                        else
                        {
                            mapController.UpdateCellData(MapSetting.MAP_REFAB_FOLDER_NAME + prefabName.GetPathWidthoutExtension(), xIndex, zIndex, yPos,
                                                         cellData.Size, cellGo.transform.rotation, UnitType.OBSTACLE);
                        }
                    }
                    else
                    {
                        patternImporter.BuildPattern(mapController, xIndex, zIndex, yPos, cellGo.transform.rotation, isObstacle);
                    }

                    AddMapIndex(xIndex, zIndex, mapIndexList);
                }
            }
            else if (dataMode == DataMode.ERASE)
            {
                GameObject hitObject = MapUtility.GetRayCastMapObjectByMousePosition(rayCastLayer);
                if (hitObject != null)
                {
                    mapController.EraseCellData(hitObject.GetComponent <AssetCellData>());
                }
            }
            else if (dataMode == DataMode.CAN_MOVE || dataMode == DataMode.CAN_NOT_MOVE)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    if (mapController.SetCellMovable(xIndex, zIndex, dataMode == DataMode.CAN_MOVE ? true : false))
                    {
                        AddMapIndex(xIndex, zIndex, mapIndexList);
                    }
                }
            }
            else if (dataMode == DataMode.ADD_PLAYER || dataMode == DataMode.ERASE_PLAYER || dataMode == DataMode.ADD_ENEMY || dataMode == DataMode.ERASE_ENEMY)
            {
                int xIndex = -1;
                int zIndex = -1;
                MapUtility.CalCellIndexByMousePosition(mapController.CenterPosition, mapController.MapSizeX, mapController.MapSizeZ, rayCastMapLayer, ref xIndex, ref zIndex);
                if (xIndex >= 0 && zIndex >= 0)
                {
                    AddMapIndex(xIndex, zIndex, mapIndexList);
                }
            }
        }
    }