Ejemplo n.º 1
0
 public Broadcaster()
 {
     // Save our hub context so we can easily use it
     // to send to its connected clients
     this.hubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>();
     this.model = new ControllerModel();
     this.modelUpdated = false;
     // Start the broadcast loop
     this.broadcastLoop = new Timer(
         this.Broadcast,
         null,
         this.BroadcastInterval,
         this.BroadcastInterval);
 }
Ejemplo n.º 2
0
        public void Collision(ControllerModel clientModel, string affectedId)
        {
            if (clients.Any(c => c.Key == affectedId))
            {
                bool isRight = clients[clientModel.ConnectionId].DLeft > 0;
                bool isDown = clients[clientModel.ConnectionId].DTop > 0;

                // Right Down
                if (isRight && isDown)
                {
                    clients[clientModel.ConnectionId].Left += -(10 * 2);
                    clients[clientModel.ConnectionId].Top += -(10 * 2);

                    clients[affectedId].Left += (10 * 3);
                    clients[affectedId].Top += (10 * 3);
                }

                // Left Down
                if (!isRight && isDown)
                {
                    clients[clientModel.ConnectionId].Left += (10 * 2);
                    clients[clientModel.ConnectionId].Top += -(10 * 2);

                    clients[affectedId].Left += -(10 * 3);
                    clients[affectedId].Top += (10 * 3);
                }

                // Left Up
                if (!isRight && !isDown)
                {
                    clients[clientModel.ConnectionId].Left += (10 * 2);
                    clients[clientModel.ConnectionId].Top += (10 * 2);

                    clients[affectedId].Left += -(10 * 3);
                    clients[affectedId].Top += -(10 * 3);
                }

                // Right Up
                if (isRight && !isDown)
                {
                    clients[clientModel.ConnectionId].Left += -(10 * 2);
                    clients[clientModel.ConnectionId].Top += (10 * 2);

                    clients[affectedId].Left += (10 * 3);
                    clients[affectedId].Top += -(10 * 3);
                }
            }
        }
Ejemplo n.º 3
0
        private bool IsInRing(ControllerModel clientModel)
        {
            // Check Outside circle
            double dx = Math.Abs(clientModel.Left - 640);
            double dy = Math.Abs(clientModel.Top - 360);
            double R = 400;

            if (dx + dy <= R)
            {
                return true;
            }

            if (dx > R)
            {
                return false;
            }

            if (dy > R)
            {
                return false;
            }

            if (Math.Sqrt(dx) + Math.Sqrt(dy) <= Math.Sqrt(R))
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 4
0
        public void UpdateControllerModel(ControllerModel clientModel)
        {
            // Try add new Controllers
            if (!clients.Any(i => i.Key == clientModel.ConnectionId))
            {
                clients.TryAdd(clientModel.LastUpdatedBy, clientModel);
                if(clientModel.ClientType == ClientType.Controller)
                {
                    ControllerUpdate(clientModel.LastUpdatedBy, (clients.Where(c => c.Value.ClientType != ClientType.Game).Count() - 1).ToString());
                }
            }

            // Update existing controller in memory
            foreach (ControllerModel client in clients.Values)
            {
                if (client.ConnectionId == clientModel.ConnectionId)
                {
                    //Check Wall collision
                    if (!IsInRing(client))
                    {
                        // Set player back to middle
                        // TODO: will need a timer here so the player doesnt come right back maybe player needs button

                        client.Left = 640;
                        client.Top = 360;
                        client.DTop = -clientModel.DTop;
                        if(client.ClientType != ClientType.Game)
                        {
                            GameSomeoneDiedUpdate(client.PlayerId);
                        }
                    }
                    client.DistplayName = clientModel.DistplayName;
                    client.DLeft = clientModel.DLeft;
                    client.DTop = clientModel.DTop;
                    client.PlayerId = clientModel.PlayerId;
                    client.Left += clientModel.DLeft;
                    client.Top += clientModel.DTop;
                    client.LastUpdatedBy = clientModel.LastUpdatedBy;
                    this.model = client;
                    this.modelUpdated = true;
                }
            }
        }
Ejemplo n.º 5
0
 public void UpdateControllerModel(ControllerModel clientModel)
 {
     clientModel.LastUpdatedBy = Context.ConnectionId;
     //TODO Reset the brodcaster with 0 clients
     broadcaster.UpdateControllerModel(clientModel);
 }
Ejemplo n.º 6
0
 public void Collision(ControllerModel clientModel, string affectedId)
 {
     // Do collision on server side model so that next time they are drawn
     // they will be updated
     broadcaster.Collision(clientModel, affectedId);
 }