public float getAngle() { if (this.angle.X == 0) { if (this.angle.Y > 0) { return(Math2.QUARTER_CIRCLE); } else { return(Math2.THREE_QUARTER_CIRCLE); } } return((float)Math.Sin(this.angle.Y / Math2.getQuadSum(this.angle.X, this.angle.Y))); }
public static bool inRadius(Vector2 pos1, Vector2 pos2, float radius) { return(((float)Math2.getQuadSum(pos2.X - pos1.X, pos2.Y - pos1.Y)) < radius); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (Keyboard.GetState().IsKeyDown(Keys.Enter) && connected == false) { connectToServer(); } if (!player.isLanding()) { if (Keyboard.GetState().IsKeyDown(Keys.W) == true) { player.thrust(); } if (Keyboard.GetState().IsKeyDown(Keys.S) == true) { player.reverse(); } if (Keyboard.GetState().IsKeyDown(Keys.D) == true) { player.rotateRight(); } if (Keyboard.GetState().IsKeyDown(Keys.A) == true) { player.rotateLeft(); } if (Keyboard.GetState().IsKeyDown(Keys.Q) == true) { player.leftThrust(); } if (Keyboard.GetState().IsKeyDown(Keys.E) == true) { player.rightThrust(); } if (Keyboard.GetState().IsKeyDown(Keys.Space) == true) { player.brake(); } if (Keyboard.GetState().IsKeyDown(Keys.H) == true && player.getSpeed() < player.MAX_SPEED_TO_LAND) { List <SpaceObject> spaceObjects = world.getSpaceObjects(); for (int index = 0; index < spaceObjects.Count; index++) { if (Math2.inRadius(player.getPos().X, player.getPos().Y, spaceObjects[index].getXpos(), spaceObjects[index].getYpos(), spaceObjects[index].getRadius())) { player.land(spaceObjects[index]); index = spaceObjects.Count; } } } } if (Keyboard.GetState().IsKeyDown(Keys.G) == true) { player.takeOff(); } if (Mouse.GetState().LeftButton == ButtonState.Pressed) { Laser l = player.fireWeapon(new Vector2(Mouse.GetState().Position.X, Mouse.GetState().Position.Y)); if (l != null) { movingObjects.Add(l); } } bob.nearby(); bob.updatePosition(world); foreach (MovingObject mo in movingObjects) { mo.update(world); } connectToServer(); cam.UpdateCamera(viewport); base.Update(gameTime); }
//MAPFUNC /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Viewport = viewport; GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, cam.Transform); switch (ps) { case PlayState.PLAYING: if (player.isInMap()) { float SCALE = (float)2.8; Vector2 topLeft = new Vector2(player.getPos().X - SCREEN_WIDTH * (float)1.5 + SCREEN_WIDTH * (3 - SCALE) / 2, player.getPos().Y - SCREEN_HEIGHT * (float)1.5 + SCREEN_HEIGHT * (3 - SCALE) / 2); Vector2 mousePos = new Vector2(topLeft.X - 18 + Mouse.GetState().Position.X *(float)2.851, topLeft.Y - 18 + Mouse.GetState().Position.Y *(float)2.851); foreach (Object o in objects) { if (o.getType() == ObjectType.STAR) { Vector2 pos = new Vector2(topLeft.X + o.getPos().X *SCREEN_WIDTH *SCALE / MAP_WIDTH, topLeft.Y + o.getPos().Y *SCREEN_HEIGHT *SCALE / MAP_HEIGHT); spriteBatch.Draw(minimap_star, new Rectangle((int)pos.X, (int)pos.Y, minimap_star.Width, minimap_star.Height), null, Color.White, 0, new Vector2(minimap_star.Width / 2, minimap_star.Height / 2), SpriteEffects.None, 0); if (Math2.inRadius(mousePos, pos, 16)) { spriteBatch.DrawString(bigFont, "ID: " + o.getID().ToString(), new Vector2(topLeft.X + SCREEN_WIDTH * (float)1.4, topLeft.Y + 16), Color.White); spriteBatch.DrawString(bigFont, "OWNER: \"" + o.getOwner() + "\"", new Vector2(topLeft.X + SCREEN_WIDTH * (float)1.4, topLeft.Y + 48), Color.White); } } } foreach (AI o in ships) { Vector2 pos = new Vector2(topLeft.X + o.getPos().X *SCREEN_WIDTH *SCALE / MAP_WIDTH, topLeft.Y + o.getPos().Y *SCREEN_HEIGHT *SCALE / MAP_HEIGHT); spriteBatch.Draw(minimap_star, new Rectangle((int)pos.X, (int)pos.Y, 5, 5), null, Color.White, 0, new Vector2(minimap_star.Width / 2, minimap_star.Height / 2), SpriteEffects.None, 0); } Vector2 shipPos = new Vector2(topLeft.X + player.getPos().X *SCREEN_WIDTH *SCALE / MAP_WIDTH, topLeft.Y + player.getPos().Y *SCREEN_HEIGHT *SCALE / MAP_HEIGHT); spriteBatch.Draw(minimap_ship, new Rectangle((int)shipPos.X, (int)shipPos.Y, minimap_ship.Width, minimap_ship.Height), null, Color.White, 0, new Vector2(minimap_ship.Width / 2, minimap_ship.Height / 2), SpriteEffects.None, 0); spriteBatch.Draw(minimap_ship, new Rectangle((int)mousePos.X, (int)mousePos.Y, minimap_ship.Width, minimap_ship.Height), null, Color.White, 0, new Vector2(minimap_ship.Width / 2, minimap_ship.Height / 2), SpriteEffects.None, 0); } else { int i = 0; foreach (Object o in objects) { bool draw = Math2.inRadius(player.getPos(), o.getPos(), RENDER_RADIUS); ObjectType type = o.getType(); if ((type == ObjectType.STAR || type == ObjectType.MINING_PLANET || type == ObjectType.ASTEROID) && draw) { spriteBatch.Draw(o.getTexture(), new Rectangle((int)o.getPos().X, (int)o.getPos().Y, o.getTexture().Width, o.getTexture().Height), null, Color.White, o.getAngle() + Math2.QUARTER_CIRCLE, new Vector2(o.getTexture().Width / 2, o.getTexture().Height / 2), SpriteEffects.None, 0); if (o.getType() == ObjectType.STAR) { spriteBatch.DrawString(font, "Star ID: " + ((Star)o).getID().ToString(), new Vector2(o.getPos().X, o.getPos().Y + 200), Color.White); } } } foreach (PlayerShip o in players) { spriteBatch.Draw(o.getTexture(), new Rectangle((int)o.getPos().X, (int)o.getPos().Y, o.getTexture().Width, o.getTexture().Height), null, Color.White, o.getAngle() + Math2.QUARTER_CIRCLE, new Vector2(o.getTexture().Width / 2, o.getTexture().Height / 2), SpriteEffects.None, 0); } foreach (Asteroid o in asteroids) { if (Math2.inRadius(player.getPos(), o.getPos(), RENDER_RADIUS)) { spriteBatch.Draw(o.getTexture(), new Rectangle((int)o.getPos().X, (int)o.getPos().Y, o.getTexture().Width, o.getTexture().Height), null, Color.White, o.getAngle() + Math2.QUARTER_CIRCLE, new Vector2(o.getTexture().Width / 2, o.getTexture().Height / 2), SpriteEffects.None, 0); } } foreach (AI o in ships) { if (Math2.inRadius(player.getPos(), o.getPos(), RENDER_RADIUS)) { spriteBatch.Draw(o.getTexture(), new Rectangle((int)o.getPos().X, (int)o.getPos().Y, o.getTexture().Width, o.getTexture().Height), null, Color.White, o.getAngle() + Math2.QUARTER_CIRCLE, new Vector2(o.getTexture().Width / 2, o.getTexture().Height / 2), SpriteEffects.None, 0); spriteBatch.DrawString(font, o.getTask() + "\n" + o.getOwner() + "\n" + o.getID(), new Vector2(o.getPos().X, o.getPos().Y + 200), Color.White); } } foreach (Projectile o in projectiles) { if (Math2.inRadius(player.getPos(), o.getPos(), RENDER_RADIUS)) { spriteBatch.Draw(o.getTexture(), new Rectangle((int)o.getPos().X, (int)o.getPos().Y, o.getTexture().Width, o.getTexture().Height), null, Color.White, o.getAngle() + Math2.QUARTER_CIRCLE, new Vector2(o.getTexture().Width / 2, o.getTexture().Height / 2), SpriteEffects.None, 0); } } spriteBatch.DrawString(font, "Speed: " + Math.Round((decimal)player.getSpeed()).ToString(), new Vector2(player.getPos().X, player.getPos().Y + 80), Color.White); spriteBatch.DrawString(font, "Iron: " + Math.Round((decimal)player.getIron()).ToString(), new Vector2(player.getPos().X, player.getPos().Y + 96), Color.White); spriteBatch.DrawString(font, "Gems: " + Math.Round((decimal)player.getGems()).ToString(), new Vector2(player.getPos().X, player.getPos().Y + 112), Color.White); spriteBatch.DrawString(font, "Cap: " + Math.Round((decimal)player.getCapacity()).ToString(), new Vector2(player.getPos().X, player.getPos().Y + 128), Color.White); } break; case PlayState.MENU: spriteBatch.DrawString(font, "yolo", new Vector2(player.getPos().X - (SCREEN_WIDTH / 2) + 20, player.getPos().Y), Color.White); foreach (Button b in buttonList) { spriteBatch.Draw(b.tex, b.bounds, Color.White); System.Diagnostics.Debug.WriteLine("Yolo"); } break; default: break; } spriteBatch.End(); base.Draw(gameTime); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } //bob.travelToTarget(player.getPos(), 1, 4); //bob.rotateToTarget(player.getPos()); player.update(world); if (Keyboard.GetState().IsKeyDown(Keys.Enter) && connected == false) { try { handler.connectToServer(); connected = true; } catch (Exception e) { connected = false; System.Diagnostics.Debug.WriteLine("Could not connect!"); } } if (Keyboard.GetState().IsKeyDown(Keys.T) == true) { player.onMap(); } else { player.offMap(); if (player.isAlive()) { if (!player.isLanding()) { if (!player.isCooling()) { if (Keyboard.GetState().IsKeyDown(Keys.W) == true) { player.thrust(); } } if (/*!player.isBoosting() && !player.isCooling()*/ 1 == 1) { if (Keyboard.GetState().IsKeyDown(Keys.S) == true) { player.reverse(); } if (Keyboard.GetState().IsKeyDown(Keys.D) == true) { player.rotateRight(); } if (Keyboard.GetState().IsKeyDown(Keys.A) == true) { player.rotateLeft(); } if (Keyboard.GetState().IsKeyDown(Keys.Q) == true) { player.leftThrust(); } if (Keyboard.GetState().IsKeyDown(Keys.E) == true) { player.rightThrust(); } if (Keyboard.GetState().IsKeyDown(Keys.Space) == true) { player.brake(); } if (Keyboard.GetState().IsKeyDown(Keys.H) == true && player.getSpeed() < player.MAX_SPEED_TO_LAND) { for (int index = 0; index < objects.Count; index++) { if (objects[index].getType() == ObjectType.ASTEROID || objects[index].getType() == ObjectType.MINING_PLANET) { if (Math2.inRadius(player.getPos(), objects[index].getPos(), ((SpaceObject)objects[index]).getRadius())) { player.land((SpaceObject)objects[index]); index = objects.Count; } } } } if (!player.isCooling()) { if (Mouse.GetState().LeftButton == ButtonState.Pressed) { Laser l = player.fireWeapon(new Vector2(Mouse.GetState().Position.X, Mouse.GetState().Position.Y)); if (l != null) { projectiles.Add(l); } //Console.WriteLine(Mouse.GetState().Position.X.ToString() + ", " + Mouse.GetState().Position.Y.ToString()); } } } if (!player.isCooling()) { if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) == true) { player.boost(); } } if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) == false || player.isCooling()) { player.noBoost(); } } else { if (Keyboard.GetState().IsKeyDown(Keys.M) == true) { if (player.getCapacity() > 0) { player.mine(); } } if (Keyboard.GetState().IsKeyDown(Keys.G) == true) { player.takeOff(); } } } } toBeDestroyed.Clear(); foreach (AI o in ships.ToList()) { if (!o.isAlive()) { ships.Remove(o); } else { o.update(world); /*foreach(Object obj in objects) { * if (obj.getType() == ObjectType.PROJECTILE) { * Projectile p = (Projectile)obj; * //p.update(world); * if (o.isHit(p) && p.getID() != o.getID()) { * p.getHit(0); * o.getHit(p.getPower()); * //Console.WriteLine("Projectile " + o2.getID() + " hit " + o.getID()); * } * } * }*/ } } foreach (Projectile p in projectiles.ToList()) { if (!p.isAlive()) { projectiles.Remove(p); } else { p.update(world); } } foreach (Faction f in world.factions) { f.update(); } foreach (Object o in toBeDestroyed) { objects.Remove(o); } //connectToServer(); cam.UpdateCamera(viewport); base.Update(gameTime); }