Skip to content

Commit

Permalink
Create LookingDirectionVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Nov 24, 2024
1 parent e6b2871 commit 456ebb6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.typewritermc.core.utils.point

import org.intellij.lang.annotations.Language
import kotlin.math.sqrt

data class Vector(
Expand All @@ -11,6 +12,9 @@ data class Vector(

companion object {
val ZERO = Vector(0.0, 0.0, 0.0)
val UNIT = Vector(1.0, 1.0, 1.0)
@Language("JSON")
const val UNIT_JSON = "{\"x\": 1.0, \"y\": 1.0, \"z\": 1.0}"
const val EPSILON: Double = 0.000001
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.typewritermc.basic.entries.variables

import com.typewritermc.core.books.pages.Colors
import com.typewritermc.core.extension.annotations.*
import com.typewritermc.core.utils.point.Vector
import com.typewritermc.engine.paper.entry.entries.VarContext
import com.typewritermc.engine.paper.entry.entries.VariableEntry
import com.typewritermc.engine.paper.entry.entries.getData
import com.typewritermc.engine.paper.loader.serializers.VectorSerializer
import com.typewritermc.engine.paper.utils.toVector
import kotlin.reflect.cast

@Entry(
"looking_direction_variable",
"A variable that returns the direction the player is looking at",
Colors.GREEN,
"mingcute:look-left-fill"
)
@GenericConstraint(Vector::class)
@VariableData(LookingDirectionVariableData::class)
class LookingDirectionVariable(
override val id: String = "",
override val name: String = "",
@Help("The vector to add to the direction")
val vector: Vector = Vector.ZERO,
@Default("1.0")
val scalar: Double = 1.0,
) : VariableEntry {
override fun <T : Any> get(context: VarContext<T>): T {
val dataScalar = context.getData<LookingDirectionVariableData>()?.scalar ?: 1.0
val scalar = this.scalar * dataScalar

val dataVector = context.getData<LookingDirectionVariableData>()?.vector ?: Vector.ZERO
val vector = this.vector.mul(dataVector)

val player = context.player
val lookDirection = player.location.direction.toVector()
return context.klass.cast(lookDirection.add(vector).mul(scalar))
}
}

data class LookingDirectionVariableData(
@Help("The vector to add to the direction")
val vector: Vector = Vector.ZERO,
@Default("1.0")
val scalar: Double = 1.0,
)

0 comments on commit 456ebb6

Please sign in to comment.