Exemplo n.º 1
0
        CreateNew
            (NetworkedPlayerController controller)
        {
            MyContract.RequireArgumentNotNull(
                controller,
                "NetworkedPlayerController"
                );

            PlayerIdentifier ExtantId
                = controller
                  .gameObject
                  .GetComponent <PlayerIdentifier>();
            PlayerIdentifier NewId = ExtantId;

            if (ExtantId == null)
            {
                NewId = controller
                        .gameObject
                        .AddComponent <PlayerIdentifier>();
                NewId.SetBackingController(controller);
                Debug.Log(
                    "No existing PlayerIdentifier for "
                    + NewId.ToString()
                    + "\nCreating new PlayerIdentifier with net id "
                    + NewId.netId
                    );
            }
            return(NewId);
        }
 LocalShipDestroyedAction
     (PlayerIdentifier killer, Vector3 deathLocation)
 {
     Debug.Log("Our player is dead!");
     this.transform.position = deathLocation;
     LocalPlayerShipDestroyed(killer);
     StartCoroutine(RequestRespawn());
 }
Exemplo n.º 3
0
 /// <summary>
 /// I would expand this to include a new parameter
 /// of enum HitType with value of PHASER_BOLT etc.
 /// </summary>
 public void OnProjectileHit(PlayerIdentifier shooter)
 {
     // Stops useless messages propagating
     // Easier to reason about with one path
     if (isServer)
     {
         TakeDamage(1.0, shooter);
     }
 }
        public void OnServerDisconnect(NetworkConnection conn)
        {
            PlayerDisconnectedHandler handler = PlayerDisconnected;

            if (oem.shouldTriggerEvent(handler))
            {
                handler(PlayerIdentifier.CreateNew(conn));
            }
        }
Exemplo n.º 5
0
 OnKillEvent
     (PlayerIdentifier hunter,
     PlayerIdentifier hunted)
 {
     Debug.Log(PrintPlayerScore());
     MyContract.RequireField(PlayerScore.ContainsKey(hunter.PlayerID),
                             "contains an entry for netid " + hunter.ToString(),
                             "PlayerScore");
     PlayerScore[hunter.PlayerID] += 1;
     ScoreUpdate(hunter, PlayerScore[hunter.PlayerID]);
 }
 ShipDestroyedClientAction
     (PlayerIdentifier killer,
      Vector3 deathLocation,
      Vector3 deathEulerRotation)
 {
     AnyShipDestroyedAction(deathLocation, deathEulerRotation);
     if (hasAuthority)
     {
         LocalShipDestroyedAction(killer, deathLocation);
     }
 }
Exemplo n.º 7
0
 GetScoreList()
 {
     return(PlayerScore
            .Select(
                kvp =>
                new KeyValuePair <PlayerIdentifier, int>
                    (PlayerIdentifier.CreateNew(kvp.Key),
                    kvp.Value)
                )
            .ToList());
 }
Exemplo n.º 8
0
        public void OnLocalPlayerShipDestroyed(PlayerIdentifier killer, float respawnDelay)
        {
            Debug.Log("UIManager: received player kill message - swapping to respawn UI");
            TransitionToUIElements(UiElementTransitionType.Subtractive, UIElements.GameplayUI);
            TransitionToUIElements(UiElementTransitionType.Additive, UIElements.Respawn);
            RespawnUIManager RespawnUI =
                ComponentRegistry
                .RetrieveManager <RespawnUIManager>(UIElements.Respawn);

            RespawnUI.SetKiller(killer);
            RespawnUI.StartTimer(respawnDelay);
        }
 ShipDestroyedServerAction
     (PlayerIdentifier killer,
      Vector3 deathLocation,
      Vector3 deathEulerRotation)
 {
     Debug.Log("Ship destroyed - taking server action");
     ShipSpawned = false;
     ShipDestroyed.Invoke(killer);
     Vector3 respawn_location = chooseSpawnLocation();
     StartCoroutine(destroyShipWithDelayCoroutine());
     StartCoroutine(EnableRespawnAfterDelayCoroutine());
 }
Exemplo n.º 10
0
        public void AddNewPlayer(PlayerIdentifier player, int score)
        {
            MyContract.RequireArgumentNotNull(player, "player");
            MyContract.RequireField(
                !ScoreElement.ContainsKey(player.PlayerID),
                "does not already contain an entry for player "
                + player.ToString(),
                "ScoreElement"
                );

            Debug.Log(
                "ScoreboardUIManager: Adding new element ["
                + player.ToString()
                + " -> "
                + score + "]"
                );

            GameObject NewScoreUiElement;
            string     DisplayName = "[Uninitialised]";

            if (player.PlayerID == LocalPlayerId)
            {
                NewScoreUiElement
                            = Instantiate(LocalPlayerScoreUiElementPrefab);
                DisplayName = player.ToString() + " (Me)";
            }
            else
            {
                NewScoreUiElement
                            = Instantiate(ScoreUiElementPrefab);
                DisplayName = player.ToString();
            }

            NewScoreUiElement.transform.SetParent(UiElementListParent, false);

            ScoreUiElementManager ElementManager
                = NewScoreUiElement.GetComponent <ScoreUiElementManager>();

            // This is programmer/compile-time error if it occurs
            // as in, this shouldn't be needed in production code
            MyContract.RequireFieldNotNull(ElementManager, "ScoreUiElementManager");
            ElementManager.Initialise(DisplayName, score);
            NewScoreUiElement.SetActive(true);

            ScoreUiElements.Add(ElementManager);
            SortScoreElements();
            ScoreElement.Add(player.PlayerID, ElementManager);
        }
