示例#1
0
    //attackerName should be the name of the player who dealt the damage
    public override void TakeDamage(float Damage, string attackerName, Vector3 direction, int weaponID)
    {
        //tell the other player to take damage
        HurtEvent evnt = HurtEvent.Create(Bolt.GlobalTargets.OnlyServer, Bolt.ReliabilityModes.ReliableOrdered);

        evnt.Amount    = Damage;
        evnt.Source    = attackerName;
        evnt.Target    = this.Username;
        evnt.Direction = direction;
        evnt.WeaponID  = weaponID;
        evnt.Send();
    }
示例#2
0
    public override void OnEvent(HurtEvent evnt)
    {
        if (BoltNetwork.isServer)
        {
            //TODO damage stats can be recorded here if needed, all HurtEvents pass through here exactly once

            //Direct damage events, either by consuming them here (if they were meant for the server player)
            //Or by redirecting them to the right player

            //if this HurtEvent is targeted at the server itself, apply it directly without forwarding it first
            if (evnt.Target == GameManager.instance.CurrentUserName)
            {
                GameManager.instance.CurrentPlayer.TakeDamage(evnt.Amount, evnt.Source, evnt.Direction, evnt.WeaponID);
            }
            else    //this event is not directed at the server player, it needs to be forwarded to the correct player
            {
                BoltConnection destination = PlayerRegistry.GetConnectionFromUserName(evnt.Target);
                if (destination == null)  //quick error check
                {
                    Debug.LogError("The destination connection for a HurtEvent could not be found! " + evnt);
                }
                else
                {
                    //forward the event to the player that was actually damaged
                    HurtEvent redir = HurtEvent.Create(destination, Bolt.ReliabilityModes.ReliableOrdered);
                    redir.Amount    = evnt.Amount;
                    redir.Target    = evnt.Target;
                    redir.Source    = evnt.Source;
                    redir.WeaponID  = evnt.WeaponID;
                    redir.Direction = evnt.Direction;
                    redir.Send();
                }
            }
        }
        else                                                         //this is a client
        {
            if (evnt.Target == GameManager.instance.CurrentUserName) //make sure that this hurtEvent is intended for this client
            {
                GameManager.instance.CurrentPlayer.TakeDamage(evnt.Amount, evnt.Source, evnt.Direction, evnt.WeaponID);
            }
            else
            {
                Debug.LogError("Recieved HurtEvent that is not directed to this player! " + evnt.ToString());
            }
        }
    }