public void InitializeScreen <TShip>(ShipTier tier) where TShip : BaseAllyShip { //Reset music _gameHasStarted = false; //_allowMusicHandling = false; playerSbObjects.Clear(); Sprites.Sprites.Clear(); enemies.Clear(); BackgroundSprite bgspr = new BackgroundSprite(bgImg, Sprites.SpriteBatch, 10, 2); bgspr.Drawn += new EventHandler(bgspr_Drawn); worldCam.Pos = new Vector2(bgspr.TotalWidth / 2, bgspr.TotalHeight - (bgspr.Height / 2)); BackgroundSprite = bgspr; Vector2 minSpawnArea = _playableAreaOffset; Vector2 maxSpawnArea = new Vector2(bgspr.TotalWidth, bgspr.TotalHeight) - _playableAreaOffset; for (int i = 0; i < 4; i++) { Texture2D enemyTexture = GameContent.GameAssets.Images.Ships[ShipType.Drone, StateManager.RandomGenerator.NextShipTier(ShipTier.Tier1, ShipTier.Tier2)]; EnemyDrone enemy = new EnemyDrone(GameContent.GameAssets.Images.Ships[ShipType.EnemyBattleCruiser, ShipTier.Tier1], Vector2.Zero, Sprites.SpriteBatch); enemy.WorldCoords = StateManager.RandomGenerator.NextVector2(minSpawnArea, maxSpawnArea); //TODO: Different texture enemy.Color = Color.Green; enemy.Tier = ShipTier.Tier1; Sprites.Add(enemy); enemies.Add(enemy); } miniMap = new Sprite(new PlainTexture2D(Sprites.SpriteBatch.GraphicsDevice, 1, 1, new Color(Color.Navy.R, Color.Navy.G, Color.Navy.B, 128)), Vector2.Zero, playerSb); miniMap.Width = bgspr.TotalWidth / MinimapDivAmount; miniMap.Color = Color.Transparent; miniMap.Height = bgspr.TotalHeight / MinimapDivAmount; miniMap.Y = 7.5f; miniMap.Updated += new EventHandler(miniMap_Updated); miniMap.X = playerSb.GraphicsDevice.Viewport.Width - miniMap.Width - 7.5f; miniShipInfoBg = new Sprite(new PlainTexture2D(Sprites.SpriteBatch.GraphicsDevice, 1, 1, new Color(0, 0, 0, 192)), new Vector2(7.5f, miniMap.Y), playerSb); miniShipInfoBg.Height = 0.01f; miniShipInfoBg.Width = 767.5f - miniShipInfoBg.X - 7.5f - miniMap.Width - 266.6666667f; miniShipInfoBg.X = miniMap.X - miniShipInfoBg.Width - 7.5f; miniShipInfoBg.Color = Color.Transparent; playerSbObjects.Add(miniShipInfoBg); playerSbObjects.Add(miniMap); if (typeof(TShip) == typeof(Drone)) { throw new Exception("Can't create a Drone as the main ship"); } TShip ship = null; if (typeof(TShip) == typeof(FighterCarrier)) { ship = new FighterCarrier(null, Vector2.Zero, playerSb, GameContent.GameAssets.Images.Ships[ShipType.Drone, ShipTier.Tier1]).Cast <TShip>(); } else { ship = Activator.CreateInstance(typeof(TShip), null, Vector2.Zero, playerSb).Cast <TShip>(); } ship.Texture = GameContent.GameAssets.Images.Ships[ship.ShipType, ShipTier.Tier1]; ship.UseCenterAsOrigin = true; ship.WorldSb = Sprites.SpriteBatch; ship.Tier = tier; ship.Position = ship.GetCenterPosition(Sprites.SpriteBatch.GraphicsDevice.Viewport, true); playerShip = ship; playerShip.IsPlayerShip = true; playerShip.RotateTowardsMouse = true; playerSbObjects.Add(ship); playerShip.InitialHealth = 100; //Set as own ship playerShip.PlayerType = PlayerType.MyShip; }
public override void Update(GameTime gameTime) { if (!_gameHasStarted) { //_allowMusicHandling = false; MediaPlayer.Stop(); if (StateManager.Options.MusicEnabled) { MediaPlayer.Play(_gameSong); } //_allowMusicHandling = true; } base.Update(gameTime); BackgroundSprite bg = BackgroundSprite.Cast <BackgroundSprite>(); //TODO: UPDATE SPRITES KeyboardState keyboard = Keyboard.GetState(); if (_lastState.IsKeyUp(Keys.Escape) && keyboard.IsKeyDown(Keys.Escape)) { StateManager.ScreenState = ScreenType.Pause; //_allowMusicHandling = false; MediaPlayer.Pause(); } for (int i = 0; i < playerShip.FlyingBullets.Count; i++) { Bullet b = playerShip.FlyingBullets[i]; if (b.IsDead || b.X <= 0 || b.X >= bg.TotalWidth || b.Y <= 0 || b.Y >= bg.TotalHeight) { playerShip.FlyingBullets.RemoveAt(i); i--; } } for (int e = 0; e < enemies.Count; e++) { BaseEnemyShip enemy = enemies[e]; if (enemy.IsDead) { enemies.Remove(enemy); } for (int i = 0; i < enemy.FlyingBullets.Count; i++) { Bullet b = enemy.FlyingBullets[i]; if (b.IsDead || b.X <= 0 || b.X >= bg.TotalWidth || b.Y <= 0 || b.Y >= bg.TotalHeight) { enemy.FlyingBullets.RemoveAt(i); i--; } } } if (playerShip.GetType() == typeof(FighterCarrier)) { FighterCarrier ship = playerShip.Cast <FighterCarrier>(); foreach (Drone drone in ship.Drones) { for (int i = 0; i < drone.FlyingBullets.Count; i++) { if (drone.FlyingBullets[i].IsDead || drone.FlyingBullets[i].X <= 0 || drone.FlyingBullets[i].X >= bg.TotalWidth || drone.FlyingBullets[i].Y <= 0 || drone.FlyingBullets[i].Y >= bg.TotalHeight) { drone.FlyingBullets.RemoveAt(i); i--; } } } } foreach (Ship shootShip in StateManager.ActiveShips) { foreach (Ship hitShip in StateManager.ActiveShips) { if (shootShip != hitShip && shootShip.PlayerType != hitShip.PlayerType) { if (hitShip.PlayerType == PlayerType.MyShip && shootShip.PlayerType == PlayerType.Ally) { } else if (shootShip.PlayerType == PlayerType.MyShip && hitShip.PlayerType == PlayerType.Ally) { } else { foreach (Bullet b in shootShip.FlyingBullets) { if (b.Rectangle.Intersects(hitShip.Rectangle)) { hitShip.CurrentHealth -= b.Damage; b.IsDead = true; } } } } } } Vector2 camMove = Vector2.Zero; if (StateManager.InputManager.ShouldMove(MoveDirection.Up)) { float ymoveAmount = -playerShip.MovementSpeed.Y; #if XBOX ymoveAmount *= Math.Abs(GamePadManager.One.Current.ThumbSticks.Left.Y); #endif if (worldCam.Pos.Y + ymoveAmount >= bg.Height / 2) { camMove.Y = ymoveAmount; } else { camMove.Y = bg.Height / 2 - worldCam.Pos.Y; } } else if (StateManager.InputManager.ShouldMove(MoveDirection.Down)) { float ymoveAmount = playerShip.MovementSpeed.Y; #if XBOX ymoveAmount *= Math.Abs(GamePadManager.One.Current.ThumbSticks.Left.Y); #endif if (worldCam.Pos.Y + ymoveAmount <= bg.TotalHeight - (bg.Height / 2)) { camMove.Y = ymoveAmount; } else { camMove.Y = (bg.TotalHeight - (bg.Height / 2)) - worldCam.Pos.Y; } } if (StateManager.InputManager.ShouldMove(MoveDirection.Right)) { float xmoveAmount = playerShip.MovementSpeed.X; #if XBOX xmoveAmount *= Math.Abs(GamePadManager.One.Current.ThumbSticks.Left.X); #endif if (worldCam.Pos.X + xmoveAmount <= bg.TotalWidth - (bg.Width / 2)) { camMove.X = xmoveAmount; } else { camMove.X = (bg.TotalWidth - (bg.Width / 2)) - worldCam.Pos.X; } } else if (StateManager.InputManager.ShouldMove(MoveDirection.Left)) { float xmoveAmount = -playerShip.MovementSpeed.X; #if XBOX xmoveAmount *= Math.Abs(GamePadManager.One.Current.ThumbSticks.Left.X); #endif if (worldCam.Pos.X + xmoveAmount >= bg.Width / 2) { camMove.X = xmoveAmount; } else { camMove.X = bg.Width / 2 - worldCam.Pos.X; } } #if WINDOWS if (_lastState.IsKeyUp(Keys.M) && keyboard.IsKeyDown(Keys.M)) { miniMap.Color = miniMap.Color == Color.White ? Color.Transparent : Color.White; } #endif if (_lastState.IsKeyUp(Keys.F11) && keyboard.IsKeyDown(Keys.F11)) { StateManager.Options.ToggleFullscreen(); } worldCam.Move(camMove); playerShip.WorldCoords = worldCam.Pos; foreach (ISprite s in playerSbObjects) { if (s != miniMap) { if (s.GetType().Implements(typeof(ITimerSprite))) { s.Cast <ITimerSprite>().Update(gameTime); } else { s.Update(); } } } _gameHasStarted = true; miniMap.Update(); _lastState = keyboard; }
public static BaseAllyShip CreateShip(ShipType type, ShipTier tier, SpriteBatch spawnSpriteBatch, Boolean isAllyShip) { BaseAllyShip bas = null; switch (type) { case ShipType.BattleCruiser: bas = new BattleCruiser(GameContent.Assets.Images.Ships[type, tier], Vector2.Zero, spawnSpriteBatch, isAllyShip); break; case ShipType.FighterCarrier: bas = new FighterCarrier(GameContent.Assets.Images.Ships[type, tier], Vector2.Zero, spawnSpriteBatch, GameContent.Assets.Images.Ships[ShipType.Drone, ShipTier.Tier1], isAllyShip); break; case ShipType.TorpedoShip: bas = new TorpedoShip(GameContent.Assets.Images.Ships[type, tier], Vector2.Zero, spawnSpriteBatch, isAllyShip); break; default: throw new NotImplementedException("Cannot create the specified ship type."); } bas.Tier = tier; bas.UseCenterAsOrigin = true; //bas.FriendlyName = type.ToFriendlyString(); return bas; }