Explorar o código

Checks of players are blocked by heaps of trash. Closes #11

Sacha Ligthert %!s(int64=2) %!d(string=hai) anos
pai
achega
ec8c3fb8a3
Modificáronse 1 ficheiros con 38 adicións e 0 borrados
  1. 38 0
      player.go

+ 38 - 0
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