Example #1
0
func MoveCharacter(character types.Character, direction types.Direction) error {
	room := GetRoom(character.GetRoomId())

	if room == nil {
		return errors.New("Character doesn't appear to be in any room")
	}

	if !room.HasExit(direction) {
		return errors.New("Attempted to move through an exit that the room does not contain")
	}

	if room.IsLocked(direction) {
		return errors.New("That way is locked")
	}

	newLocation := room.NextLocation(direction)
	newRoom := GetRoomByLocation(newLocation, room.GetZoneId())

	if newRoom == nil {
		zone := GetZone(room.GetZoneId())
		fmt.Printf("No room found at location %v %v, creating a new one (%s)\n", zone.GetName(), newLocation, character.GetName())

		var err error
		newRoom, err = CreateRoom(GetZone(room.GetZoneId()), newLocation)
		newRoom.SetTitle(room.GetTitle())
		newRoom.SetDescription(room.GetDescription())

		if err != nil {
			return err
		}

		switch direction {
		case types.DirectionNorth:
			newRoom.SetExitEnabled(types.DirectionSouth, true)
		case types.DirectionNorthEast:
			newRoom.SetExitEnabled(types.DirectionSouthWest, true)
		case types.DirectionEast:
			newRoom.SetExitEnabled(types.DirectionWest, true)
		case types.DirectionSouthEast:
			newRoom.SetExitEnabled(types.DirectionNorthWest, true)
		case types.DirectionSouth:
			newRoom.SetExitEnabled(types.DirectionNorth, true)
		case types.DirectionSouthWest:
			newRoom.SetExitEnabled(types.DirectionNorthEast, true)
		case types.DirectionWest:
			newRoom.SetExitEnabled(types.DirectionEast, true)
		case types.DirectionNorthWest:
			newRoom.SetExitEnabled(types.DirectionSouthEast, true)
		case types.DirectionUp:
			newRoom.SetExitEnabled(types.DirectionDown, true)
		case types.DirectionDown:
			newRoom.SetExitEnabled(types.DirectionUp, true)
		default:
			panic("Unexpected code path")
		}
	}

	MoveCharacterToRoom(character, newRoom)
	return nil
}
Example #2
0
func MoveCharacterToRoom(character types.Character, newRoom types.Room) {
	oldRoomId := character.GetRoomId()
	character.SetRoomId(newRoom.GetId())

	oldRoom := GetRoom(oldRoomId)

	// Leave
	dir := DirectionBetween(oldRoom, newRoom)
	events.Broadcast(events.LeaveEvent{Character: character, RoomId: oldRoomId, Direction: dir})

	// Enter
	dir = DirectionBetween(newRoom, oldRoom)
	events.Broadcast(events.EnterEvent{Character: character, RoomId: newRoom.GetId(), Direction: dir})
}