diff --git a/gradle.properties b/gradle.properties index 02e125a..a2c0521 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = dev.stillya.vpet pluginName = vpet pluginRepositoryUrl = https://github.com/stillya/vpet # SemVer format -> https://semver.org -pluginVersion = 0.2.2 +pluginVersion = 0.2.3 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 242 diff --git a/src/main/kotlin/dev/stillya/vpet/game/ecs/SpatialGrid.kt b/src/main/kotlin/dev/stillya/vpet/game/ecs/SpatialGrid.kt index 206f879..f94c196 100644 --- a/src/main/kotlin/dev/stillya/vpet/game/ecs/SpatialGrid.kt +++ b/src/main/kotlin/dev/stillya/vpet/game/ecs/SpatialGrid.kt @@ -2,7 +2,7 @@ package dev.stillya.vpet.game.ecs import dev.stillya.vpet.game.ecs.components.Transform import dev.stillya.vpet.game.physics.AABB -import kotlin.math.floor +import dev.stillya.vpet.game.utils.toTileInt class SpatialGrid(private val cellSize: Int = 4) { @@ -18,8 +18,8 @@ class SpatialGrid(private val cellSize: Int = 4) { } fun query(at: Transform, bounds: AABB): Set { - val x = floor(at.x).toInt() - val y = floor(at.y).toInt() - (bounds.height - 1) + val x = at.x.toTileInt() + val y = at.y.toTileInt() - (bounds.height - 1) val minCx = x / cellSize - (if (x < 0 && x % cellSize != 0) 1 else 0) val maxCx = (x + bounds.width - 1) / cellSize @@ -36,8 +36,8 @@ class SpatialGrid(private val cellSize: Int = 4) { } private fun insert(id: EntityID, t: Transform, c: AABB) { - val x = floor(t.x).toInt() - val y = floor(t.y).toInt() - (c.height - 1) + val x = t.x.toTileInt() + val y = t.y.toTileInt() - (c.height - 1) val minCx = x / cellSize - (if (x < 0 && x % cellSize != 0) 1 else 0) val maxCx = (x + c.width - 1) / cellSize diff --git a/src/main/kotlin/dev/stillya/vpet/game/ecs/World.kt b/src/main/kotlin/dev/stillya/vpet/game/ecs/World.kt index 96c177c..f653965 100644 --- a/src/main/kotlin/dev/stillya/vpet/game/ecs/World.kt +++ b/src/main/kotlin/dev/stillya/vpet/game/ecs/World.kt @@ -5,7 +5,7 @@ import dev.stillya.vpet.game.ecs.components.PhysicsState import dev.stillya.vpet.game.ecs.components.SpriteState import dev.stillya.vpet.game.ecs.components.Transform import dev.stillya.vpet.game.ecs.components.Velocity -import kotlin.math.floor +import dev.stillya.vpet.game.utils.toTileInt data class World( val registry: EntityRegistry = EntityRegistry(), @@ -17,7 +17,7 @@ data class World( val isOnGround: Boolean get() = playerPhysicsState().isOnGround val sprite: SpriteState get() = playerSprite() val phase: GamePhase get() = playerPhaseState().phase - val displayLine: Int get() = floor(transform.y).toInt() + val displayLine: Int get() = transform.y.toTileInt() } fun World.playerTransform(): Transform = diff --git a/src/main/kotlin/dev/stillya/vpet/game/ecs/systems/CollisionSystem.kt b/src/main/kotlin/dev/stillya/vpet/game/ecs/systems/CollisionSystem.kt index 018e190..909fc2e 100644 --- a/src/main/kotlin/dev/stillya/vpet/game/ecs/systems/CollisionSystem.kt +++ b/src/main/kotlin/dev/stillya/vpet/game/ecs/systems/CollisionSystem.kt @@ -6,7 +6,7 @@ import dev.stillya.vpet.game.ecs.SpatialGrid import dev.stillya.vpet.game.ecs.components.Collectible import dev.stillya.vpet.game.ecs.components.Transform import dev.stillya.vpet.game.physics.AABB -import kotlin.math.floor +import dev.stillya.vpet.game.utils.toTileInt object CollisionSystem { @@ -31,10 +31,10 @@ object CollisionSystem { } private fun aabbsOverlap(aPos: Transform, a: AABB, bPos: Transform, b: AABB): Boolean { - val ax = floor(aPos.x).toInt() - val ay = floor(aPos.y).toInt() - (a.height - 1) - val bx = floor(bPos.x).toInt() - val by = floor(bPos.y).toInt() - (b.height - 1) + val ax = aPos.x.toTileInt() + val ay = aPos.y.toTileInt() - (a.height - 1) + val bx = bPos.x.toTileInt() + val by = bPos.y.toTileInt() - (b.height - 1) return ax < bx + b.width && ax + a.width > bx && diff --git a/src/main/kotlin/dev/stillya/vpet/game/physics/PhysicsBody.kt b/src/main/kotlin/dev/stillya/vpet/game/physics/PhysicsBody.kt index 067ea76..df6a90d 100644 --- a/src/main/kotlin/dev/stillya/vpet/game/physics/PhysicsBody.kt +++ b/src/main/kotlin/dev/stillya/vpet/game/physics/PhysicsBody.kt @@ -4,9 +4,9 @@ import dev.stillya.vpet.game.VirtualTileMap import dev.stillya.vpet.game.ecs.Physics import dev.stillya.vpet.game.ecs.components.Transform import dev.stillya.vpet.game.ecs.components.Velocity +import dev.stillya.vpet.game.utils.toTileInt import kotlin.math.abs import kotlin.math.ceil -import kotlin.math.floor import kotlin.math.max import kotlin.math.sqrt @@ -43,7 +43,7 @@ class PhysicsBody(val collider: AABB) { } fun boundsAt(transform: Transform): IntRange { - val left = floor(transform.x).toInt() + val left = transform.x.toTileInt() return left.. { val newX = transform.x + velocity.x * dt - val prevCatLeft = floor(transform.x).toInt() + val prevCatLeft = transform.x.toTileInt() val prevCatRight = prevCatLeft + collider.width - 1 - val newCatLeft = floor(newX).toInt() + val newCatLeft = newX.toTileInt() val newCatRight = newCatLeft + collider.width - 1 - val bodyLine = floor(transform.y).toInt() - 1 + val bodyLine = transform.y.toTileInt() - 1 // Skip wall collision if cat already overlaps solid cells on body line val alreadyOverlaps = (prevCatLeft..prevCatRight).any { tileMap.isSolid(bodyLine, it) } @@ -125,12 +125,12 @@ class PhysicsBody(val collider: AABB) { val newLineY = transform.y + vy * dt - val catLeft = floor(transform.x).toInt() + val catLeft = transform.x.toTileInt() val catRight = catLeft + collider.width - 1 if (vy >= 0) { val sweepStart = ceil(transform.y - SWEEP_EPSILON).toInt() - val sweepEnd = floor(newLineY).toInt() + val sweepEnd = newLineY.toTileInt() if (sweepStart <= sweepEnd) { val landLine = tileMap.findGroundBelow(sweepStart, catLeft, catRight, sweepEnd) @@ -143,8 +143,8 @@ class PhysicsBody(val collider: AABB) { } } } else { - val oldBodyLine = floor(transform.y).toInt() - 1 - val newBodyLine = floor(newLineY).toInt() - 1 + val oldBodyLine = transform.y.toTileInt() - 1 + val newBodyLine = newLineY.toTileInt() - 1 for (line in (oldBodyLine - 1) downTo newBodyLine) { if (tileMap.hasCeilingAt(line, catLeft, catRight)) { @@ -171,10 +171,10 @@ class PhysicsBody(val collider: AABB) { prevBodyLine: Int, tileMap: VirtualTileMap ): Pair { - val newBodyLine = floor(transform.y).toInt() - 1 + val newBodyLine = transform.y.toTileInt() - 1 if (newBodyLine == prevBodyLine) return transform to velocity - val catLeft = floor(transform.x).toInt() + val catLeft = transform.x.toTileInt() val catRight = catLeft + collider.width - 1 var solidLeft = Int.MAX_VALUE diff --git a/src/main/kotlin/dev/stillya/vpet/game/rendering/RenderSystem.kt b/src/main/kotlin/dev/stillya/vpet/game/rendering/RenderSystem.kt index fecfc44..7f18685 100644 --- a/src/main/kotlin/dev/stillya/vpet/game/rendering/RenderSystem.kt +++ b/src/main/kotlin/dev/stillya/vpet/game/rendering/RenderSystem.kt @@ -9,6 +9,7 @@ import dev.stillya.vpet.game.ecs.World import dev.stillya.vpet.game.ecs.components.AnimationComponent import dev.stillya.vpet.game.ecs.components.Transform import dev.stillya.vpet.game.resources.AnimationCache +import dev.stillya.vpet.game.utils.toTileInt import java.awt.BasicStroke import java.awt.Color import java.awt.Font @@ -94,7 +95,7 @@ class RenderSystem( val frames = resource.frames if (frames.isEmpty()) continue - val coinLine = kotlin.math.floor(t.y).toInt() + val coinLine = t.y.toTileInt() val lineFrac = t.y - coinLine val baseY = editor.logicalPositionToXY(LogicalPosition(coinLine, 0)).y val pixelY = baseY + (lineFrac * lineHeight).toInt() diff --git a/src/main/kotlin/dev/stillya/vpet/game/utils/MathExtensions.kt b/src/main/kotlin/dev/stillya/vpet/game/utils/MathExtensions.kt new file mode 100644 index 0000000..88c768b --- /dev/null +++ b/src/main/kotlin/dev/stillya/vpet/game/utils/MathExtensions.kt @@ -0,0 +1,5 @@ +package dev.stillya.vpet.game.utils + +import kotlin.math.floor + +fun Float.toTileInt(): Int = floor(this).toInt()