Quellcode durchsuchen

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

Sacha Ligthert vor 2 Jahren
Ursprung
Commit
ec8c3fb8a3
1 geänderte Dateien mit 38 neuen und 0 gelöschten Zeilen
  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