////////////////////////////////////////////////

    public static void CreateUnitOnClient(NetworkNodeStruct nodeStruct, int playerID) // an attempt to make units like ships
    {
        Vector3Int nodeID = new Vector3Int(Mathf.FloorToInt(nodeStruct.NodeID.x), Mathf.FloorToInt(nodeStruct.NodeID.y), Mathf.FloorToInt(nodeStruct.NodeID.z));

        //Debug.Log("CreateUnitOnClient UnitStartingNodeID " + nodeStruct.UnitData.UnitStartingNodeID);
        //Debug.Log("CreateUnitOnClient nodeID " + nodeID);

        CubeLocationScript cubeScript = LocationManager.GetLocationScript_CLIENT(nodeID);

        //cubeScript.MapNodeParent.ActivateMapPiece(true);

        if (cubeScript != null)
        {
            GameObject prefab = _unitBuilder.GetUnitModel(nodeStruct.UnitData.UnitModel);
            GameObject unit   = Instantiate(prefab, cubeScript.gameObject.transform, false);
            unit.transform.SetParent(cubeScript.gameObject.transform);
            unit.transform.localPosition = nodeStruct.CurrLoc;
            GameObject unitContainer = unit.transform.FindDeepChild("UnitContainer").gameObject;
            unitContainer.transform.localEulerAngles = nodeStruct.CurrRot;

            UnitScript unitScript = unit.GetComponent <UnitScript>();

            unitScript.UnitData           = nodeStruct.UnitData;
            unitScript.UnitID             = nodeID;
            unitScript.PlayerControllerID = playerID;
            unitScript.UnitModel          = nodeStruct.UnitData.UnitModel;
            unitScript.UnitCanClimbWalls  = nodeStruct.UnitData.UnitCanClimbWalls;
            unitScript.UnitCombatStats    = nodeStruct.UnitData.UnitCombatStats;

            LocationManager.SetUnitOnCube_CLIENT(unit.GetComponent <UnitScript>(), unitScript.UnitID); //sets CubeUnitIsOn

            AddUnitToGame(playerID, unitScript.UnitID, unitScript);                                    // add unit to generic unit manager pool
            PlayerManager.PlayerAgent.GetComponent <UnitsAgent>().AddUnitToUnitAgent(unitScript);      // add unit to a more specific player unit pool

            if (PlayerManager.PlayerID == playerID)
            {
                if (nodeStruct.UnitData.UnitCombatStats[0] == 1) // if rank is 'Captain'???? then make active
                {
                    SetUnitActive(true, playerID, unitScript.UnitID);
                }
            }
            Debug.Log("Unit Succesfully created on CLIENT: " + unitScript.UnitID);
        }
        else
        {
            Debug.LogError("Got a problem here > " + nodeID);
        }
    }
    public void CmdTellServerToUpdateUnitPosition(NetworkInstanceId clientNetID, Vector3 nodeID, int networkNodeIndex, Vector3 locPos, Vector3 locRot)
    {
        if (!isServer)
        {
            return;
        }

        CheckSyncVars();
        NetworkNodeStruct nodeStruct = _syncedVars._network_Units_Container[networkNodeIndex];

        nodeStruct.CurrLoc = locPos;
        nodeStruct.CurrRot = locRot;

        // tell all clients to move unit
        RpcTellOtherClientsToMoveUnit(clientNetID, nodeStruct);
    }
    void RpcTellOtherClientsToMoveUnit(NetworkInstanceId clientIDWhoOwnsUnit, NetworkNodeStruct nodeStruct)
    {
        if (clientIDWhoOwnsUnit != PlayerManager.PlayerAgent.NetworkInstanceID) // to stop sending data to the clients OWN ship thats already moving being told to move
        {
            //GameObject unit = LocationManager.GetUnitFrom_Client(nodeStruct.NodeID);

            //float dist = Vector3Int.Distance(unit.transform.position, nodeStruct.CurrLoc);
            //if (dist > 10f)
            //{
            //    unit.transform.position = nodeStruct.CurrLoc;
            //    unit.transform.localEulerAngles = nodeStruct.CurrRot;
            //}

            //unit.MakeNodeMoveToLoc(nodeStruct.CurrLoc, nodeStruct.CurrRot, false); this needs to be made, using the ships lerp thingy
        }
    }
