func (gameWrapper *GameWrapper) verifyValidMove(unitId int, player *game.Player, locations []game.Location) error { if len(locations) < 1 { message := "must supply more than zero locations" logger.Warnf(message) return errors.New(message) } tiles := make([]game.Terrain, len(locations)) terrain := gameWrapper.world.GetTerrain() for i, location := range locations { tiles[i] = terrain[location.GetX()][location.GetY()] } for _, location := range locations[1:] { err := gameWrapper.world.IsUnitAtLocation(location) if err != nil { logger.Warn(err) return errors.New(fmt.Sprintf("Cannot pass through units at (%d, %d)", location.GetX(), location.GetY())) } } unit, err := gameWrapper.world.GetAndVerifyOwnedUnit(player, unitId) if err != nil { return err } return game.ValidMove(unit.GetMovement().GetDistance(), unit.GetMovement(), tiles, locations) }
func (world *World) IsUnitAtLocation(location Location) error { _, ok := world.unitMap[location] if ok { logger.Warn(world.unitMap) return errors.New(fmt.Sprintf("There is a unit at the location (%d, %d)", location.x, location.y)) } else { return nil } }
func DamageUnit(attacker *Unit, index int, defender *Unit) (bool, error) { if len(attacker.attacks) > index { alive, err := defender.receiveDamage(calculateDamage(attacker.attacks[index], defender.armor)) logger.Info("Defender is %t still alive with %d health", alive, defender.health) return alive, err } else { logger.Warn("Player does not have %d attacks (has %d)", index, len(attacker.attacks)) return true, errors.New("Player does not have that many attacks") } }
func (world *World) GetUnitAtLocation(location Location) (*Unit, error) { unit, ok := world.unitMap[location] if ok { return unit, nil } else { message := fmt.Sprintf("No unit located at (%d, %d)", location.x, location.y) logger.Warn(message) return nil, errors.New(message) } }
func (world *World) GetUnitFromId(unitId int) (*Unit, error) { unit, ok := world.units[unitId] if ok { return unit, nil } else { message := fmt.Sprintf("No unit with id %d", unitId) logger.Warn(message) return nil, errors.New(message) } }