예제 #1
0
        public static float getAnglePredictPlayerPos(Enemy e, Player p)
        {
            Vector2 vect1 = p.Position - e.Position;

            float a = p.Velocity.LengthSquared() - (15 * 15);
            float b = 2 * (Vector2.Dot(p.Velocity, vect1));
            float c = vect1.LengthSquared();

            if ((b * b - 4 * a * c) > 0 && a != 0)
            {
                float t1 = (-b + (float)Math.Sqrt((double)(b * b - 4 * a * c))) / (2 * a);
                float t2 = (-b + (float)Math.Sqrt((double)(b * b - 4 * a * c))) / (2 * a);

                float goodtime;
                if (t1 > 0 && t2 > 0)
                {
                    if (t1 > t2)
                        goodtime = t2;
                    else
                        goodtime = t1;
                }
                else if (t1 > 0) { goodtime = t1; }
                else { goodtime = t2; }

                Vector2 predictPos = p.Position + p.Velocity * goodtime * -3;

                return getAnglePointingAt(e.Position, predictPos);
            }
            else
            {
                return -1;
            }
        }
예제 #2
0
        protected void HandleMessage(NetIncomingMessage msg)
        {
            int packetId = msg.ReadByte();

            switch (packetId)
            {
                case 99: // server wants client ID!
                    SendMessage(this.ClientID);
                    break;

                case 1: // player disconnected
                    this.GameController.removePlayer(msg.ReadString());
                    break;

                case 5: // position and rotation of some player (maybe us)
                    svrID = msg.ReadString();

                    // not in list yet so we need to create a new player entity
                    if (!this.GameController.inPlayerList(svrID))
                    {
                        Player newP = new Player();
                        newP.Name = svrID;
                        this.GameController.addPlayer(newP);
                    }
                    // in list already so just update
                    else if (this.GameController.inPlayerList(svrID))
                    {
                        if (this.ClientID == svrID) // this is us!
                        {
                            // cool story bro!
                        }

                        this.GameController.getPlayer(svrID).setSvrPosRot(msg.ReadFloat(), msg.ReadFloat(), msg.ReadFloat());
                    }
                    break;

                case 10: // projectile
                    int key = msg.ReadByte();
                    if (this.GameController.inProjectileList(key))
                    {
                        this.GameController.getProjectile(key).setSvrPosRot(msg.ReadFloat(), msg.ReadFloat(), msg.ReadFloat());
                    }
                    else
                    {
                        float f1, f2, f3;
                        f1 = msg.ReadFloat();
                        f2 = msg.ReadFloat();
                        f3 = msg.ReadFloat();
                        this.GameController.projectiles.Add(new Projectile(new Vector2(f1, f2), f3, key, this.ClientID));
                    }
                    break;

                case 11: // enemy
                    int ekey = msg.ReadByte();
                    if (this.GameController.inEnemyList(ekey))
                    {
                        this.GameController.getEnemy(ekey).setSvrPosRot(msg.ReadFloat(), msg.ReadFloat(), msg.ReadFloat());
                    }
                    else
                    {
                        float f1, f2, f3;
                        f1 = msg.ReadFloat();
                        f2 = msg.ReadFloat();
                        f3 = msg.ReadFloat();
                        this.GameController.enemies.Add(new Enemy(new Vector2(f1, f2), ekey));
                    }
                    break;

                case 15: // bullet collision
                    this.GameController.getPlayer(msg.ReadString()).getHit();
                    int tempID = msg.ReadByte();
                    if (this.GameController.inProjectileList(tempID))
                        this.GameController.getProjectile(tempID).Active = false;
                    break;

                default:
                    // unknown packet id
                    break;
            }
        }
