CyberTank

Counters for the Defenders

Now we’re going to look at the Image and ImageView setup for the counters that represent the defending units.

The Counters

CheckPoint Alpha

CheckPoint Alpha

Heavy Tank

Heavy Tank

Light Tank

Light Tank

Missile Tank

Missile Tank

Ground Effect Vehicle (GEV)

GEV

Howitzer

Howitzer

Infantry (Single Unit)

Single Infantry

Infantry (Two Units)

Double Infantry

Infantry are the only units where two are allowed to occupy the same Tile, so we need to have a counter for each situation.

Linking the Images to Data

We need a class to be able to treat these Images more like data. Since it’s a set of discrete values, an Enum makes sense:

enum class CounterType(val label: String, private val resourceName: String) {
   NONE("None", ""),
   HVYTK("Heavy Tank", "HeavyTank"),
   LGHTK("Light Tank", "LightTank"),
   HWZR("Howitzer", "Howitzer"),
   INFTY1("Infantry 1", "Infantry1"),
   INFTY2("Infantry 2", "Infantry2"),
   GEV("Gev", "Gev"),
   MSLTK("Missile Tank", "MslTank"),
   CPA("CP Alpha", "CpAlpha"),
   CT1("CyberTank1", "cybertank1");

   companion object {
      private val images = mutableMapOf<CounterType, Image?>()

      private fun createImage(resourceName: String) =
         CounterType::class.java.getResource("/counters/black/${resourceName}.png")?.toExternalForm()?.let { Image(it) }

      private fun getImage(counterType: CounterType): Image? {
         if (!images.contains(counterType)) {
            images[counterType] = createImage(counterType.resourceName)
            println("Creating image: ${counterType.name} -> ${images[counterType]}")
         }
         return images[counterType]
      }

      fun byLabel(label: String) = values().find { it.label == label } ?: NONE
   }

   val image: Image? by lazy { getImage(this) }
}

Now we can refer to the counters by their Enum value, and pull the image via the getImage() function.

For now, that’s all we need.