private void updateCollision() { Location leftFoot = Location.valueOf(ScreenPosition.Left + Settings.BLOCK_SIZE / 16, ScreenPosition.Bottom); Location rightFoot = Location.valueOf(ScreenPosition.Right - Settings.BLOCK_SIZE / 16, ScreenPosition.Bottom); Block left = getWorld().getBlockAt(leftFoot); Block right = getWorld().getBlockAt(rightFoot); if (moveState == MovementState.WALKING) { if ((!left.getType().isSolid() && !right.getType().isSolid()) /*|| coordinates.getY() % 1 != 0*/) { speed.Y = -4; moveState = MovementState.FALLING; } } else if (moveState == MovementState.FALLING) { if (left.getType().isSolid() || right.getType().isSolid()) { speed.Y = 0; moveState = MovementState.WALKING; } } Block sideBlock, topBlock, botBlock; Location top, side, bottom; if (DIRECTION.X != STABLE) { if (DIRECTION.X == RIGHT) { side = Location.valueOf(getBounds().Right + Settings.BLOCK_SIZE / 8, getBounds().Center.Y); top = Location.valueOf(getBounds().Right + Settings.BLOCK_SIZE / 8, getBounds().Top); bottom = Location.valueOf(getBounds().Right + Settings.BLOCK_SIZE / 8, getBounds().Bottom); } else //left { side = Location.valueOf(getBounds().Left - Settings.BLOCK_SIZE / 8, getBounds().Center.Y); top = Location.valueOf(getBounds().Left - Settings.BLOCK_SIZE / 8, getBounds().Top); bottom = Location.valueOf(getBounds().Left - Settings.BLOCK_SIZE / 8, getBounds().Bottom); } sideBlock = getWorld().getBlockAt(side); topBlock = getWorld().getBlockAt(top); botBlock = getWorld().getBlockAt(bottom); if (sideBlock.getType().isSolid() || topBlock.getType().isSolid() || (botBlock.getType().isSolid() && moveState == MovementState.FALLING)) { DIRECTION.X = 0; } } }
/* * To do: Clean up all of this to match new use of rectangles */ private void updateMovement(GameTime gameTime) { if (kbState.IsKeyDown(Keys.A) || kbState.IsKeyDown(Keys.S)) { speed.X = MOVEMENT_SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds; DIRECTION.X = LEFT; } else if (kbState.IsKeyDown(Keys.W) || kbState.IsKeyDown(Keys.D)) { speed.X = MOVEMENT_SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds; DIRECTION.X = RIGHT; } else { DIRECTION.X = STABLE; speed.X = 0; } float left = ScreenPosition.Left; float right = ScreenPosition.Right; Location leftFoot = Location.valueOf(left + Settings.BLOCK_SIZE / 8, ScreenPosition.Bottom); Location rightFoot = Location.valueOf(right - Settings.BLOCK_SIZE / 8, ScreenPosition.Bottom); Location rightEar = Location.valueOf(right - Settings.BLOCK_SIZE / 8, ScreenPosition.Top); Location leftEar = Location.valueOf(left + Settings.BLOCK_SIZE / 8, ScreenPosition.Top); Location above = Location.valueOf(ScreenPosition.Center.X, ScreenPosition.Top + Settings.BLOCK_SIZE / 8); if (moveState == MovementState.WALKING) { if (Keyboard.GetState().IsKeyDown(Keys.Space) && (getWorld().getBlockAt(leftFoot).getType().isSolid()) && (getWorld().getBlockAt(rightFoot).getType().isSolid()) && !getWorld().getBlockAt(above).getType().isSolid()) { speed.Y = 4; DIRECTION.Y = UP; moveState = MovementState.JUMPING; originalY = coordinates.getY(); } else { speed.Y = 0; } } if (moveState == MovementState.JUMPING && ((coordinates.getY() - this.originalY >= JUMP_HEIGHT) || !(getWorld().getBlockAt(above).getType() is Air))) { moveState = MovementState.FALLING; speed.Y = -4; originalY = 0; } }
private void updateInteraction(GameTime gameTime) { if (Mouse.GetState().LeftButton == ButtonState.Pressed) { Location mouseCoords = Location.valueOf(Mouse.GetState().X, Mouse.GetState().Y); milliseconds += (long)gameTime.ElapsedGameTime.TotalMilliseconds; if (milliseconds >= interval) { milliseconds = 0; if (Math.Abs(mouseCoords.getX() - coordinates.getX()) <= 4 && Math.Abs(mouseCoords.getY() - coordinates.getY()) <= 4) { Block block = getWorld().getBlockAt(mouseCoords); if (block.getType() is Air) { return; } getWorld().getBlockAt(mouseCoords).damage(5); } } } else if (Mouse.GetState().RightButton == ButtonState.Pressed) { Location mouseCoords = Location.valueOf(Mouse.GetState().X, Mouse.GetState().Y); Block block = getWorld().getBlockAt(mouseCoords); if (canPlaceBlock(block)) { block.setType(getToolbar().getSelectedObj().getType()); getToolbar().getSelectedObj().modifyAmount(-1); if (getToolbar().getSelectedObj().getAmount() <= 0) { getInventory().setAt(getToolbar().getCurrentIndex(), 0, null); } } } }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CadetBlue); player.getWorld().draw(); player.Draw(spriteBatch); Location mouseCoords = Location.valueOf(Mouse.GetState().X, Mouse.GetState().Y); Location mouseroofCoords = new Location(mouseCoords.getX(), Math.Ceiling(mouseCoords.getY())); if (Math.Abs(mouseCoords.getX() - player.coordinates.getX()) <= 4 && Math.Abs(mouseCoords.getY() - player.coordinates.getY()) <= 4) { Screen.render(player.getWorld().getBlockAt(mouseCoords).getLocation(), selectionTile, Settings.BLOCK_SIZE, Settings.BLOCK_SIZE, false); } if (Settings.DEBUG) { Screen.renderString(font, SideCraft.player.getLocation().toString() + Environment.NewLine + mouseCoords.toString() + Environment.NewLine + "Mouse position" + mouseCoords.toVector2().ToString(), new Vector2(10, 10), Color.Black); Screen.renderString(font, "0", new Location(0, 0).toVector2(), Color.White); } base.Draw(gameTime); }