Beispiel #1
0
        public Grid(int width, int height, float cellSize, Vector3 originPosition, Func <Grid <TGridObject>, int, int, TGridObject> createGridObject)
        {
            this.width          = width;
            this.height         = height;
            this.cellSize       = cellSize;
            this.originPosition = originPosition;
            gridArray           = new TGridObject[width, height];
            debugTextArray      = new TextMesh[width, height];

            for (int x = 0; x < gridArray.GetLength(0); x++)
            {
                for (int y = 0; y < gridArray.GetLength(1); y++)
                {
                    gridArray[x, y] = createGridObject(this, x, y);
                }
            }

            bool showDebug = true;

            if (showDebug)
            {
                for (int x = 0; x < gridArray.GetLength(0); x++)
                {
                    for (int y = 0; y < gridArray.GetLength(1); y++)
                    {
                        debugTextArray[x, y] = LC_Debug.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorlPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 20, Color.red, TextAnchor.MiddleCenter).GetComponent <TextMesh>();
                        Debug.Log("Cell x: " + x + " y: " + y);
                        Debug.DrawLine(GetWorlPosition(x, y), GetWorlPosition(x, y + 1), Color.red, 100f);
                        Debug.DrawLine(GetWorlPosition(x, y), GetWorlPosition(x + 1, y), Color.red, 100f);
                    }
                }
                Debug.DrawLine(GetWorlPosition(0, height), GetWorlPosition(width, height), Color.red, 100f);
                Debug.DrawLine(GetWorlPosition(width, 0), GetWorlPosition(width, height), Color.red, 100f);

                OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs args) =>
                {
                    debugTextArray[args.x, args.y].text = gridArray[args.x, args.y]?.ToString();
                };
            }
        }
        void Update()
        {
            if (Input.GetMouseButtonDown(0) && placedObjectTypeSO != null)
            {
                Vector3 mousePosition = LC_Utils.GetMouseWorldPosition_Ray();
                grid.GetXZ(mousePosition, out int x, out int z);

                Vector2Int placedObjectOrigin = new Vector2Int(x, z);
                placedObjectOrigin = grid.ValidateGridPosition(placedObjectOrigin);
                List <Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionsList(new Vector2Int(x, z), dir);

                // Test
                bool canBuild = true;
                foreach (Vector2Int gridPosition in gridPositionList)
                {
                    Debug.Log("Search: " + gridPosition.x + "; " + gridPosition.y);
                    if (!grid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
                    {
                        Debug.Log("Cannot here!!");
                        // Cannot build here
                        canBuild = false;
                        break;
                    }
                }

                //GridObject gridObject = grid.GetGridObject(x, z);
                if (canBuild)
                {
                    Vector2Int rotationOffset            = placedObjectTypeSO.GetRotationOffset(dir);
                    Vector3    placedObjectWorldPosition = grid.GetWorldPosition(x, z) + new Vector3(rotationOffset.x, 0, rotationOffset.y) * grid.GetCellSize();

                    PlacedObject placedObject = PlacedObject.Create(placedObjectWorldPosition, new Vector2Int(x, z), dir, placedObjectTypeSO);

                    foreach (Vector2Int gridPosition in gridPositionList)
                    {
                        Debug.Log("Placed = x: " + gridPosition.x + "; " + gridPosition.y);
                        grid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
                    }

                    OnObjectPlaced?.Invoke(this, EventArgs.Empty);
                }
                else
                {
                    LC_Debug.CreateWorldTextPopup("Cannot build here!!", LC_Utils.GetMouseWorldPosition_Ray(), new Vector3(0, 1f));
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                GridObject gridObject = grid.GetGridObject(LC_Utils.GetMouseWorldPosition_Ray());
                if (gridObject != null)
                {
                    PlacedObject placedObject = gridObject.GetPlacedObject();

                    if (placedObject != null)
                    {
                        placedObject.DestroySelf();
                        List <Vector2Int> gridPositionList = placedObject.GetGridPositionsList();

                        foreach (Vector2Int gridPosition in gridPositionList)
                        {
                            grid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
                        }
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.R))
            {
                dir = PlacedObjectTypeSO.GetNextDir(dir);
            }

            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                placedObjectTypeSO = placedObjectTypeSOList[0]; RefreshSelectedObjectType();
            }
            if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                placedObjectTypeSO = placedObjectTypeSOList[1]; RefreshSelectedObjectType();
            }
            if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                placedObjectTypeSO = placedObjectTypeSOList[2]; RefreshSelectedObjectType();
            }

/*            if (Input.GetKeyDown(KeyCode.Alpha4)) { placedObjectTypeSO = placedObjectTypeSOList[3]; RefreshSelectedObjectType(); }
 *          if (Input.GetKeyDown(KeyCode.Alpha5)) { placedObjectTypeSO = placedObjectTypeSOList[4]; RefreshSelectedObjectType(); }*/
        }
    private void Start()
    {
        GameObject btnPanel = LC_Debug.CreateButtonPanel(Vector3.up);

        DebugButtonPanel.AddButton(btnPanel.GetComponent <DebugButtonPanel>(), "Test 1", () => LC_Debug.TextPopup("Test1", Vector3.up));
        DebugButtonPanel.AddButton(btnPanel.GetComponent <DebugButtonPanel>(), "Test 2", () => LC_Debug.TextPopup("Test2", Vector3.up));
        DebugButtonPanel.AddButton(btnPanel.GetComponent <DebugButtonPanel>(), "Test 3", () => LC_Debug.TextPopup("Test3", Vector3.up), out GameObject debugBtn);

        if (debugBtn != null)
        {
            debugBtn.GetComponent <DebugButton>().OnClickAction = () => { LC_Debug.TextPopup("Action replace test", debugBtn.transform.position + debugBtn.transform.right * (LC_Utils.GetDefaultButtonSize().x / 2 + LC_Utils.GetDefaultButtonPanelOffset())); }
        }
        ;

        CameraSwitcher.SetupReturnAction(() => Debug.Log("Returning to original camera"));


        LC_Utils.CreateKeyCodeAction(KeyCode.F1,
                                     () => {
            if (LC_Utils.IsBuildForProduction())
            {
                return;
            }
            LC_Debug.UseFreeLookCamera();
        });

        LC_Utils.CreateKeyCodeAction(KeyCode.F2,
                                     () => {
            if (LC_Utils.IsBuildForProduction())
            {
                return;
            }
            LC_Debug.UseFreeLookCamera();
        });


        LC_Utils.CreateKeyCodeAction(KeyCode.E,
                                     () => {
            if (LC_Utils.IsBuildForProduction())
            {
                return;
            }
            LC_Debug.RuntimeDebugObjectsManager.DisableDebugObjects();
        });

        LC_Utils.CreateKeyCodeAction(KeyCode.R,
                                     () => {
            if (LC_Utils.IsBuildForProduction())
            {
                return;
            }
            LC_Debug.RuntimeDebugObjectsManager.EnableDebugObjects();
        });

        LC_Utils.CreateKeyCodeAction(KeyCode.Backspace,
                                     () => {
            if (LC_Utils.IsBuildForProduction())
            {
                return;
            }
            CameraSwitcher.ReturnToOriginalCamera();
        });
    }
}