public bool Update(int gravity, WallCollection walls, PlayerCollection players) { if (HitBox.Y > KILL_LEVEL) { return(false); } AccelY = gravity; // Save Velocity and Rec values to be manipulated Rectangle rectangle = HitBox; Point velocity = Velocity; // update velocity based on acceeleration velocity.X += AccelX; velocity.Y += AccelY; if (velocity.X > MAX_SPEED) { velocity.X = MAX_SPEED; } else if (velocity.X < -MAX_SPEED) { velocity.X = -MAX_SPEED; } if (OnGround) { velocity.Y = 0; if (AccelX == 0) { if (velocity.X > DAMPEN) { velocity.X -= DAMPEN; } else if (velocity.X < -DAMPEN) { velocity.X += DAMPEN; } else { velocity.X = 0; } } } // test to see if on the ground OnGround = false; if (walls.Intersects(new Rectangle(HitBox.X, HitBox.Bottom, HitBox.Width, 1)) != new Rectangle()) { OnGround = true; } // test to see if on the ground Rectangle test; Rectangle wall = new Rectangle(); Point offset = new Point(0); // Test Y if (velocity.Y > 0) // fall on v { test = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height + velocity.Y); wall = walls.Intersects(test); if (wall != new Rectangle()) { velocity.Y = wall.Top - rectangle.Bottom; } } else if (velocity.Y < 0) // hit from top ^ { test = new Rectangle(rectangle.X, rectangle.Y + velocity.Y, rectangle.Width, rectangle.Height - velocity.Y); wall = walls.Intersects(test); if (wall != new Rectangle()) { velocity.Y = 0; } } rectangle.Y += velocity.Y; // Test X if (velocity.X > 0) // hit from right side ->| { test = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width + velocity.X, rectangle.Height / 2); wall = walls.Intersects(test); if (wall != new Rectangle()) { velocity.X = -rectangle.Right + wall.Left; AccelX = 0; } else { // test for moveing over an incline test = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width + velocity.X, rectangle.Height); wall = walls.Intersects(test); if (wall != new Rectangle()) { test = new Rectangle(rectangle.X, rectangle.Y - wall.Height, rectangle.Width + velocity.X, rectangle.Height); test = walls.Intersects(test); if (test == new Rectangle()) { offset.Y -= wall.Height; } else { velocity.X = -rectangle.Right + wall.Left; AccelX = 0; } } } } else if (velocity.X < 0) // hit from right side |<- { test = new Rectangle(rectangle.X + velocity.X, rectangle.Y, rectangle.Width - velocity.X, rectangle.Height / 2); wall = walls.Intersects(test); if (wall != new Rectangle()) { velocity.X = wall.Right - rectangle.Left; AccelX = 0; } else { // test for moveing over an incline test = new Rectangle(rectangle.X + velocity.X, rectangle.Y, rectangle.Width - velocity.X, rectangle.Height); wall = walls.Intersects(test); if (wall != new Rectangle()) { test = new Rectangle(rectangle.X + velocity.X, rectangle.Y - wall.Height, rectangle.Width - velocity.X, rectangle.Height); test = walls.Intersects(test); if (test == new Rectangle()) { offset.Y -= wall.Height; } else { velocity.X = wall.Right - rectangle.Left; AccelX = 0; } } } } // update weapon if exists if (weapon != null) { weapon.Update(this, walls, players); } // uppdate the rectangle based on velocity rectangle.Y += offset.Y; rectangle.X += velocity.X + offset.X; // reset Velocity and Rec values Velocity = velocity; HitBox = rectangle; return(Health > 0); }
/// <summary> /// This method is used to update anything needed with the weapon like location. /// </summary> /// <param name="owner"></param> /// <param name="walls"></param> /// <param name="players"></param> public abstract void Update(Hero owner, WallCollection walls, PlayerCollection players);