public override void Update(GameTime gameTime) { base.Update(gameTime); if (waitTime > 0) { waitTime -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (waitTime <= 0.0f) { TurnAround(); } } else { TileField tiles = GameWorld.Find("tiles") as TileField; float posX = BoundingBox.Left; if (!Mirror) { posX = BoundingBox.Right; } int tileX = (int)Math.Floor(posX / tiles.CellWidth); int tileY = (int)Math.Floor(position.Y / tiles.CellHeight); if (tiles.GetTileType(tileX, tileY - 1) == TileType.Normal || tiles.GetTileType(tileX, tileY) == TileType.Background) { waitTime = 0.5f; velocity.X = 0.0f; } } CheckPlayerCollision(); CheckBombCollision(); }
/// <summary>Update the projectile.</summary> /// <param name="tiles">The field of the level.</param> public void Update(GameTime gameTime, TileField tiles) { if (!active) { return; } base.Update(gameTime); // check bounds int x_floor = (int)position.X / tiles.CellWidth; int y_floor = (int)position.Y / tiles.CellHeight; for (int y = y_floor - 1; y <= y_floor + 1; y++) { for (int x = x_floor - (mirrored ? 1 : 0); x <= x_floor + (mirrored ? 0 : 1); x++) { TileType tileType = tiles.GetTileType(x, y); if (tileType == TileType.Normal || tileType == TileType.Platform) { Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight, tiles.CellWidth, tiles.CellHeight); if (tileBounds.Intersects(BoundingBox)) { hit = true; } } } } if (hit) { active = false; } }
private void HandleCollisions(bool movingThroughPlatform) { isOnTheGround = false; walkingOnIce = false; walkingOnHot = false; TileField tiles = GameWorld.Find("tiles") as TileField; int xFloor = (int)position.X / tiles.CellWidth; int yFloor = (int)position.Y / tiles.CellHeight; for (int y = yFloor - 2; y <= yFloor + 1; ++y) { for (int x = xFloor - 1; x <= xFloor + 1; ++x) { TileType tileType = tiles.GetTileType(x, y); if (tileType == TileType.Background) { continue; } Tile currentTile = tiles.Get(x, y) as Tile; Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight, tiles.CellWidth, tiles.CellHeight); Rectangle boundingBox = this.BoundingBox; boundingBox.Height += 1; if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox)) { continue; } Vector2 depth = Collision.CalculateIntersectionDepth(boundingBox, tileBounds); if (Math.Abs(depth.X) < Math.Abs(depth.Y)) { if (tileType == TileType.Normal) { position.X += depth.X; } continue; } if (previousYPosition <= tileBounds.Top && tileType != TileType.Background) { if (tileType == TileType.Platform && movingThroughPlatform) { continue; } isOnTheGround = true; velocity.Y = 0; if (currentTile != null) { walkingOnIce = walkingOnIce || currentTile.Ice; walkingOnHot = walkingOnHot || currentTile.Hot; } } if (tileType == TileType.Normal || isOnTheGround) { position.Y += depth.Y + 1; } } } position = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y)); previousYPosition = position.Y; }
/// <summary>Update the projectile.</summary> /// <param name="tiles">The field of the level.</param> public void Update(GameTime gameTime, TileField tiles) { if (!active) return; base.Update(gameTime); // check bounds int x_floor = (int)position.X / tiles.CellWidth; int y_floor = (int)position.Y / tiles.CellHeight; for (int y = y_floor - 1; y <= y_floor + 1; y++) for (int x = x_floor - (mirrored ? 1 : 0); x <= x_floor + (mirrored ? 0 : 1); x++) { TileType tileType = tiles.GetTileType(x, y); if (tileType == TileType.Normal || tileType == TileType.Platform) { Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight, tiles.CellWidth, tiles.CellHeight); if (tileBounds.Intersects(BoundingBox)) { hit = true; } } } if (hit) active = false; }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (inJump) { velocity.Y += 2; if (position.Y > landingHeight) { position.Y = landingHeight; velocity.Y = 0; inJump = false; } } if (waitTime > 0) { waitTime -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (waitTime <= 0.0f) { TurnAround(); } } else { TileField tiles = GameWorld.Find("tiles") as TileField; float posX = BoundingBox.Left; if (!Mirror) { posX = BoundingBox.Right; } int tileX = (int)Math.Floor(posX / tiles.CellWidth); int tileY = (int)Math.Floor(position.Y / tiles.CellHeight); if ((tiles.GetTileType(tileX, tileY - 1) == TileType.Normal || tiles.GetTileType(tileX, tileY) == TileType.Background) && !inJump) { float jumpLength = 0; float jumpHeight = 0; float vel = -120f; while (true) { jumpLength += velocity.X / 60; jumpHeight += vel / 60; vel += 2; if (jumpHeight < 0) { if (tiles.GetTileType(tileX + (int)Math.Floor(jumpLength / tiles.CellWidth), tileY) == TileType.Normal) { waitTime = 0.5f; velocity.X = 0.0f; break; } } else { if (position.X + jumpLength <= 0 || position.X + jumpLength >= GameEnvironment.camera.levelwidth - this.Width) { waitTime = 0.5f; velocity.X = 0.0f; break; } else if (tiles.GetTileType(tileX + (int)Math.Floor(jumpLength / tiles.CellWidth), tileY) == TileType.Normal) { velocity.Y = -120.0f; inJump = true; landingHeight = position.Y; break; } else { waitTime = 0.5f; velocity.X = 0.0f; break; } } } } } CheckPlayerCollision(); CheckBombCollision(); }
/// <summary>Check and handle the collisions.</summary> public void CheckCollisions() { TileField tiles = GameWorld.Find("tiles") as TileField; int x_floor = (int)position.X / tiles.CellWidth; int y_floor = (int)position.Y / tiles.CellHeight; // tile collisions for (int y = y_floor - 2; y <= y_floor + 1; ++y) { for (int x = x_floor - 1; x <= x_floor + 1; ++x) { if (y < 0 || y >= tiles.Rows || x < 0 || x >= tiles.Columns) { continue; } TileType tileType = tiles.GetTileType(x, y); if (tileType == TileType.Normal || tileType == TileType.Platform) { Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight, tiles.CellWidth, tiles.CellHeight); if (tileBounds.Intersects(BoundingBox)) { Reset(); return; } } } } Player player = GameWorld.Find("player") as Player; // projectile collisions foreach (Projectile projectile in player.Projectiles) { if (BoundingBox.Intersects(projectile.BoundingBox) && projectile.Active) { projectile.Hit = true; hp--; if (hp <= 0) { Reset(); return; } } } // player collision if (CollidesWith(player)) { Vector2 depth = Collision.CalculateIntersectionDepth(BoundingBox, player.BoundingBox); if (player.BoundingBox.Y < BoundingBox.Y && player.Velocity.Y > 0 && depth.X > depth.Y) { velocity.Y = 600; player.Jump(); } else { player.LowerHP(50); Reset(); } } }
/// <summary>Handle the player collisions.</summary> private void HandleCollisions() { isOnTheGround = false; walkingOnIce = false; walkingOnHot = false; TileField tiles = GameWorld.Find("tiles") as TileField; int x_floor = (int)position.X / tiles.CellWidth; int y_floor = (int)position.Y / tiles.CellHeight; for (int y = y_floor - 2; y <= y_floor + 1; ++y) { for (int x = x_floor - 1; x <= x_floor + 1; ++x) { TileType tileType = tiles.GetTileType(x, y); if (tileType == TileType.Background) { continue; } // solid tile Tile currentTile = tiles.Get(x, y) as Tile; Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight, tiles.CellWidth, tiles.CellHeight); Rectangle boundingBox = BoundingBox; boundingBox.Height += 1; if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox)) { continue; } // colliding Vector2 depth = Collision.CalculateIntersectionDepth(boundingBox, tileBounds); if (Math.Abs(depth.X) < Math.Abs(depth.Y)) { if (tileType == TileType.Normal) { position.X += depth.X; } continue; } if (previousYPosition <= tileBounds.Top && tileType != TileType.Background) { if (velocity.Y > 1250) // fall damage { LowerHP(((int)velocity.Y - 1250) / 100); } isOnTheGround = true; velocity.Y = 0; if (currentTile != null) // update status effects { walkingOnIce = currentTile.Ice; walkingOnHot = currentTile.Hot; } } if (tileType == TileType.Normal || isOnTheGround) { position.Y += depth.Y + 1; // make sure we stand on top of the tile } } } position = new Vector2((int)position.X, (int)position.Y); previousYPosition = position.Y; }
public void tileCollide() { TileField tiles = GameWorld.Find("tiles") as TileField; for (int y = 0; y < tiles.CellHeight - 1; y++) { for (int x = 0; x < tiles.CellWidth - 1; x++) { if (tiles.GetTileType(x, y) != TileType.Background && tiles.Get(x, y) != null) { Tile collisionTile = tiles.Get(x, y) as Tile; if (CollidesWith(collisionTile)) { if (!collisionTile.Ice && !collisionTile.Hot) { Vector2 vectorFromTile = (collisionTile.Center + collisionTile.GlobalPosition) - (this.Center + this.GlobalPosition); if (Math.Abs(vectorFromTile.X) > Math.Abs(vectorFromTile.Y) * (((float)collisionTile.Width) / (float)collisionTile.Height)) { if (Math.Sign(vectorFromTile.X) == Math.Sign(velocity.X)) { velocity.X *= -.9f; } if (velocity.X > 0) { position.X = collisionTile.Position.X + collisionTile.Width; } else { position.X = collisionTile.Position.X - this.Width; } } else { if (Math.Sign(vectorFromTile.Y) == Math.Sign(velocity.Y)) { velocity.Y *= -.9f; } if (velocity.Y > 0) { position.Y = collisionTile.Position.Y + collisionTile.Width; } else { position.Y = collisionTile.Position.Y - this.Height; } } return; } else if (collisionTile.Ice) { velocity = Vector2.Zero; frozen = true; } else if (collisionTile.Hot) { explode(); } } } } } }