public static void DrawPlayerView(PaintEventArgs args, Game game, Dictionary <string, Bitmap> textures, int distance) { var time1 = DateTime.Now; args.Graphics.FillRectangle(Brushes.CornflowerBlue, 0, 0, 1000 / game.CastedRays.Length * game.CastedRays.Length + 10, 500); args.Graphics.FillRectangle(Brushes.Gray, 0, 500, 1000 / game.CastedRays.Length * game.CastedRays.Length + 10, 500); var offset = 0; foreach (var tuples in game.CastedRays) { foreach (var(ray, wall) in tuples) { if (!(ray.B.X > 0) || !(ray.B.Y > 0)) { continue; } var bmp = textures[wall.Name]; var h = (int)(distance / ray.Length); args.Graphics.DrawImage( bmp, new RectangleF(0 + offset, 500 - h, 6, h * 2), new RectangleF((int)Utils.GetDist(wall.line.A, ray.B) % (bmp.Width - 1), 0, 1, bmp.Height), GraphicsUnit.Pixel); } offset += 1000 / game.CastedRays.Length; } var time2 = DateTime.Now; args.Graphics.DrawString("ticks on frame:" + time2.Subtract(time1).Ticks.ToString(), SystemFonts.MenuFont, Brushes.Black, 1200, 0); //args.Graphics.FillRectangle(Brushes.White, 0, 600, (float) Math.PI * 201, 800); }
public static void DrawPlayerView(PaintEventArgs args, Game game, Dictionary <string, Bitmap> textures, int distance, int heights) { args.Graphics.FillRectangle(Brushes.CornflowerBlue, 0, 0, 1000 / game.CastedRays.Length * game.CastedRays.Length + 10, 500); args.Graphics.FillRectangle(Brushes.Gray, 0, 500, 1000 / game.CastedRays.Length * game.CastedRays.Length + 10, 500); var offset = 0; foreach (var tuples in game.CastedRays) { foreach (var(ray, wall) in tuples) { if (ray.B.X > 0 && ray.B.Y > 0) { var bmp = textures[wall.Name]; var d1 = ray.Length; var h1 = (distance / d1 * heights) * 2; var width = bmp.Width; var height = bmp.Height; args.Graphics.DrawImage( bmp, new RectangleF(0 + offset, 500 - (float)h1 / 2, 6, (float)h1), new RectangleF((int)Utils.GetDist(wall.line.A, ray.B) % (width - 1), 0, 1, height), GraphicsUnit.Pixel); } } offset += 1000 / game.CastedRays.Length; } //args.Graphics.FillRectangle(Brushes.White, 0, 600, (float) Math.PI * 201, 800); }
public void Shot() { var ray = new Ray(PlayerPos, _player.Direction); var minDistance = double.MaxValue; var s = new Vector(-85, -85); foreach (var w in _map.Walls.Select(wall => ray.Cast(wall)).Where(w => minDistance > Utils.GetDist(w, PlayerPos) && w.X > 0)) { minDistance = Utils.GetDist(w, PlayerPos); } var bestEnemy = new Enemy(s); foreach (var enemy in _map.Enemies) { foreach (var w in enemy.EnemyWalls.Select(wall => ray.Cast(wall)).Where(w => minDistance > Utils.GetDist(w, PlayerPos) && w.X > 0)) { bestEnemy = enemy; minDistance = Utils.GetDist(w, PlayerPos); } } if (bestEnemy.Position.X != -85) { _map.Enemies.Remove(bestEnemy); } }
public void CastRays() { for (var i = 0; i < CastedRays.Length; i++) { CastedRays[i] = new List <Tuple <Line, Wall> >(); } for (var i = 0; i < CastedRays.Length; i++) { var ray = player.Rays[i]; var minLenght = double.MaxValue; var zeroVector = new Vector(0, 0); var bestCollision = new Tuple <Line, Wall>(new Line(zeroVector, zeroVector), new Wall(new Line(zeroVector, zeroVector), "")); foreach (var wall in map.Walls.Concat(map.Doors.Select(z => z.DorWall))) { var currentPoint = ray.Cast(wall); if (!(currentPoint.X > 0) || !(currentPoint.Y > 0) || !(Utils.GetDist(currentPoint, ray.Pos) < minLenght)) { continue; } minLenght = Utils.GetDist(currentPoint, ray.Pos); bestCollision = Tuple.Create( new Line(new Vector(ray.Pos.X, ray.Pos.Y), new Vector(currentPoint.X, currentPoint.Y)), wall); } CastedRays[i].Add(bestCollision); foreach (var wall in map.Enemies.SelectMany(x => x.EnemyWalls)) { var currentPoint = ray.Cast(wall); if (currentPoint.X > 0 && currentPoint.Y > 0) { CastedRays[i].Add(Tuple.Create( new Line(new Vector(ray.Pos.X, ray.Pos.Y), new Vector(currentPoint.X, currentPoint.Y)), wall)); } } CastedRays[i] = CastedRays[i].OrderBy(x => - Utils.GetDist(x.Item1.B, ray.Pos)).ToList(); } }
public void CastRays() { for (var i = 0; i < CastedRays.Length; i++) { CastedRays[i] = new List <Tuple <Line, Wall> >(); } for (var i = 0; i < _player.Rays.Count; i++) { var ray = _player.Rays[i]; foreach (var wall in _map.Walls.Concat(_map.Enemies.SelectMany(x => x.EnemyWalls).Concat(_map.Doors.Select(z => z.DorWall)))) { var currentPoint = ray.Cast(wall); if (currentPoint.X > 0 && currentPoint.Y > 0) { CastedRays[i].Add(Tuple.Create( new Line(new Vector(ray.Pos.X, ray.Pos.Y), new Vector(currentPoint.X, currentPoint.Y)), wall)); } } CastedRays[i] = CastedRays[i].OrderBy(x => - Utils.GetDist(x.Item1.B, ray.Pos)).ToList(); } }
private Vector CorrectVector(Vector moveVector) { foreach (var wall in _map.Walls.Concat(_map.Doors.Select(x => x.DorWall))) { var t = new Vector(moveVector.X, moveVector.Y); t.Normalize(); var playerRay = new Ray(new Vector(_player.X, _player.Y), 0) { Direction = t }; var distanceToWall = playerRay.Cast(wall); if (distanceToWall.X >= 0 && distanceToWall.Y >= 0 && moveVector.Length > Utils.GetDist(new Vector(_player.X, _player.Y), distanceToWall)) { var x1 = wall.line.A.X; var x2 = wall.line.B.X; var y1 = wall.line.A.Y; var y2 = wall.line.B.Y; var normalX = y2 - y1; var normalY = x1 - x2; var c = x1 * (y1 - y2) + y1 * (x2 - x1); var px = moveVector.X + _player.X; var py = moveVector.Y + _player.Y; var distance = Math.Abs(normalX * px + normalY * py + c) / Math.Sqrt(normalX * normalX + normalY * normalY) + 0.1; var normalVector = new Vector(normalX, normalY); normalVector.Normalize(); normalVector *= distance; moveVector += normalVector.X * moveVector.X + normalVector.Y * moveVector.Y > 0 ? -normalVector : normalVector; } } return(moveVector); }