Exemplo n.º 11
0
        public void OnPlayerRemovedFromScoreboard(PlayerIdentifier playerId)
        {
            MyContract.RequireField(
                ComponentRegistry.Contains(UIElements.Scoreboard),
                "contains a scoreboard",
                "ComponentRegistry"
                );

            ComponentRegistry
            .RetrieveManager <ScoreboardUiManager>(UIElements.Scoreboard)
            .RemovePlayer(playerId);
            // Debug
            Debug.Log("Player "
                      + playerId.PlayerID
                      + " was removed");
        }
Exemplo n.º 12
0
 public void RemovePlayer(PlayerIdentifier playerId)
 {
     MyContract.RequireArgumentNotNull(playerId, "newPlayerId");
     MyContract.RequireField(
         PlayerScore.ContainsKey(playerId.PlayerID),
         "PlayerScore contains the player ID "
         + playerId.ToString(),
         "playerId " + playerId.ToString()
         );
     PlayerScore.Remove(playerId.PlayerID);
     Debug.Log("Scoreboard: Removed player id "
               + playerId.ToString()
               + " from the scoreboard.");
     Debug.Log(PrintPlayerScore());
     if (oem.shouldTriggerEvent(PlayerRemoved))
     {
         PlayerRemoved(playerId);
     }
 }
Exemplo n.º 13
0
 public void RegisterNewPlayer(PlayerIdentifier newPlayerId)
 {
     MyContract.RequireArgumentNotNull(newPlayerId, "newPlayerId");
     MyContract.RequireField(
         !PlayerScore.ContainsKey(newPlayerId.PlayerID),
         "PlayerScore does not contain the new player ID "
         + newPlayerId.ToString(),
         "newPlayerId " + newPlayerId.ToString()
         );
     PlayerScore.Add(newPlayerId.PlayerID, InitialScore);
     Debug.Log("Scoreboard: Added player id "
               + newPlayerId.ToString()
               + " to the scoreboard.");
     Debug.Log(PrintPlayerScore());
     if (oem.shouldTriggerEvent(ScoreUpdate))
     {
         ScoreUpdate(newPlayerId, InitialScore);
     }
 }
Exemplo n.º 14
0
 private void TakeDamage(double amount, PlayerIdentifier shooter)
 {
     Debug.Log("Taking Damage");
     if (amount >= Health)
     {
         Health = 0;
         KillThisUnit(shooter);
     }
     else
     {
         Health -= amount;
         HealthChangeHandler handler = EventHealthChanged;
         if (oem.shouldTriggerEvent(handler))
         {
             Debug.Log("Triggering Health change event");
             handler(Health);
         }
     }
 }
Exemplo n.º 15
0
        public void OnPlayerJoin(NetworkedPlayerController playerController)
        {
            MyContract.RequireArgumentNotNull(playerController, "playerController");
            PlayerIdentifier NewPlayerId = PlayerIdentifier.CreateNew(playerController);

            playerController.ShipDestroyed
                += delegate(PlayerIdentifier hunter)
                {
                Scoreboard.OnKillEvent(hunter, NewPlayerId);
                };
            Scoreboard.ScoreUpdate += playerController.OnScoreUpdate;
            Scoreboard.PlayerRemoved
                += playerController.OnPlayerRemovedFromScoreboardPassthrough;
            Scoreboard.RegisterNewPlayer(NewPlayerId);
            //Debug.Log("Game State Manager: Checking if we have a spaceship class manager");
            MyContract.RequireFieldNotNull(SSClassManager, "Spaceship Class Manager");
            //Debug.Log("Game State Manager: Initialising given NPC with our SSCManager");
            playerController.initialiseShipClassManager(SSClassManager);
        }
Exemplo n.º 16
0
        OnScoreUpdate
            (PlayerIdentifier playerId,
            int newScore)
        {
            MyContract.RequireField(
                ComponentRegistry.Contains(UIElements.Scoreboard),
                "contains a scoreboard",
                "ComponentRegistry"
                );

            ComponentRegistry
            .RetrieveManager <ScoreboardUiManager>(UIElements.Scoreboard)
            .ChangePlayerScore(playerId, newScore);
            // Debug
            Debug.Log("Score was updated for player "
                      + playerId.PlayerID
                      + " to new score "
                      + newScore);
        }
