Skip to content

Instantly share code, notes, and snippets.

View AndrewWeston9's full-sized avatar

Andrew Weston AndrewWeston9

  • Victoria, Australia
View GitHub Profile
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:43
WorldManager.cs Snippet: Setting up network messages (serialisation), and registering handlers for incoming client messages.
/// Receiving a single emote from a player.
public const short EmoteSingleReceiver = MsgType.Highest + 5;
/// Send emote and playerID of emote sender to each client.
public const short EmoteSingleSender = MsgType.Highest + 6;
/// Send Resource update from client to server.
public const short ResourceUpdate = MsgType.Highest + 7;
/// Send Send and receive a player flag request.
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:42
WorldManager.cs Snippet: Random resource brick generator.
/// Resource brick generator.
/// Randomly generates resources and adds them to the brickstructure[].
public void resourceBrickGenerator()
{
for (int i = 0; i < 100; i++)
{
Vector3 pos = new Vector3 ((int) UnityEngine.Random.Range (0f, 100.0f) + 0.5f, Math.Max ((int) (UnityEngine.Random.Range (0f, 12.0f)), (int) WorldManager.minLevelHeight), (int) UnityEngine.Random.Range (0f, 100.0f) + 0.5f);
int blockHeight = (int) (pos.y - WorldManager.minLevelHeight);
levelStructure.setBlock(pos.x + 0.5f, pos.z + 0.5f, blockHeight, 5, -1);
}
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:42
WorldManager.cs Snippet: Send all clients an emote. Loops through all active clients, sending them network messages.
//Send all clients an emote
void sendAllClientEmote(NetworkInstanceId netId, int emote)
{
foreach(KeyValuePair<int, ClientDetails> entry in playerMonitoring)
{
SendEmoteMessageAndClientID m = new SendEmoteMessageAndClientID ();
m.emoteType = emote;
m.netId = netId;
NetworkServer.SendToClient (entry.Key, LevelMsgType.EmoteSingleSender, m);
}
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:42
WorldManager.cs Snippet: Handling incoming client messages.
case LevelMsgType.EmoteSingleSender:
/// Receiving a single emote from a player.
{
SendEmoteMessageAndClientID m = netMsg.ReadMessage<SendEmoteMessageAndClientID> ();
sendAllClientEmote(m.netId, m.emoteType);
Debug.Log ("Emote Received from Client.");
}
break;
case LevelMsgType.ResourceUpdate:
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:42
WorldManager.cs Snippet: Setting up network message classes.
/// The message sent when the player is updating the level.
class BlockAddMessage : MessageBase
{
public float px;
public float pz;
public float height;
public int blocktype;
}
@AndrewWeston9
AndrewWeston9 / LocalWorld.cs
Last active October 6, 2017 06:41
LocalWorld.cs Snippet: Message handling, client side.
NetworkClient.allClients[0].RegisterHandler (LevelMsgType.EmoteSingleSender, ServerCommandHandler); // Handle incoming emotes from server
NetworkClient.allClients[0].RegisterHandler (LevelMsgType.PlayerFlagRequest, ServerCommandHandler);
case LevelMsgType.EmoteSingleSender:
/// Handle incoming emotes from server
{
SendEmoteMessageAndClientID m = netMsg.ReadMessage<SendEmoteMessageAndClientID> ();
displayEmote(m.emoteType, m.netId);
Debug.Log ("Incoming emote to client from server from network id: " + m.netId);
}
@AndrewWeston9
AndrewWeston9 / EmoteWheel.cs
Last active October 6, 2017 09:04
EmoteWheel.cs Snippet: Created by another team member, I took their code and changed it to work in a network environment, sending the emotes between clients through the server.
public void ButtonAction()
{
buttons[CurMenuItem].sceneimage.color = buttons[CurMenuItem].PressedColor;
GameObject Player = this.transform.parent.gameObject;
NetworkInstanceId netID = Player.GetComponent<NetworkIdentity>().netId;
Debug.Log ("Sending emote with networkID: " + netID.ToString());
Vector3 offset = new Vector3(0.0f, 2.5f, 0.0f);
//IconObject DisplayEmote;
if (CurMenuItem == 0)
{
@AndrewWeston9
AndrewWeston9 / EmoteWheel.cs
Last active October 6, 2017 09:04
EmoteWheel.cs Snippet: Camera pause function when GUI is in use. Created by myself and another team member.
if(menuon == true)
{
setUIToFalse = false;
PCamera = this.transform.parent.gameObject;
netIDC = PCamera.GetComponent<NetworkIdentity>().netId;
PCamera = ClientScene.FindLocalObject(netIDC);
pmove = PCamera.GetComponent<PlayerMove>();
pmove.SetUIOpenTrue();
}
else if (setUIToFalse == false)
@AndrewWeston9
AndrewWeston9 / PlayerAnimator.cs
Created October 6, 2017 06:35
Animation controller for the player character. Controls walking and gathering animations.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using System;
public class PlayerAnimator : NetworkBehaviour {
public Animator anim;
public float speed = 2.0f;
@AndrewWeston9
AndrewWeston9 / PlayMove.cs
Last active October 6, 2017 09:04
PlayMove.cs Snippet: Camera pause function when GUI is in use. Created by myself and another team member.
bool setUIToFalse = true;
public void SetUIOpenTrue()
{
UIOpen = true;
}
public void SetUIOpenFalse()
{
UIOpen = false;