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 / 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 / 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 / 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 / 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: 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: 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: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:43
WorldManager.cs Snippet: Update Flag, checks if flag object was created by the requesting player and deletes it with a network message to client confirming.
public void updateFlag(float x, float y, float z, int connid)
{
int rx = ((int) x) % blockSize;
int ry = ((int) y) % blockSize;
int rz = ((int) z);
RegionBlock rb = getRegion (x, y);
if (rb != null)
{
if (rb.removeFlag (rx, ry, rz + 1, connid))
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:43
WorldManager.cs Snippet: Update Resource block levels by passing location and amount to adjust by.
/// Update a change of resource in the level structure.
public void updateResource(float x, float y, float z, int amount)
{
int rx = ((int) x) % blockSize;
int ry = ((int) y) % blockSize;
int rz = ((int) z);
RegionBlock rb = getRegion (x, y);
if (rb != null)
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:44
WorldManager.cs Snippet: Placing blocks in the world, setting up the different brick objects and flag object.
public void placeSingleBlock (GameObject block, Vector3 position, Transform parentObjectTransform)
{
Vector3 blockpos;
if (block.tag == "Flag")
{
blockpos = new Vector3 (position.x, position.z - 0.5f, position.y);
}
else
{