public void Initialize() { ImageCache.ClearCache(); Data.CurrentRace.DriversChanged += OnDriversChanged; this.Dispatcher.Invoke(() => { Data.CurrentRace.DriversChanged += ((MainDataContext)this.DataContext).OnDriversChanged; }); Data.CurrentRace.NextRace += NextRace; //ImageCache.ClearCache(); }
//Draw the track every frame public static BitmapSource DrawTrack(Track track) { CreateArrayWithTrack(track); canvas = ImageCache.CreateBitmap(GetHighestXValue(SectionBuildingGridDetails) * 256, GetHighestYValue(SectionBuildingGridDetails) * 256); CompleteTrack = new Bitmap[GetHighestYValue(SectionBuildingGridDetails), GetHighestXValue(SectionBuildingGridDetails)]; BuildTrackArray(CompleteTrack, SectionBuildingGridDetails); AddGrass(CompleteTrack); Graphics g = PlaceTrack(canvas); PlaceParticipantsOnTrack(g, SectionBuildingGridDetails); return(ImageCache.CreateBitmapSourceFromGdiBitmap(canvas)); }
//Get the image of the car based on the direction public static Bitmap GetParticipantImage(IParticipant participant, Direction direction) { switch (participant.TeamColor) { case TeamColors.Blue: return(RotateImage(ImageCache.GetImage(Blue), direction)); case TeamColors.Grey: return(RotateImage(ImageCache.GetImage(Grey), direction)); case TeamColors.Green: return(RotateImage(ImageCache.GetImage(Green), direction)); case TeamColors.Red: return(RotateImage(ImageCache.GetImage(Red), direction)); case TeamColors.Yellow: return(RotateImage(ImageCache.GetImage(Yellow), direction)); default: throw new ArgumentOutOfRangeException("Color null"); } }
//Add grass on the empty places public static void AddGrass(Bitmap[,] completeTrack) { Random random = new Random(); for (int i = 0; i < CompleteTrack.GetLength(0); i++) { for (int j = 0; j < CompleteTrack.GetLength(1); j++) { if (CompleteTrack[i, j] == null) { CompleteTrack[i, j] = ImageCache.GetImage(GrassTile); /*if(random.Next(0,2) == 0) * { * CompleteTrack[i, j] = ImageCache.GetImage(WaterTile); * * } else * { * CompleteTrack[i, j] = ImageCache.GetImage(GrassTile); * }*/ } } } }
//Build the complete track array public static void BuildTrackArray(Bitmap[,] completeTrack, List <SectionBuildingDetails> sectionBuildingDetails) { foreach (var Section in sectionBuildingDetails) { //Start track horizontal & vertical if (Section.Section.SectionType == SectionTypes.StartGrid) { //Horizontal if (Section.Direction == Direction.East || Section.Direction == Direction.West) { //Check if driver is on section completeTrack[Section.Y, Section.X] = ImageCache.GetImage(TrackHorizontal); // completeTrack[Section.Y, Section.X] = ImageCache.GetImage(TrackHorizontal); } //Vertical else { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(TrackVertical); } } //Straight track horizontal & vertical else if (Section.Section.SectionType == SectionTypes.Straight) { //Horizontal if (Section.Direction == Direction.East || Section.Direction == Direction.West) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(TrackHorizontal); } //Vertical else { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(TrackVertical); } } //Finish track horizontal & vertical else if (Section.Section.SectionType == SectionTypes.Finish) { //Horizontal if (Section.Direction == Direction.East || Section.Direction == Direction.West) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(Finish); } //Vertical else { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(Finish); } } //Corner left else if (Section.Section.SectionType == SectionTypes.LeftCorner) { //Left -> North if (Section.Direction == Direction.North) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerLeftHorizontal); } //Left -> East else if (Section.Direction == Direction.East) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerRightHorizontal); } //Left South else if (Section.Direction == Direction.South) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerRightVertical); } else if (Section.Direction == Direction.West) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerLeftVertical); } } //Corner right else if (Section.Section.SectionType == SectionTypes.RightCorner) { //Right -> North if (Section.Direction == Direction.North) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerRightHorizontal); } //Right -> East else if (Section.Direction == Direction.East) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerRightVertical); } //Right South else if (Section.Direction == Direction.South) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerLeftVertical); } //Right West else if (Section.Direction == Direction.West) { completeTrack[Section.Y, Section.X] = ImageCache.GetImage(CornerLeftHorizontal); } } } }