public virtual void Update(ref Map map) { if (!IsOnGround) velocity += gravity; SweepMove(ref map, velocity); }
/// <summary> /// This automatically binds Game1's function to window's events /// </summary> /// <param name="window">The GameWindow that will control this Game1</param> public Game1(OpenTK.GameWindow window) { //I believe this is just a reference to the window object this.window = window; window.Load += this.Load; window.Resize += this.Resize; window.RenderFrame += this.Render; window.UpdateFrame += this.Update; window.VSync = VSyncMode.Off; My.Initialize(window); Spritebatch.Initialize(window); view = new View(window, Vector2.Zero, 10.0, 1, 0.0); view.SetPosition(view.SizeWorld / 2f); view.enableRounding = false; map = new Map("DevRoom"); obj = new SolidObject("PlayerTest", new Vector2(32, 32), new Vector2(24, 31), new Vector2(0, 0.5f)); }
/// <summary> /// Moves this solid object using Calc.SweepTest to take /// into account the map collisionTypes grid /// </summary> private void SweepMove(ref Map map, Vector2 amount) { RectangleF startRec = ColRec; position += amount; RectangleF endRec = ColRec; position -= amount; RectangleF uni = RectangleF.Union(startRec, endRec); int minX = Math.Max(0, (int)Math.Floor(uni.Left / (float)map.GridSize)); int maxX = Math.Min(map.Width - 1, (int)Math.Ceiling(uni.Right / (float)map.GridSize)); int minY = Math.Max(0, (int)Math.Floor(uni.Top / (float)map.GridSize)); int maxY = Math.Min(map.Height - 1, (int)Math.Ceiling(uni.Bottom / (float)map.GridSize)); Vector2 solution = amount; bool hitBlock = false; #region Loop Through Tiles for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { RectangleF tileRec = map.GetColRec(x, y); if (map.colGrid[x, y] == CollisionGrid.CollisionType.Solid || map.colGrid[x,y] == CollisionGrid.CollisionType.Platform) { RectangleF solutionRec = new RectangleF(); hitBlock = (hitBlock || Calc.SweepTest(tileRec, startRec, amount, out solutionRec, map.colGrid[x, y] == CollisionGrid.CollisionType.Platform)); if (!hitBlock) { continue; } else { if (Math.Abs(solutionRec.Left - startRec.Left) < Math.Abs(solution.X)) { //We found a closer solution along the X-axis solution.X = solutionRec.Left - startRec.Left; } if (Math.Abs(solutionRec.Top - startRec.Top) < Math.Abs(solution.Y)) { //We found a closer solution along the Y-axis solution.Y = solutionRec.Top - startRec.Top; } } } } } #endregion position += solution; //This region does the calls to AddCollisionBlock #region Update CollisionBlocks blocks = new List<Solid.CollisionBlock>(); colLeft = false; colRight = false; colTop = false; colBottom = false; //re-using the old min and max variables minX = Math.Max(0, (int)Math.Floor(ColRec.Left / (float)map.GridSize) - 1); maxX = Math.Min(map.Width - 1, (int)Math.Ceiling(ColRec.Right / (float)map.GridSize) + 1); minY = Math.Max(0, (int)Math.Floor(ColRec.Top / (float)map.GridSize) - 1); maxY = Math.Min(map.Height - 1, (int)Math.Ceiling(ColRec.Bottom / (float)map.GridSize) + 1); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { bool isPlatform = map.colGrid[x, y] == CollisionGrid.CollisionType.Platform; if (map.colGrid[x, y] == CollisionGrid.CollisionType.Solid || isPlatform) { bool sTop = map.colGrid.GetValue(x, y - 1) == CollisionGrid.CollisionType.Solid; bool sBottom = map.colGrid.GetValue(x, y + 1) == CollisionGrid.CollisionType.Solid; bool sLeft = map.colGrid.GetValue(x - 1, y) == CollisionGrid.CollisionType.Solid; bool sRight = map.colGrid.GetValue(x + 1, y) == CollisionGrid.CollisionType.Solid; RectangleF tileRec = map.GetColRec(x, y); bool checkX = (ColRec.Right >= tileRec.Left && ColRec.Left <= tileRec.Right); bool checkY = (ColRec.Bottom >= tileRec.Top && ColRec.Top <= tileRec.Bottom); if (tileRec.Top == ColRec.Bottom && checkX && !sTop) { AddCollisionBlock(new Solid.CollisionBlock(x, y, Solid.Side.Bottom, ref map)); } if (!isPlatform && tileRec.Bottom == ColRec.Top && checkX && !sBottom) { AddCollisionBlock(new Solid.CollisionBlock(x, y, Solid.Side.Top, ref map)); } if (!isPlatform && tileRec.Left == ColRec.Right && checkY && !sLeft) { AddCollisionBlock(new Solid.CollisionBlock(x, y, Solid.Side.Right, ref map)); } if (!isPlatform && tileRec.Right == ColRec.Left && checkY && !sRight) { AddCollisionBlock(new Solid.CollisionBlock(x, y, Solid.Side.Left, ref map)); } } } } #endregion }