PurpleKingdomGames / indigo

An FP game engine for Scala.
https://indigoengine.io/
MIT License
648 stars 60 forks source link

Provide Circe codec instances of common Indigo types #736

Open davesmith00000 opened 6 months ago

davesmith00000 commented 6 months ago

Perhaps they could be generated and live in the JSON module to keep all the circe stuff together? Be nice if you could do:

import indigo.circe.given (you have to import given's explicitly)

davesmith00000 commented 6 months ago

Reference from a recent project:

import indigo.*

import io.circe.*
import io.circe.syntax.*
import io.circe.generic.semiauto.*

object CirceIndigoInstances:

  given Decoder[Size] = deriveDecoder
  given Encoder[Size] = deriveEncoder

  given Decoder[Point] = deriveDecoder
  given Encoder[Point] = deriveEncoder

  given Decoder[Rectangle] = deriveDecoder
  given Encoder[Rectangle] = deriveEncoder

  given [A](using Decoder[A]): Decoder[Batch[A]] =
    Decoder.instance(_.as[List[A]].map(Batch.fromList))

  given [A](using Encoder[A]): Encoder[Batch[A]] =
    Encoder.instance(_.toList.asJson)