public void placeBlockClick() { //decrement the gold if (myTeam.Equals(Team.Purple)) { myEcon.reduceLeftGold(currentCost); } else if (myTeam.Equals(Team.Yellow)) { myEcon.reduceRightGold(currentCost); } startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); directionChosen = false; if (debugMessage) { Debug.Log("Began mouse at: " + startPos); } GameState.Block b2 = new GameState.Block(); b2.position = new Vector3(startPos.x, startPos.y, 6f); b2.type = type; b2.role = gameManager.GetComponent<GameEngine>().getTeamRole(); gameManager.GetComponent<GameEngine>().sendBlockUpdate(b2, true); // Update statistics of number of blocks placed by me. EventManagerNetworked.Instance.BroadcastEvent(EventType.BlockPlaced, new EventManagerArg(myTeam)); }
void Update() { //Tick Cooldown explodeCooldown(); //If we pressed "X" button - **this button will probably get changed later if (player.gamepad.GetButton(InputButton.Grab) == ButtonState.JustPressed && explodeOnCooldown == false) { explodeOnCooldown = true; explodeCooldownTime = coolDown; //Debug.Log ("Smashing Basil!"); //if our list is not empty if (colliderList.Count > 0) { //destory each game object that is within our circle collider for (int i = 0; i < colliderList.Count; i++) { try { GameState.Block b2 = new GameState.Block(); b2.position = new Vector3(colliderList[i].transform.position.x, colliderList[i].transform.position.y, 6f); GameObject.Find("Game Manager").GetComponent <GameEngine>().sendBlockUpdate(b2, false); } catch (Exception e) { //can't solve this error right now, doesn't seem to be game breaking. print("uh ohh..."); } } colliderList.RemoveRange(0, colliderList.Count); } } }
private void ReceiveBlockUpdates() { while (nwMgr.PacketQueue.HasPacket <BlockUpdatePacket>()) { if (playersInfo.IsHosts[(int)myTeamRole]) { print("im host, and i got your block update packet."); INetworkConnection recvConn; BlockUpdatePacket packet = nwMgr.PacketQueue.GetNextPacket <BlockUpdatePacket>(out recvConn); if (packet.blockWasAdded) { GameState.Block b = new GameState.Block(); b.position = packet.position; b.type = packet.type; b.role = packet.role; GameObject.Find("Renderer").GetComponent <Renderer>().RenderAddBlock(b); SendGameState(); print("i'm host and i just sent a packet back after adding"); } else { GameState.Block b = new GameState.Block(); b.position = packet.position; b.type = packet.type; b.role = packet.role; GameObject.Find("Renderer").GetComponent <Renderer>().RenderRemoveBlock(b); SendGameState(); print("i'm host and i just sent a packet back after removing"); } } } }
public void sendBlockUpdate(GameState.Block b, bool add) { if (add) { if (playersInfo.IsHosts[(int)myTeamRole]) { GameObject.Find("Renderer").GetComponent <Renderer>().RenderAddBlock(b); SendGameState(); } else { BlockUpdatePacket bup = new BlockUpdatePacket(b.position, b.type, add); bup.role = b.role; nwMgr.SendPacket("info", bup); print("im not host, I'm trying to send blockupdate packet"); } } else { if (playersInfo.IsHosts[(int)myTeamRole]) { GameObject.Find("Renderer").GetComponent <Renderer>().RenderRemoveBlock(b); SendGameState(); } else { BlockUpdatePacket bup = new BlockUpdatePacket(b.position, b.type, add); bup.role = b.role; nwMgr.SendPacket("info", bup); print("im not host, I'm trying to send blockupdate packet"); } } }
void OnCollisionExit2D(Collision2D coll) { if (coll.gameObject.name == "hero 1(Clone)") { GameState.Block b2 = new GameState.Block(); b2.position = new Vector3(transform.position.x, transform.position.y, 6f); b2.type = GetComponent <Block>().type; b2.role = GetComponent <Block>().role; GameObject.Find("Game Manager").GetComponent <GameEngine>().sendBlockUpdate(b2, false); Destroy(gameObject); } }
//when a block is removed, just remove the single block O(n) public void RenderRemoveBlock(GameState.Block block) { GameObject[] blocks = GameObject.FindGameObjectsWithTag("Block"); //go through all blocks until we find the one we need to remove foreach (GameObject b in blocks) { if (b.transform.position == block.position) { if (b.gameObject.GetComponent <Block>().type.Equals("onetime")) { Destroy(b.gameObject); return; } if (b.gameObject != null) { DestroyImmediate(b.gameObject); } return; } } }
//reads in the objects in scene and generates the gamestate O(n) public GameState generateCurrentBlockState() { //only generates block states, does not do player/traps (yet) GameState state = new GameState(false); GameObject[] blocks = GameObject.FindGameObjectsWithTag("Block"); foreach (GameObject block in blocks) { try { GameState.Block b = new GameState.Block(); b.type = block.GetComponent <Block>().type; b.position = block.transform.position; b.role = block.GetComponent <Block>().role; state.blocks.Add(b); } catch (Exception e) { print("ffs"); } } state.timestamp = DateTime.Now; return(state); }
//when just a new block is sent, just render the single block O(1) public void RenderAddBlock(GameState.Block block, float delay) { GameObject[] blocks = GameObject.FindGameObjectsWithTag("Block"); GameObject added; GameObject addedHand; //go through all blocks to see if one is already at that locattion foreach (GameObject b in blocks) { print("pos1: " + b.transform.position); print("pos2: " + block.position); if ((Math.Abs(b.transform.position.x - block.position.x) < 0.5) && (Math.Abs(b.transform.position.y - block.position.y) < 0.5)) { //don't place block if there is already one there return; } } //adds block to screen, and snaps it to nearest 0.5 (snapping to be used for "grid size" later) if (block.type.Equals("normal") /* && block.x > -90*/) { added = Instantiate(NormalBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("fire") /* && block.x > -90*/) { added = Instantiate(fireBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("ice") /*&& block.x > -90*/) { added = Instantiate(iceBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } //<<<<<<< HEAD else if (block.type.Equals("mine") /* && block.x > -90*/) { added = Instantiate(mineBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("smoke") /* && block.x > -90*/) { added = Instantiate(smokeBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("cannon") /*&& block.x > -90*/) { added = Instantiate(cannonBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("temp") /*&& block.x > -90*/) { added = Instantiate(tempBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("tar")) { added = Instantiate(tarBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("jumppad")) { added = Instantiate(jumpPadBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("ghost")) { added = Instantiate(ghostBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("onetime")) { added = Instantiate(oneTimeBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } else if (block.type.Equals("spikes")) { added = Instantiate(spikeBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } // else { //======= //else if (block.type.Equals("cannon") /*&& block.x > -90*/) //{ // added = Instantiate(cannonTrap, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), // Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); //} //else if (block.type.Equals("smokebomb") /*&& block.x > -90*/) //{ // added = Instantiate(smokebomb, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), // Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); //} else { //>>>>>>> develop added = Instantiate(NormalBlock, new Vector3(Mathf.Sign(block.position.x) * (Mathf.Abs((int)block.position.x) + 0.5f), Mathf.Sign(block.position.y) * (Mathf.Abs((int)block.position.y) + 0.5f), 6f), Quaternion.identity); } added.GetComponent <Block>().delayFromServer = delay; added.GetComponent <Block>().role = block.role; //tint blocks based on team. SpriteRenderer[] pencilColor = added.GetComponentsInChildren <SpriteRenderer>(); if (added.GetComponent <Block>().role == Doodle.Game.TeamRole.PurpleBuilder || added.GetComponent <Block>().role == Doodle.Game.TeamRole.PurpleRunner) { added.transform.GetComponent <SpriteRenderer>().color = new Color32(0xff, 0x90, 0xdc, 0xFF); //RGBA if (!block.type.Equals("smoke")) { pencilColor[1].color = new Color32(0xff, 0x90, 0xdc, 0xFF); //RGBA } else { pencilColor[0].color = new Color32(0xff, 0x90, 0xdc, 0xFF); //RGBA } } else { added.transform.GetComponent <SpriteRenderer>().color = new Color32(0xff, 0xff, 0x00, 0xFF); //RGBA if (!block.type.Equals("smoke")) { pencilColor[1].color = new Color32(0xff, 0xff, 0x00, 0xFF); //RGBA } else { pencilColor[0].color = new Color32(0xff, 0xff, 0x00, 0xFF); //RGBA } } }
//defaults delay to 0 public void RenderAddBlock(GameState.Block block) { RenderAddBlock(block, 0); }