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: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
{
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:44
WorldManager.cs Snippet: Function to remove flag from the region block.
// Checks to see if the flag can be removed by the player requesting the removal or not.
public bool removeFlag(int x, int y, int z, int connid)
{
if ((x >= 0) && (x < blockSize) &&
(y >= 0) && (y < blockSize) &&
(z >= 0) && (z < MaxBlockHeight))
{
if (blockStructure [getIndex (x, y, z)].playerConnectionId == connid)
{
return true;
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:44
WorldManager.cs Snippet: Retrieving block health, updating block health, deleting a block.
public int getBlockHealth (int x, int y, int z)
{
if ((x >= 0) && (x < blockSize) &&
(y >= 0) && (y < blockSize) &&
(z >= 0) && (z < MaxBlockHeight))
{
return blockStructure[getIndex (x, y, z)].brickHealth;
}
return 0;
}