public void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; float posX = 0; float posY = 0; int tileX = 0; int tileY = 0; // Calculate tile position based on the side we are moving towards. posX = Position.X + localBounds.Width / 2 * (int)direction; tileX = (int)Math.Floor(posX / 64) - (int)direction; tileY = (int)Math.Floor(Position.Y / 48); if (isSmashBlock) { // Calculate tile position based on the side we are moving towards. posY = Position.Y + localBounds.Height / 2 * (int)direction; tileX = (int)Math.Floor(Position.X / 64); tileY = (int)Math.Floor(posY / 48) - (int)direction; } if (waitTime > 0) { // Wait for some amount of time. waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds); if (waitTime <= 0.0f) { // Then turn around. direction = (G.Globals.Enums.FaceDirection)(-(int)direction); } } else { if (isSmashBlock) { if (Statics.Level.TileEngine.GetCollision(tileX, tileY + (int)direction) == TileCollision.Impassable || Statics.Level.TileEngine.GetCollision(tileX, tileY + (int)direction) == TileCollision.Platform) { velocity = new Vector2(0.0f, 0.0f); waitTime = MaxWaitTime; } } //If we're about to run into a wall that isn't a MovableTile move in other direction. if (!isSmashBlock && Statics.Level.TileEngine.GetCollision(tileX + (int)direction, tileY) == TileCollision.Impassable || Statics.Level.TileEngine.GetCollision(tileX + (int)direction, tileY) == TileCollision.Platform) { velocity = new Vector2(0.0f, 0.0f); waitTime = MaxWaitTime; } else { if (isSmashBlock) { // Move in the current direction. velocity = new Vector2(0.0f, (int)direction * (MoveSpeed / 2) * elapsed); //velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } else { velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } position = position + velocity; } } if (Statics.Level.TileEngine.movableTiles.Count > 0) { //If we're about to run into a MovableTile move in other direction. foreach (var movableTile in Statics.Level.TileEngine.movableTiles) { if (BoundingRectangle != movableTile.BoundingRectangle) { if (BoundingRectangle.Intersects(movableTile.BoundingRectangle)) { direction = (G.Globals.Enums.FaceDirection)(-(int)direction); velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } } } } }
public void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; float posX = 0; float posY = 0; int tileX = 0; int tileY = 0; // Calculate tile position based on the side we are moving towards. posX = Position.X + localBounds.Width / 2 * (int)direction; tileX = (int)Math.Floor(posX / 64) - (int)direction; tileY = (int)Math.Floor(Position.Y / 48); if (isSmashBlock) { // Calculate tile position based on the side we are moving towards. posY = Position.Y + localBounds.Height / 2 * (int)direction; tileX = (int)Math.Floor(Position.X / 64); tileY = (int)Math.Floor(posY / 48) - (int)direction; } if (waitTime > 0) { // Wait for some amount of time. waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds); if (waitTime <= 0.0f) { // Then turn around. direction = (G.Globals.Enums.FaceDirection)(-(int)direction); } } else { if (isSmashBlock) { if (Statics.Level.TileEngine.GetCollision(tileX, tileY + (int)direction) == TileCollision.Impassable || Statics.Level.TileEngine.GetCollision(tileX, tileY + (int)direction) == TileCollision.Platform) { velocity = new Vector2(0.0f, 0.0f); waitTime = MaxWaitTime; } } //If we're about to run into a wall that isn't a MovableTile move in other direction. if (!isSmashBlock && Statics.Level.TileEngine.GetCollision(tileX + (int)direction, tileY) == TileCollision.Impassable || Statics.Level.TileEngine.GetCollision(tileX + (int)direction, tileY) == TileCollision.Platform) { velocity = new Vector2(0.0f, 0.0f); waitTime = MaxWaitTime; } else { if (isSmashBlock) { // Move in the current direction. velocity = new Vector2(0.0f, (int)direction * (MoveSpeed/2) * elapsed); //velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } else { velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } position = position + velocity; } } if (Statics.Level.TileEngine.movableTiles.Count > 0) { //If we're about to run into a MovableTile move in other direction. foreach (var movableTile in Statics.Level.TileEngine.movableTiles) { if (BoundingRectangle != movableTile.BoundingRectangle) { if (BoundingRectangle.Intersects(movableTile.BoundingRectangle)) { direction = (G.Globals.Enums.FaceDirection)(-(int)direction); velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); } } } } }