Пример #1
0
    public static int SpawnFromPool(int type, Vector3 pos, out MeshesCPUInfo mesh)
    {
        // retrieve ID from Nullregister
        int?id = FetchFromNullregister(type);

        if (id == null)
        {
            mesh = new MeshesCPUInfo();
            Debug.LogWarning($"Could not find model with id {id}");
            return(0);
        }

        if (!MeshRegister.TryGetValue((int)id, out mesh))
        {
            Debug.LogWarning($"Could not find model with id {id} in MeshRegister");
            return(0);
        }

        // set model to spawn position
        mesh.position = pos;
        mesh.isNull   = false;

        pos = mesh.center + pos;
        QueueUpdate(ref mesh, new Vector4(pos.x, pos.y, pos.z, 1));

        // update register
        MeshRegister[(int)id] = mesh;

        return((int)id);
    }
Пример #2
0
    private void UpdateHexagons(int mid, ref MeshesCPUInfo model)
    {
        if (tk.State == TerrainToolkit.state.removeModel)
        {
            //remove model
            // take away fron current hexagon
            currentHexagon.UpdateModels(mid);
            model.hexagon = 0;
            return;
        }

        if (tk.State == TerrainToolkit.state.addModel)
        {
            // add model
            // add to current hexagon
            currentHexagon.UpdateModels(mid);
            model.hexagon = currentHexagon.ID;
            return;
        }

        if (tk.State == TerrainToolkit.state.moveModel)
        {
            // move model
            // get hexagon for last mpos /= current hexagon)
            // add to current, remove from old
            gridUtils.GetHexByID(model.hexagon).UpdateModels(mid);
            currentHexagon.UpdateModels(mid);
            model.hexagon = currentHexagon.ID;
            return;
        }
    }
Пример #3
0
    private static void QueueUpdate(ref MeshesCPUInfo mesh, Vector4 position)
    {
        MeshUpdate update = new MeshUpdate {
            IDInput  = new Vector2(mesh.vertID, mesh.vertcount),
            PosInput = position
        };

        if (mesh.isSingle)
        {
            if (!SinglePoolRegister.TryGetValue(mesh.meshtype, out GPUSinglePoolModel pool))
            {
                Debug.LogWarning($"Could not find model with id {mesh.vertID} in SinglePoolRegister");
            }

            pool.QueueUpdate(update);
        }
        else
        {
            MeshInit.QueueUpdate(update);
        }
    }
Пример #4
0
 public SerializedModelRegister(MeshesCPUInfo mesh)
 {
     Meshtype = mesh.meshtype;
     Position = new Vector4(mesh.position.x, 0, mesh.position.z, mesh.hexagon);
 }
Пример #5
0
 private static Vector2 MeshUpdateDataPacket(MeshesCPUInfo mesh) => new Vector2(mesh.vertID, mesh.vertcount);
Пример #6
0
    private void Update()
    {
        // DEBUG
        if (Input.GetKeyDown(KeyCode.Space))
        {
            FindObjectOfType <SerializationHandler>().SerializeData(gridUtils.Grid.SerializeData());
        }

        if (Input.GetKeyDown(KeyCode.H))
        {
            HexagonDebugInfo();
            meshUtils.MeshInit.CollectDebugVertices();
        }
        // DEBUG END


        state = GetInputState(out Vector2Int hex, out currentMousePos);

        if (state == inputstate.invalid)
        {
            if (currentSelectedModel != null)
            {
                meshUtils.ReturnToPool((int)currentSelectedModel);
            }
            return;
        }


        currentHexagon = gridUtils.Grid.Hexagons[hex.x, hex.y];

        if (TerrainToolkit.state.terrain.HasFlag(tk.State))
        {
            if (state == inputstate.interacting)
            {
                // collect information
                if (!collection.Contains(currentHexagon))
                {
                    collection.Add(currentHexagon);
                }
            }
            else if (state == inputstate.selectedCompleted)
            {
                if (collection.Count <= 0)
                {
                    return;
                }

                // submit info
                SetStateAndType(hex, out uint type, out uint state);

                uint[] types  = new uint[collection.Count];
                uint[] states = new uint[collection.Count];
                for (int i = 0; i < collection.Count; i++)
                {
                    types[i]  = type;
                    states[i] = state;
                }

                gridUtils.Grid.UpdateHexagons(collection.ToArray(), states, types);

                if (state == 0)
                {
                    // delte all models from hexagon
                    for (int i = 0; i < collection.Count; i++)
                    {
                        for (int j = 0; j < collection[i].Models.Count; j++)
                        {
                            meshUtils.ReturnToPool(collection[i].Models[j]);
                        }
                        meshUtils.ReturnToPool(collection[i].Models.ToArray());
                        collection[i].Models.Clear();
                    }
                }


                collection.Clear();
            }
        }
        else if (tk.State == TerrainToolkit.state.addModel)
        {
            if (state == inputstate.awaiting)
            {
                // collect & submit info

                if (currentSelectedModel == null || currentSelectedM.meshtype != TerrainToolkit.AddModelID)
                {
                    currentSelectedModel = meshUtils.SpawnFromPool(TerrainToolkit.AddModelID, new Vector3(currentMousePos.x, 0, currentMousePos.y), out MeshesCPUInfo model);
                    UpdateHexagons((int)currentSelectedModel, ref model);
                    currentSelectedM = model;
                    return;
                }

                meshUtils.UpdateInPool((int)currentSelectedModel, new Vector3(currentMousePos.x, 0, currentMousePos.y));

                return;
            }

            if (state == inputstate.selectedCompleted)
            {
                UpdateHexagons((int)currentSelectedModel, ref currentSelectedM);

                currentSelectedModel = null;
            }
        }
        else if (TerrainToolkit.state.selectModel.HasFlag(tk.State))
        {
            bool modelInReach = currentHexagon.GetClosestMeshFromPoint(currentMousePos, out int mid);
            bool modelValid   = meshUtils.MeshRegister.TryGetValue(mid, out MeshesCPUInfo model);

            if (state == inputstate.awaiting)
            {
                if (!modelInReach || !modelValid)
                {
                    DisableHighlights();
                    currentSelectedModel = null;
                    return;
                }

                Highlight(mid);
            }
            else if (state == inputstate.interacting)
            {
                if (modelInReach && modelValid)
                {
                    currentSelectedModel ??= mid;
                }
            }


            if (currentSelectedModel == null)
            {
                return;
            }

            if (tk.State == TerrainToolkit.state.removeModel)
            {
                if (state == inputstate.selectedCompleted)
                {
                    // collect & submit info
                    meshUtils.ReturnToPool((int)currentSelectedModel);
                    UpdateHexagons((int)currentSelectedModel, ref model);
                }
            }
            else if (tk.State == TerrainToolkit.state.moveModel)
            {
                float height = state == inputstate.interacting ? _modelMoveHeight : 0;
                if (state == inputstate.interacting || state == inputstate.selectedCompleted)
                {
                    // collect & submit info
                    meshUtils.UpdateInPool((int)currentSelectedModel, new Vector3(currentMousePos.x, height, currentMousePos.y));
                    UpdateHexagons((int)currentSelectedModel, ref model);
                }
            }

            if (state == inputstate.selectedCompleted)
            {
                DisableHighlights();
                currentSelectedModel = null;
            }
        }
    }