-
Notifications
You must be signed in to change notification settings - Fork 9
/
onlineGameplay.cs
90 lines (79 loc) · 2.74 KB
/
onlineGameplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using UnityEngine;
using Steamworks;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System;
public class onlineGameplay : MonoBehaviour {
protected Callback<P2PSessionRequest_t> Callback_newConnection;
public List<CSteamID> lobby_members_;
public List<string> lobby_names_;
void Start () {
lobby_names_ = new List<string>();
lobby_members_ = new List<CSteamID>();
Callback_newConnection = Callback<P2PSessionRequest_t>.Create(OnNewConnection);
}
void Update()
{
getNetworkData();
}
void getNetworkData()
{
//Recieve packets from other members in the lobby with us
uint msgSize;
while(SteamNetworking.IsP2PPacketAvailable(out msgSize))
{
byte[] packet = new byte[msgSize];
CSteamID steamIDRemote;
uint bytesRead = 0;
if (SteamNetworking.ReadP2PPacket(packet, msgSize, out bytesRead, out steamIDRemote))
{
int TYPE = packet[0];
string msg = System.Text.Encoding.UTF8.GetString(SubArray(packet, 1, packet.Length-1));
switch (TYPE)
{
case 1: //Packet type 1 == GOT IN-GAME CHAT MESSAGE
Debug.log(lobby_names_[getPlayerIndex(steamIDRemote)] + " says: "+msg);
break;
default: Debug.Log("BAD PACKET"); break;
}
}
}
}
int getPlayerIndex(CSteamID input)
{
for (int i = 0; i < lobby_members_.Count; i++)
if (lobby_members_[i] == input)
return i;
return -1;
}
public void net_broadcast(int TYPE, string message)
{
for(int i=0; i<lobby_members_.Count; i++)
{
if (lobby_members_[i] == SteamUser.GetSteamID())
continue;
byte[] b_message = System.Text.Encoding.UTF8.GetBytes(message);
byte[] sendBytes = new byte[b_message.Length + 1];
sendBytes[0] = (byte)TYPE;
b_message.CopyTo(sendBytes, 1);
SteamNetworking.SendP2PPacket(lobby_members_[i], sendBytes, (uint)sendBytes.Length, EP2PSend.k_EP2PSendReliable);
}
}
void OnNewConnection(P2PSessionRequest_t result)
{
foreach(CSteamID id in lobby_members_)
if (id == result.m_steamIDRemote)
{
SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote);
return;
}
}
// IMPORTED
public T[] SubArray<T>(T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
}