Example #1
0
        /// <summary>
        /// The message read loop will call this for
        /// NetIncomingMessageType.Data typed NetIncomingMessages
        /// </summary>
        /// <param name="msgType">The MessageType of the message which the client sent !!!NOT THE SAME AS NetIncomingMessageType!!!</param>
        /// <param name="msgIn">The message from the client</param>
        private void HandleMessage(MessageType msgType, NetIncomingMessage msgIn)
        {
            // Based on the NetIncomingMessage RemoteUniqueIdentifier (UID) gets the PlayerEntity from the WorldData
            PlayerEntity entity = Server.Data.Get(msgIn.SenderConnection.RemoteUniqueIdentifier);

            if (entity != null)
            {
                switch (msgType)
                {
                case MessageType.Respawn:
                {
                    bool homeOrNearest = msgIn.ReadBoolean();
                    if (entity.isDead)
                    {
                        entity.isDead = false;
                        entity.Respawn();
                    }
                }
                break;

                case MessageType.PlayerMovement:

                    if (!entity.isDead)
                    {
                        Vector3 newPosition = new Vector3(msgIn.ReadFloat(), 0, msgIn.ReadFloat());

                        if (CheckPosition(entity, newPosition))
                        {
                            return;
                        }

                        entity.SetPosition(newPosition);

                        if (entity.IsSkillCasting)
                        {
                            if (Vector3.SqrDistance(entity.ChannelingPosition, entity.Position) > 2f)
                            {
                                entity.IsSkillCasting = false;
                            }
                        }
                    }
                    break;

                case MessageType.Interact:
                {
                    EntityBase interactedEntity = Server.Data.GetInteractedEntity(msgIn.ReadUInt16());
                    interactedEntity.Interact(entity);
                }
                break;

                case MessageType.SkillRequestRawPosition:
                {
                    Console.WriteLine("RAW SKILL");
                    SkillID skillId        = (SkillID)msgIn.ReadByte();
                    Vector3 targetPosition = new Vector3(msgIn.ReadFloat(), 0, msgIn.ReadFloat());

                    if (entity != null)
                    {
                        AbstractSkill skill = SkillFactory.Instance.GetSkill(skillId);

                        skill.Create(entity, targetPosition, Server);

                        if (entity.AddSkill(skill))
                        {
                            Console.WriteLine("ADDED");
                        }
                        else
                        {
                            Console.WriteLine("FAILED TO ADD");
                        }
                    }
                }
                break;

                case MessageType.SkillRequest:
                {
                    Entity  target  = Server.Data.Get(msgIn.ReadUInt16());
                    SkillID skillId = (SkillID)msgIn.ReadByte();

                    Console.WriteLine("DEFAULT CHECK");
                    if (target != null && entity != null && target.Health.Value > 0 && !entity.IsSkillCasting)
                    {
                        Console.WriteLine("RANGE CHECK...");
                        AbstractSkill skill = SkillFactory.Instance.GetSkill(skillId);

                        if (Vector3.SqrDistance(entity.Position, target.Position) < skill.MaxTargetRange * skill.MaxTargetRange)
                        {
                            Console.WriteLine("OK");
                            skill.Create(entity, target, Server);
                            float tickTime        = 1000f / Server.PlayerTickRate;
                            float lagCompensation = (int)((entity.Connection.AverageRoundtripTime * 1000f) / tickTime);
                            skill.CastDuration -= (int)lagCompensation;

                            if (entity.AddSkill(skill))
                            {
                                Console.WriteLine("ADDED");
                            }
                            else
                            {
                                Console.WriteLine("FAILED TO ADD");
                            }
                        }
                    }
                }
                break;
                }
            }
        }