//create bullet public static GameObject CreateBullet(float x, float y, Quaternion rot, float damage, float speed, Boolean sentByRemote, Boolean firedByWhale, WeaponTypes weaponUsed) { GameObject bullet = new GameObject("Bullet"); bullet.transform.position = new Vector2(x, y); bullet.transform.localScale = new Vector2(0.5F, 0.5F); if (firedByWhale) { bullet.layer = 11; } else { bullet.layer = 10; } SpriteRenderer renderer = bullet.AddComponent <SpriteRenderer>(); BoxCollider2D collider = bullet.AddComponent <BoxCollider2D>(); collider.size = new Vector2(0.25F, 0.1F); BulletPhysicsScript physics = bullet.AddComponent <BulletPhysicsScript>(); physics.canCollide = sentByRemote; Rigidbody2D body = bullet.AddComponent <Rigidbody2D>(); body.mass = 1F; body.collisionDetectionMode = CollisionDetectionMode2D.Continuous; body.gravityScale = 0; bullet.transform.rotation = rot; physics.bulletDamage = damage; if (weaponUsed.Equals(WeaponTypes.nematocyst)) { renderer.sprite = SpriteHandler.jellySpineSprite; } else if (weaponUsed.Equals(WeaponTypes.harpoonGun)) { renderer.sprite = SpriteHandler.harpoonSprite; } else if (weaponUsed.Equals(WeaponTypes.eggBazooka)) { renderer.sprite = SpriteHandler.eggSprite; } renderer.sortingOrder = -2; body.AddRelativeForce(new Vector2(speed, 0), ForceMode2D.Impulse); return(bullet); }
//send data to server private List <String> ReadWriteServer() { List <String> data = new List <String>(); NetworkStream nwStream = clientInstance.GetStream(); StreamWriter writer = new StreamWriter(nwStream); StreamReader reader = new StreamReader(writer.BaseStream); //write player data if (!sentBaseData) { String command = "n"; writer.Flush(); writer.WriteLine(command); writer.Flush(); writer.WriteLine(player.transform.position.x); writer.Flush(); writer.WriteLine(player.transform.position.y); writer.Flush(); writer.WriteLine(player.transform.GetComponent <Rigidbody2D>().rotation); writer.Flush(); writer.WriteLine(type.ToString()); writer.Flush(); writer.WriteLine(player.GetComponent <PlayerData>().health + "/" + player.GetComponent <PlayerData>().maxHealth); writer.Flush(); writer.WriteLine(player.transform.Find("Weapon").transform.rotation.z); writer.Flush(); writer.WriteLine(weaponType); writer.Flush(); writer.WriteLine(vestType); writer.Flush(); writer.WriteLine(hatType); writer.Flush(); writer.WriteLine(mouthpieceType); writer.Flush(); writer.WriteLine(eyepieceType); writer.Flush(); writer.WriteLine(playerLevel); writer.Flush(); sentBaseData = true; } else { String command = "u"; writer.Flush(); writer.WriteLine(command); writer.Flush(); writer.WriteLine(player.transform.position.x); writer.Flush(); writer.WriteLine(player.transform.position.y); writer.Flush(); writer.WriteLine(player.transform.GetComponent <Rigidbody2D>().rotation); writer.Flush(); writer.WriteLine(player.GetComponent <PlayerData>().health + "/" + player.GetComponent <PlayerData>().maxHealth); writer.Flush(); writer.WriteLine(player.transform.Find("Weapon").transform.rotation.z); writer.Flush(); } //write bullet data foreach (GameObject bullet in bulletsFiredByPlayer) { if (bullet != null) { BulletPhysicsScript physics = bullet.GetComponent <BulletPhysicsScript>(); writer.WriteLine(bullet.transform.position.x); writer.Flush(); writer.WriteLine(bullet.transform.position.y); writer.Flush(); writer.WriteLine(bullet.transform.rotation.eulerAngles.z); writer.Flush(); writer.WriteLine(physics.bulletDamage); writer.Flush(); writer.WriteLine(physics.bulletSpeed); writer.Flush(); } else { continue; } } writer.WriteLine("ENDOFBULLETS"); writer.Flush(); //write end writer.WriteLine("END"); writer.Flush(); //read data Stopwatch watch = Stopwatch.StartNew(); if (nwStream.DataAvailable) { String line; while (!(line = reader.ReadLine()).Equals("END")) { if (!line.Equals("")) { data.Add(line); } if (line.Equals("") || line == null || watch.ElapsedMilliseconds >= 3000) //fail safe, this is hanging some times { break; } } } watch.Stop(); //update ping label GameObject.Find("pingText").GetComponent <UnityEngine.UI.Text>().text = watch.ElapsedMilliseconds / amountOfDataPerNewPlayer + "ms"; return(data); }