Exemplo n.º 17
0
 public void ChangePlayerScore(PlayerIdentifier player, int score)
 {
     MyContract.RequireArgumentNotNull(player, "player");
     if (ScoreElement.ContainsKey(player.PlayerID))
     {
         ScoreElement[player.PlayerID].UpdateScore(score);
         SortScoreElements();
     }
     else
     {
         Debug.Log(
             "ScoreboardUIManager: Received a ChangePlayerScore "
             + "request for an unregistered player ("
             + player.ToString()
             + ")"
             );
         AddNewPlayer(player, score);
     }
 }
Exemplo n.º 18
0
 private void KillThisUnit(PlayerIdentifier killer)
 {
     // This lock was put in as a result of bugs,
     // it's not just experimental!
     lock (DeathLock)
     {
         if (!IsDead) // If we're the first responder
         {
             Debug.Log("Player is dead!");
             IsDead = true;
             DeathHandler handler = EventDeath;
             if (oem.shouldTriggerEvent(handler))
             {
                 handler(
                     killer,
                     transform.position,
                     transform.eulerAngles
                     );
             }
         }
     }
 }
Exemplo n.º 19
0
        public void RemovePlayer(PlayerIdentifier player)
        {
            MyContract.RequireArgumentNotNull(player, "player");
            MyContract.RequireField(
                ScoreElement.ContainsKey(player.PlayerID),
                "Contains an entry for player "
                + player.ToString(),
                "ScoreElement"
                );

            ScoreUiElementManager ElementManager = ScoreElement[player.PlayerID];

            ScoreElement.Remove(player.PlayerID);
            ScoreUiElements.Remove(ElementManager);
            // If needed this can be optimised by pooling the
            // score ui elements,
            // so when a player is removed they are deactivated,
            // then when a new player is added they try to reactivate
            // an old slot before instantiating a new one.
            Destroy(ElementManager.gameObject);
            //SortScoreElements(); should remain sorted
        }
 public void OnPlayerRemovedFromScoreboardPassthrough (PlayerIdentifier playerId)
 {
     EventPlayerRemovedFromScoreboard(playerId);
 }
Exemplo n.º 21
0
 public void OnPlayerDisconnect(PlayerIdentifier player)
 {
     Scoreboard.RemovePlayer(player);
 }
 public void OnScoreUpdate(PlayerIdentifier playerId, int newScore)
 {
     EventScoreUpdated(playerId, newScore);
 }
        private void SpawnSpaceShip(SpaceShipClass spaceShipType)
        {
            lock (SpaceshipSpawnLock)
            {
                if (ShipSpawned)
                {
                    Debug.LogWarning(ShipAlreadySpawnedWarning);
                    return;
                }
                MyContract.RequireArgument(
                    spaceShipType != SpaceShipClass.NONE,
                    "is not NONE",
                    "spaceShipType"
                );
                MyContract.RequireFieldNotNull(
                    SpaceshipClassManager,
                    "Spaceship Class Manager"
                );
                GameObject SpaceshipPrefab
                    = SpaceshipClassManager.getSpaceShipPrefab(spaceShipType);
                MyContract.RequireFieldNotNull(SpaceshipPrefab, "Spaceship Prefab");

                // Should not remain null unless Unity.Instantiate can return null
                GameObject ServerSpaceship = null;
                if (CurrentSpaceship != null
                && ShipController.getSpaceshipClass() == spaceShipType)
                {
                    // current_spaceship was just despawned, not destroyed,
                    // so it simply needs to be respawned
                    ServerSpaceship = CurrentSpaceship;
                    ServerSpaceship.SetActive(true);
                    MyContract.RequireFieldNotNull(
                        ShipController,
                        "ShipController"
                    );
                    ShipController.Respawn();
                }
                else
                {
                    // Create the ship locally (local to the server)
                    // NB: the ship will be moved to an appropriate NetworkStartPosition
                    //     by the server so the location specified here is irrelevant
                    ServerSpaceship = (GameObject)Instantiate(
                        SpaceshipPrefab,
                        transform.TransformPoint(chooseSpawnLocation()),
                        transform.rotation);

                    ShipController = ServerSpaceship.GetComponent<PlayerShipController>();
                    ShipController.SetSpaceshipClass(spaceShipType);
                    ShipController.owner = PlayerIdentifier.CreateNew(this);
                    ShipController.EventDeath += ShipDestroyedServerAction;
                }
                MyContract.RequireFieldNotNull(
                    ServerSpaceship,
                    "Server Spaceship"
                );
                CanRespawn = false;
                ShipSpawned = true;

                // Spawn the ship on the clients
                NetworkServer.SpawnWithClientAuthority(
                    ServerSpaceship,
                    connectionToClient
                );
                CurrentSpaceship = ServerSpaceship; // Update [SyncVar]
                RpcPlayerShipSpawned(CurrentSpaceship);
            }
        }
Exemplo n.º 24
0
 public void SetKiller(PlayerIdentifier killer)
 {
     KillerIdentifierDisplay.text = killer.ToString();
 }