A Go version of the https://lcthw.dev/learn-code-the-hard-way/curseyou-python-rogue that makes a tiny Rogue in Go.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.4 KiB
67 lines
1.4 KiB
package main
|
|
|
|
import (
|
|
"slices"
|
|
)
|
|
|
|
func compass(near Position, offset int) []Position {
|
|
return []Position{
|
|
Position{near.X, near.Y - offset},
|
|
Position{near.X, near.Y + offset},
|
|
Position{near.X + offset, near.Y},
|
|
Position{near.X - offset, near.Y},
|
|
}
|
|
}
|
|
|
|
func (game *Game) CloneMap() Map {
|
|
// this is a shallow copy though
|
|
new_map := slices.Clone(game.Level)
|
|
|
|
for i, row := range new_map {
|
|
// this makes sure the row is an actual copy
|
|
new_map[i] = slices.Clone(row)
|
|
}
|
|
|
|
return new_map
|
|
}
|
|
|
|
func (game *Game) Inbounds(pos Position, offset int) bool {
|
|
return pos.X >= offset &&
|
|
pos.X < game.Width - offset &&
|
|
pos.Y >= offset &&
|
|
pos.Y < game.Height - offset
|
|
}
|
|
|
|
func (game *Game) Occupied(pos Position) bool {
|
|
_, is_enemy := game.Enemies[pos]
|
|
is_player := pos == game.Player.Pos
|
|
|
|
// Inbounds comes first to prevent accessing level with bad x,y
|
|
return !game.Inbounds(pos, 1) ||
|
|
game.Level[pos.Y][pos.X] == WALL ||
|
|
is_enemy ||
|
|
is_player
|
|
}
|
|
|
|
func (game *Game) FillMap(target Map, setting rune) {
|
|
for y := 0 ; y < game.Height; y++ {
|
|
target[y] = slices.Repeat([]rune{setting}, game.Width)
|
|
}
|
|
}
|
|
|
|
func (game *Game) NewMap() {
|
|
game.FillMap(game.Level, '#')
|
|
}
|
|
|
|
func (game *Game) Neighbors(near Position) []Position {
|
|
result := make([]Position, 0, 4)
|
|
points := compass(near, 2)
|
|
|
|
for _, pos := range points {
|
|
if game.Inbounds(pos, 0) {
|
|
result = append(result, pos)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|