private void reset() { objectMoving = false; objectDrag = false; Projectiles.Clear(); myTurn = false; zoom = 1.0f; lastScale = 1.0f; LastVelocity = Vector2.Zero; CurrentProjectile = null; myTurnPending = null; myTurn = true; freeMove = false; camY = 0; camX = 0; isGameScreen = false; isEndScreen = false; isMenuScreen = true; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { if (!isMenuScreen) { if(websocket != null && websocket.State == WebSocketState.Open) websocket.Close(); isGameScreen = false; isMenuScreen = true; } else { if (websocket != null && websocket.State == WebSocketState.Open) websocket.Close(); Exit(); } reset(); } if (isMenuScreen) { TouchCollection touchCollection = TouchPanel.GetState(); Menu.Update(touchCollection, gameTime); if (Menu.StartPress) { isMenuScreen = false; isGameScreen = true; Menu.StartPress = false; websocket = new WebSocket("ws://134.87.140.178:8142/"); websocket.EnableAutoSendPing = true; websocket.Opened += websocket_Opened; websocket.Closed += websocket_Closed; websocket.Error += websocket_Error; websocket.MessageReceived += websocket_MessageReceived; websocket.Open(); } } else if (isEndScreen) { TouchCollection touchCollection = TouchPanel.GetState(); End.Update(touchCollection, gameTime); } if (betweenTurns) return; if (myTurnPending == "true") { objectMoving = false; objectDrag = false; Projectiles.Clear(); zoom = 1.0f; lastScale = 1.0f; LastVelocity = Vector2.Zero; myTurnPending = null; myTurn = true; freeMove = false; if (playerNum == "one") { if (CurrentProjectile != null && CurrentProjectile.Proj != null) { CurrentProjectile.Proj.OnCollision -= proj_OnCollision; } CurrentProjectile = new Projectile(new Vector2(20, 20), new Vector2(DS1Pos + 100, 250), world, true); CurrentProjectile.Proj.OnCollision += proj_OnCollision; } else if (playerNum == "two") { if (CurrentProjectile != null && CurrentProjectile.Proj != null) { CurrentProjectile.Proj.OnCollision -= proj_OnCollision; } CurrentProjectile = new Projectile(new Vector2(20, 20), new Vector2(DS2Pos - 100, 250), world, true); CurrentProjectile.Proj.OnCollision += proj_OnCollision; } //THIS FOCUSES ON THE CAMERA WHEN YOU DO A PINCH ZOOM BACK IN, SO WE AREN'T LOST camY = (int)(-CurrentProjectile.Position.Y + windowHeight / 2.0f); camX = (int)(-CurrentProjectile.Position.X + windowWidth / 2.0f); } else if (myTurnPending == "false") { objectMoving = false; objectDrag = false; Projectiles.Clear(); zoom = 1.0f; lastScale = 1.0f; LastVelocity = Vector2.Zero; myTurnPending = null; myTurn = false; freeMove = false; if (playerNum == "one") { if (CurrentProjectile != null && CurrentProjectile.Proj != null) { CurrentProjectile.Proj.OnCollision -= proj_OnCollision; } CurrentProjectile = new Projectile(new Vector2(20, 20), new Vector2(DS2Pos - 100, 250), world, false); CurrentProjectile.Proj.OnCollision += proj_OnCollision; } else if (playerNum == "two") { if (CurrentProjectile != null && CurrentProjectile.Proj != null) { CurrentProjectile.Proj.OnCollision -= proj_OnCollision; } CurrentProjectile = new Projectile(new Vector2(20, 20), new Vector2(DS1Pos + 100, 250), world, false); CurrentProjectile.Proj.OnCollision += proj_OnCollision; } //THIS FOCUSES ON THE CAMERA WHEN YOU DO A PINCH ZOOM BACK IN, SO WE AREN'T LOST camY = (int)(-CurrentProjectile.Position.Y + windowHeight / 2.0f); camX = (int)(-CurrentProjectile.Position.X + windowWidth / 2.0f); } Vector2 projForce = Vector2.Zero; if (TouchPanel.IsGestureAvailable && (myTurn == false || objectMoving)) //THIS IS SO YOU CAN ONLY PINCH ZOOM OUT ON OPPONENT'S TURN { GestureSample gs = TouchPanel.ReadGesture(); switch (gs.GestureType) { case GestureType.Pinch: break; case GestureType.PinchComplete: if (zoom >= 1) { zoom = 0.25f; } else { zoom = 1.0f; camY = (int)(-CurrentProjectile.Position.Y + windowHeight / 2.0f); camX = (int)(-CurrentProjectile.Position.X + windowWidth / 2.0f); } //blah break; } } else if (!objectMoving && myTurn == true) //THIS MAKES IT SO YOU CAN'T SHOOT THE PROJ ON YOUR OPPONENTS TURN { TouchCollection touchCollection = TouchPanel.GetState(); foreach (TouchLocation tl in touchCollection) { if ((tl.State == TouchLocationState.Pressed) || (tl.State == TouchLocationState.Moved)) { TouchLocation old; tl.TryGetPreviousLocation(out old); if (CurrentProjectile != null && Vector2.Distance(tl.Position - new Vector2(camX, camY), CurrentProjectile.Position) < 50) { if (old.Position.X > 0) CurrentProjectile.Position = (CurrentProjectile.Position + (tl.Position - old.Position)); else originalPos = tl.Position; objectDrag = true; } else { if (old.Position.X > 0) { camX = camX + (tl.Position.X - old.Position.X); camY = camY + (tl.Position.Y - old.Position.Y); } else originalPos = tl.Position; } } else if (tl.State == TouchLocationState.Released && objectDrag) { Debug.WriteLine("Shit dawg"); objectMoving = true; objectDrag = false; projForce = originalPos - tl.Position; Vector2 oldForce = projForce; float len = projForce.Length() * 15; projForce.Normalize(); projForce = projForce * len * pixelToUnit; camX = CurrentProjectile.Position.X; camY = CurrentProjectile.Position.Y; JsonObject msg = new JsonObject(); msg["type"] = "initialFire"; websocket.Send(SimpleJson.SimpleJson.SerializeObject(msg)); } } } else { TouchCollection touchCollection = TouchPanel.GetState(); foreach (TouchLocation tl in touchCollection) { if ((tl.State == TouchLocationState.Pressed) || (tl.State == TouchLocationState.Moved)) { TouchLocation old; tl.TryGetPreviousLocation(out old); if (Vector2.Distance(tl.Position - new Vector2(camX, camY), CurrentProjectile.Position) > 50) { if (old.Position.X > 0) { camX = camX + (tl.Position.X - old.Position.X); camY = camY + (tl.Position.Y - old.Position.Y); } else originalPos = tl.Position; freeMove = true; } } } } if (CurrentProjectile != null && !CurrentProjectile.Mine && CurrentProjectile.THISVARIABLEFUCKINGSUCKS) { objectMoving = true; objectDrag = false; projForce = LastVelocity; float len = projForce.Length() * 15; projForce.Normalize(); projForce = projForce * len * pixelToUnit; camY = (int)(-CurrentProjectile.Position.Y + windowHeight / 2.0f); camX = (int)(-CurrentProjectile.Position.X + windowWidth / 2.0f); CurrentProjectile.THISVARIABLEFUCKINGSUCKS = false; } if (objectMoving) { foreach (DeathStar d in DeathStars) { Vector2 forcedir = (d.Position) - CurrentProjectile.Position; float len = forcedir.Length(); forcedir.Normalize(); if (len < 1000) { forcedir = forcedir * (1 - len / 1000) * d.Pull; projForce += forcedir; } } if (CurrentProjectile.Mine) { try { JsonObject msg = new JsonObject(); msg["type"] = "fire"; msg["pos_x"] = CurrentProjectile.Position.X; msg["pos_y"] = CurrentProjectile.Position.Y; msg["ang"] = CurrentProjectile.Proj.AngularVelocity; msg["vel_x"] = CurrentProjectile.Proj.LinearVelocity.X; msg["vel_y"] = CurrentProjectile.Proj.LinearVelocity.Y; msg["inertia"] = CurrentProjectile.Proj.Inertia; msg["rot"] = CurrentProjectile.Proj.Rotation; websocket.Send(SimpleJson.SimpleJson.SerializeObject(msg)); } catch (Exception e) { } CurrentProjectile.Proj.ApplyForce(projForce); } else { } } if (!(colliding == false && CurrentProjectile != null)) { colliding = false; Debug.WriteLine("GG PLANET, YOU HIT"); betweenTurns = true; try { if (CurrentProjectile != null && CurrentProjectile.Proj != null) { CurrentProjectile.Proj.OnCollision -= proj_OnCollision; } JsonObject msg = new JsonObject(); msg["type"] = "turn"; if (playerHit == 1) msg["hit"] = "one"; else if (playerHit == 2) msg["hit"] = "two"; else if (playerHitCity == 1) msg["cityHit"] = "one"; else if (playerHitCity == 2) msg["cityHit"] = "two"; else { msg["hit"] = "zero"; msg["cityHit"] = "zero"; } playerHit = 0; playerHitCity = 0; if (myTurn) { websocket.Send(SimpleJson.SimpleJson.SerializeObject(msg)); } CurrentProjectile.Proj.OnCollision += proj_OnCollision; } catch (Exception e) { } } world.Step((float)0.03f); if (objectMoving && !freeMove) { camY = (int)(-CurrentProjectile.Position.Y + windowHeight / 2.0f); camX = (int)(-CurrentProjectile.Position.X + windowWidth / 2.0f); } base.Update(gameTime); }