public void SetReady(Gunfish fish, bool ready) { if (!readyFish.ContainsKey(fish)) { AddGunfish(fish); } if (readyFish[fish] == ready) { return; } readyFish[fish] = ready; if (ready) { readyCount++; notReadyCount--; } else { print("Set not ready ++"); readyCount--; notReadyCount++; } RaceManager.instance.TrySwapLevel(); }
private void OnGunshot(NetworkMessage netMsg) { GunfishMsg msg = netMsg.ReadMessage <GunfishMsg>(); Gunfish gunfish = ClientScene.FindLocalObject(msg.netId).GetComponent <Gunfish>(); gunfish.DisplayShoot(); }
private void Start() { grounded = true; //Necessary to start grounded to give player control overself by default if ((gunfish = GetComponent <Gunfish>()) == null) { gunfish = transform.parent.GetComponent <Gunfish>(); } }
//We're just treating gun as a single raycaster, but making a multiraycaster should be very easy public RayHitInfo ServerShoot(Gunfish gunfish) { rb = gunfish.rb; RayHitInfo rayHitInfo = new RayHitInfo(); float angle = NetworkManager.singleton.client.GetRTT() / 1000f * rb.angularVelocity; Vector3 deltaPos = NetworkManager.singleton.client.GetRTT() / 1000f * rb.velocity; //float x = Mathf.Tan(angle * Mathf.Deg2Rad); Vector3 point = barrelPoint.transform.right; // + barrelPoint.transform.up * x; Ray ray = new Ray(barrelPoint.transform.position + deltaPos, point); //- barrelPoint.transform.position); RaycastHit2D rayHit = Physics2D.Raycast(ray.origin, ray.direction, shotInfo.distance); if (rayHit) { GameObject hit = rayHit.collider.gameObject; //if gunfish if (hit.CompareTag("Gunfish")) { rayHitInfo.netId = hit.GetComponentInParent <Gunfish>().netId; rayHitInfo.color = Color.red; rayHitInfo.hitType = HitType.Fish; } //if generic object else if (hit.CompareTag("Ground")) { //rayHitInfo.netId.Value defaults to zero rayHitInfo.color = hit.gameObject.GetComponent <SpriteRenderer>().color; rayHitInfo.hitType = HitType.Wood; } else if (hit.CompareTag("Object")) { rayHitInfo.color = hit.gameObject.GetComponent <SpriteRenderer>().color; rayHitInfo.hitType = HitType.Wood; if (hit.GetComponent <Rigidbody2D>()) { hit.GetComponent <Rigidbody2D>().AddForce(-rayHit.normal * shotInfo.force); } } rayHitInfo.normal = rayHit.normal; rayHitInfo.end = rayHit.point; } else { //if nothing was hit rayHitInfo.netId = NetworkInstanceId.Invalid; rayHitInfo.end = barrelPoint.transform.position + (transform.right * shotInfo.distance); } rayHitInfo.origin = barrelPoint.transform.position; rayHitInfo.shotType = shotType; return(rayHitInfo); }
public void AddGunfish(Gunfish fish) { readyFish.Add(fish, false); if (RaceManager.instance.gameActive) { fish.Stun(3f); } notReadyCount++; RaceManager.instance.TrySwapLevel(); }
public void OnChangeFeesh(NetworkMessage netMsg) { GunfishSelectMsg msg = netMsg.ReadMessage <GunfishSelectMsg>(); Gunfish gunfish = NetworkServer.FindLocalObject(msg.netId).GetComponent <Gunfish>(); //GunfishSelectMsg gunMsg = new GunfishSelectMsg(msg.netId, gunfish.ChangeFeesh()); //NetworkServer.SendToAll(MessageTypes.CHANGEFEEESH, msg); gunfish.ChangeFeesh(msg.index); }
public void OnGunshot(NetworkMessage netMsg) { GunfishMsg msg = netMsg.ReadMessage <GunfishMsg>(); //Get the gunfish, tell it to shoot, and get the hit information Gunfish gunfish = NetworkServer.FindLocalObject(msg.netId).GetComponent <Gunfish>(); //Presuming our gun is a single raycaster RayHitInfo rayHitInfo = gunfish.ServerShoot(gunfish); NetworkServer.SendToAll(MessageTypes.RAYHIT, new RayHitMsg(rayHitInfo)); //This message handles gunshot audio and muzzle flash NetworkServer.SendToAll(MessageTypes.GUNSHOT, new GunfishMsg(msg.netId)); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); fish = (Gunfish)target; if (GUILayout.Button("Update Network Transforms")) { NetworkTransformUtility.UpdateTransforms(fish.gameObject); } if (GUILayout.Button("Remove Network Transforms")) { NetworkTransformUtility.RemoveTransforms(fish.gameObject); } }
//When player collides with the finish box, this method is called private void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Gunfish")) { Gunfish gunfish = other.gameObject.GetComponentInParent <Gunfish>(); //gunfish.Stun(1000); foreach (var fish in finishedFish) { if (fish == gunfish) { return; } } finishedFish.Add(gunfish); int points; switch (finishedFish.Count) { case 1: points = 5; break; case 2: points = 3; break; case 3: points = 2; break; case 4: points = 1; break; default: points = 0; break; } RaceManager.instance.PlayerFinish(gunfish, points); } }
public void PlayerFinish(Gunfish gunfish, int points = 0) { if (!pointTable.ContainsKey(gunfish.connectionToClient)) { pointTable.Add(gunfish.connectionToClient, points); } else { pointTable[gunfish.connectionToClient] += points; } if (secondsRemaining > secondsToWaitAfterFirst) { secondsRemaining = secondsToWaitAfterFirst; } fishFinished.Add(gunfish); ConnectionManager.instance.SetReady(gunfish, true); TrySwapLevel(); }
public void RemoveGunfish(NetworkConnection conn) { Gunfish fish = null; foreach (var thisFish in readyFish.Keys) { if (thisFish.connectionToServer == conn) { fish = thisFish; } } if (fish == null) { return; } bool ready; if (!readyFish.TryGetValue(fish, out ready)) { return; } if (ready) { readyCount--; } else { notReadyCount--; } readyFish.Remove(fish); RaceManager.instance.TrySwapLevel(); }
private void Start() { if (!instance) { instance = this; } else { Destroy(gameObject); } DontDestroyOnLoad(gameObject); //Debug.Log("ConnId: " + connectionToServer.connectionId); NetworkManager.singleton.client.RegisterHandler(MessageTypes.DEBUGLOGMSG, OnDebugLog); NetworkManager.singleton.client.RegisterHandler(MessageTypes.RAYHIT, OnRayHit); NetworkManager.singleton.client.RegisterHandler(MessageTypes.MULTIRAYHIT, OnMultiRayHit); NetworkManager.singleton.client.RegisterHandler(MessageTypes.GUNSHOT, OnGunshot); NetworkManager.singleton.client.RegisterHandler(MessageTypes.SPAWNCROWN, SpawnCrown); foreach (Gunfish fish in FindObjectsOfType <Gunfish>()) { if (fish.hasAuthority) { ownedGunfish = fish; } } if (isClient) { MusicManager.instance.PlayMusic(); } }
public RayHitInfo ServerShoot(Gunfish gunfish) { return(gun.ServerShoot(gunfish)); }
private void CreateGunfish() { GameObject[] fishPieces = new GameObject[numberOfDivisions]; fishPieces[0] = new GameObject(fishName); float fishWidth = texture.width / gunfishSprite.pixelsPerUnit; float fishHeight = texture.height / gunfishSprite.pixelsPerUnit; LineRenderer lineFish = fishPieces[0].AddComponent <LineRenderer>(); lineFish.positionCount = numberOfDivisions; lineFish.startWidth = fishHeight; lineFish.endWidth = fishHeight; lineFish.alignment = LineAlignment.TransformZ; lineFish.material = material; for (int i = 0; i < numberOfDivisions; i++) { if (i > 0) { fishPieces [i] = new GameObject("Fish[" + i.ToString() + "]"); } fishPieces[i].layer = LayerMask.NameToLayer("Player"); //Line Renderer /****************************************************************/ LineSegment segment = fishPieces[i].AddComponent <LineSegment>(); segment.segment = lineFish; segment.index = i; /****************************************************************/ //Sprite Renderer /****************************************************************/ //SpriteRenderer sr = fishPieces[i].AddComponent<SpriteRenderer>(); //sr.sprite = sprites[i]; /****************************************************************/ //Box Collider /****************************************************************/ BoxCollider2D col = fishPieces[i].AddComponent <BoxCollider2D>(); float spacing = fishWidth / numberOfDivisions; float textureSpacing = (float)texture.width / numberOfDivisions; int height = texture.height; int textureX = Mathf.RoundToInt(textureSpacing * i); int textureStartY = 0; int textureEndY = height - 1; int textureOffset = Mathf.RoundToInt(textureSpacing / 2); for (int y = 0; y < height; y++) { if (texture.GetPixel(textureX + textureOffset, y).a > Mathf.Epsilon) { textureStartY = y; break; } } for (int y = height - 1; y >= 0; y--) { if (texture.GetPixel(textureX + textureOffset, y).a > Mathf.Epsilon) { textureEndY = y; break; } } if (textureEndY <= textureStartY) { textureStartY = 0; textureEndY = height - 1; } float resultHeight = (textureEndY - textureStartY) / (float)height * 1.4f; if (resultHeight < 0.3f) { resultHeight = 0.3f; } if (resultHeight > height / gunfishSprite.pixelsPerUnit) { resultHeight = height / gunfishSprite.pixelsPerUnit; } float midpoint = height / 2f; float offsetY = -(((textureEndY + textureStartY) / 2f) - midpoint) / gunfishSprite.pixelsPerUnit; col.size = new Vector2(spacing, resultHeight); col.offset = new Vector2(0f, offsetY); /****************************************************************/ //Rigidbody /****************************************************************/ Rigidbody2D rb = fishPieces[i].AddComponent <Rigidbody2D>(); rb.collisionDetectionMode = CollisionDetectionMode2D.Discrete; rb.mass = 1f / numberOfDivisions; /****************************************************************/ if (i > 0) { //Hinge Joint /****************************************************************/ HingeJoint2D joint = fishPieces [i].AddComponent <HingeJoint2D> (); joint.connectedBody = fishPieces [i - 1].GetComponent <Rigidbody2D> (); joint.anchor = new Vector2(-spacing / 2, 0); joint.useLimits = true; JointAngleLimits2D limits = joint.limits; limits.min = 0f; limits.max = 1f; joint.limits = limits; fishPieces[i].transform.position = fishPieces[0].transform.position + Vector3.right * spacing * i; fishPieces [i].transform.SetParent(fishPieces[0].transform); //fishPieces [i].transform.localScale = new Vector3 (1.5f, 1f, 1f); /****************************************************************/ } } //Gunfish Script /****************************************************************/ Gunfish gf = fishPieces[0].AddComponent <Gunfish>(); gf.ApplyVariableDefaults(); /****************************************************************/ //Gun /****************************************************************/ GameObject gun = Instantiate(gunList[selectedGunIndex]) as GameObject; gun.name = gun.name.Remove(gun.name.Length - 7); gun.transform.SetParent(fishPieces[0].transform); gun.transform.localPosition = Vector3.zero; gun.transform.eulerAngles = new Vector3(0f, 0f, -180f); /****************************************************************/ fishPieces[0].transform.eulerAngles = new Vector3(0f, 0f, 180f); //Flip fish around cause it upside down from LineRenderer //Create prefab of Scene instance and destroy the instance PrefabUtility.CreatePrefab(prefabPath + fishName + ".prefab", fishPieces[0]); if (!putInScene) { DestroyImmediate(fishPieces[0]); } }