Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is functional for the most part, but there are serious flaws. I can't seem to do collisions against the top of the player and bottom of a block and the player is prone to
- falling through the floor if he jumps into corners in a specific way, such as this:
- http://i.imgur.com/DhkPL.png
- */
- //=========== Level.cs ============
- //Go through each block
- public static List<Block> CollisionsBottom()
- {
- List<Block> ret = new List<Block>();
- foreach (Block b in blocks)
- {
- if (b.IsCollision)
- {
- //If b is a collision block, then make a rectangle from player to bottom of screen
- if (b.Rect.Intersects(new Rectangle(Player.Rect.X, Player.Rect.Y, Player.Rect.Width, Util._STAGEHEIGHT)))
- {
- ret.Add(b);
- }
- }
- }
- return ret;
- }
- public static List<Block> CollisionsRight()
- {
- List<Block> ret = new List<Block>();
- foreach (Block b in blocks)
- {
- if (b.IsCollision)
- {
- if (b.Rect.Intersects(new Rectangle(Player.Rect.X, Player.Rect.Y, Util._STAGEWIDTH - Player.Rect.X, Player.Rect.Height)))
- {
- ret.Add(b);
- }
- }
- }
- return ret;
- }
- public static List<Block> CollisionsLeft()
- {
- List<Block> ret = new List<Block>();
- foreach (Block b in blocks)
- {
- if (b.IsCollision)
- {
- if (b.Rect.Intersects(new Rectangle(0, Player.Rect.Y, Player.Rect.X, Player.Rect.Height)))
- {
- ret.Add(b);
- }
- }
- }
- return ret;
- }
- //============== Player.cs ===============
- private static void Physics()
- {
- if (Velocity.X > 0)
- {
- if (Math.Abs(DistToRight() - Rect.Width) > Velocity.X)
- {
- if (Player.Rect.X < Util._STAGEWIDTH / 2 + 30)
- {
- Player.Rect.X += (int)Velocity.X;
- }
- else
- {
- MoveAll(Velocity);
- }
- }
- else
- {
- Velocity.X = 0;
- }
- }
- if (Velocity.X < 0)
- {
- if (Math.Abs(DistToLeft()) > Math.Abs(Velocity.X))
- {
- if (Player.Rect.X > Util._STAGEWIDTH / 2 - 30)
- {
- Player.Rect.X += (int)Velocity.X;
- }
- else
- {
- MoveAll(Velocity);
- }
- }
- }
- //Only allow the player to move as long as distance to B is greater than his speed.
- //Otherwise, player doesn't move.
- if (Math.Abs(DistToFloor()) > Player.Velocity.Y)
- {
- Velocity.Y += 1;
- Rect.Y += (int)Velocity.Y;
- onGround = false;
- }
- else
- {
- onGround = true;
- Velocity.Y = 0;
- }
- }
- //Take the nearest block to the player that has collided with the rectangle and
- //calculate the distance to it
- private static float DistToFloor()
- {
- Block b = Level.CollisionsBottom()[0];
- return Rect.Y + Rect.Height - b.Rect.Y;
- }
- private static float DistToRight()
- {
- if (Level.CollisionsRight().Count > 0)
- {
- Block b = Level.CollisionsRight()[0];
- return b.Rect.X - Rect.X;
- }
- return Velocity.X + 1;
- }
- private static float DistToLeft()
- {
- if (Level.CollisionsLeft().Count > 0)
- {
- Block b = Level.CollisionsLeft().Last();
- return Rect.X - b.Rect.X - b.Rect.Width;
- }
- return Math.Abs(Velocity.X) + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement