Example #1
0
    public static void PlayerShoot(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        MyClientBehaviour client = caller as MyClientBehaviour;

        //playerId and bullet id
        uint playerId  = stream.ReadUInt(ref context);
        uint networkId = stream.ReadUInt(ref context);

        //position
        float posX = stream.ReadFloat(ref context);
        float posY = stream.ReadFloat(ref context);
        float posZ = stream.ReadFloat(ref context);
        //rotation
        float rotX = stream.ReadFloat(ref context);
        float rotY = stream.ReadFloat(ref context);
        float rotZ = stream.ReadFloat(ref context);

        //spawn a bullet with the received networkId and position/direction
        GameObject bullet;
        uint       id = NetworkId.Spawn(Resources.Load("ClientBullet"), ref client.repository, networkId);

        if (NetworkId.Find(id, out bullet, ref client.repository))
        {
            //set playerId on bullet script, set position and direction of player that's shooting
            bullet.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ);
            bullet.transform.position = new Vector3(posX, posY, posZ);
        }
    }
    public void ConsumeTurn()
    {
        //NetworkId spawn a bullet
        GameObject bullet;
        uint       id = NetworkId.Spawn(Resources.Load("ServerBullet"), ref repository);

        if (NetworkId.Find(id, out bullet, ref repository))
        {
            //set playerId on bullet script, set position and direction of player that's shooting
            Bullet b = bullet.GetComponent <Bullet>();
            b.SetData(currentTurnIndex, true);
            b.positionChanged   += CheckCollisionWithPlayers;
            b.outOfLife         += DestroyNetworkedBullet;
            b.transform.rotation = Quaternion.Euler(playerData[currentTurnIndex].euler);
            b.transform.position = playerData[currentTurnIndex].position + b.transform.forward;

            Debug.Log("PLAYER SHOOTING!");

            //Send PLAYER_SHOOT event
            //event, playerId, networkId, position (3), rotation(3)
            //TODO: Add networkTime
            using (var writer = new DataStreamWriter(12 + (6 * 4), Allocator.Temp))
            {
                writer.Write((uint)GameEvent.PLAYER_SHOOT);
                writer.Write((uint)currentTurnIndex);
                writer.Write(id);

                writer.Write(b.transform.position.x);
                writer.Write(b.transform.position.y);
                writer.Write(b.transform.position.z);

                writer.Write(b.transform.eulerAngles.x);
                writer.Write(b.transform.eulerAngles.y);
                writer.Write(b.transform.eulerAngles.z);

                for (int i = 0; i < m_Connections.Length; ++i)
                {
                    m_Connections[i].Send(m_Driver, writer);
                }
            }
        }
        else
        {
            Debug.LogError("Couldn't find object we just spawned!");
        }

        //increment turn
        NextTurn();
    }