Skip to content

Instantly share code, notes, and snippets.

@pablitar
Created February 1, 2019 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablitar/2b873f14248afb227fccdb2780616d4c to your computer and use it in GitHub Desktop.
Save pablitar/2b873f14248afb227fccdb2780616d4c to your computer and use it in GitHub Desktop.
class GiantClaw(_position: Vector2, val world: World)(implicit resources: Resources) extends Enemy with PositionedWithTransform with SingleAppearance {
class ClawHydraulics(val flipped: Boolean = false) extends RotableJoint(-0.30f, 0.75f) {
val edge = new ClawEdge(this)
def update(delta: Float): Unit = {
if (Gdx.input.isKeyPressed(Keys.R)) {
rotationFactor += delta
}
if (Gdx.input.isKeyPressed(Keys.T)) {
rotationFactor -= delta
}
updateTransform()
edge.update(delta)
}
override val localPosition: Vector2 = new Vector2(138, 67)
override def resetTransform: Affine2 = super.resetTransform.scale(1, if (flipped) 1 else -1)
override def parentTransform: Affine2 = GiantClaw.this.transform
}
class ClawEdge(val parent: ClawHydraulics) extends RotableJoint(-0.15f, 0.6f) {
override val localPosition = new Vector2(121, 69)
def update(delta: Float): Unit = {
if (Gdx.input.isKeyPressed(Keys.U)) {
rotationFactor += delta
}
if (Gdx.input.isKeyPressed(Keys.Y)) {
rotationFactor -= delta
}
updateTransform()
}
override def parentTransform: Affine2 = parent.transform
}
object topHydraulics extends ScalableMovableJoint(0.7f, 1.02f, -15, 5) {
object edge extends ScalableMovableJoint(0.9f, 1.05f, -5, 0) {
def update(delta: Float): Unit = {
if (Gdx.input.isKeyPressed(Keys.Y)) {
factor += delta
}
if (Gdx.input.isKeyPressed(Keys.U)) {
factor -= delta
}
updateTransform()
}
override val localPosition: Vector2 = new Vector2(110, 0)
override def parentTransform: Affine2 = topHydraulics.transform
}
def update(delta: Float): Unit = {
if (Gdx.input.isKeyPressed(Keys.T)) {
factor += delta
}
if (Gdx.input.isKeyPressed(Keys.R)) {
factor -= delta
}
edge.update(delta: Float)
updateTransform()
}
override val localPosition: Vector2 = new Vector2(143, 0)
override def parentTransform: Affine2 = GiantClaw.this.transform
}
lazy val leftHydraulics = new ClawHydraulics()
lazy val rightHydraulics = new ClawHydraulics(true)
this.translate(_position)
override def initAppearance(): Appearance = resources.giantClawAppearance(this)
override def reactToCollisionWithHead(aCat: Cat, collision: CollisionUtils.Collision): Unit = {}
override def bounds: Seq[Shape2D] = Seq(appearance.bounds.shape)
override def score: Int = 5000
override def update(delta: Float): Unit = {
leftHydraulics.update(delta)
rightHydraulics.update(delta)
topHydraulics.update(delta)
}
}
abstract class RotableJoint(minAngle: Float, maxAngle: Float, initialRotationFactor: Float = 0.5f) extends PositionedWithParent {
private var _localRotation: Float = 0f
override def localRotation: Float = _localRotation
def localRotation_=(value: Float): Unit = _localRotation = value
private var _rotationFactor = 0.5f
def updateRotation(): Unit = {
localRotation = MathUtils.lerp(minAngle, maxAngle, rotationFactor)
}
def rotationFactor_=(value: Float): Unit = {
_rotationFactor = value.max(0).min(1)
updateRotation()
}
def rotationFactor = _rotationFactor
rotationFactor = initialRotationFactor
}
abstract class ScalableMovableJoint(
minScale: Float, maxScale: Float, minOffset: Float, maxOffset: Float, initialFactor: Float = 0.5f) extends PositionedWithParent {
//Hay un poco de código repetido. Ver si vale la pena extraer a algo estilo "interpolated value"
private var _factor: Float = initialFactor
override val localScale: Vector2 = new Vector2(1, 1)
var offset: Float = 0
def updateScale(): Unit = {
localScale.x = MathUtils.lerp(minScale, maxScale, factor)
}
def updateOffset(): Unit = {
offset = MathUtils.lerp(minOffset, maxOffset, factor)
}
override def updateTransform(): Unit = {
super.updateTransform()
this.transform.preTranslate(offset, 0)
}
def factor_=(value: Float): Unit = {
_factor = value.max(0).min(1)
updateScale()
updateOffset()
}
def factor = _factor
factor = initialFactor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment