private bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact) { var projectile = fixtureA.Body.UserData as Projectile; var player = fixtureB.Body.UserData as Player; if (projectile != null && player != null) { player.health -= 1; BroadcastPlayerData(); } var packet = new ProjectilePacket(); packet.pid = projectile.pid; packet.destroyed = true; for (var i = 0; i < players.Count; i++) { players[i].client.Send(packet); } entitites.Remove(fixtureA.Body); world.RemoveBody(fixtureA.Body); return(true); }
private void ReceiveProjtilePacket() { while (nwMgr.PacketQueue.HasPacket <ProjectilePacket>()) { ProjectilePacket packet = nwMgr.PacketQueue.GetNextPacket <ProjectilePacket>(); //do something with packets if (playersInfo.IsHosts[(int)myTeamRole]) { print("I am host. i have received a request to create projectile."); if (packet.instanti) { Vector3 bulletSpawnPosition = new Vector3(packet.pos.x, packet.pos.y, 6f); var bullet = Instantiate(cannonball, bulletSpawnPosition, this.transform.rotation); bullet.GetComponent <Rigidbody2D>().velocity = packet.velocity; bullet.GetComponent <Rigidbody2D>().angularVelocity = packet.AnglarVel; bullet.GetComponent <Rigidbody2D>().rotation = packet.rotation; if (packet.role == TeamRole.PurpleBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(350, 0f)); } else if (packet.role == TeamRole.YellowBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(-350, 0f)); } ProjectilePacket p = new ProjectilePacket(true, bullet); p.formHost = true; p.instanti = true; p.role = packet.role; p.ID = lastCannonId + 1; lastCannonId++; nwMgr.SendPacket("info", p); } } else { print("i am not host. i am receiving an update to the packets location"); if (packet.formHost) { if (packet.instanti) { print("i am not host. instantiating projectile."); Vector3 bulletSpawnPosition = new Vector3(packet.pos.x, packet.pos.y, 6f); var bullet = Instantiate(cannonball, bulletSpawnPosition, this.transform.rotation); if (packet.role == TeamRole.PurpleBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(350, 0f)); } else if (packet.role == TeamRole.YellowBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(-350, 0f)); } bullet.GetComponent <CannonBall>().ID = packet.ID; } } } } }
public void SendProjectile(Location from, Location to, ProjectileType projectile) { NetworkMessage outMessage = new NetworkMessage(); ProjectilePacket.Add( outMessage, from, to, projectile ); Send(outMessage); }
public void SendProjectilePacket(GameObject bullet, TeamRole role) { //if they are the host if (playersInfo.IsHosts[(int)myTeamRole]) { //get bullet obhect to make packet ProjectilePacket p = new ProjectilePacket(false, bullet); p.formHost = true; p.role = role; nwMgr.SendPacket("info", p); print("i am host. i am sending a location packet."); } else { ProjectilePacket p = new ProjectilePacket(true, bullet); p.role = role; nwMgr.SendPacket("info", p); print("I am not host, sent initial packet for projectile."); } }
public void SendProjectileFromHost(GameObject cb) { print("sendprojfromhost called."); //Vector3 bulletSpawnPosition = new Vector3(cb.transform.position.x + 1, cb.transform.position.y, 6f); var bullet = Instantiate(cannonball, cb.transform.position, this.transform.rotation); if (myTeamRole == TeamRole.PurpleBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(350, 0f)); } if (myTeamRole == TeamRole.YellowBuilder) { bullet.GetComponent <Rigidbody2D>().AddForce(new Vector2(-350, 0f)); } ProjectilePacket p = new ProjectilePacket(true, bullet); p.formHost = true; p.instanti = true; p.ID = lastCannonId + 1; p.role = myTeamRole; lastCannonId++; nwMgr.SendPacket("info", p); }
private void SendProjectile(Player player) { const float ProjectileSpawnOffset = 32.0f; var px = ConvertUnits.ToDisplayUnits(player.body.Position.X); var py = ConvertUnits.ToDisplayUnits(player.body.Position.Y); var hOrientation = player.hOrientation; var vOrientation = player.vOrientation; var velocity = new Vector2(hOrientation * 2.0f, vOrientation * 2.0f); // Add offset x. if (hOrientation == -1) { px -= ProjectileSpawnOffset; } else if (hOrientation == 1) { px += ProjectileSpawnOffset; } // Add offset y. if (vOrientation == -1) { py -= ProjectileSpawnOffset; } else if (vOrientation == 1) { py += ProjectileSpawnOffset; } var npid = pidGenerator++; // Create dynamic rectangle for the projectile. var body = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(Tiles.Width - 16), // Leave 8-pixels from size so players ConvertUnits.ToSimUnits(Tiles.Height - 16), // Can move more smoothly. 10.0f, player); body.Position = new Vector2(ConvertUnits.ToSimUnits(px), ConvertUnits.ToSimUnits(py)); body.IsStatic = false; body.Mass = 80.0f; body.Friction = 0.2f; body.Restitution = 0.2f; body.BodyType = BodyType.Dynamic; body.FixedRotation = true; body.LinearVelocity = velocity; body.UserData = new Projectile(player, npid); body.CollidesWith = Category.Cat2 | Category.Cat1; body.CollisionCategories = Category.Cat2; body.OnCollision += Body_OnCollision; entitites.Add(body); var projectilePacket = new ProjectilePacket(); projectilePacket.x = px; projectilePacket.y = py; projectilePacket.velx = velocity.X; projectilePacket.vely = velocity.Y; projectilePacket.destroyed = false; projectilePacket.pid = npid; projectilePacket.ownerGuid = player.client.Guid.ToString(); for (var i = 0; i < players.Count; i++) { players[i].client.Send(projectilePacket); } }