void CreatePlayer(NetworkConnection conn) { GameObject playerGameObject = Instantiate(playerPrefab); //instantiates playerGameObject in all clients automatically NetworkServer.AddPlayerForConnection(conn, playerGameObject); PlayerClientI playerClientI = playerGameObject.GetComponent <PlayerClientI>(); playerClients.Add(playerClientI); }
void InitPlayer(NetworkConnection conn, int orderNum) { PlayerClientI playerClientI = playerClients[orderNum]; //only create a server state for the last entered player if (orderNum == playerServerStates.Count) { //only add one server state per player PlayerServerState newPlayerState = new PlayerServerState(); newPlayerState.position = possibleSpawnPositions[selectedSpawnPositions[orderNum]].position; newPlayerState.orderNum = orderNum; if (!IsGuard(newPlayerState))//if robber create a dropout point in the position { dropoutPoints.Add(newPlayerState.position); cmge.InstantiateDropoutObject(newPlayerState.position); newPlayerState.money = 0; } else { newPlayerState.money = totalGalleryValue; } Vector3 startingRotation = new Vector3(0, possibleSpawnPositions[selectedSpawnPositions[orderNum]].eulerAngles.y, 0); newPlayerState.rotation = startingRotation; newPlayerState.carriedObjIndex = -1; playerServerStates.Add(newPlayerState); playerClientI.UpdatePlayerState(newPlayerState.position, newPlayerState.rotation); playerClientI.UpdatePlayerState(newPlayerState.position, newPlayerState.rotation); } playerClientI.moveUp = false; playerClientI.moveLeft = false; playerClientI.moveRight = false; //all these methods are broadcasted to each client playerClientI.Init(orderNum); }
private void PlayerMovementChanges() { if (!isGameReady) { return; } foreach (PlayerServerState pss in playerServerStates) { PlayerClientI playerClientI = playerClients[pss.orderNum]; if ((playerClientI.moveUp || playerClientI.moveDown) && !(playerClientI.moveUp && playerClientI.moveDown)) { var movement = playerClientI.IsGuard()? guardMovementSpeed: robberMovementSpeed; if (playerClientI.moveDown) { movement *= -(1 - backwardsMovePenalty); } float xPos = Mathf.Sin(pss.rotation.y * Mathf.Deg2Rad) * Mathf.Cos(pss.rotation.x * Mathf.Deg2Rad); float yPos = Mathf.Sin(-pss.rotation.x * Mathf.Deg2Rad); float zPos = Mathf.Cos(pss.rotation.x * Mathf.Deg2Rad) * Mathf.Cos(pss.rotation.y * Mathf.Deg2Rad); float newPosY = yPos + (float)Math.Sin(Time.time * 20) * 0.1f; Vector3 nextPosition = pss.position + new Vector3(xPos, newPosY, zPos) * movement; nextPosition.y = nextPosition.y < 0.0f ? 0.0f : nextPosition.y; nextPosition = CollisionHandling(nextPosition, colliderRadius); // transform.position += transform.forward * movement; pss.position = nextPosition; } playerClientI.UpdatePlayerState(pss.position, pss.rotation); if (playerClientI.moveRight) { pss.rotation += new Vector3(0.0f, 2.5f, 0.0f); playerClientI.UpdatePlayerState(pss.position, pss.rotation); } if (playerClientI.moveLeft) { pss.rotation -= new Vector3(0.0f, 2.5f, 0.0f); playerClientI.UpdatePlayerState(pss.position, pss.rotation); } if (playerClientI.stealPress) { if (pss.carriedObjIndex == -1) { for (int i = 0; i < selectedStealablesIndexes.Count; i++) { GameObject stealable = selectedStealables[i]; if ((stealable.transform.position - pss.position).sqrMagnitude < 20.0f) { pss.carriedObjIndex = i; if (!IsGuard(pss)) { pss.money += selectedStealableValues[pss.carriedObjIndex]; playerClients[pss.orderNum].UpdateMoney(pss.money); foreach (PlayerServerState innerPss in playerServerStates) { if (IsGuard(innerPss)) { innerPss.money -= selectedStealableValues[pss.carriedObjIndex]; } playerClients[innerPss.orderNum].UpdateMoney(innerPss.money); } } } } } else { if (!IsGuard(pss)) { bool isReadyToDropout = false; foreach (Vector3 drpPoint in dropoutPoints) { if ((drpPoint - pss.position).sqrMagnitude < 50.0f) { isReadyToDropout = true; } } if (isReadyToDropout) { selectedStealables[pss.carriedObjIndex].transform.position = new Vector3(0.0f, -100.0f, 0.0f); //remove it from reach cmge.DespawnStealable(pss.carriedObjIndex); totalStolen++; CheckIfStolenEverything(); } else { pss.money -= selectedStealableValues[pss.carriedObjIndex]; playerClients[pss.orderNum].UpdateMoney(pss.money); foreach (PlayerServerState innerPss in playerServerStates) { if (IsGuard(innerPss)) { innerPss.money += selectedStealableValues[pss.carriedObjIndex]; } playerClients[innerPss.orderNum].UpdateMoney(innerPss.money); } } } pss.carriedObjIndex = -1; } playerClientI.stealPress = false; } if (pss.carriedObjIndex != -1) { float xPos = Mathf.Sin(pss.rotation.y * Mathf.Deg2Rad) * Mathf.Cos(pss.rotation.x * Mathf.Deg2Rad); float zPos = Mathf.Cos(pss.rotation.x * Mathf.Deg2Rad) * Mathf.Cos(pss.rotation.y * Mathf.Deg2Rad); Vector3 newStealablePos = pss.position; newStealablePos.x += xPos * 1.5f; newStealablePos.y = selectedStealables[pss.carriedObjIndex].transform.position.y; newStealablePos.z += zPos * 1.5f; selectedStealables[pss.carriedObjIndex].transform.position = newStealablePos; Vector3 newStealableRot = pss.rotation; Vector3 currRot = selectedStealables[pss.carriedObjIndex].transform.rotation.eulerAngles; selectedStealables[pss.carriedObjIndex].transform.rotation = Quaternion.Euler(currRot + newStealableRot); cmge.UpdateStealableTransform(pss.carriedObjIndex, newStealablePos, newStealableRot); } // if (playerClientI.mouseMoved) // { // // } } CheckGuardCaughtRobber(playerServerStates[0], playerServerStates[1]); }