add scripts

This commit is contained in:
in0finite 2019-11-15 12:37:37 +01:00
parent c3382928c2
commit d7e9db901b
5 changed files with 211 additions and 0 deletions

8
Assets/Scripts/Chat.meta Normal file
View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cc108b0e0b2f96d469ca10f7a5aea69a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,113 @@
using UnityEngine;
using SanAndreasUnity.Net;
namespace SanAndreasUnity.Chat
{
public class ChatMessage
{
public ChatMessage (string msg, string sender)
{
this.msg = msg;
this.sender = sender;
}
public string msg = "";
public string sender = "";
}
public class ChatManager : MonoBehaviour
{
public static ChatManager singleton { get ; private set ; }
public string serverChatNick = "<color=green>Server</color>";
public static event System.Action<ChatMessage> onChatMessage = delegate {};
void Awake ()
{
singleton = this;
onChatMessage += (ChatMessage chatMsg) => Debug.Log ("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
ChatSync.onChatMessageReceivedOnServer += (Player p, string msg) => SendChatMessageToAllPlayers( msg, "player " + p.netId ) ;
ChatSync.onChatMessageReceivedOnLocalPlayer += (ChatMessage chatMsg) => onChatMessage (chatMsg);
}
void OnSceneChanged( SanAndreasUnity.Behaviours.SceneChangedMessage info ) {
if (NetStatus.IsServer) {
SendChatMessageToAllPlayersAsServer ("Map changed to " + info.s2.name + ".");
}
}
public static void SendChatMessageToAllPlayersAsServer( string msg ) {
if (NetStatus.IsServerStarted) {
SendChatMessageToAllPlayers (msg, singleton.serverChatNick);
}
}
public static void SendChatMessageToAllPlayersAsLocalPlayer( string msg ) {
if (null == Player.Local) {
return;
}
var chatSync = Player.Local.GetComponent<ChatSync> ();
if (chatSync != null) {
chatSync.SendChatMsgToServer (msg);
}
}
/// <summary> Use only on server. </summary>
public static void SendChatMessageToAllPlayers( string msg, string sender ) {
if (!NetStatus.IsServerStarted)
return;
foreach (var player in Player.AllPlayers) {
SendChatMessageToPlayer ( player, msg, sender );
}
if (!NetStatus.IsHost ()) {
// running as dedicated server
// we should invoke the event here, because there is no local player to receive the chat message
onChatMessage( new ChatMessage(msg, sender) );
}
}
/// <summary> Use only on server. </summary>
public static void SendChatMessageToPlayer( Player player, string msg ) {
if (!NetStatus.IsServerStarted)
return;
SendChatMessageToPlayer (player, msg, singleton.serverChatNick);
}
private static void SendChatMessageToPlayer( Player player, string msg, string sender ) {
if (!NetStatus.IsServerStarted)
return;
var chatSync = player.GetComponent<ChatSync> ();
if (chatSync != null) {
chatSync.SendChatMsgToClient (player.connectionToClient, msg, sender);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 08a96953b142a0943927d6d273feba92
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,68 @@
using UnityEngine;
using Mirror;
using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities;
namespace SanAndreasUnity.Chat
{
public class ChatSync : NetworkBehaviour
{
Player m_player;
public static event System.Action<Player, string> onChatMessageReceivedOnServer = delegate {};
public static event System.Action<ChatMessage> onChatMessageReceivedOnLocalPlayer = delegate {};
void Awake()
{
m_player = this.GetComponentOrThrow<Player>();
}
[Command]
void CmdChatMsg( string msg ) {
Player p = m_player;
// Remove tags.
msg = msg.Replace ("<", ""); // the only easy way :D
msg = msg.Replace (">", "");
// msg = msg.Replace ("<color", "color");
// msg = msg.Replace ("<size", "size");
// msg = msg.Replace ("<b>", "");
// msg = msg.Replace ("<i>", "");
// msg = msg.Replace (">", "\\>");
// Forward this message to all clients including the sender.
// ChatManager.SendChatMessageToAllPlayers( msg, p.playerName );
onChatMessageReceivedOnServer( p, msg );
}
public void SendChatMsgToServer( string msg )
{
this.CmdChatMsg(msg);
}
[TargetRpc]
void TargetChatMsg( NetworkConnection conn, string msg, string sender ) {
if (!this.isLocalPlayer) {
return;
}
onChatMessageReceivedOnLocalPlayer (new ChatMessage (msg, sender));
}
public void SendChatMsgToClient( NetworkConnection conn, string msg, string sender )
{
this.TargetChatMsg(conn, msg, sender);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dde946d1fa4b8774c96e953122a36d77
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: