private void updateEnemyBlock(EnemyBlockNode block) { int min_x = int.MaxValue, min_y = int.MaxValue, max_x = 0, max_y = 0; foreach (Node enemies in lst_enemies) { EnemyNode en = (EnemyNode)enemies; // min calcul if (min_x > en.pos.point.x) { min_x = (int)en.pos.point.x; } if (min_y > en.pos.point.y) { min_y = (int)en.pos.point.y; } // max calcul if (max_x < en.pos.point.x + en.display.bitmap.Width) { max_x = (int)en.pos.point.x + en.display.bitmap.Width; } if (max_y < en.pos.point.y + en.display.bitmap.Height) { max_y = (int)en.pos.point.y + en.display.bitmap.Height; } } block.block.upperLeft.x = min_x; block.block.upperLeft.y = min_y; block.block.bottomRight.x = max_x; block.block.bottomRight.y = max_y; }
public override void update(float time, Graphics g) { if (runnable) { // We set up the gunnode to allow it to shoot again GunControlNode gunctrlnode = null; GameStateNode gstatenode = null; SpaceShipCollisionNode sscn = null; EnemyBlockNode eblocknode = null; if (lst_gun.Count > 0) { gunctrlnode = (GunControlNode)lst_gun.First(); } if (lst_game.Count > 0) { gstatenode = (GameStateNode)lst_game.First(); } if (lst_block.Count > 0) { eblocknode = (EnemyBlockNode)lst_block.First(); } #region bullet bunker collision foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping { foreach (Node bunker in lst_bunker.ToList()) { BulletCollisionNode bullcn = (BulletCollisionNode)bullet; BunkerCollisionNode bunkcn = (BunkerCollisionNode)bunker; if (bunkcn.bunker.life > 0 && CheckRectangleCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos) && CheckPixelCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos) ) { CheckPixelCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos, bullcn.bullet.damage); bunkcn.bunker.life -= pixel_destroyed; pixel_destroyed = 0; if (gunctrlnode != null && bullcn.bullet.ally) { gunctrlnode.gun.shoot = true; } ef.removeEntity(bullcn.entity); } } } #endregion #region enemy bullet collision gstatenode.gs.enemiesCount = lst_enemy.Count; foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping { foreach (Node enemy in lst_enemy.ToList()) { BulletCollisionNode bullcn = (BulletCollisionNode)bullet; EnemyNode en = (EnemyNode)enemy; if (en.enemy.life > 0 && bullcn.bullet.ally && CheckRectangleCollision(bullcn.display, bullcn.pos, en.display, en.pos) && CheckPixelCollision(bullcn.display, bullcn.pos, en.display, en.pos) ) { ef.removeEntity(bullcn.entity); en.enemy.life -= 1; if (en.enemy.life <= 0) { if (gstatenode != null) { switch (en.enemy.type) { case Enemy.Type.Big: gstatenode.gs.score += 500; break; case Enemy.Type.Medium: gstatenode.gs.score += 300; break; case Enemy.Type.Small: gstatenode.gs.score += 100; break; } } ef.removeEntity(en.entity); } pixel_destroyed = 0; if (gunctrlnode != null) { gunctrlnode.gun.shoot = true; } if (Game.rand.Next(20) == 1) // Drop a bonus { Array values = Enum.GetValues(typeof(Bonus.Type)); ef.CreateBonus(en.pos, (Bonus.Type)values.GetValue(Game.rand.Next(values.Length))); } } } } #endregion #region spaceship bullet collision if (lst_spaceship.Count > 0) { sscn = (SpaceShipCollisionNode)lst_spaceship.First(); foreach (Node bullet in lst_bullet.ToList()) { BulletCollisionNode bullcn = (BulletCollisionNode)bullet; if (sscn.spaceship.life > 0 && !bullcn.bullet.ally && CheckRectangleCollision(bullcn.display, bullcn.pos, sscn.display, sscn.pos) && CheckPixelCollision(bullcn.display, bullcn.pos, sscn.display, sscn.pos) ) { ef.removeEntity(bullcn.entity); sscn.spaceship.life -= bullcn.bullet.damage; gstatenode.gs.lives = sscn.spaceship.life; pixel_destroyed = 0; } } } #endregion #region bullet bullet collision foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping { foreach (Node bull in lst_bullet.ToList()) { BulletCollisionNode bullcn = (BulletCollisionNode)bullet; BulletCollisionNode en = (BulletCollisionNode)bull; if (bullcn.entity.Name != en.entity.Name) { if ( CheckRectangleCollision(bullcn.display, bullcn.pos, en.display, en.pos) && CheckPixelCollision(bullcn.display, bullcn.pos, en.display, en.pos) ) { ef.removeEntity(bullcn.entity); ef.removeEntity(en.entity); if (gunctrlnode != null) { gunctrlnode.gun.shoot = true; } } } } } #endregion #region spaceship bonus collision if (lst_spaceship.Count > 0) { sscn = (SpaceShipCollisionNode)lst_spaceship.First(); foreach (Node bonus in lst_bonus.ToList()) { BonusCollisionNode bonusn = (BonusCollisionNode)bonus; if (sscn.spaceship.life > 0 && CheckRectangleCollision(bonusn.display, bonusn.pos, sscn.display, sscn.pos) && CheckPixelCollision(bonusn.display, bonusn.pos, sscn.display, sscn.pos) ) { switch (bonusn.bonus.type) { case Bonus.Type.PlusLife: sscn.spaceship.life++; bonus_label = "+ 1 life"; break; case Bonus.Type.ScoreBoost: gstatenode.gs.score *= 2; bonus_label = "Score doubled"; break; case Bonus.Type.RestoreBunker: foreach (Node bunker in lst_bunker.ToList()) { ef.removeEntity(bunker.entity); } ef.CreateBunkerEntities(size); bonus_label = "Bunker restored"; break; case Bonus.Type.EnemySlow: foreach (Node enemy in lst_enemy.ToList()) { EnemyNode enem = (EnemyNode)enemy; enem.enemy.vitesse.x /= 5; } bonus_label = "Enemies slowdown"; break; case Bonus.Type.doubleShoot: Gun gun = (Gun)sscn.entity.GetComponent("Gun"); gun.doubleShoot = true; bonus_label = "Double bullet"; break; case Bonus.Type.controlledBullet: Gun pisto = (Gun)sscn.entity.GetComponent("Gun"); pisto.controllableShoot = true; bonus_label = "Controlled Bullet"; break; } Console.WriteLine(bonus_label); ef.removeEntity(bonusn.entity); bonus_sound.Play(); gstatenode.gs.lives = sscn.spaceship.life; pixel_destroyed = 0; } } if (g != null && compteur_label++ < 1000) { g.DrawString( bonus_label, Game.font, Game.blackBrush, size.Width - 130, size.Height - 50 ); } else if (g != null && compteur_label++ > 200) { compteur_label = 0; bonus_label = ""; } } #endregion #region enemyblock collision if (eblocknode.block.direction == EnemyBlock.Direction.Left && eblocknode.block.upperLeft.x <= 0) { eblocknode.block.collided = true; eblocknode.block.direction = EnemyBlock.Direction.Right; eblocknode.block.shootProbability -= 10; } else if (eblocknode.block.direction == EnemyBlock.Direction.Right && eblocknode.block.bottomRight.x >= size.Width) { eblocknode.block.collided = true; eblocknode.block.direction = EnemyBlock.Direction.Left; eblocknode.block.shootProbability -= 10; } if (lst_spaceship.Count > 0) { sscn = (SpaceShipCollisionNode)lst_spaceship.First(); // we check the collision we the spaceship and then set the game to lose if collision occured if ( sscn.pos.point.x + sscn.display.bitmap.Width >= eblocknode.block.upperLeft.x && sscn.pos.point.x <= eblocknode.block.bottomRight.x && sscn.pos.point.y + sscn.display.bitmap.Height >= eblocknode.block.upperLeft.y && sscn.pos.point.y <= eblocknode.block.bottomRight.y ) { gstatenode.gs.state = Game.State.Lost; } } #endregion } }
public override void update(float time, Graphics g) { if (runnable) { EnemyBlockNode ben = (EnemyBlockNode)lst_block.First(); GameStateNode gsn = (GameStateNode)lst_game.First(); foreach (Node node in lst) { GunControlNode gcn = (GunControlNode)node; if (keyPool.Contains(gcn.gunControl.space) && gcn.gun.shoot) { gcn.gun.shootingPoint = gcn.pos.point + new Vector2D(gcn.display.bitmap.Width / 2, 0); sp.Play(); gcn.gun.shoot = false; keyPool.Remove(gcn.gunControl.space); if (gcn.gun.doubleShoot && timer_bonus < 5) { timer_bonus++; } else { timer_bonus = 0; gcn.gun.doubleShoot = false; } ef.CreateSpaceShipBullet(gcn.gun); } } // we update enemies gun shooting point foreach (Node enemy in lst_enemies) { EnemyNode en = (EnemyNode)enemy; en.gun.shootingPoint = en.pos.point + new Vector2D(en.display.bitmap.Width / 2, 0); } // we see if we shoot anything, if so we choose a random enemy to do so if (Game.rand.Next(ben.block.shootProbability) <= gsn.gs.level * 2) { int index = Game.rand.Next(lst_enemies.Count); if (lst_enemies.Count > 0) { EnemyNode en = (EnemyNode)lst_enemies.ToList()[index]; int damage = 0; Bitmap b = null; switch (en.enemy.type) { case Enemy.Type.Big: b = Properties.Resources.shoot4; damage = 3 + gsn.gs.level; break; case Enemy.Type.Medium: damage = 2 + gsn.gs.level; b = Properties.Resources.shoot3; break; case Enemy.Type.Small: b = Properties.Resources.shoot2; damage = 1 + gsn.gs.level; break; } ef.CreateEnemyBullet(en.gun, b, damage); } } } }
public override void update(float time, Graphics g) { if (runnable) { #region basics movement GunControlNode gunnode = (GunControlNode)lstgun.First(); foreach (Node n in lstmovement.ToList()) // We transform to list to entity from node { MovementNode mn = (MovementNode)n; Velocity v = mn.vitesse; Position p = mn.pos; Vector2D newv = time * v.speedVect + p.point; if (newv.x < size.Width || newv.x > 0) { p.point = newv; } if (newv.y < 0) { factory.removeEntity(mn.entity); gunnode.gun.shoot = true; } else if (newv.y > size.Height) { factory.removeEntity(mn.entity); } } #endregion #region enemies movement GameStateNode gstatenode = null; EnemyBlockNode eblocknode = null; if (lst_game.Count > 0) { gstatenode = (GameStateNode)lst_game.First(); } if (lst_block.Count > 0) { eblocknode = (EnemyBlockNode)lst_block.First(); } // First we move enemies int compteur = 0; foreach (Node enemies in lst_enemies) { EnemyNode en = (EnemyNode)enemies; switch (eblocknode.block.direction) { case EnemyBlock.Direction.Right: en.pos.point.x += en.enemy.vitesse.x * time + (gstatenode.gs.level * time); break; case EnemyBlock.Direction.Left: en.pos.point.x -= en.enemy.vitesse.x * time + (gstatenode.gs.level * time); break; } if (eblocknode.block.collided) { if (time == 0) { time = (float)0.005; // In case the draw update happens at impact } en.pos.point.y += en.enemy.vitesse.y * time; en.enemy.vitesse.y += size.Width / 10 + (gstatenode.gs.level * time); en.enemy.vitesse.x += 3 * gstatenode.gs.level; } } if (eblocknode.block.collided) { eblocknode.block.collided = false; } updateEnemyBlock(eblocknode); #endregion } }