import { BinaryClassSerializationAdapter } from "../../ecs/storage/binary/BinaryClassSerializationAdapter.js";
import GridObstacle from "./GridObstacle.js";

export class GridObstacleSerializationAdapter extends BinaryClassSerializationAdapter {

    klass = GridObstacle;
    version = 0;

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {GridObstacle} value
     */
    serialize(buffer, value) {

        const w = value.size.x;
        const h = value.size.y;

        buffer.writeUint16(w);
        buffer.writeUint16(h);

        buffer.writeBytes(value.data, 0, w * h);
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {GridObstacle} value
     */
    deserialize(buffer, value) {

        const x = buffer.readUint16();
        const y = buffer.readUint16();

        value.size.set(x, y);

        const length = x * y;

        value.data = new Uint8Array(length);

        buffer.readBytes(value.data, 0, length);
    }
}