Example #4
0
    ////////////////////////////////////////////////
    ////////////////////////////////////////////////
    public static void CreatePlayerShip(NetworkNodeStruct nodeStruct, int playerID)
    {
        Vector3Int nodeID = new Vector3Int(Mathf.FloorToInt(nodeStruct.NodeID.x), Mathf.FloorToInt(nodeStruct.NodeID.y), Mathf.FloorToInt(nodeStruct.NodeID.z));

        WorldNodeStruct worldNodeData = new WorldNodeStruct()
        {
            NodeID  = nodeID,
            CurrLoc = new Vector3Int(Mathf.FloorToInt(nodeStruct.CurrLoc.x), Mathf.FloorToInt(nodeStruct.CurrLoc.y), Mathf.FloorToInt(nodeStruct.CurrLoc.z)),
            CurrRot = new Vector3Int(Mathf.FloorToInt(nodeStruct.CurrRot.x), Mathf.FloorToInt(nodeStruct.CurrRot.y), Mathf.FloorToInt(nodeStruct.CurrRot.z)),
        };

        WorldNode worldNode = WorldBuilder._nodeBuilder.CreateWorldNode(worldNodeData);

        LocationManager.SaveNodeTo_CLIENT(nodeID, worldNode);

        BasePlayerData playerData = PlayerManager.GetPlayerData(playerID);

        worldNode.transform.eulerAngles = new Vector3Int(0, 90, 0);

        worldNode.NodeData.worldNodeMapPieces = playerData.shipMapPieces;

        MapNodeBuilder.CreateMapNodesForWorldNode(worldNode);

        Dictionary <Vector3Int, WorldNode> dict = new Dictionary <Vector3Int, WorldNode>();

        dict.Add(nodeID, worldNode);
        //LayerManager.AssignLayerCountsToWorldAndMapNodes(dict);

        //if (playerID == PlayerManager.PlayerID)
        //{
        foreach (MapNode mapNode in worldNode.mapNodes)
        {
            mapNode.ActivateMapPiece(true);
        }
        //}

        if (playerData.playerID == PlayerManager.PlayerID)
        {
            MoveShip(playerData, worldNode);

            worldNode.transform.Find("RotationalNode").GetComponent <RotationalNode>()._rotate = true;
            PlayerManager.PlayersShipLoaded();
        }
    }
    public void CmdTellServerToUpdateWorldNodePosition(NetworkInstanceId clientNetID, Vector3 worldNodeIndex, Vector3 locPos, Vector3 locRot)
    {
        if (!isServer)
        {
            return;
        }

        CheckSyncVars();
        Vector3Int        vectInt    = new Vector3Int(Mathf.FloorToInt(worldNodeIndex.x), Mathf.FloorToInt(worldNodeIndex.y), Mathf.FloorToInt(worldNodeIndex.z));
        int               vectIndex  = DataManipulation.ConvertVectorIntoInt(vectInt);
        int               index      = _syncedVars._network_Ships_Indexs.IndexOf(vectIndex);
        NetworkNodeStruct nodeStruct = _syncedVars._network_Ships_Container[index]; // this is not good and will be horribly innefficient

        nodeStruct.CurrLoc = locPos;
        nodeStruct.CurrRot = locRot;

        // tell all clients to move ship
        RpcTellOtherClientsToMoveClientShip(clientNetID, nodeStruct);
    }
    public void CmdTellServerToSpawnPlayersShip(NetworkInstanceId clientNetID, Vector3 loc, Vector3 rot, int playerID) // this player ID is dumb and needs the player ship info (thats all its here for) to be fed in here rather than pulled from a script on the clients machines, beacuse that cant happen if people are joining games on the fly
    {
        if (!isServer)
        {
            return;
        }

        CheckSyncVars();

        Vector3Int nodeID = new Vector3Int(Mathf.FloorToInt(loc.x), Mathf.FloorToInt(loc.y), Mathf.FloorToInt(loc.z));

        NetworkNodeStruct nodeStruct = new NetworkNodeStruct()
        {
            NodeID      = nodeID,
            StructIndex = _syncedVars._network_Ships_Indexs.Count,
            ClientNetID = clientNetID,
            PlayerID    = playerID,
            CurrLoc     = loc,
            CurrRot     = rot
        };

        // tell all clients to load new ship
        RpcTellAllClientsToSpawnPlayerShip(nodeStruct, playerID);

        // now load old SHIPS into this client
        foreach (NetworkNodeStruct node in _syncedVars._network_Ships_Container)
        {
            NetworkConnection netConnect = TargetSpecificClient(clientNetID);
            TargetTellClientToSpawnExistingShips(netConnect, node);
        }

        // AND now load old UNITS into this client
        foreach (NetworkNodeStruct unit in _syncedVars._network_Units_Container)
        {
            TargetTellClientToLoadExistingUnits(TargetSpecificClient(clientNetID), unit);
        }

        // two lists, first is the NetworkNodeIDIndex that links to the second nodestruct, basicly a shitty dictionary, to get the list index to 'then' use to get the nodestruct
        int index = DataManipulation.ConvertVectorIntoInt(nodeID);

        _syncedVars._network_Ships_Indexs.Add(index);
        _syncedVars._network_Ships_Container.Add(nodeStruct);
    }
    void RpcTellOtherClientsToMoveClientShip(NetworkInstanceId clientIDWhoOwnsShip, NetworkNodeStruct nodeStruct)
    {
        if (clientIDWhoOwnsShip != PlayerManager.PlayerAgent.NetworkInstanceID) // to stop sending data to the clients OWN ship thats already moving being told to move
        {
            Vector3Int nodeID = new Vector3Int(Mathf.FloorToInt(nodeStruct.NodeID.x), Mathf.FloorToInt(nodeStruct.NodeID.y), Mathf.FloorToInt(nodeStruct.NodeID.z));

            if (LocationManager.GetNodeFrom_Client(nodeID) != null)
            {
                BaseNode worldNode = LocationManager.GetNodeFrom_Client(nodeID);

                float dist = Vector3.Distance(worldNode.transform.position, nodeStruct.CurrLoc);
                if (dist > 10f)
                {
                    worldNode.transform.position         = nodeStruct.CurrLoc;
                    worldNode.transform.localEulerAngles = nodeStruct.CurrRot;
                }

                worldNode.MakeNodeMoveToLoc(nodeStruct.CurrLoc, nodeStruct.CurrRot, false);
            }
        }
    }
    public void CmdTellServerToSpawnPlayerUnit(NetworkInstanceId clientNetID, UnitStruct unitData, int playerID)
    {
        if (!isServer)
        {
            return;
        }

        // figure out what Cube to put unit in
        KeyValuePair <Vector3Int, Vector3Int> playerPosRot = PlayerManager.GetPlayerStartPosition(playerID);


        //Debug.Log("Ship Node location >> " + playerPosRot.Key);

        Vector3Int unitShipLoc = new Vector3Int(Mathf.FloorToInt(unitData.UnitShipLoc.x), Mathf.FloorToInt(unitData.UnitShipLoc.y), Mathf.FloorToInt(unitData.UnitShipLoc.z));

        Vector3Int unitStartLoc = new Vector3Int((unitShipLoc.x + playerPosRot.Key.x), (unitShipLoc.y + playerPosRot.Key.y), (unitShipLoc.z + playerPosRot.Key.z)); // This is nessicary, We need to get the cube ID

        unitData.UnitStartingNodeID = unitStartLoc;

        NetworkNodeStruct nodeStruct = new NetworkNodeStruct()
        {
            NodeID      = unitStartLoc,
            StructIndex = _syncedVars._network_Units_Indexs.Count,
            ClientNetID = clientNetID,
            PlayerID    = playerID,
            CurrLoc     = Vector3.zero,
            CurrRot     = unitData.UnitRot,
            UnitData    = unitData
        };

        // tell all clients to load new unit
        RpcTellAllClientsToSpawnPlayerUnit(nodeStruct, playerID);

        // two lists, first is the NetworkNodeIDIndex that links to the second nodestruct, basicly a shitty dictionary, to get the list index to 'then' use to get the nodestruct
        _syncedVars._network_Units_Indexs.Add(DataManipulation.ConvertVectorIntoInt(unitStartLoc));
        _syncedVars._network_Units_Container.Add(nodeStruct);
    }
 private void TellClientToLoadOtherClientUnit(NetworkNodeStruct nodeStruct, int playerID)
 {
     UnitsManager.CreateUnitOnClient(nodeStruct, playerID);
 }
 public void TargetTellClientToLoadExistingUnits(NetworkConnection target, NetworkNodeStruct nodeStruct)
 {
     TellClientToLoadOtherClientUnit(nodeStruct, nodeStruct.PlayerID);
 }
 void RpcTellAllClientsToSpawnPlayerUnit(NetworkNodeStruct nodeStruct, int playerID)
 {
     TellClientToLoadOtherClientUnit(nodeStruct, playerID);
 }
 private void TellClientToSpawnOtherClientShip(NetworkNodeStruct nodeStruct, int playerID)
 {
     PlayerShipBuilder.CreatePlayerShip(nodeStruct, playerID);
 }
 public void TargetTellClientToSpawnExistingShips(NetworkConnection target, NetworkNodeStruct nodeStruct)
 {
     TellClientToSpawnOtherClientShip(nodeStruct, nodeStruct.PlayerID);
 }
 void RpcTellAllClientsToSpawnPlayerShip(NetworkNodeStruct nodeStruct, int playerID)
 {
     TellClientToSpawnOtherClientShip(nodeStruct, playerID);
 }