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 / PlayerMove.cs
Last active October 6, 2017 06:45
PlayerMove.cs Snippet: Placing a block in the world.
if (Input.GetKeyDown(KeyCode.LeftControl) && (isGrounded () || attached))
{
if (playerFlagPlaced == false || currentBlockType != 10)
{
if(!checkFlagRange() && (checkResource() || currentBlockType == 10 || currentBlockType == 1))
{
int px = (int)(playerpos.x + 0.5f);
int py = Math.Max ((int)(playerpos.y), (int)WorldManager.minLevelHeight);
int pz = (int)(playerpos.z + 0.5f);
@AndrewWeston9
AndrewWeston9 / LocalWorld.cs
Last active October 6, 2017 06:45
LocalWorld.cs Snippet: Placing a block in the world.
public void placeBlock (float x, float z, float height, int type)
{
// Coordinates for the region block - only horizontal elements.
Vector3 llbpos = new Vector3 (x, 0.0f, z);
LocalLevelBlock llb = findLevelBlock (llbpos);
int blockHeight = (int) (height - WorldManager.minLevelHeight);
if (llb != null)
{
Vector3 regionpos = new Vector3 (x - llb.region.blockCoordX, z - llb.region.blockCoordY, blockHeight);
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:45
WorldManager.cs Snippet: Placing a block in the world.
public struct brickClass
{
/// stores the type of brick.
public int brickType;
/// Stores the current health of the brick. 0 would mean it is destroyed.
public int brickHealth;
/// Stores the connection id for the player that placed the brick. Is -1 if generated by the server.
public int playerConnectionId;
}
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:45
WorldManager.cs Snippet: Generating different blocks at the start, randomly.
for (int k = 0; k < MaxBlockHeight; k += 1)
{
if (k < zc)
{
int rand = (int) UnityEngine.Random.Range (0f, 100f);
if (rand <= 70)
{
blockStructure [getIndex (i, j, k)].brickType = 1;
blockStructure [getIndex (i, j, k)].brickHealth = 1;
blockStructure [getIndex (i, j, k)].playerConnectionId = -1;
@AndrewWeston9
AndrewWeston9 / WorldManager.cs
Last active October 6, 2017 06:44
WorldManager.cs Snippet: Setting the blocks health, type, and player connection id when placing a block.
public void setBlock (int x, int y, int z, int type, int connid)
{
if ((x >= 0) && (x < blockSize) &&
(y >= 0) && (y < blockSize) &&
(z >= 0) && (z < MaxBlockHeight))
{
blockStructure[getIndex (x, y, z)].brickType = type;
blockStructure[getIndex (x, y, z)].playerConnectionId = connid;
if (type == 1) {
blockStructure[getIndex (x, y, z)].brickHealth = 1;
@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;
}
@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: 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: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: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))