59 lines
884 B
Go
59 lines
884 B
Go
package actions
|
|
|
|
import (
|
|
"dk/internal/models/control"
|
|
"dk/internal/models/towns"
|
|
"dk/internal/models/users"
|
|
"fmt"
|
|
)
|
|
|
|
type Direction int
|
|
|
|
const (
|
|
North Direction = iota
|
|
East
|
|
South
|
|
West
|
|
)
|
|
|
|
func (d Direction) String() string {
|
|
switch d {
|
|
case North:
|
|
return "North"
|
|
case East:
|
|
return "East"
|
|
case South:
|
|
return "South"
|
|
case West:
|
|
return "West"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|
|
|
|
func Move(user *users.User, dir Direction) (string, int, int, error) {
|
|
control := control.Get()
|
|
|
|
newX, newY := user.GetPosition()
|
|
switch dir {
|
|
case North:
|
|
newY++
|
|
case East:
|
|
newX++
|
|
case South:
|
|
newY--
|
|
case West:
|
|
newX--
|
|
}
|
|
|
|
if !control.IsWithinWorldBounds(newX, newY) {
|
|
return user.Currently, user.X, user.Y, fmt.Errorf("You've hit the edge of the world.")
|
|
}
|
|
|
|
if towns.ExistsAt(newX, newY) {
|
|
return "In Town", newX, newY, nil
|
|
}
|
|
|
|
return "Exploring", newX, newY, nil
|
|
}
|