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); }
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); } } }
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; }
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; } } }
public void UpdateControllerModel(ControllerModel clientModel) { clientModel.LastUpdatedBy = Context.ConnectionId; //TODO Reset the brodcaster with 0 clients broadcaster.UpdateControllerModel(clientModel); }
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); }