mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-10 06:34:16 +00:00
add scripts
This commit is contained in:
parent
c3382928c2
commit
d7e9db901b
5 changed files with 211 additions and 0 deletions
8
Assets/Scripts/Chat.meta
Normal file
8
Assets/Scripts/Chat.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cc108b0e0b2f96d469ca10f7a5aea69a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
113
Assets/Scripts/Chat/ChatManager.cs
Normal file
113
Assets/Scripts/Chat/ChatManager.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/Chat/ChatManager.cs.meta
Normal file
11
Assets/Scripts/Chat/ChatManager.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 08a96953b142a0943927d6d273feba92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
68
Assets/Scripts/Chat/ChatSync.cs
Normal file
68
Assets/Scripts/Chat/ChatSync.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/Chat/ChatSync.cs.meta
Normal file
11
Assets/Scripts/Chat/ChatSync.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dde946d1fa4b8774c96e953122a36d77
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue