[Editor] Make MapInitializer generator implementations accepts additional arguments

This commit is contained in:
2023-11-16 10:56:51 +01:00
parent 40faccac5f
commit 14dd99f79a
2 changed files with 16 additions and 5 deletions

View File

@@ -1,9 +1,12 @@
package com.bartlomiejpluta.base.editor.code.build.generator
import com.bartlomiejpluta.base.api.context.Context
import com.bartlomiejpluta.base.api.context.ContextHolder
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
import com.bartlomiejpluta.base.editor.map.model.layer.ObjectLayer
import com.bartlomiejpluta.base.editor.map.model.map.GameMap
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import com.bartlomiejpluta.base.editor.project.model.Project
import com.bartlomiejpluta.base.internal.map.MapInitializer
import com.squareup.javapoet.*
import org.springframework.beans.factory.annotation.Autowired
@@ -27,11 +30,11 @@ class MapObjectsCodeGenerator : CodeGenerator {
projectContext.project?.let { project ->
project.maps
.map { it to projectContext.loadMap(it.uid) }
.forEach { generateMapInitializer(project.buildGeneratedCodeDirectory, it.first, it.second) }
.forEach { generateMapInitializer(project, project.buildGeneratedCodeDirectory, it.first, it.second) }
}
}
private fun generateMapInitializer(directory: File, asset: GameMapAsset, map: GameMap) {
private fun generateMapInitializer(project: Project, directory: File, asset: GameMapAsset, map: GameMap) {
val packageName = "com.bartlomiejpluta.base.generated.map"
val className = ClassName.get(
packageName,
@@ -50,6 +53,8 @@ class MapObjectsCodeGenerator : CodeGenerator {
.addModifiers(Modifier.FINAL, Modifier.PUBLIC)
.addAnnotation(annotation)
val runnerType = className(project.runner)
map.layers.forEachIndexed { index, layer ->
if (layer is ObjectLayer) {
layer.objects.forEach {
@@ -60,6 +65,8 @@ class MapObjectsCodeGenerator : CodeGenerator {
.addParameter(TypeName.INT, "x", Modifier.FINAL)
.addParameter(TypeName.INT, "y", Modifier.FINAL)
.addParameter(MAP_PIN_TYPE, "here", Modifier.FINAL)
.addParameter(runnerType, "runner", Modifier.FINAL)
.addParameter(Context::class.java, "context", Modifier.FINAL)
.addCode(it.code.replace("\$", "\$\$"))
.build()
.let(generatedClass::addMethod)
@@ -69,12 +76,15 @@ class MapObjectsCodeGenerator : CodeGenerator {
val initializeMethod = MethodSpec.methodBuilder("initialize")
.addAnnotation(Override::class.java)
.addParameter(Context::class.java, "context", Modifier.FINAL)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
initializeMethod.addStatement("var runner = (\$T) context.getGameRunner()", runnerType)
map.layers.forEachIndexed { index, layer ->
if (layer is ObjectLayer) {
layer.objects.forEach {
initializeMethod.addStatement("_layer${index}_${it.x}x${it.y}(map, map.getObjectLayer(${index}), ${it.x}, ${it.y}, \$T.builder().map(\"${map.uid}\").layer($index).x(${it.x}).y(${it.y}).build())", MAP_PIN_TYPE)
initializeMethod.addStatement("_layer${index}_${it.x}x${it.y}(map, map.getObjectLayer(${index}), ${it.x}, ${it.y}, \$T.builder().map(\"${map.uid}\").layer($index).x(${it.x}).y(${it.y}).build(), runner, context)", MAP_PIN_TYPE)
}
}
}

View File

@@ -84,13 +84,14 @@ class ObjectPaintingTrace(
get() = """
/*
* Following final parameters are available to use:
* this: ? extends ${className(map.handler)} - an object of ${className(map.handler)}'s generated subclass
* so has an access to all members except the private ones
* here: MapPin - the composite object containing current map UID,
* layer's index and x,y coordinates of the current tile
* x: int - the x coordinate of the current tile
* y: int - the y coordinate of the current tile
* layer: ObjectLayer - current object layer
* map: GameMap - current map
* handler: ${className(map.handler)} - current map handler
* map: GameMap - current map
* runner: ${className(projectContext.project?.runner)} - the game runner of the project
* context: Context - the game context
*/