예제 #1
0
    ///<summary>Opdatere vores lokale spiller med nyt data</summary>
    private void HandleNetworkedPlayer(ShipPacketData ship)
    {
        if (ship.ID != -1)
        {
            if (gameManager.GetPlayerShip() != null)
            {
                gameManager.GetPlayerShip().transform.position = ship.position;
                gameManager.GetPlayerShip().transform.rotation = ship.rotation;
                gameManager.GetPlayerShip().SetShipHealth(ship.health, true);
                gameManager.GetPlayerShip().SetShipFuel(ship.fuel, true);
                gameManager.GetUIManager().GetCompass().transform.eulerAngles = new Vector3(gameManager.GetUIManager().GetCompass().transform.eulerAngles.x, gameManager.GetUIManager().GetCompass().transform.eulerAngles.y, gameManager.GetPlayerShip().transform.eulerAngles.y);

                gameManager.GetPlayerShip().UpdateNetworkState();
            }
            else
            {
                gameManager.SpawnPlayerShip(ship.position, ship.rotation, false, ship.ID).UpdateNetworkState();
            }
        }
    }
예제 #2
0
    ///<summary>Generer en packet med alt nødvendigt information omkring scenen</summary>
    private ServerDataPacket GenerateServerPacket(ServerDataPacket oldPacket)
    {
        ShipPacketData playerData;

        if (gameManager.GetPlayerShip() != null)
        {
            playerData = new ShipPacketData
            {
                ID       = gameManager.GetPlayerShip().GetShipID(),
                position = gameManager.GetPlayerShip().transform.position,
                rotation = gameManager.GetPlayerShip().transform.rotation,
                health   = gameManager.GetPlayerShip().GetShipHealth(),
                fuel     = gameManager.GetPlayerShip().GetShipFuel()
            };
        }
        else
        {
            playerData = new ShipPacketData
            {
                ID       = -1,
                position = new Vector3(),
                rotation = new Quaternion(),
                health   = 0,
                fuel     = 0
            };
        }

        List <ShipPacketData> enemyShipData = new List <ShipPacketData>();

        for (int i = 0; i < gameManager.GetAllShips().Count; i++)
        {
            if (gameManager.GetAllShips()[i] as EnemyShip != null)
            {
                ShipPacketData shipData = new ShipPacketData
                {
                    ID       = gameManager.GetAllShips()[i].GetShipID(),
                    position = gameManager.GetAllShips()[i].transform.position,
                    rotation = gameManager.GetAllShips()[i].transform.rotation,
                    health   = gameManager.GetAllShips()[i].GetShipHealth(),
                    fuel     = gameManager.GetAllShips()[i].GetShipFuel()
                };

                enemyShipData.Add(shipData);
            }
        }

        List <MinePacketData> minesData = new List <MinePacketData>();

        for (int i = 0; i < gameManager.GetMines().Count; i++)
        {
            MinePacketData mineData = new MinePacketData
            {
                ID       = gameManager.GetMines()[i].GetMineID(),
                position = gameManager.GetMines()[i].transform.position
            };

            minesData.Add(mineData);
        }

        List <CannonBallPacketData> cannonBallsData = new List <CannonBallPacketData>();

        for (int i = 0; i < gameManager.GetCannonBalls().Count; i++)
        {
            CannonBallPacketData cannonBallData = new CannonBallPacketData
            {
                ID       = gameManager.GetCannonBalls()[i].GetCannonBallID(),
                ownerID  = gameManager.GetCannonBalls()[i].GetCannonBallOwner().GetShipID(),
                position = gameManager.GetCannonBalls()[i].transform.position,
                rotation = gameManager.GetCannonBalls()[i].transform.rotation
            };

            cannonBallsData.Add(cannonBallData);
        }

        //Indsæt vores data ind i en packet klasse
        ServerDataPacket serverPacket = new ServerDataPacket
        {
            ID          = oldPacket.ID + 1,
            fullUpdate  = (oldPacket.ID + 1) <= 1,
            gameSession = updateIntervalSession,
            player      = playerData,
            enemyShips  = enemyShipData,
            mines       = minesData,
            cannonBalls = cannonBallsData
        };

        return(serverPacket);
    }