Skip to content

Commit

Permalink
Improve overlap logic when two sections are not actually across a tra…
Browse files Browse the repository at this point in the history
…nsition and call transition event when moving between them
  • Loading branch information
0ffz committed Nov 5, 2021
1 parent 8be98e1 commit c5637a0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ object MovementHandler {
private val sectionCheckers = listOf(ConfigSectionChecker)

fun handleMovement(player: Player, from: Location, to: Location) {

if (sectionCheckers.any { it.inSection(player) })
if (sectionCheckers.any { it.inSection(player) }) {
sectionCheckers.firstNotNullOfOrNull { it.checkForTransition(player, from, to) }?.let {
with(getTeleportHandler(player, it)) {
if (this.isValidTeleport()) {
Expand All @@ -28,7 +27,8 @@ object MovementHandler {
this.handleTeleport()
}
}
} else {
}
} else {
applyOutOfBoundsDamage(player)
}
}
Expand Down Expand Up @@ -59,6 +59,11 @@ object MovementHandler {
player: Player,
sectionTransition: SectionTransition
): TeleportHandler {
if (sectionTransition.teleportUnnecessary) return object : TeleportHandler {
override fun handleTeleport() {}

override fun isValidTeleport() = true
}
if (player.gameMode != GameMode.SPECTATOR && sectionTransition.to.block.isSolid) {
return if (sectionTransition.kind == TransitionKind.ASCEND) {
UndoMovementInvalidTeleportHandler(
Expand All @@ -77,4 +82,4 @@ object MovementHandler {

return TransitionTeleportHandler(player, sectionTransition.from, sectionTransition.to);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ object ConfigSectionChecker : SectionChecker {
from: Location,
to: Location
): SectionTransition? {
return to.takeIf { to.inSectionTransition }?.correspondingLocation?.let {
SectionTransition(
from,
it,
from.section!!, // If inSectionTransition, must be non-null
it.section!!,
if (to.y < from.y) TransitionKind.DESCEND else TransitionKind.ASCEND
)
}
val fromSection = from.section ?: return null
val toSection = to.section ?: return null
val corrLoc = when {
to.inSectionTransition -> to.correspondingLocation
fromSection != toSection -> to
else -> null
} ?: return null

return SectionTransition(
from,
corrLoc,
fromSection,
corrLoc.section!!, // If inSectionTransition, must be non-null
if (to.y < from.y) TransitionKind.DESCEND else TransitionKind.ASCEND,
teleportUnnecessary = fromSection != toSection
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ data class SectionTransition(
val to: Location,
val fromSection: Section,
val toSection: Section,
val kind: TransitionKind
val kind: TransitionKind,
val teleportUnnecessary: Boolean,
)

enum class TransitionKind {
Expand All @@ -26,4 +27,4 @@ internal fun SectionTransition.toEvent(player: Player): PlayerChangeSectionEvent
} else {
PlayerDescendEvent(player, fromSection, toSection)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import kotlin.math.min
*/
@Serializable(with = RegionSerializer::class)
class Region(val a: CubePoint, val b: CubePoint) {
val min = CubePoint(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z))
val max = CubePoint(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z))

constructor(ax: Int, ay: Int, az: Int, bx: Int, by: Int, bz: Int) : this(
CubePoint(ax, ay, az),
CubePoint(bx, by, bz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ fun Location.sharedBetween(section: Section, otherSection: Section): Boolean {

fun Section.overlapWith(other: Section): Int? {
if (!isAdjacentTo(other)) return null
if(min(this.region.max.y, other.region.max.y) <= max(this.region.min.y, other.region.min.y)) return null
// We decide which two points we are translating between.
val (locA, locB) = when (isOnTopOf(other)) {
true -> referenceBottom to other.referenceTop
false -> referenceTop to other.referenceBottom
}
val yA = locA.blockY
val yB = locB.blockY
return (world.maxHeight - max(yA, yB)) + (min(yA, yB) - world.minHeight)
return max(this.region.max.y, other.region.max.y) - max(yA, yB) +
(min(yA, yB) - min(this.region.min.y, other.region.min.y))
}

fun Section.isOnTopOf(other: Section) = key == other.aboveKey
Expand Down

0 comments on commit c5637a0

Please sign in to comment.