Example #1
0
        public static Track Load(string path)
        {
            List<string> lines = new List<string>();
            using( Stream stream=File.OpenRead(path) )
            {
                using( StreamReader sr=new StreamReader(stream) )
                {
                    while( sr.EndOfStream==false )
                    {
                        lines.Add(sr.ReadLine());
                    }
                }
            }

            if( lines.Count>0 )
            {
                Track result = new Track(lines[0].Length, lines.Count);
                for( int y = 0; y < lines.Count; y++ )
                {
                    for( int x = 1; x < lines[y].Length; x++ )
                    {
                        int id = int.Parse(lines[y][x].ToString());
                        result.Tiles[x, y] = (TrackTile)id;
                    }
                }
                return result;
            }
            return null;
        }
Example #2
0
        public Game(Track track)
        {
            Track = track;
            State = GameState.CountDown;
            CountDown = TimeSpan.FromSeconds(3);

            Vector goal = (Vector)Track.GoalPosition;

            Vector startOffset1 = default(Vector);
            Vector startOffset2 = default(Vector);

            float startRotation = 0f;

            switch (Track.GetTileByIndex((int)goal.X, (int)goal.Y))
            {
                case TrackTile.GoalDown:
                    startOffset1 = new Vector(0.75f, 0.25f);
                    startOffset2 = new Vector(0.25f, 0.25f);
                    startRotation = 180f;
                    break;
                case TrackTile.GoalLeft:
                    startOffset1 = new Vector(0.75f, 0.75f);
                    startOffset2 = new Vector(0.75f, 0.25f);
                    startRotation = -90f;
                    break;
                case TrackTile.GoalRight:
                    startOffset1 = new Vector(0.25f, 0.25f);
                    startOffset2 = new Vector(0.25f, 0.75f);
                    startRotation = 90f;
                    break;
                case TrackTile.GoalUp:
                    startOffset1 = new Vector(0.25f, 0.75f);
                    startOffset2 = new Vector(0.75f, 0.75f);
                    break;
            }

            Player1 = new Player()
            {
                Position = (Point)((goal + startOffset1) * Track.CELLSIZE),
                Direction = startRotation
            };
        }
Example #3
0
        /// <summary>
        /// Baut ein <see cref="Track"/> Objekt aus den gegebenen Tiles.
        /// </summary>
        /// <param name="tilesPerLine">Die Anzahl an Tiles pro Zeile.</param>
        /// <param name="allTiles">Die Liste aller Tiles.</param>
        /// <returns>Ein <see cref="Track"/> Objekt zusammengesetzt aus den Tiles.</returns>
        private static Track BuildTrack(int tilesPerLine, IEnumerable<TrackTile[]> allTiles)
        {
            int y = 0;

            var track = new Track(tilesPerLine, allTiles.Count());

            foreach (var tileRow in allTiles)
            {
                for (var x = 0; x < tilesPerLine; x++)
                {
                    track.Tiles[x, y] = tileRow[x];
                }

                y++;
            }
            return track;
        }
Example #4
0
        public static Track LoadFromTxt(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (StreamReader sr = new StreamReader(stream, Encoding.ASCII))
            {
                if (sr.EndOfStream)
                {
                    throw new FormatException("The file must not be empty.");
                }

                // reading the first line is outside the loop to calculate the size for the list
                string line = sr.ReadLine();

                int tilesPerLine = line.Length;

                List<TrackTile[]> tiles = new List<TrackTile[]>((int)Math.Ceiling((float)stream.Length / line.Length));

                for (int y = 0; ; y++, line = sr.ReadLine())
                {
                    if (line.Length == 0)
                    {
                        throw new FormatException("The file must not contain empty lines.");
                    }

                    if (line.Length != tilesPerLine)
                    {
                        throw new FormatException(string.Format("Line {0} contains a deviating amount of tiles.", y + 1));
                    }

                    TrackTile[] tilesForThisLine = new TrackTile[tilesPerLine];

                    for (int x = 0; x < line.Length; x++)
                    {
                        string tileTypeAsString = line.Substring(x, 1);
                        int tileTypeAsInt;

                        if (!int.TryParse(tileTypeAsString, out tileTypeAsInt) || !Enum.IsDefined(typeof(TrackTile), tileTypeAsInt))
                        {
                            throw new FormatException(string.Format("Line {0} contains a not supported tile identifier {1}.", y, tileTypeAsString));
                        }

                        TrackTile tileType = (TrackTile)tileTypeAsInt;

                        if  (tileType != default(TrackTile))
                        {
                            tilesForThisLine[x] = tileType;
                        }
                    }

                    tiles.Add(tilesForThisLine);

                    if (sr.EndOfStream)
                    {
                        break;
                    }
                }

                Track result = new Track(tilesPerLine, tiles.Count);

                {
                    int y = 0;

                    foreach (TrackTile[] curTiles in tiles)
                    {
                        for (int x = 0; x < tilesPerLine; x++)
                        {
                            result.Tiles[x, y] = curTiles[x];
                        }

                        y++;
                    }

                }

                return result;
            }
        }
Example #5
0
 public void NewGame(Track track)
 {
     Game = new Game(track);
 }
Example #6
0
        /// <summary>
        /// Baut ein <see cref="Track"/> Objekt aus den gegebenen Tiles.
        /// </summary>
        /// <param name="tilesPerLine">Die Anzahl an Tiles pro Zeile.</param>
        /// <param name="allTiles">Die Liste aller Tiles.</param>
        /// <param name="goal">Die Position des Ziels.</param>
        /// <returns>Ein <see cref="Track"/> Objekt zusammengesetzt aus den Tiles.</returns>
        private static Track BuildTrack(string name, string key, int tilesPerLine, IEnumerable<TrackTile[]> allTiles, Point goal)
        {
            int y = 0;

            var track = new Track(name, key, tilesPerLine, allTiles.Count(), goal);

            foreach (var tileRow in allTiles)
            {
                for (var x = 0; x < tilesPerLine; x++)
                {
                    track.Tiles[x, y] = tileRow[x];
                }

                y++;
            }
            return track;
        }