package ru.dbotthepony.kstarbound.world.entities.tile

import ru.dbotthepony.kommons.io.StreamCodec
import ru.dbotthepony.kommons.io.readSignedVarInt
import ru.dbotthepony.kommons.io.readVarInt
import ru.dbotthepony.kommons.io.writeSignedVarInt
import ru.dbotthepony.kommons.io.writeVarInt
import ru.dbotthepony.kstarbound.math.vector.Vector2i
import java.io.DataInputStream
import java.io.DataOutputStream

data class WireConnection(val entityLocation: Vector2i, val index: Int = 0) {
	constructor(stream: DataInputStream) : this(Vector2i(stream.readSignedVarInt(), stream.readSignedVarInt()), stream.readVarInt())

	// ephemeral property for use inside WorldObjects
	var otherEntity: WorldObject? = null

	fun write(stream: DataOutputStream) {
		stream.writeSignedVarInt(entityLocation.x)
		stream.writeSignedVarInt(entityLocation.y)
		stream.writeVarInt(index)
	}

	companion object {
		val CODEC = StreamCodec.Impl(::WireConnection, { a, b -> b.write(a) })
	}
}
