public static Bullet LookForBullet(Camera cam, Player player, ContentManager Content, bool powerup, GameTime gameTime) { LastBullet += (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (powerup) { if (LastBullet >= ShootLimit / 2) { GamePadState pad = GamePad.GetState(PlayerIndex.One); if (pad.ThumbSticks.Right.X != 0 || pad.ThumbSticks.Right.Y != 0) { Vector2 offset = new Vector2(21, 51); Matrix m = Matrix.CreateRotationZ(cam.Rotation); offset = Vector2.Transform(offset, m); Vector2 Velocity = pad.ThumbSticks.Right; Velocity.Y *= -1; Velocity.Normalize(); LastBullet = 0; GameConfig.StartVibrate(); SoundManager.PlayRandomShootingSound(); return new Bullet(player.Position + offset, player.Position + offset + Velocity, false, Content, true); } } } else { if (LastBullet >= ShootLimit) { GamePadState pad = GamePad.GetState(PlayerIndex.One); if (pad.ThumbSticks.Right.X != 0 || pad.ThumbSticks.Right.Y != 0) { Vector2 offset = new Vector2(21, 51); Matrix m = Matrix.CreateRotationZ(cam.Rotation); offset = Vector2.Transform(offset, m); Vector2 Velocity = pad.ThumbSticks.Right; Velocity.Y *= -1; Velocity.Normalize(); LastBullet = 0; GameConfig.StartVibrate(); SoundManager.PlayRandomShootingSound(); return new Bullet(player.Position + offset, player.Position + offset + Velocity, false, Content, true); } } } return null; }
private void PlayerCollision(Player player) { for (int i = 0; i < enemyFiredBulletList.Count; i++) { Rectangle playerBound = new Rectangle((int)player.Position.X - 100, (int)player.Position.Y - 100, 200, 200); //Rectangle enemyBound2 = new Rectangle((int)enemyList[i].Position.X + 100, (int)enemyList[i].Position.Y + 100, 200, 200); Rectangle bulletBound = new Rectangle((int)enemyFiredBulletList[i].Position.X - 100, (int)enemyFiredBulletList[i].Position.Y - 100, 50, 50); //Rectangle bulletBound2 = new Rectangle((int)bulletList[j].Position.X + 100, (int)bulletList[j].Position.Y + 100, 50, 50); if (playerBound.Intersects(bulletBound)) { if (enemyFiredBulletList[i].IsColliding(player)) { enemyFiredBulletList.RemoveAt(i); player.Health -= 34; if (player.Health <= 0) { GameConfig.StartVibrate(); player.Die(); } return; } } } }
public void Update(Vector2 playerPosition, ContentManager content, List<Enemy> enemyList, GameTime gameTime, Player player) { Vector2 directionToMoveTowardPlayer = new Vector2(); Vector2 directionToMoveTowardPlanet = new Vector2(); if (mode == EnemyMode.random) { directionToMoveTowardPlayer = (playerPosition - Position); directionToMoveTowardPlayer.Normalize(); Position += directionToMoveTowardPlayer * speedInPixels; gameLoopsSinceLastFire++; } else { if( mode == EnemyMode.defender) { if (defenderEnemyState == DefenderEnemyState.killing) { Vector2 planetPosition = new Vector2(); switch (type) { case EnemyType.Baconite: planetPosition = GameConfig.BaconPos; break; case EnemyType.EggMan: planetPosition = GameConfig.EggPos; break; //case EnemyType.Cerealian: // planetPosition = GameConfig; // break; case EnemyType.Porrigian: planetPosition = GameConfig.PorridgePos; break; } Rectangle areaToDefend = new Rectangle((int)planetPosition.X - 2500, (int)planetPosition.Y - 2500, 5000, 5000); if (Bounds.Intersects(areaToDefend)) { //We're in the defend zone directionToMoveTowardPlayer = (playerPosition - Position); directionToMoveTowardPlayer.Normalize(); Position += directionToMoveTowardPlayer * speedInPixels; gameLoopsSinceLastFire++; } else { directionToMoveTowardPlanet = (planetPosition - Position); directionToMoveTowardPlanet.Normalize(); Position += directionToMoveTowardPlanet * (speedInPixels * 4); gameLoopsSinceLastFire++; } } if(defenderEnemyState == DefenderEnemyState.patrolling) { Vector2 planetPosition = new Vector2(); switch (type) { case EnemyType.Baconite: planetPosition = GameConfig.BaconPos; break; case EnemyType.EggMan: planetPosition = GameConfig.EggPos; break; //case EnemyType.Cerealian: // planetPosition = GameConfig; // break; case EnemyType.Porrigian: planetPosition = GameConfig.PorridgePos; break; } if (positionToMoveToOnPatrol == Vector2.Zero || Position == positionToMoveToOnPatrol) { positionToMoveToOnPatrol = new Vector2(planetPosition.X + Game1.getRandom(-100, 100), planetPosition.Y + Game1.getRandom(-1000, 1000)); directionToMoveTowardsPatrol = (positionToMoveToOnPatrol - Position); directionToMoveTowardsPatrol.Normalize(); Position += directionToMoveTowardsPatrol * (speedInPixels); angletoPointRightWay = VectorToAngle(directionToMoveTowardsPatrol); Rotation = angletoPointRightWay; } else { Position += directionToMoveTowardsPatrol * (speedInPixels); angletoPointRightWay = VectorToAngle(directionToMoveTowardsPatrol); Rotation = angletoPointRightWay; } if ((playerPosition.X - Position.X) < weaponRange && (playerPosition.Y - Position.Y) < weaponRange) { defenderEnemyState = DefenderEnemyState.killing; } } } } if ((playerPosition.X - Position.X) < weaponRange && (playerPosition.Y - Position.Y) < weaponRange) { //Within Range, //FIRE! if (gameLoopsSinceLastFire >= fireRateInGameLoops) { enemyFiredBulletList.Add(new Bullet(Position, playerPosition, false, content, false)); gameLoopsSinceLastFire = 0; } } for (int i = 0; i < enemyFiredBulletList.Count; i++) { if (enemyFiredBulletList[i].UpdateBullets(gameTime)) { enemyFiredBulletList.RemoveAt(i); break; } } Bounds.X = (int)Position.X; Bounds.Y = (int)Position.Y; foreach (Enemy e in enemyList) { Vector2 directionTowardsOtherEnemy = (e.Position - Position); directionTowardsOtherEnemy.Normalize(); if (e.ID != ID && Bounds.Intersects(e.Bounds)) { Position -= directionTowardsOtherEnemy * (speedInPixels); } } angletoPointRightWay = VectorToAngle(directionToMoveTowardPlayer); Rotation = angletoPointRightWay; gameLoopsSinceLastFire++; PlayerCollision(player); }
private void NewGame() { starList = new List<Stars>(); bulletList = new List<Bullet>(); enemyList = new List<Enemy>(); planetList = new List<Planet>(); HighscoreManager.Reset(); powerupList = new List<Powerup>(); powerupsAvailable = new List<Powerup>(); starList = getStars(Content); particleTex = Content.Load<Texture2D>("particle"); MakeMap(); GameConfig.PlayerStartPos = GameConfig.EarthPos - new Vector2(15, 15); GameConfig.ParticleTexture = Content.Load<Texture2D>("particle"); GameConfig.EnemyExplosion = new ParticleProject.ParticleEngine(GameConfig.ParticleTexture, Vector2.Zero, 0); GameConfig.EnemyExplosion.Location.X = -1; MakePowerups(); player = new Player(Content); player.Lives = 3; Enemy.LoadContent(Content); EnemySpawner.InitialSpawn(ref enemyList); Powerup.SpawnPowerups(Content); }