// CONSTRUCTOR public GameMainMulti(string IP) { menu = new MainMenu(Global.Handler, 0f); this.IP = Global.MainMenu.ip; Walls = new List<Wall>(); bonus = new List<Bonus>(); enemies = new List<Enemy>(); enemies2 = new List<Enemy2>(); Global.GameMainMulti = this; player = new Player(); player2 = new Player(); readStream = new MemoryStream(); writeStream = new MemoryStream(); reader = new BinaryReader(readStream); writer = new BinaryWriter(writeStream); bullets = new Bullets(Resources.bullet); speedbonus = new List<VitesseBonus>(); for (int x = 0; x < map.GetLength(1); x++) { for (int y = 0; y < map.GetLength(0); y++) { int number = map[y, x]; if (number == 1) Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Brick_top, size, size, Color.White)); if (number == 2) Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Brick, size, size, Color.White)); if (number == 3) Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Platform, 100, 16, Color.Gray)); if (number == 4) speedbonus.Add(new VitesseBonus(x * size + 40, y * size + 74, Resources.Speed, 20, 20, Color.White)); } } }
// CONSTRUCTOR public GameMainMulti(string IP) { menu = new MainMenu(Global.Handler, 0f); this.IP = Global.MainMenu.ip; Walls = new List <Wall>(); bonus = new List <Bonus>(); enemies = new List <Enemy>(); enemies2 = new List <Enemy2>(); Global.GameMainMulti = this; player = new Player(); player2 = new Player(); readStream = new MemoryStream(); writeStream = new MemoryStream(); reader = new BinaryReader(readStream); writer = new BinaryWriter(writeStream); bullets = new Bullets(Resources.bullet); speedbonus = new List <VitesseBonus>(); for (int x = 0; x < map.GetLength(1); x++) { for (int y = 0; y < map.GetLength(0); y++) { int number = map[y, x]; if (number == 1) { Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Brick_top, size, size, Color.White)); } if (number == 2) { Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Brick, size, size, Color.White)); } if (number == 3) { Walls.Add(new Wall(x * size, y * size + size / 2, Resources.Platform, 100, 16, Color.Gray)); } if (number == 4) { speedbonus.Add(new VitesseBonus(x * size + 40, y * size + 74, Resources.Speed, 20, 20, Color.White)); } } } }
// UPDATE & DRAW public void Update(MouseState souris, KeyboardState clavier, List <Wall> walls, List <Bonus> bonus) { if (Global.Collisions.collision_speed) { speed = 6; } else if (Global.Collisions.LowSpeed) { speed = 2; } else { speed = 4; } GameTime gametime; gametime = new GameTime(); Main.Update(gametime); #region Bullets if (Keyboard.GetState().IsKeyDown(Keys.Q) && bullets.Count() < 3 && !shot && Global.Handler.ammo_left > 0 && state == "standing") { shot = true; Bullets bullet = new Bullets(Resources.bullet); bullet.velocity = 5; if (Direction == Direction.Right) { bullet.position = new Vector2(Hitbox.X + Hitbox.Width / 2, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); Old_Dir = Direction.Right; } else { bullet.position = new Vector2(Hitbox.X, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); Old_Dir = Direction.Left; } bullet.isVisible = true; bullets.Add(bullet); } else { shot = false; } foreach (Bullets bullet in bullets) { if (Old_Dir == Direction.Right) { bullet.position.X += bullet.velocity; // va vers la droite } else { bullet.position.X -= bullet.velocity; // va vers la gauche } // la balle disparait si elle parcourt la distance ou rencontre un obstacle if (Vector2.Distance(bullet.position, new Vector2(Hitbox.X, Hitbox.Y)) >= 200) { bullet.isVisible = false; } foreach (Wall wall in walls) { if (wall.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 5, 2))) { bullet.isVisible = false; } } } //on retire la balle de la liste si elle disparait for (int i = 0; i < bullets.Count; i++) { if (!bullets[i].isVisible) { bullets.RemoveAt(i); i--; } } #endregion #region Gravité bool gravity = collisions.CollisionDown(Hitbox, walls, this.fallspeed); if (!gravity) { if (collisions.CollisionUp(Hitbox, walls, fallspeed)) { speedjump = 0; } if (hasjumped && speedjump > 1) { this.Hitbox.Y -= speedjump; speedjump -= 1; } else { this.Hitbox.Y += this.fallspeed; framecolumn = 1; state = "jump"; } } else { if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 4)) { if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 2)) { this.Hitbox.Y += 2; } else { this.Hitbox.Y++; } } if (state == "jump") { state = "standing"; } } #endregion #region Couteau if (clavier.IsKeyDown(Keys.D) && state == "standing") { SoundEffectInstance couteau_sound_instance = Resources.couteau.CreateInstance(); couteau_sound_instance.Play(); framecolumn = 1; compteur = 1; state = "cut"; Hitbox.Width = 50; Hitbox.Y -= 16; Hitbox.Height = 55; } #endregion #region Déplacements //Le sprite et la hitbox accroupi quand le personnage est baissé ou coincé sous un mur if (clavier.IsKeyDown(Keys.Down)) { Hitbox.Width = 35; Hitbox.Height = 25; state = "squat"; } //Celui debout quand on se relève et qu'il y a suffisament de place if (clavier.IsKeyUp(Keys.Down) && Hitbox.Height == 25 && !collisions.CollisionUp(new Rectangle(Hitbox.X, Hitbox.Y - 15, Hitbox.Width, Hitbox.Height), walls, speed)) { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; if (collisions.CollisionDown(new Rectangle(Hitbox.X, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed)) { Hitbox.Y = Hitbox.Y - 15; } } // Tir Marco if (clavier.IsKeyDown(Keys.Q) && state == "standing") //On tire avec "Q" et debout uniquement { if (state != "shoot") { framecolumn = 1; compteur = 1; } if (Global.Handler.ammo_left > 0) { fire_sound_instance.Play(); state = "shoot"; } else { vide_sound_instance.Play(); } } if (state == "cut") { if (compteur % 2 == 0) { if (framecolumn < 8) { framecolumn++; compteur++; } else { framecolumn = 1; if (clavier.IsKeyUp(Keys.D)) { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; Hitbox.Y += 15; } } } else { compteur++; } } else if (state == "shoot") { if (compteur % 4 == 0) { if (framecolumn < 10) { Hitbox.Width = 52; Hitbox.Height = 40; framecolumn++; } else { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; framecolumn = 1; compteur = 1; } } compteur++; } else if (clavier.IsKeyDown(Keys.Right) && state != "squat") // Permet de bouger à droite { // Droite si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionRight(Hitbox, walls, speed)) { this.Hitbox.X += speed; this.Direction = Direction.Right; if (collisions.CollisionDown(Hitbox, walls, speed)) { this.Animate(1, 15, 2); } } else { framecolumn = 1; compteur = 1; } } else if (clavier.IsKeyDown(Keys.Left) && state != "squat") // Permet de bouger à gauche { // Gauche si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionLeft(Hitbox, walls, this.speed) && Hitbox.X > 0) { this.Hitbox.X -= this.speed; this.Direction = Direction.Left; if (collisions.CollisionDown(Hitbox, walls, this.speed)) { this.Animate(1, 15, 2); } } else { framecolumn = 1; compteur = 1; } } if (clavier.IsKeyDown(Keys.Right) && state == "squat") // Permet de bouger à droite accroupi { // Droite si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionRight(Hitbox, walls, this.speed)) { this.Hitbox.X += this.speed - 1; this.Direction = Direction.Right; if (collisions.CollisionDown(Hitbox, walls, this.speed)) { this.Animate(2, 8, 2); } } else { framecolumn = 1; compteur = 1; } } else if (clavier.IsKeyDown(Keys.Left) && state == "squat") // Permet de bouger à gauche accroupi { // Gauche si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionLeft(Hitbox, walls, this.speed) && Hitbox.X > 0) { this.Hitbox.X -= this.speed - 1; this.Direction = Direction.Left; if (collisions.CollisionDown(Hitbox, walls, this.speed)) { this.Animate(2, 8, 2); } } else { framecolumn = 1; compteur = 1; } } #endregion #region Déplacements test else if (clavier.IsKeyDown(Keys.A)) { Hitbox = new Rectangle(50, 370, Hitbox.Width, Hitbox.Height); } else if (clavier.IsKeyDown(Keys.N)) { Hitbox = new Rectangle(4500, 100, Hitbox.Width, Hitbox.Height); } #endregion #region Saut if (clavier.IsKeyDown(Keys.Space)) { if (compteursaut == 1) { spaceIsPressed = false; } else { spaceIsPressed = true; compteursaut++; } } else { compteursaut = 0; } if (clavier.IsKeyDown(Keys.Space) && spaceIsPressed && !hasjumped && gravity && state == "standing") { framecolumn = 1; if (!collisions.CollisionUp(Hitbox, walls, this.fallspeed)) { SoundEffectInstance saut_instance = Resources.saut.CreateInstance(); saut_instance.Play(); this.Hitbox.Y -= 12; speedjump = 12; } hasjumped = true; state = "jump"; } if (hasjumped == true && collisions.CollisionDown(Hitbox, walls, this.fallspeed) == true && state != "squat") { hasjumped = false; state = "standing"; } #endregion #region Effets et animation if (clavier.IsKeyUp(Keys.Right) && clavier.IsKeyUp(Keys.Left) && state == "standing") // Ici c'est une partie de l'animation { framecolumn = 1; } switch (Direction) { case Direction.Left: effect = SpriteEffects.FlipHorizontally; break; case Direction.Right: effect = SpriteEffects.None; break; default: break; } #endregion #region Chute dead = Hitbox.Y > 480; if (dead) { Hitbox = new Rectangle(50, 350, Hitbox.Width, Hitbox.Height); } #endregion if (Global.Collisions.accu > 0) { change = false; } if (!change && Global.Collisions.accu > 0) { if (marco_color.G < 20) { up = true; } if (marco_color.G > 235) { up = false; } if (up) { marco_color.G += 15; marco_color.B += 15; } else { marco_color.G -= 15; marco_color.B -= 15; } Global.Collisions.accu--; } else { marco_color = new Color(254, 255, 255, 255); } }
// UPDATE & DRAW public void Update(MouseState souris, KeyboardState clavier, List<Wall> walls, List<Bonus> bonus) { GameTime gametime; gametime = new GameTime(); Main.Update(gametime); #region Bullets if (Keyboard.GetState().IsKeyDown(Keys.Q) && bullets.Count() < 3 && !shot && Global.Handler.ammo_left > 0 && state == "standing") { shot = true; Bullets bullet = new Bullets(Resources.bullet); bullet.velocity = 5; if (Direction == Direction.Right) bullet.position = new Vector2(Hitbox.X + Hitbox.Width / 2, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); else bullet.position = new Vector2(Hitbox.X, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); bullet.isVisible = true; bullets.Add(bullet); } else shot = false; foreach (Bullets bullet in bullets) { if (Direction == Direction.Right) bullet.position.X += bullet.velocity; // va vers la droite else bullet.position.X -= bullet.velocity; // va vers la gauche // la balle disparait si elle parcourt la distance ou rencontre un obstacle if (Vector2.Distance(bullet.position, new Vector2(Global.Player.Hitbox.X, Global.Player.Hitbox.Y)) > 150) bullet.isVisible = false; foreach (Wall wall in Global.GameMain.Walls) { if (wall.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 5, 2))) bullet.isVisible = false; } } //on retire la balle de la liste si elle disparait for (int i = 0; i < bullets.Count; i++) { if (!bullets[i].isVisible) { bullets.RemoveAt(i); i--; } } #endregion #region Gravité bool gravity = collisions.CollisionDown(Hitbox, walls, this.fallspeed); if (!gravity) { if (collisions.CollisionUp(Hitbox, walls, fallspeed)) speedjump = 0; if (hasjumped && speedjump > 1) { this.Hitbox.Y -= speedjump; speedjump -= 1; } else { this.Hitbox.Y += this.fallspeed; framecolumn = 1; state = "jump"; } } else { if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 4)) if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 2)) this.Hitbox.Y += 2; else this.Hitbox.Y++; if (state == "jump") state = "standing"; } #endregion #region Déplacements //Le sprite et la hitbox accroupi quand le personnage est baissé ou coincé sous un mur if (clavier.IsKeyDown(Keys.Down)) { Hitbox.Width = 35; Hitbox.Height = 25; state = "squat"; } //Celui debout quand on se relève et qu'il y a suffisament de place if (clavier.IsKeyUp(Keys.Down) && Hitbox.Height == 25 && !collisions.CollisionUp(new Rectangle(Hitbox.X, Hitbox.Y - 15, Hitbox.Width, Hitbox.Height), walls, speed)) { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; if (collisions.CollisionDown(new Rectangle(Hitbox.X, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed)) { Hitbox.Y = Hitbox.Y - 15; } } if (clavier.IsKeyDown(Keys.D) && state == "standing") { couteau_sound_instance.Play(); framecolumn = 1; compteur = 1; state = "cut"; Hitbox.Width = 50; Hitbox.Y -= 16; Hitbox.Height = 55; } // Tir Marco if (clavier.IsKeyDown(Keys.Q) && state == "standing") //On tire avec "Q" et debout uniquement { if (state != "shoot") { framecolumn = 1; compteur = 1; } if (Global.Handler.ammo_left > 0) { fire_sound_instance.Play(); state = "shoot"; } else vide_sound_instance.Play(); } if (state == "cut") { if (compteur % 2 == 0) { if (framecolumn < 8) { framecolumn++; compteur++; } else { framecolumn = 1; if (clavier.IsKeyUp(Keys.D)) { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; Hitbox.Y += 15; } } } else compteur++; } else if (state == "shoot") { if (compteur % 4 == 0) { if (framecolumn < 10) { Hitbox.Width = 52; Hitbox.Height = 40; framecolumn++; } else { state = "standing"; Hitbox.Width = 36; Hitbox.Height = 40; framecolumn = 1; compteur = 1; } } compteur++; } else if (clavier.IsKeyDown(Keys.Right) && state != "squat") // Permet de bouger à droite { // Droite si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionRight(Hitbox, walls, this.speed)) { this.Hitbox.X += this.speed; this.Direction = Direction.Right; if (collisions.CollisionDown(Hitbox, walls, this.speed)) this.Animate(1, 15, 2); } else { framecolumn = 1; compteur = 1; } } else if (clavier.IsKeyDown(Keys.Left) && state != "squat") // Permet de bouger à gauche { // Gauche si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionLeft(Hitbox, walls, this.speed) && Hitbox.X > 0) { this.Hitbox.X -= this.speed; this.Direction = Direction.Left; if (collisions.CollisionDown(Hitbox, walls, this.speed)) this.Animate(1, 15, 2); } else { framecolumn = 1; compteur = 1; } } if (clavier.IsKeyDown(Keys.Right) && state == "squat") // Permet de bouger à droite accroupi { // Droite si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionRight(Hitbox, walls, this.speed)) { this.Hitbox.X += this.speed - 1; this.Direction = Direction.Right; if (collisions.CollisionDown(Hitbox, walls, this.speed)) this.Animate(2, 8, 2); } else { framecolumn = 1; compteur = 1; } } else if (clavier.IsKeyDown(Keys.Left) && state == "squat") // Permet de bouger à gauche accroupi { // Gauche si pas de collisions et si le perso est encore sur l'écran if (!collisions.CollisionLeft(Hitbox, walls, this.speed) && Hitbox.X > 0) { this.Hitbox.X -= this.speed - 1; this.Direction = Direction.Left; if (collisions.CollisionDown(Hitbox, walls, this.speed)) this.Animate(2, 8, 2); } else { framecolumn = 1; compteur = 1; } } else if (clavier.IsKeyDown(Keys.A)) Hitbox = new Rectangle(50, 380, Hitbox.Width, Hitbox.Height); #endregion #region Saut if (clavier.IsKeyDown(Keys.Space) && hasjumped == false && gravity == true && state == "standing") { framecolumn = 1; if (!collisions.CollisionUp(Hitbox, walls, this.fallspeed)) { SoundEffectInstance saut_instance = Resources.saut.CreateInstance(); saut_instance.Play(); this.Hitbox.Y -= 12; speedjump = 12; } hasjumped = true; state = "jump"; } if (hasjumped == true && collisions.CollisionDown(Hitbox, walls, this.fallspeed) == true && state != "squat") { hasjumped = false; state = "standing"; } #endregion #region Effets et animation if (clavier.IsKeyUp(Keys.Right) && clavier.IsKeyUp(Keys.Left) && state == "standing") // Ici c'est une partie de l'animation { framecolumn = 1; } switch (Direction) { case Direction.Left: effect = SpriteEffects.FlipHorizontally; break; case Direction.Right: effect = SpriteEffects.None; break; default: break; } #endregion #region Chute dead = Hitbox.Y > 480; if (dead) { Hitbox = new Rectangle(50, 350, Hitbox.Width, Hitbox.Height); } #endregion }
public void Update(List<Wall> walls) { compt++; // Cette petite ligne correspond à l'IA ( WAAAW Gros QI ) distance2player = Hitbox.X - Global.Player.Hitbox.X; #region Mort Ennemi for (int i = 0; i < bullets.Count(); i++) { if (Hitbox.Intersects(new Rectangle((int)bullets[i].position.X, (int)bullets[i].position.Y, 30, 30))) { bullets[i].isVisible = false; bullets.RemoveAt(i); i--; if (pv > 1) pv--; else isDead = true; } } if (!isDead && Keyboard.GetState().IsKeyDown(Keys.D) && Global.Player.Hitbox.Intersects(Hitbox)) isDead = true; #endregion #region Animation if (!isDead) { if (distance2player != 0) { if (framecolumn > 8) framecolumn = 1; else if (compt % 2 == 0) framecolumn++; } else framecolumn = 9; } #endregion #region Gravité if (!isDead) { bool gravity = collisions.CollisionDown(Hitbox, walls, this.fallspeed); if (!gravity) { if (collisions.CollisionUp(Hitbox, walls, fallspeed)) speedjump = 0; if (speedjump > 1) { this.Hitbox.Y -= speedjump; speedjump -= 1; } else this.Hitbox.Y += this.fallspeed; } else if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 4)) if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 2)) this.Hitbox.Y += 2; else this.Hitbox.Y++; } #endregion #region Déplacements if (!isDead) { if (distance2player == 0) speed = 0; else { speed = 1; if (distance2player < 0 && distance2player >= -200) { left = false; compt = 1; } else if (distance2player > 0 && distance2player <= 200) { left = true; compt = 1; } else if (distance2player > 200 || distance2player <= -200) { if (this.Hitbox.X <= origin - 200) { left = false; compt = 1; } else if (this.Hitbox.X >= origin + 200) { left = true; compt = 1; } } if (left) { if (!collisions.CollisionLeft(Hitbox, walls, speed) && Hitbox.X > 0 && collisions.CollisionDown(new Rectangle(Hitbox.X - Hitbox.Width, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed)) { this.Hitbox.X -= speed; this.Direction = Direction.Left; } else { framecolumn = 1; compt += 10; } } if (!left) { if (!collisions.CollisionRight(Hitbox, walls, speed) && collisions.CollisionDown(new Rectangle(Hitbox.X + Hitbox.Width, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed) && Hitbox.X < 4600) { this.Hitbox.X += speed; this.Direction = Direction.Right; } else { framecolumn = 1; compt += 10; } } } } #endregion #region Tir //enemy_bullets.Capacity = 5; if ((distance2player >= -200 && distance2player <= -100) || (distance2player > 100 && distance2player <= 200)) a_portee = true; else a_portee = false; if (!isDead && Math.Abs(Hitbox.Y - Global.Player.Hitbox.Y) < 20 && a_portee && !shot && enemy_bullets.Count == 0 && Global.Player.health > 0) { shot_sound_instance.Play(); shot = true; Bullets bullet = new Bullets(Resources.bullet); bullet.velocity = 2; bullet.isVisible = true; enemy_bullets.Add(bullet); if (Direction == Direction.Right) { Old_Direction = Direction.Right; bullet.position = new Vector2(Hitbox.X + Hitbox.Width / 2, Hitbox.Y + Hitbox.Height / 4) + new Vector2(bullet.velocity * 5, 0); } else { Old_Direction = Direction.Left; bullet.position = new Vector2(Hitbox.X, Hitbox.Y + Hitbox.Height / 4) + new Vector2(bullet.velocity * 5, 0); } } else shot = false; // la balle disparait si elle parcourt la distance ou rencontre un obstacle foreach (Bullets bullet in enemy_bullets) { if (!isDead) { if (Old_Direction == Direction.Right) bullet.position.X += bullet.velocity; // va vers la droite else bullet.position.X -= bullet.velocity; // va vers la gauche if (Vector2.Distance(bullet.position, new Vector2(Hitbox.X, Hitbox.Y)) >= 200) bullet.isVisible = false; else if (Global.Player.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 3, 3))) { Global.Player.health -= 3; bullet.isVisible = false; } } foreach (Wall wall in walls) { if (wall.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 5, 2))) bullet.isVisible = false; } } for (int i = 0; i < enemy_bullets.Count; i++) { if (!enemy_bullets[i].isVisible) { enemy_bullets.RemoveAt(i); i--; } } #endregion switch (Direction) { case Direction.Right: effect = SpriteEffects.FlipHorizontally; break; case Direction.Left: effect = SpriteEffects.None; break; } switch (pv) { case 1: color.A = 100; break; case 2: color.A = 200; break; case 3: color = Color.White; break; } }
public void Update(List<Wall> walls) { compt++; // Cette petite ligne correspond à l'IA ( WAAAW Gros QI ) distance2player = Hitbox.X - Global.Player.Hitbox.X; #region Mort Ennemi for (int i = 0; i < bullets.Count(); i++) if (Hitbox.Intersects(new Rectangle((int)bullets[i].position.X, (int)bullets[i].position.Y, 30, 30))) { Hitbox.Width = 0; Hitbox.Height = 0; isdead = true; bullets[i].isVisible = false; bullets.RemoveAt(i); i--; } #endregion #region Animation if (framecolumn > 8) framecolumn = 1; else if (compt % 2 == 0) framecolumn++; #endregion #region Gravité bool gravity = collisions.CollisionDown(Hitbox, walls, this.fallspeed); if (!gravity) { if (collisions.CollisionUp(Hitbox, walls, fallspeed)) speedjump = 0; if (speedjump > 1) { this.Hitbox.Y -= speedjump; speedjump -= 1; } else this.Hitbox.Y += this.fallspeed; } else if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 4)) if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 2)) this.Hitbox.Y += 2; else this.Hitbox.Y++; #endregion #region Déplacements if (distance2player == 0) speed = 0; else { speed = 1; if (distance2player < 0 && distance2player >= -200) { left = false; compt = 1; } else if (distance2player > 0 && distance2player <= 200) { left = true; compt = 1; } else if (distance2player > 200 || distance2player <= -200) { if (this.Hitbox.X <= 2089) { left = false; compt = 1; } else if (this.Hitbox.X >= 2750) { left = true; compt = 1; } } } if (left) { if (!collisions.CollisionLeft(Hitbox, walls, speed)) { this.Hitbox.X -= speed; this.Direction = Direction.Left; } else { framecolumn = 1; compt += 10; } } if (!left) { if (!collisions.CollisionRight(Hitbox, walls, speed)) { this.Hitbox.X += speed; this.Direction = Direction.Right; } else { framecolumn = 1; compt += 10; } } #endregion #region Tir if ((distance2player >= -100 && distance2player <= 0) || (distance2player > 0 && distance2player <= 100)) a_portee = true; else a_portee = false; if (Math.Abs(Hitbox.Y - Global.Player.Hitbox.Y) < 3 && a_portee && !enemy_shot && enemy_bullets.Count <= 5) { shot_sound_instance.Play(); enemy_shot = true; Bullets bullet = new Bullets(Resources.bullet); bullet.velocity = 5; bullet.isVisible = true; enemy_bullets.Add(bullet); if (Direction == Direction.Right) bullet.position = new Vector2(Hitbox.X + Hitbox.Width / 2, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); else bullet.position = new Vector2(Hitbox.X, Hitbox.Y + Hitbox.Height / 3) + new Vector2(bullet.velocity * 5, 0); } else enemy_shot = false; // la balle disparait si elle parcourt la distance ou rencontre un obstacle foreach (Bullets bullet in enemy_bullets) { if (Direction == Direction.Right) bullet.position.X += bullet.velocity; // va vers la droite else bullet.position.X -= bullet.velocity; // va vers la gauche if (Vector2.Distance(bullet.position, new Vector2(Hitbox.X, Hitbox.Y)) >= 100) bullet.isVisible = false; else if (Global.Player.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 10, 10))) { Global.Player.health++; bullet.isVisible = false; } foreach (Wall wall in Global.GameMain.Walls) { if (wall.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 5, 2))) bullet.isVisible = false; } } for (int i = 0; i < enemy_bullets.Count; i++) { if (!enemy_bullets[i].isVisible) { enemy_bullets.RemoveAt(i); i--; } } #endregion switch (Direction) { case Direction.Right: effect = SpriteEffects.FlipHorizontally; break; case Direction.Left: effect = SpriteEffects.None; break; } }
public void Update(List <Wall> walls) { compt++; // Cette petite ligne correspond à l'IA ( WAAAW Gros QI ) distance2player = Hitbox.X - Global.Player.Hitbox.X; #region Mort Ennemi for (int i = 0; i < bullets.Count(); i++) { if (Hitbox.Intersects(new Rectangle((int)bullets[i].position.X, (int)bullets[i].position.Y, 30, 30))) { bullets[i].isVisible = false; bullets.RemoveAt(i); i--; if (pv > 1) { pv--; } else { isDead = true; } } } if (!isDead && Keyboard.GetState().IsKeyDown(Keys.D) && Global.Player.Hitbox.Intersects(Hitbox)) { isDead = true; } #endregion #region Animation if (!isDead) { if (distance2player != 0) { if (framecolumn > 8) { framecolumn = 1; } else if (compt % 2 == 0) { framecolumn++; } } else { framecolumn = 9; } } #endregion #region Gravité if (!isDead) { bool gravity = collisions.CollisionDown(Hitbox, walls, this.fallspeed); if (!gravity) { if (collisions.CollisionUp(Hitbox, walls, fallspeed)) { speedjump = 0; } if (speedjump > 1) { this.Hitbox.Y -= speedjump; speedjump -= 1; } else { this.Hitbox.Y += this.fallspeed; } } else if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 4)) { if (!collisions.CollisionDown(Hitbox, walls, this.fallspeed - 2)) { this.Hitbox.Y += 2; } else { this.Hitbox.Y++; } } } #endregion #region Déplacements if (!isDead) { if (distance2player == 0) { speed = 0; } else { speed = 1; if (distance2player < 0 && distance2player >= -200) { left = false; compt = 1; } else if (distance2player > 0 && distance2player <= 200) { left = true; compt = 1; } else if (distance2player > 200 || distance2player <= -200) { if (this.Hitbox.X <= origin - 200) { left = false; compt = 1; } else if (this.Hitbox.X >= origin + 200) { left = true; compt = 1; } } if (left) { if (!collisions.CollisionLeft(Hitbox, walls, speed) && Hitbox.X > 0 && collisions.CollisionDown(new Rectangle(Hitbox.X - Hitbox.Width, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed)) { this.Hitbox.X -= speed; this.Direction = Direction.Left; } else { framecolumn = 1; compt += 10; } } if (!left) { if (!collisions.CollisionRight(Hitbox, walls, speed) && collisions.CollisionDown(new Rectangle(Hitbox.X + Hitbox.Width, Hitbox.Y, Hitbox.Width, Hitbox.Height), walls, speed) && Hitbox.X < 4600) { this.Hitbox.X += speed; this.Direction = Direction.Right; } else { framecolumn = 1; compt += 10; } } } } #endregion #region Tir //enemy_bullets.Capacity = 5; if ((distance2player >= -200 && distance2player <= -100) || (distance2player > 100 && distance2player <= 200)) { a_portee = true; } else { a_portee = false; } if (!isDead && Math.Abs(Hitbox.Y - Global.Player.Hitbox.Y) < 20 && a_portee && !shot && enemy_bullets.Count == 0 && Global.Player.health > 0) { shot_sound_instance.Play(); shot = true; Bullets bullet = new Bullets(Resources.bullet); bullet.velocity = 2; bullet.isVisible = true; enemy_bullets.Add(bullet); if (Direction == Direction.Right) { Old_Direction = Direction.Right; bullet.position = new Vector2(Hitbox.X + Hitbox.Width / 2, Hitbox.Y + Hitbox.Height / 4) + new Vector2(bullet.velocity * 5, 0); } else { Old_Direction = Direction.Left; bullet.position = new Vector2(Hitbox.X, Hitbox.Y + Hitbox.Height / 4) + new Vector2(bullet.velocity * 5, 0); } } else { shot = false; } // la balle disparait si elle parcourt la distance ou rencontre un obstacle foreach (Bullets bullet in enemy_bullets) { if (!isDead) { if (Old_Direction == Direction.Right) { bullet.position.X += bullet.velocity; // va vers la droite } else { bullet.position.X -= bullet.velocity; // va vers la gauche } if (Vector2.Distance(bullet.position, new Vector2(Hitbox.X, Hitbox.Y)) >= 200) { bullet.isVisible = false; } else if (Global.Player.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 3, 3))) { Global.Player.health -= 3; bullet.isVisible = false; } } foreach (Wall wall in walls) { if (wall.Hitbox.Intersects(new Rectangle((int)bullet.position.X, (int)bullet.position.Y, 5, 2))) { bullet.isVisible = false; } } } for (int i = 0; i < enemy_bullets.Count; i++) { if (!enemy_bullets[i].isVisible) { enemy_bullets.RemoveAt(i); i--; } } #endregion switch (Direction) { case Direction.Right: effect = SpriteEffects.FlipHorizontally; break; case Direction.Left: effect = SpriteEffects.None; break; } switch (pv) { case 1: color.A = 100; break; case 2: color.A = 200; break; case 3: color = Color.White; break; } }