예제 #3
0
        public void Start()
        {
            this.server.Start();
            this.running = true;

            double nextSendUpdates = NetTime.Now;
            double now;

            while (running)
            {
                NetIncomingMessage msg;
                while ((msg = server.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.DiscoveryRequest:
                            // Respond to the discovery request.
                            server.SendDiscoveryResponse(null, msg.SenderEndpoint);
                            break;

                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.ErrorMessage:
                            // Print the message.
                            // TODO log errors?
                            Console.WriteLine(msg.ReadString());
                            break;

                        case NetIncomingMessageType.StatusChanged:
                            NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                            if (status == NetConnectionStatus.Connected)
                            {
                                // Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!");
                            }
                            else if (status == NetConnectionStatus.Disconnected)
                            {
                                // closing client makes it timeout and after a few seconds sends this message?
                                Console.WriteLine(msg.SenderConnection.Tag.ToString() + " disconnected!");

                                disconnectEvent = true;
                                if (cloudGC.pList.TryGetValue(msg.SenderConnection.RemoteUniqueIdentifier, out val))
                                {
                                    disconnectPlayer = val;
                                    disconnectRID = msg.SenderConnection.RemoteUniqueIdentifier;
                                }
                            }
                            else if (status == NetConnectionStatus.Disconnecting)
                            {
                                // can't get this one to trigger just yet.
                                Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnecting!");
                            }
                            break;

                        case NetIncomingMessageType.Data:  // handle message from client

                            //Console.WriteLine(string.Format("got msg from: " + NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier)));
                            handleMessage(msg);
                            break;
                    }
                }

                now = NetTime.Now;
                if (now > nextSendUpdates)
                {
                    foreach (NetConnection connection in server.Connections)
                    {
                        // storing client id in Tag.  If it's null, ask client to send it over.
                        // Also send client it's session id.
                        if (connection.Tag == null)
                        {
                            NetOutgoingMessage sendMsg = server.CreateMessage();
                            sendMsg.Write((byte)99);
                            server.SendMessage(sendMsg, connection, NetDeliveryMethod.ReliableUnordered);
                        }
                        else
                        {
                            // tell player of everyone's position including itself.
                            foreach (NetConnection player in server.Connections)
                            {
                                if (player.Tag != null) // have client id
                                {
                                    p1 = cloudGC.getPlayer(player.Tag.ToString());
                                    if (p1 != null) // player still connected
                                    {
                                        NetOutgoingMessage sendMsg = server.CreateMessage();
                                        sendMsg.Write((byte)5); // packet id
                                        sendMsg.Write(player.Tag.ToString()); // client id
                                        sendMsg.Write(p1.Position.X);
                                        sendMsg.Write(p1.Position.Y);
                                        sendMsg.Write(p1.Rotation);
                                        server.SendMessage(sendMsg, connection, NetDeliveryMethod.Unreliable);
                                    }
                                }
                            }

                            // tell player of the bullets
                            foreach (Projectile proj in cloudGC.projectiles)
                            {
                                if (proj.Active)
                                {
                                    NetOutgoingMessage sendMsg = server.CreateMessage();
                                    sendMsg.Write((byte)10); // packet id
                                    sendMsg.Write((byte)proj.ID);
                                    sendMsg.Write(proj.Position.X);
                                    sendMsg.Write(proj.Position.Y);
                                    sendMsg.Write(proj.Rotation);
                                    server.SendMessage(sendMsg, connection, NetDeliveryMethod.Unreliable);
                                }
                            }

                            // tell player of the enemies
                            if (cloudGC.enemyList.Count != 0)
                            {
                                foreach (Enemy enemy in cloudGC.enemyList)
                                {
                                    NetOutgoingMessage sendMsg = server.CreateMessage();
                                    sendMsg.Write((byte)11);
                                    sendMsg.Write((byte)enemy.ID);
                                    sendMsg.Write(enemy.Position.X);
                                    sendMsg.Write(enemy.Position.Y);
                                    sendMsg.Write(enemy.Rotation);
                                    server.SendMessage(sendMsg, connection, NetDeliveryMethod.Unreliable);
                                }
                            }

                            // tell player of collisions
                            if (cloudGC.collisionList.Count != 0)
                            {
                                foreach (Collision c in cloudGC.collisionList)
                                {
                                    NetOutgoingMessage sendMsg = server.CreateMessage();
                                    sendMsg.Write((byte)15); // packet id
                                    sendMsg.Write(c.player.Name);
                                    sendMsg.Write(c.projectile.ID);
                                    server.SendMessage(sendMsg, connection, NetDeliveryMethod.Unreliable);
                                }
                            }

                            // inform player someone disconnected
                            if (disconnectEvent)
                            {
                                NetOutgoingMessage sendMsg = server.CreateMessage();
                                sendMsg.Write((byte)1); // packet id
                                sendMsg.Write(disconnectPlayer); // client id
                                server.SendMessage(sendMsg, connection, NetDeliveryMethod.Unreliable);
                            }
                        }
                    }

                    cloudGC.clearCollisionsList();

                    if (disconnectEvent)
                    {
                        disconnectEvent = false;
                        cloudGC.removePlayer(disconnectPlayer, disconnectRID);
                    }

                    nextSendUpdates += (1.0 / messagesPerSecond);
                }

                Thread.Sleep(1);
            }

            this.server.Shutdown("Server stopping.");
        }
예제 #4
0
 public void createBullet(Player player)
 {
     if (player.canShoot())
     {
         projectiles.Add(new Projectile(player.Position, player.Rotation, bulletkey, player.Name));
         player.firecounter = player.FireRate;
         bulletkey++;
     }
 }
예제 #5
0
 public void addPlayer(String name, long remoteID)
 {
     Player newP = new Player();
     newP.Name = name;
     playerList.Add(newP);
     pList.Add(remoteID, name);
 }
예제 #6
0
 public Collision(Player p, Projectile proj)
     : this()
 {
     this.player = p;
     this.projectile = proj;
 }
예제 #7
0
 public void addPlayer(Player p)
 {
     playerList.Add(p);
 }