Energy-preserving algorithm for gyrocenter dynamics of charged particles