public override void Update(GameTimerEventArgs gameTime) { // Check if it is time to take a shot if (!IsActive && App.g_isTwoHumanPlayers) { //we are checking here for any new message from remote user msg = MoveMessage.GetCurrentInstance(); if (msg.Type.Equals("SHOT")) { remoteFirstSample = Vector2.Zero; Catapult.ShotVelocity = (float)Convert.ToDouble(msg.ShotVelocity); Catapult.ShotAngle = (float)Convert.ToDouble(msg.ShotAngle); Catapult.CurrentState = CatapultState.Firing; ResetDragState(); } else if (msg.Type.Equals("DRAGGING")) { // If drag just began save the sample for future // calculations and start Aim "animation" remoteCurrentSample = new Vector2(); remoteCurrentSample.X = (float)Convert.ToDouble(msg.X); remoteCurrentSample.Y = (float)Convert.ToDouble(msg.Y); if (Vector2.Zero == remoteFirstSample) { remoteFirstSample = remoteCurrentSample; Catapult.CurrentState = CatapultState.Aiming; } // save the current gesture sample remotePrevSample = remoteCurrentSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = remotePrevSample - remoteFirstSample; Catapult.ShotStrength = delta.Length() / maxDragDelta; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) { Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); } else { Catapult.ShotAngle = MinShotAngle; } float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } } Catapult.Update(gameTime); }
public static MoveMessage GetCurrentInstance() { MoveMessage msg = new MoveMessage(); if (dragJsonObj != null) { msg.sender = dragJsonObj["sender"].ToString(); if (!msg.sender.Equals(GlobalContext.localUsername)) { msg.Type = dragJsonObj["Type"].ToString(); if (msg.Type.Equals("SHOT")) { msg.ShotVelocity = dragJsonObj["ShotVelocity"].ToString(); msg.ShotAngle = dragJsonObj["ShotAngle"].ToString(); } else if (msg.Type.Equals("DRAGGING")) { GlobalContext.fireNumber = Convert.ToInt32(GlobalContext.tableProperties["fireNumber"]); int fireNumber = Convert.ToInt32(dragJsonObj["fireNumber"].ToString()); int udpPacketNumber = Convert.ToInt32(dragJsonObj["packetNumber"].ToString()); if ((fireNumber == GlobalContext.fireNumber) && (udpPacketNumber > GlobalContext.prevUDPPacketNumber)) { GlobalContext.prevUDPPacketNumber = udpPacketNumber; msg.X = dragJsonObj["X"].ToString(); msg.Y = dragJsonObj["Y"].ToString(); } else {//igonre the udp packet msg.Type = "NONE"; } } } dragJsonObj = null; } return(msg); // Vector2 vect = new Vector2((float)Convert.ToDouble(msg.X), (float)Convert.ToDouble(msg.Y)); }
public static MoveMessage GetCurrentInstance() { MoveMessage msg = new MoveMessage(); if (dragJsonObj != null) { msg.sender = dragJsonObj["sender"].ToString(); if (!msg.sender.Equals(GlobalContext.localUsername)) { msg.Type = dragJsonObj["Type"].ToString(); if (msg.Type.Equals("SHOT")) { msg.ShotVelocity = dragJsonObj["ShotVelocity"].ToString(); msg.ShotAngle = dragJsonObj["ShotAngle"].ToString(); } else if (msg.Type.Equals("DRAGGING")) { GlobalContext.fireNumber = Convert.ToInt32(GlobalContext.tableProperties["fireNumber"]); int fireNumber = Convert.ToInt32(dragJsonObj["fireNumber"].ToString()); int udpPacketNumber = Convert.ToInt32(dragJsonObj["packetNumber"].ToString()); if ((fireNumber == GlobalContext.fireNumber) && (udpPacketNumber > GlobalContext.prevUDPPacketNumber)) { GlobalContext.prevUDPPacketNumber = udpPacketNumber; msg.X = dragJsonObj["X"].ToString(); msg.Y = dragJsonObj["Y"].ToString(); } else {//igonre the udp packet msg.Type = "NONE"; } } } dragJsonObj = null; } return msg; // Vector2 vect = new Vector2((float)Convert.ToDouble(msg.X), (float)Convert.ToDouble(msg.Y)); }
/// <summary> /// Function processes the user input /// </summary> /// <param name="gestureSample"></param> public void HandleInput(GestureSample gestureSample) { // Process input only if in Human's turn if (IsActive) { // Process any Drag gesture if (gestureSample.GestureType == GestureType.FreeDrag) { // If drag just began save the sample for future // calculations and start Aim "animation" if (null == firstSample) { firstSample = gestureSample; if (App.g_isTwoHumanPlayers && GlobalContext.IsUDPEnableOnNetwork) { GlobalContext.currentUDPPacketNumber = 1; if (GlobalContext.IsUDPEnableOnNetwork) { WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } } Catapult.CurrentState = CatapultState.Aiming; } if (App.g_isTwoHumanPlayers && GlobalContext.IsUDPEnableOnNetwork) { tempSample = gestureSample; if (prevSample != null) { Vector2 tempDelta = tempSample.Value.Position - prevSample.Value.Position; if (tempDelta.Length() != 0) { if (skipCounter == 5) { skipCounter = 0; GlobalContext.currentUDPPacketNumber++; WarpClient.GetInstance().SendUDPUpdatePeers(MoveMessage.buildDragingMessageBytes(GlobalContext.currentUDPPacketNumber, gestureSample.Position.X.ToString(), gestureSample.Position.Y.ToString())); } else { skipCounter++; } } } } // save the current gesture sample prevSample = gestureSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotStrength = delta.Length() / maxDragDelta; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) { Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); } else { Catapult.ShotAngle = MinShotAngle; } float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } else if (gestureSample.GestureType == GestureType.DragComplete) { // calc velocity based on delta between first and last // gesture samples if (null != firstSample) { Vector2 delta = prevSample.Value.Position - firstSample.Value.Position; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) { Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); } else { Catapult.ShotAngle = MinShotAngle; } if (App.g_isTwoHumanPlayers) { //Since Drag is Completed so now we will send SHOT strength and SHOT velocity to remote user //It is the final data for current fire,thats why Here I am also updating property "fireNumber" //so if remote user will get any UDP packet after getting TCP data for current fire,It will ignore them GlobalContext.fireNumber = Convert.ToInt32(GlobalContext.tableProperties["fireNumber"]); GlobalContext.fireNumber++; Dictionary <string, object> fireProperties = new Dictionary <string, object>(); fireProperties.Add("fireNumber", GlobalContext.fireNumber); WarpClient.GetInstance().UpdateRoomProperties(GlobalContext.GameRoomId, fireProperties, null); WarpClient.GetInstance().SendUpdatePeers(MoveMessage.buildSHOTMessageBytes(Catapult.ShotVelocity.ToString(), Catapult.ShotAngle.ToString())); //Here i am resetting this counter because host user has sent his final shot via TCP so in next //step remote user will send his data,before receive remote user UDP packets we need to reset this counter GlobalContext.prevUDPPacketNumber = 0; } Catapult.CurrentState = CatapultState.Firing; } // turn off dragging state ResetDragState(); } } }
public void onUpdatePeersReceived(UpdateEvent eventObj) { MoveMessage.buildMessage(eventObj.getUpdate()); }
public override void Update(GameTimerEventArgs gameTime) { // Check if it is time to take a shot if (!IsActive&&App.g_isTwoHumanPlayers) { //we are checking here for any new message from remote user msg = MoveMessage.GetCurrentInstance(); if (msg.Type.Equals("SHOT")) { remoteFirstSample =Vector2.Zero; Catapult.ShotVelocity = (float)Convert.ToDouble(msg.ShotVelocity); Catapult.ShotAngle = (float)Convert.ToDouble(msg.ShotAngle); Catapult.CurrentState = CatapultState.Firing; ResetDragState(); } else if (msg.Type.Equals("DRAGGING")) { // If drag just began save the sample for future // calculations and start Aim "animation" remoteCurrentSample = new Vector2(); remoteCurrentSample.X = (float)Convert.ToDouble(msg.X); remoteCurrentSample.Y = (float)Convert.ToDouble(msg.Y); if (Vector2.Zero == remoteFirstSample) { remoteFirstSample = remoteCurrentSample; Catapult.CurrentState = CatapultState.Aiming; } // save the current gesture sample remotePrevSample = remoteCurrentSample; // calculate the delta between first sample and current // sample to present visual sound on screen Vector2 delta = remotePrevSample - remoteFirstSample; Catapult.ShotStrength = delta.Length() / maxDragDelta; Catapult.ShotVelocity = MinShotVelocity + Catapult.ShotStrength * (MaxShotVelocity - MinShotVelocity); if (delta.Length() > 0) Catapult.ShotAngle = MathHelper.Clamp((float)Math.Asin(-delta.Y / delta.Length()), MinShotAngle, MaxShotAngle); else Catapult.ShotAngle = MinShotAngle; float baseScale = 0.001f; arrowScale = baseScale * delta.Length(); isDragging = true; } } Catapult.Update(gameTime); }