package ru.dbotthepony.kstarbound.math

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import ru.dbotthepony.kstarbound.math.vector.Vector4f

object Vector4fTypeAdapter : TypeAdapter<Vector4f>() {
	override fun write(out: JsonWriter, value: Vector4f) {
		`out`.beginArray()
		`out`.value(value.x)
		`out`.value(value.y)
		`out`.value(value.z)
		`out`.value(value.w)
		`out`.endArray()
	}

	override fun read(`in`: JsonReader): Vector4f {
		`in`.beginArray()

		val x = `in`.nextDouble().toFloat()
		val y = `in`.nextDouble().toFloat()
		val z = `in`.nextDouble().toFloat()
		val w = `in`.nextDouble().toFloat()

		`in`.endArray()

		return Vector4f(x, y, z, w)
	}
}
