diff --git a/player.go b/player.go index 37990ed..1cae9e5 100644 --- a/player.go +++ b/player.go @@ -5,6 +5,12 @@ func (game *Game) drawPlayer() { } func (game *Game) movePlayer(press int) { + + if game.blockedByTrash(press) { + game.player.moves++ + return + } + if press == Left { if game.player.position.x != 2 { game.player.position.x-- @@ -55,6 +61,38 @@ func (game *Game) movePlayer(press int) { } } +// blockedByTrash() Checks if player can move into a direction and if trash isn't blocking +func (game *Game) blockedByTrash(press int) bool { + var checkPos Position + + if press == Left { + checkPos.x = game.player.position.x - 1 + checkPos.y = game.player.position.y + } else if press == Right { + checkPos.x = game.player.position.x + 1 + checkPos.y = game.player.position.y + } else if press == Up { + checkPos.x = game.player.position.x + checkPos.y = game.player.position.y - 1 + } else if press == Down { + checkPos.x = game.player.position.x + checkPos.y = game.player.position.y + 1 + } else if press == upleft { + checkPos.x = game.player.position.x - 1 + checkPos.y = game.player.position.y - 1 + } else if press == upright { + checkPos.x = game.player.position.x + 1 + checkPos.y = game.player.position.y - 1 + } else if press == downright { + checkPos.x = game.player.position.x + 1 + checkPos.y = game.player.position.y + 1 + } else if press == downleft { + checkPos.x = game.player.position.x - 1 + checkPos.y = game.player.position.y + 1 + } + return game.onTrash(checkPos) +} + func (game *Game) teleport() { var pos Position