package com.lloydm.geosword.common;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class KeyFrame implements Serializable
{
     /**
         *
         *
/
     private static final long serialVersionUID = 1L;
     public byte[] shipids;
     public byte[] shiptypes;
     public byte[] shipteam;
     public KeyFrameData[] shipdata;

     public KeyFrame()
     {

     }

     public int writetobuffer(byte[] buffer, int offset)
     {
                     int off
= offset;
                     for (int i = 0; i < shipids.length; i++)
                     {
                                     buffer[off] = shipids[i];
                                     off++;
                                     buffer[off] = shiptypes[i];
                                     off++;
                                     buffer[off] = shipteam[i];
                                     off++;
                                     int j
= i;
                                     {
                                                     // byte[] bytes =
                                                     // ByteBuffer.allocate(4).putFloat(shipdata[j].dx).order(ByteOrder.BIG_ENDIAN).array();
                                                     // buffer[off] = bytes[0];
                                                     // buffer[off+1] = bytes[1];
                                                     // buffer[off+2] = bytes[2];
                                                     // buffer[off+3] = bytes[3];
                                                     // off+=4;
                                                     // bytes =
                                                     // ByteBuffer.allocate(4).putFloat(shipdata[j].dy).order(ByteOrder.BIG_ENDIAN).array();
                                                     // buffer[off] = bytes[0];
                                                     // buffer[off+1] = bytes[1];
                                                     // buffer[off+2] = bytes[2];
                                                     // buffer[off+3] = bytes[3];
                                                     // off+=4;
                                                     // bytes =
                                                     // ByteBuffer.allocate(4).putFloat(shipdata[j].dz).order(ByteOrder.BIG_ENDIAN).array();
                                                     // buffer[off] = bytes[0];
                                                     // buffer[off+1] = bytes[1];
                                                     // buffer[off+2] = bytes[2];
                                                     // buffer[off+3] = bytes[3];
                                                     // off+=4;
                                                     /*
                                                         * bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].updx).order (ByteOrder.BIG_ENDIAN).array(); buffer[off] = bytes[0]; buffer[off+1] = bytes[1]; buffer[off+2] = bytes[2];
                                                         * buffer[off+3] = bytes[3]; off+=4; bytes = ByteBuffer.allocate( 4).putFloat(shipdata[j].updy).order(ByteOrder .BIG_ENDIAN).array(); buffer[off] = bytes[0]; buffer[off+1] = bytes[1];
                                                         * buffer[off+2] = bytes[2]; buffer[off+3] = bytes[3]; off+=4; bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].updz ).order(ByteOrder.BIG_ENDIAN).array(); buffer[off] = bytes[0];
                                                         * buffer[off+1] = bytes[1]; buffer[off+2] = bytes[2]; buffer[off+3] = bytes[3]; off+=4;
                                                         *
/
                                                     byte[] bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].x).order(ByteOrder.BIG_ENDIAN).array();
                                                     buffer[off] = bytes[0];
                                                     buffer[off + 1] = bytes[1];
                                                     buffer[off + 2] = bytes[2];
                                                     buffer[off + 3] = bytes[3];
                                                     off += 4;
                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].y).order(ByteOrder.BIG_ENDIAN).array();
                                                     buffer[off] = bytes[0];
                                                     buffer[off + 1] = bytes[1];
                                                     buffer[off + 2] = bytes[2];
                                                     buffer[off + 3] = bytes[3];
                                                     off += 4;
                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].z).order(ByteOrder.BIG_ENDIAN).array();
                                                     buffer[off] = bytes[0];
                                                     buffer[off + 1] = bytes[1];
                                                     buffer[off + 2] = bytes[2];
                                                     buffer[off + 3] = bytes[3];
                                                     off += 4;
                                                     bytes = ByteBuffer.allocate(2).putShort(shipdata[j].hp).order(ByteOrder.BIG_ENDIAN).array();
                                                     buffer[off] = bytes[0];
                                                     buffer[off + 1] = bytes[1];
                                                     off += 2;
                                                     buffer[off] = (byte) shipdata[j].firingframe.length;
                                                     off++;
                                                     for (int k = 0; k < shipdata[j].firingframe.length; k++)
                                                     {
                                                                     buffer[off] = shipdata[j].firingframe[k];
                                                                     off++;
                                                                     if (shipdata[j].firingframe[k] > 0)
                                                                     {
                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fx[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fy[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fz[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fux[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fuy[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                                     bytes = ByteBuffer.allocate(4).putFloat(shipdata[j].fuz[k]).order(ByteOrder.BIG_ENDIAN).array();
                                                                                     buffer[off] = bytes[0];
                                                                                     buffer[off + 1] = bytes[1];
                                                                                     buffer[off + 2] = bytes[2];
                                                                                     buffer[off + 3] = bytes[3];
                                                                                     off += 4;

                                                                     }
                                                     }
                                     }
                     }
                     if (off - offset != getsizeinbytes())
                     {
                                     System.out.println("Error!!!!!");

                     }
                     return off;
     }

     public int getsizeinbytes()
     {
                     int size = 0;
                     size += shipids.length * 3;
                     for (int i = 0; i < shipdata.length; i++)
                     {
                                     size += 15 + shipdata[i].firingframe.length;
                                     for (int j = 0; j < shipdata[i].firingframe.length; j++)
                                     {
                                                     if (shipdata[i].firingframe[j] > 0)
                                                     {
                                                                     size += 24;
                                                     }
                                     }
                     }
                     return size;
     }

}