Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
feat: Track bounding box for entities using MEG models
Browse files Browse the repository at this point in the history
fix: Don't check bounding box when it isn't set in Geary
  • Loading branch information
0ffz committed Apr 3, 2024
1 parent ac61a68 commit 1a99e0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mineinabyss.mobzy.modelengine

import com.mineinabyss.geary.systems.query.Query
import com.mineinabyss.mobzy.modelengine.intializers.SetModelEngineModel

class MEGMobQuery : Query() {
val model by get<SetModelEngineModel>()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.mineinabyss.mobzy.modelengine

import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault
import com.mineinabyss.geary.components.relations.NoInherit
import com.mineinabyss.geary.helpers.fastForEach
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.builders.cachedQuery
import com.mineinabyss.geary.systems.query.CachedQueryRunner
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.plugin.listeners
import com.mineinabyss.idofront.plugin.service
Expand All @@ -13,15 +18,21 @@ import com.mineinabyss.mobzy.modelengine.intializers.ModelEngineWorldListener
import com.mineinabyss.mobzy.modelengine.intializers.createModelEngineListener
import com.mineinabyss.mobzy.modelengine.nametag.ModelEngineNameTagListener
import com.mineinabyss.mobzy.modelengine.riding.ModelEngineRidingListener
import com.ticxo.modelengine.api.ModelEngineAPI
import com.ticxo.modelengine.api.generator.ModelGenerator
import org.bukkit.util.BoundingBox
import org.bukkit.util.Vector

val mobzyModelEngine: ModelEngineSupport? by DI.observe()

interface ModelEngineSupport {
val animationController: AnimationController
val megMobs: CachedQueryRunner<MEGMobQuery>

companion object : GearyAddonWithDefault<ModelEngineSupport> {
override fun default() = object : ModelEngineSupport {
override val animationController = ModelEngineAnimationController()
override val megMobs = geary.cachedQuery(MEGMobQuery())
}

override fun ModelEngineSupport.install(): Unit = geary.run {
Expand All @@ -38,6 +49,27 @@ interface ModelEngineSupport {
)
service<AnimationController>(ModelEngineAnimationController())
}
ModelEngineAPI.getAPI().modelGenerator.queueTask(ModelGenerator.Phase.FINISHED) {
mobzy.plugin.launch {
updateBoundingBoxes()
}
}
}
}

fun updateBoundingBoxes() {
mobzy.logger.i("Reading bounding boxes from ModelEngine models...")
megMobs.mapWithEntity {
ModelEngineAPI.getBlueprint(model.modelId)
.mainHitbox
.createBoundingBox(Vector())
}.fastForEach { (boundingBox, entity) ->
runCatching {
entity.addRelation<NoInherit, BoundingBox>()
entity.set(boundingBox)
}.onFailure {
it.printStackTrace()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ fun GearyModule.spawnRequestListener() = listener(object : ListenerQuery() {
getSpawnInRadius(location, radius) ?: location
else location

val suitableLoc = ensureSuitableLocationOrNull(
chosenLoc,
(boundingBox?.clone()?.shift(chosenLoc)) ?: BoundingBox.of(chosenLoc.toVector(), 1.0, 2.0, 1.0)
) ?: return@repeat
val suitableLoc = if (boundingBox == null) {
chosenLoc
} else {
ensureSuitableLocationOrNull(
chosenLoc,
(boundingBox?.clone()?.shift(chosenLoc)) ?: BoundingBox.of(chosenLoc.toVector(), 1.0, 2.0, 1.0)
) ?: return@repeat
}

suitableLoc.spawnFromPrefab(prefab).onSuccess { spawnCount++ }
}
Expand Down

0 comments on commit 1a99e0c

Please sign in to comment.