Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/dev/stillya/vpet/game/ecs/SpatialGrid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -18,8 +18,8 @@ class SpatialGrid(private val cellSize: Int = 4) {
}

fun query(at: Transform, bounds: AABB): Set<EntityID> {
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/stillya/vpet/game/ecs/World.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 &&
Expand Down
24 changes: 12 additions & 12 deletions src/main/kotlin/dev/stillya/vpet/game/physics/PhysicsBody.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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..<left + collider.width
}

Expand All @@ -61,7 +61,7 @@ class PhysicsBody(val collider: AABB) {
t = xResult.first
v = xResult.second

val bodyLineBeforeY = floor(t.y).toInt() - 1
val bodyLineBeforeY = t.y.toTileInt() - 1
val yResult = moveAndResolveY(t, v, tileMap, dt)
t = yResult.transform
v = yResult.velocity
Expand All @@ -82,12 +82,12 @@ class PhysicsBody(val collider: AABB) {
): Pair<Transform, Velocity> {
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) }
Expand Down Expand Up @@ -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)
Expand All @@ -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)) {
Expand All @@ -171,10 +171,10 @@ class PhysicsBody(val collider: AABB) {
prevBodyLine: Int,
tileMap: VirtualTileMap
): Pair<Transform, Velocity> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/dev/stillya/vpet/game/utils/MathExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.stillya.vpet.game.utils

import kotlin.math.floor

fun Float.toTileInt(): Int = floor(this).toInt()
Loading