[Command] void CmdReassign(int unit, int squad) { FPControl reFP = null; Squad reSquad = null; foreach (FPControl fp in GameObject.FindObjectsOfType <FPControl>()) { if (fp.unitId == unit /* && fp.team == team*/) // Units have team-independent IDs { reFP = fp; break; } } foreach (Squad sq in GameObject.FindObjectsOfType <Squad>()) { if (sq.id == squad && sq.team == team) { reSquad = sq; break; } } if (reFP != null && reSquad != null) { reFP.Reassign(reSquad); } else { Debug.LogError("Reassign Components not found:\nFPControl: " + reFP + "\nSquad: " + reSquad); } }
void CmdReplacePlayer(NetworkInstanceId netId, bool sentFromServer) { Debug.Log("Replace Player Command"); //if(player!=null) // player.GetComponent<NetworkIdentity>().RemoveClientAuthority(connectionToClient); GameObject obj = NetworkServer.FindLocalObject(netId); player = obj.GetComponent <FPControl>(); //player = pointingUnit; if (sentFromServer) { player.control = this; player.hitIndicator = hitIndicator; player.dmgIndicator = dmgIndicator; player.gunMesh.enabled = false; player.charMesh.enabled = false; player.squad.isCommanded = true; } //NetworkServer.ReplacePlayerForConnection(connectionToClient, obj, playerControllerId); obj.GetComponent <NetworkIdentity>().AssignClientAuthority(/*(player.team==0) ?connectionToClient :*/ connectionToClient); //CLIENT ONLY obj.GetComponent<FPControl>().control = this; //obj.GetComponent<AIControl>(); //Set values in 'player' for the host //CLIENT ONLY player.control = this; //CLIENT ONLY player.hitIndicator = hitIndicator; //CLIENT ONLY player.dmgIndicator = dmgIndicator; }
void DamageAlert(int id, int amount, Vector3 dir, Vector3 point, int newHealth) { Debug.Log("DamageAlert"); FPControl target = null; foreach (FPControl fp in FindObjectsOfType <FPControl>()) { if (fp.unitId == id) { target = fp; break; } } if (target != null) { if (target.hasAuthority) { target.Damage(amount, dir, point); } else { target.OnDamage(amount, dir, point); } } }
private bool RayHit(RaycastHit hit) //Returns true if hit an enemy { end = hit.point; rangeFinal = hit.distance; FPControl fp = hit.collider.gameObject.GetComponent <FPControl>(); if (fp != null) { if (hitIndicator != null) { Color c = hitIndicator.color; c.a = 1f; hitIndicator.color = c; } if (!isEffect) { DamageUnit(fp, 25, hit.point); } return(true); } else { GameObject.FindObjectOfType <SplatParticles>().CreateParticle(hit, Manager.teamColors[team]); return(false); } }
void Spawn(int count, NetworkPlayer player) { Squad newSq = GameObject.Instantiate(squadPrefab, squadSpawns[sqSpawnInd].transform.position, squadPrefab.transform.rotation) .GetComponent <Squad>(); sqSpawnInd++; if (sqSpawnInd == squadSpawns.Length) { sqSpawnInd = 0; } for (int i = 0; i < count; i++) { int ind; if (spawnPositions.Length == 1) { ind = 0; } else { ind = (int)Mathf.Repeat(i, spawnPositions.Length - 1); } GameObject obj = GameObject.Instantiate(unitPrefab, spawnPositions[ind].transform.position, spawnPositions[ind].transform.rotation); FPControl newFp = obj.GetComponent <FPControl>(); newFp.Init(team); newFp.Assign(newSq); //NetworkServer.SpawnWithClientAuthority(obj, player.ipAddress/*connectionToClient*/); } }
void OnTriggerEnter(Collider other) { FPControl otherFP = other.gameObject.GetComponent <FPControl>(); if (otherFP != null && otherFP.team != control.team) //If enterer is on other team { control.unitsInVisCone.Add(otherFP); } }
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) { spawnPositions = GameObject.FindGameObjectsWithTag("Spawn Unit " + connTeams[conn]); squadSpawns = GameObject.FindGameObjectsWithTag("Spawn Squad " + connTeams[conn]); GameObject obj = GameObject.Instantiate(unitPrefab, spawnPositions[0].transform.position, spawnPositions[0].transform.rotation); FPControl newFp = obj.GetComponent <FPControl>(); newFp.Init(connTeams[conn] /*connNum*/); NetworkServer.AddPlayerForConnection(conn, obj, playerControllerId); //newFp.GetComponent<NetworkIdentity>().AssignClientAuthority(conn); }
public void DamageUnit(FPControl fp, int amount, Vector3 point) //Called to damage this controller, isFromServer defaults to be "sent" from other side { if (fp.hasAuthority) { fp.Damage(amount, -direction, point); } else { fp.OnDamage(amount, -direction, point); } if (fp.playerControl != null) //Player may not have joined yet; IF COMMANDING PLAYER HAS NOT JOINED, THIS WILL NOT DEAL DAMAGE. HOWEVER, THIS SHOULD NEVER OCCUR. { Manager.bulletHits.Add(new Manager.HitInfo(fp.unitId, amount, -direction, point, fp.health /*, (isServer ?1 :0)*/)); //This necessary to get around Unity's built-in refusal to let Bullet send Cmds } }
void OnTriggerEnter(Collider other) { if (target == null) //If no current target { FPControl otherFP = other.gameObject.GetComponent <FPControl>(); if (otherFP != null && otherFP.team != fp.team) //If enterer is on other team { target = other.gameObject; //Set target and ONLY target so CheckLOS() can run if (CheckLOS()) { SetTarget(other.gameObject); } else { target = null; //Set to null once finished checking } } } }
void Update() { if (!isLocalPlayer) { return; } if (Input.GetKeyDown(KeyCode.Tab) && !paused) //Can't tab in/out while paused { if (player != null || !commandMode) //If no player and in command mode, no tabbing out { commandMode = !commandMode; } } if (Input.GetKeyDown(KeyCode.Escape) && !paused) { paused = true; } if (commandMode || paused) { cursorEngaged = false; } else { cursorEngaged = true; } Cursor.visible = !cursorEngaged; Cursor.lockState = cursorEngaged ?CursorLockMode.Locked :CursorLockMode.None; // Debug.Log(paused+", "+ Cursor.lockState); if (paused) { pauseCanvas.enabled = true; //TODO: pause menu logic } else { pauseCanvas.enabled = false; if (isPlacing) { if (sqPlacing != null) { sqPlacing.transform.position = mouseToWorld(/*new Vector2(Screen.width, Screen.height)*/); } else if (wpPlacing != null) { wpPlacing.transform.position = mouseToWorld(); } } } if (commandLerp == 1) { commandStuff.SetActive(commandMode); foreach (FPControl l in visLightUnits) { if (!l.isDead) { l.enabled = true; } } } else { foreach (FPControl l in visLightUnits) { l.visLight.enabled = false; } } if (commandMode) { if (Input.GetMouseButtonDown(0) && isPlacing) { if (wpPlacing != null) { wpPlacing.placed = true; } isPlacing = false; sqPlacing = null; wpPlacing = null; } else if (Input.GetMouseButtonDown(1) && isPlacing) // Right-click to cancel { if (sqPlacing != null) { DeleteSquad(sqPlacing); //Destroy(sqPlacing.gameObject); //numSquads--; } if (wpPlacing != null) { Destroy(wpPlacing.gameObject); wpPlacing.squad.waypoints.Remove(wpPlacing); wpPlacing = null; } } else if (!isPlacing) //Can't do mouse interaction while placing a squad or waypoint { FPControl pointingUnit = null; Squad pointingSquad = null; Waypoint pointingWayp = null; Debug.DrawRay(mouseToWorld(), Vector3.up, Color.black); Collider[] founds = Physics.OverlapSphere(mouseToWorld(), 0.1f); foreach (Collider f in founds) { if (f.tag == "Squad") { pointingSquad = f.GetComponent <Squad>(); } else if (f.tag == "Waypoint") { pointingWayp = f.GetComponent <Waypoint>(); } else if (f.tag == "Unit Select" /* && f.GetComponentInParent<FPControl>()!=null*/) { if (f.GetComponentInParent <FPControl>().team == team) { pointingUnit = f.GetComponentInParent <FPControl>(); } break; } } if (pointingUnit != null) { pointingUnit.highlighted = true; if (Input.GetMouseButtonDown(0)) { if (player != null) { player.control = player.gameObject.GetComponent <AIControl>(); //Make previous controlled player an AI player.hitIndicator = null; player.dmgIndicator = null; player.gunMesh.enabled = true; player.charMesh.enabled = true; player.squad.isCommanded = false; } if (!isServer) //Server value is taken care of in the command { player = pointingUnit; player.control = this; player.hitIndicator = hitIndicator; player.dmgIndicator = dmgIndicator; player.gunMesh.enabled = false; player.charMesh.enabled = false; player.squad.isCommanded = true; } //if(NetworkServer.ReplacePlayerForConnection(connectionToClient, player.gameObject, 0)) // Debug.Log("switched"); HUDCanvas.enabled = true; //can't switch to dead unit, so re-enable HUD, as it may have been disabled by death //NetworkServer.ReplacePlayerForConnection(connectionToClient, f.gameObject, playerControllerId); // DOES NOT WORK; CANNOT RUN ON CLIENT GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);//TODO: does this solve the client not being able to send? CmdReplacePlayer(pointingUnit.netId, isServer); } else if (Input.GetMouseButtonDown(1)) //Right-click; remove player authority { if (pointingUnit == player) { if (player != null) { player.control = player.gameObject.GetComponent <AIControl>(); //Make previous controlled player an AI player.hitIndicator = null; player.dmgIndicator = null; player.gunMesh.enabled = true; player.charMesh.enabled = true; player.squad.isCommanded = false; } player = null; CmdRemovePlayer(); } } } else if (pointingSquad != null) { pointingSquad.highlighted = true; if (Input.GetAxis("Mouse ScrollWheel") != 0f) { pointingSquad.wantedMembers = Mathf.Clamp(pointingSquad.wantedMembers + (int)Input.GetAxis("Mouse ScrollWheel"), 0 /*1*/, 10); pointingSquad.UpdateMembers(); } if (Input.GetMouseButtonDown(0) && pointingSquad.waypoints.Count == 0) //If left-click and no waypoints { NewWaypoint(pointingSquad); } for (int i = 0; i < numpadCodes.Length; i++) { if (Input.GetKeyDown(numpadCodes[i]) || Input.GetKeyDown(alphaNumCodes[i])) { pointingSquad.signal = i; break; } } if (Input.GetKeyDown(KeyCode.Delete)) //Remove Squad { DeleteSquad(pointingSquad); } } else if (pointingWayp != null) { pointingWayp.highlighted = true; if (Input.GetMouseButtonDown(0) && pointingWayp.squad.waypoints.Count - 1 == pointingWayp.index) //If left-click and is final waypoint { NewWaypoint(pointingWayp.squad); } if (Input.GetMouseButtonDown(1)) //If right-click, destroy { pointingWayp.squad.RemoveWaypoint(pointingWayp.index); } for (int i = 0; i < numpadCodes.Length; i++) { if (Input.GetKeyDown(numpadCodes[i]) || Input.GetKeyDown(alphaNumCodes[i])) { pointingWayp.signal = i; break; } } } } } else { if (wpPlacing != null) { wpPlacing.placed = true; } isPlacing = false; sqPlacing = null; wpPlacing = null; } if (ragdoll != null) { Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, Quaternion.LookRotation(ragdoll.position - Camera.main.transform.position), Time.deltaTime * lerpCamSpeed); Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, ragdoll.position + lerpCamPos, Time.deltaTime * lerpCamSpeed); } if (player != null && !commandMode) { Vector3 mmVec = player.transform.position; mmVec.y = minimapCamera.transform.position.y; minimapCamera.transform.position = mmVec; } if (player != null) { if (player.health < healthSlider.value) { healthDotWait += Time.deltaTime; if (healthDotWait > 0.05f) { GameObject dotObj = Instantiate(healthDotPrefab, healthSlider.transform); dotObj.GetComponent <Image>().color = Manager.teamColors[team]; dotObj.GetComponent <RectTransform>().anchoredPosition = new Vector3((healthSlider.value * 3.20f) - 20, Random.Range(-40, 40), 0); healthDotWait = 0f; } } healthSlider.value = Mathf.Lerp(healthSlider.value, player.health, Time.deltaTime * 3f); /*float interpPerSec = 100f; * if(Mathf.Abs(player.health-healthSlider.value) < interpPerSec*Time.deltaTime) * { * healthSlider.value = player.health; * }else * { * healthSlider.value += interpPerSec*Time.deltaTime * Mathf.Sign(player.health - healthSlider.value); * }*/ } if (commandMode) { ragdoll = null; commandLerp += Time.deltaTime * lerpPerSec; commandLerp = Mathf.Clamp(commandLerp, 0, 1); if (player == null || player.isDead) { Vector2 moveVec = Vector2.zero; if (Input.GetKey(KeyCode.W)) { moveVec.y++; } if (Input.GetKey(KeyCode.S)) { moveVec.y--; } if (Input.GetKey(KeyCode.D)) { moveVec.x++; } if (Input.GetKey(KeyCode.A)) { moveVec.x--; } moveVec.Normalize(); minimapCamera.transform.position += 20f * Time.deltaTime * new Vector3(moveVec.x, 0f, moveVec.y); //TODO: maybe replace '5f' with inspector parameter } } else { commandLerp -= Time.deltaTime * lerpPerSec; commandLerp = Mathf.Clamp(commandLerp, 0, 1); } minimapMask.localScale = Vector3.Lerp(Vector3.one, new Vector3(Screen.width / 200f, Screen.height / 200f, 0), commandLerp); Vector3 newImgScale = minimapImage.localScale; if (minimapMask.localScale.x > minimapMask.localScale.y) { newImgScale.y = minimapMask.localScale.x / minimapMask.localScale.y; } else { newImgScale.x = minimapMask.localScale.y / minimapMask.localScale.x; } minimapImage.localScale = Mathf.Max(minimapMask.localScale.x, minimapMask.localScale.y) * new Vector3(1 / minimapMask.localScale.x, 1 / minimapMask.localScale.y, 1); //newImgScale; minimapCanvas.sizeDelta = 200 * new Vector2(minimapMask.localScale.x, minimapMask.localScale.y); //minimapMask.localScale = Vector3.one * ( commandLerp*(Mathf.Max(Screen.width, Screen.height)-200)/200 +1);// /HUDCanvas.referencePixelsPerUnit minimapCamera.orthographicSize = 15 * Mathf.Max(minimapMask.localScale.x, minimapMask.localScale.y); foreach (Manager.HitInfo hit in Manager.bulletHits) //This necessary to get around Unity's built-in refusal to let Bullet send Cmds { if (isServer) { RpcDamageAlert(hit.unitId, hit.amount, hit.dir, hit.point, hit.newHealth); } else // if is client { CmdDamageAlert(hit.unitId, hit.amount, hit.dir, hit.point, hit.newHealth); } } Manager.bulletHits.Clear(); }