public void Update(Collision collision, int xPacman, int yPacman) { if((this.GetAcutalCaseX(collision.Map.Tile_size) > 10 && this.GetAcutalCaseX(collision.Map.Tile_size) < 17) && (this.GetAcutalCaseY(collision.Map.Tile_size) > 11 && this.GetAcutalCaseY(collision.Map.Tile_size) < 16)) { this.GetOutOriginCage(collision); } else { //this.Dijikstra(collision, xPacman, yPacman); this.RandomMoove(collision); } this.MooveObject(collision); this.CheckActualTexture(); }
private void GetOutOriginCage(Collision collision) { if(this.GetAcutalCaseY(collision.Map.Tile_size)==11) { Random rd = new Random(); int nb = rd.Next(2); if(nb==0) { this.direction = DIRECTION.LEFT; } else { this.direction = DIRECTION.RIGHT; } } else { this.direction = DIRECTION.UP; } }
/// <summary> /// Used to moove the object. It makes it moove in the actual direction, with the actual speed. /// </summary> public void MooveObject(Collision c) { if (!c.SideCaseIsNotWall(this.direction, this)) { this.direction = DIRECTION.NONE; } switch(this.direction) { case DIRECTION.DOWN: this.position.Y += this.speed; break; case DIRECTION.UP: this.position.Y -= this.speed; break; case DIRECTION.LEFT: this.position.X -= this.speed; break; case DIRECTION.RIGHT: this.position.X += this.speed; break; } }
public void Update(Collision collision) { this.CheckActualTexture(); this.MooveObject(collision); }
public Game() { graphics = new GraphicsDeviceManager(this); graphics.PreferredBackBufferWidth = 34*20; graphics.PreferredBackBufferHeight = 31*20; graphics.ApplyChanges(); Content.RootDirectory = "Content"; this.map = new Map(28, 31, new byte[,]{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 0}, {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0, 0, 0,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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }); this.collision = new Collision(this.pacman, map); this.movementController = new MovementController(); GHOSTS = new List<Ghost>(); }
private void RandomMoove(Collision collision) { Random rd = new Random(); DIRECTION tmp; List<DIRECTION> possibilites = collision.GetPossibleDirection(this); if(this.direction!=DIRECTION.NONE) { possibilites.Remove(this.direction); possibilites.Remove(this.OppositeDirection(this.direction)); } if(possibilites.Count!=0) { tmp = possibilites.ElementAt(rd.Next(possibilites.Count)); if (this.Direction == DIRECTION.UP || this.Direction == DIRECTION.DOWN || this.Direction == DIRECTION.NONE) { if (tmp == DIRECTION.UP || tmp == DIRECTION.DOWN) { this.Direction = (tmp == DIRECTION.UP ? DIRECTION.UP : DIRECTION.DOWN); } else if (tmp == DIRECTION.RIGHT || tmp == DIRECTION.LEFT) { if (this.GetCaseYSup(collision.Map.Tile_size) != -1 || this.GetCaseYInf(collision.Map.Tile_size) != -1) { this.Direction = (tmp == DIRECTION.RIGHT ? DIRECTION.RIGHT : DIRECTION.LEFT); } } } if (this.Direction == DIRECTION.RIGHT || this.Direction == DIRECTION.LEFT || this.Direction == DIRECTION.NONE) { if (tmp == DIRECTION.RIGHT || tmp == DIRECTION.LEFT) { this.Direction = (tmp == DIRECTION.RIGHT ? DIRECTION.RIGHT : DIRECTION.LEFT); } else if (tmp == DIRECTION.UP || tmp == DIRECTION.DOWN) { if (this.GetCaseXSup(collision.Map.Tile_size) != -1 || this.GetCaseXInf(collision.Map.Tile_size) != -1) { this.Direction = (tmp == DIRECTION.UP ? DIRECTION.UP : DIRECTION.DOWN); } } } } }
private void Dijikstra(Collision collision, int xAim, int yAim) { Summit[,] graph = new Summit[collision.Map.Height, collision.Map.Width]; for(int i=0; i< collision.Map.Height; i++) { for (int j = 0; j < collision.Map.Width; j++) { if(collision.Map.Grid[i][j].Content != CELL_CONTENT.WALL) { graph[i, j] = new Summit(j, i); } } } int x = xAim; int y = yAim; graph[yAim, xAim].Weight = 0; while(x != xAim && y != yAim) { Summit actual = graph[y, x]; actual.Marqued = true; if(graph[y - 1,x] != null) { if(graph[y - 1, x].Weight > actual.Weight + 1) { graph[y - 1, x].Weight = actual.Weight + 1; graph[y - 1, x].Previous = actual; } } if (graph[y + 1, x] != null) { if (graph[y + 1, x].Weight > actual.Weight + 1) { graph[y + 1, x].Weight = actual.Weight + 1; graph[y + 1, x].Previous = actual; } } if (graph[y , x+1] != null) { if (graph[y, x+1].Weight > actual.Weight + 1) { graph[y , x+1].Weight = actual.Weight + 1; graph[y , x+1].Previous = actual; } } if (graph[y, x - 1] != null) { if (graph[y, x - 1].Weight > actual.Weight + 1) { graph[y, x - 1].Weight = actual.Weight + 1; graph[y, x - 1].Previous = actual; } } int min = Int16.MinValue; for (int i = 0; i < collision.Map.Height; i++) { for (int j = 0; j < collision.Map.Width; j++) { if (graph[i, j] != null) { if(!graph[i,j].Marqued && graph[i,j].Weight<min) { min = graph[i, j].Weight; x = j; y = i; } } } } } int xNext, yNext; if(graph[y,x].Previous == null) { xNext = x; yNext = y; } else { xNext = graph[y, x].Previous.CaseX; yNext = graph[y, x].Previous.CaseY; } if(xNext != xAim) { if(xNext > xAim) { this.direction = DIRECTION.LEFT; } else { this.direction = DIRECTION.RIGHT; } } else { if(yNext > yAim) { this.direction = DIRECTION.UP; } else { this.direction = DIRECTION.DOWN; } } }