using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Drawing; using System.Windows.Forms; namespace Bonhomme02 { class Marcher { Bonhomme bonhomme; IntPtr? handle; Graphics gr; Graphics grBuf; BufferedGraphics buff; int ySol; int resolution; int deltaXYmax = 10; int step, cooXMax = 0; int vitesse; public Marcher() { } public Marcher(ref Bonhomme bonhomme, IntPtr handle, int ySol) { step = 0; this.bonhomme = bonhomme; this.handle = handle; this.ySol = ySol; resolution = 1; deltaXYmax = (int)Math.Round(3*bonhomme.jambe1.longueur.ABS/4); //Reset de tous les angle au cas ou bonhomme.Angle = Math.PI / 2; bonhomme.jambe1.SetAngle(Math.PI / 2, Math.PI / 2, 0); bonhomme.bras.Visible = false; bonhomme.bras.Cacher(handle); if(bonhomme.jambe1.Bas.CSG.Y != ySol) // On positionne le bonhomme sur le sol { Graphics gr = Graphics.FromHwnd(handle); if(handle == null){ bonhomme.Cacher(gr); Rend(); } else bonhomme.Cacher(handle); Point newCoo; int longeurTTBonhome = bonhomme.jambe1.Bas.CSG.Y - bonhomme.CIG.Y ;// de pied.csg à bonhome.cig newCoo = new Point(bonhomme.CIG.X, ySol - longeurTTBonhome); bonhomme.Coordonnee = newCoo; if(handle == null){ bonhomme.Afficher(gr); Rend(); } else bonhomme.Afficher(handle); } } public Marcher(ref Bonhomme bonhomme, BufferedGraphics buff, int ySol) { handle = null; step = 0; grBuf = Graphics.FromHwnd(bonhomme.conteneur.Handle); this.gr = buff.Graphics; this.buff = buff; this.bonhomme = bonhomme; this.handle = null; this.ySol = ySol; resolution = 1; deltaXYmax = (int)Math.Round(3*bonhomme.jambe1.longueur.ABS/4); //Reset de tous les angle au cas ou bonhomme.Angle = Math.PI / 2; bonhomme.jambe1.SetAngle(Math.PI / 2, Math.PI / 2, 0); bonhomme.bras.Visible = false; bonhomme.bras.Cacher(gr); if(bonhomme.jambe1.Bas.CSG.Y != ySol) // On positionne le bonhomme sur le sol { bonhomme.Cacher(gr); Rend(); Point newCoo; int longeurTTBonhome = bonhomme.jambe1.Bas.CSG.Y - bonhomme.CIG.Y ;// de pied.csg à bonhome.cig newCoo = new Point(bonhomme.CIG.X, ySol - longeurTTBonhome); bonhomme.Coordonnee = newCoo; bonhomme.Afficher(gr); Rend(); } } public void Avancer() { double old = 1, neww = 0; int dejaDeplacer = 0; while(/*step<=110 || */bonhomme.X <= cooXMax) { old = 1; neww = 0; Console.WriteLine(step + "::" + step % 5); dejaDeplacer = 0; switch (step%5) { case 0: while (dejaDeplacer < deltaXYmax) //Monte jambe 1 { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.jambe1.BougerBas(0, -resolution); if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); dejaDeplacer += resolution; if (handle == null) Rend(); Thread.Sleep(vitesse); } break; case 1: while (dejaDeplacer < deltaXYmax)//avence corp + avancer jambe 1 jambe 2 fixe { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.jambe2.BougerXHaut(resolution, ref bonhomme); bonhomme.jambe1.BougerBas(resolution, 0); if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); dejaDeplacer += resolution; if (handle == null) Rend(); Thread.Sleep(vitesse); } CollerAuSol(ref bonhomme.jambe2); //Voir issues #24 break; case 2: while (bonhomme.jambe1.Bas.CSG.Y < ySol && old != neww) //Descendre jambe 1 sur sol { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.jambe1.BougerBas(0, resolution); if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); dejaDeplacer += resolution; if (handle == null) Rend(); Thread.Sleep(vitesse); if (neww != bonhomme.jambe1.Bas.CSG.Y) { neww = bonhomme.jambe1.Bas.CSG.Y; } else old = bonhomme.jambe1.Bas.CSG.Y; } break; case 3: if (bonhomme.jambe2.Bas.X > bonhomme.jambe1.Bas.X) { bonhomme.InterevrtirJambe(); } while (dejaDeplacer < (deltaXYmax*2))//Monte&avance jambe 2,avance bonhome { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.jambe1.BougerBas(-resolution, 0); if(dejaDeplacer < deltaXYmax) bonhomme.jambe2.BougerBas(resolution, -resolution); else if(bonhomme.jambe2.Bas.CSG.Y < ySol && old != neww) { bonhomme.jambe2.BougerBas(resolution, resolution); if (neww != bonhomme.jambe2.Bas.CSG.Y) { neww = bonhomme.jambe2.Bas.CSG.Y; } else old = bonhomme.jambe2.Bas.CSG.Y; } else bonhomme.jambe2.BougerBas(resolution, 0); bonhomme.Coordonnee = new Point(bonhomme.Coordonnee.X + resolution, bonhomme.Coordonnee.Y); if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); dejaDeplacer += resolution; if (handle == null) Rend(); Thread.Sleep(vitesse); } break; case 4: while (bonhomme.jambe2.Bas.CSG.Y < ySol - 1 && old != neww)//Descend jambe 2 { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.jambe2.BougerBas(0, resolution); if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); dejaDeplacer += resolution; if (handle == null) Rend(); Thread.Sleep(vitesse); } if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.InterevrtirJambe();// On inverse les deux jambe, jambe1=jambe2 et jambe2=jambe1 if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); if (handle == null) Rend(); step +=3; // retour step 3 break; } step++; } } public void Avancer(int vitesse, int cooX) { this.cooXMax = cooX; this.vitesse = vitesse; this.step = 0; Avancer(); } public void Avancer(int vitesse, int cooX, int startStep) { this.cooXMax = cooX; this.vitesse = vitesse; this.step = startStep; Avancer(); } public void CollerAuSol(ref Jambe jambe) { if (jambe.Bas.CSG.Y != ySol) // On positionne le bonhomme sur le sol { //Graphics gr = Graphics.FromHwnd(handle.Value); if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); Point newCoo; int longeurTTBonhome = jambe.Bas.CSG.Y - bonhomme.CIG.Y;// de pied.csg à bonhome.cig newCoo = new Point(bonhomme.Coordonnee.X, ySol - longeurTTBonhome); bonhomme.Coordonnee = newCoo; if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); if (handle == null) Rend(); } } public void Saut(ref PictureBox osbtacle, int vitesse) { int marcherJusqa = osbtacle.Location.X - bonhomme.conteneur.Location.X - osbtacle.Height - osbtacle.Width - 4*deltaXYmax; Avancer(vitesse, marcherJusqa); while (bonhomme.Coordonnee.X < osbtacle.Location.X - bonhomme.conteneur.Location.X)//Monter { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.X += 1; bonhomme.Y -= 1; if(bonhomme.jambe2.Bas.X < bonhomme.jambe1.Bas.X) { bonhomme.InterevrtirJambe(); } if(bonhomme.jambe2.Bas.X> bonhomme.jambe1.Bas.X) { bonhomme.jambe2.BougerBas(-resolution, 0); bonhomme.jambe1.BougerBas(resolution, 0); } if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); if (handle == null) Rend(); Thread.Sleep(vitesse); } while (bonhomme.jambe1.Bas.CSG.Y < ySol)//descendre { if(handle == null){ bonhomme.Cacher(gr); } else bonhomme.Cacher(handle.Value); bonhomme.X += 1; bonhomme.Y += 1; if (bonhomme.jambe2.Bas.X < bonhomme.jambe1.Bas.X) { bonhomme.InterevrtirJambe(); } if (Math.Abs(bonhomme.jambe2.Bas.X - bonhomme.jambe1.Bas.X )< 2*deltaXYmax) { bonhomme.jambe2.BougerBas(resolution, 0); bonhomme.jambe1.BougerBas(-resolution, 0); } if(handle == null){ bonhomme.Afficher(gr); } else bonhomme.Afficher(handle.Value); if (handle == null) Rend(); Thread.Sleep(vitesse); } Avancer(vitesse, bonhomme.conteneur.Location.X + bonhomme.conteneur.Width, 3); } public void Rend() { if (handle == null) { gr.FillRectangle(new SolidBrush(bonhomme.conteneur.BackColor), 0, 0, bonhomme.conteneur.Width, bonhomme.conteneur.Height); bonhomme.Afficher(gr); buff.Render(); } } } }