FIX: left/right manoeuvre with gamepad

Former-commit-id: cf052b05e445a1b3b180f92c0a4185e9bf49071a
Former-commit-id: 8a05c5f6667ad0511f1b605ea7f114ab3b8ac527
This commit is contained in:
Song Minjae
2016-07-30 12:27:39 +09:00
parent fb11d5c9c2
commit 30a0464820
2 changed files with 115 additions and 102 deletions

View File

@@ -211,7 +211,8 @@ open class ActorWithBody : Actor(), Visible {
var ccdCollided = false var ccdCollided = false
var isWalking = false var isWalkingH = false
var isWalkingV = false
init { init {
// some initialiser goes here... // some initialiser goes here...
@@ -613,7 +614,7 @@ open class ActorWithBody : Actor(), Visible {
} }
private fun isCollidingInternal(txStart: Int, tyStart: Int, txEnd: Int, tyEnd: Int): Boolean { private fun isCollidingInternal(txStart: Int, tyStart: Int, txEnd: Int, tyEnd: Int): Boolean {
/*for (y in tyStart..tyEnd) { for (y in tyStart..tyEnd) {
for (x in txStart..txEnd) { for (x in txStart..txEnd) {
val tile = world.getTileFromTerrain(x, y) val tile = world.getTileFromTerrain(x, y)
if (TilePropCodex.getProp(tile).isSolid) if (TilePropCodex.getProp(tile).isSolid)
@@ -621,8 +622,10 @@ open class ActorWithBody : Actor(), Visible {
} }
} }
return false*/ return false
return if (tyEnd < 348) false else true
// test code
//return if (tyEnd < 348) false else true
} }
private fun getContactingAreaFluid(side: Int, translateX: Int = 0, translateY: Int = 0): Int { private fun getContactingAreaFluid(side: Int, translateX: Int = 0, translateY: Int = 0): Int {
@@ -753,7 +756,7 @@ open class ActorWithBody : Actor(), Visible {
get() { get() {
var friction = 0 var friction = 0
val frictionCalcHitbox = val frictionCalcHitbox =
if (!isWalking) if (!isWalkingH)
Hitbox(nextHitbox.posX, nextHitbox.posY, Hitbox(nextHitbox.posX, nextHitbox.posY,
nextHitbox.width + 2.0, nextHitbox.height + 2.0) nextHitbox.width + 2.0, nextHitbox.height + 2.0)
// when not walking, enlarge the hitbox for calculation so that // when not walking, enlarge the hitbox for calculation so that
@@ -929,16 +932,19 @@ open class ActorWithBody : Actor(), Visible {
if (this > 0 && this > limit) limit if (this > 0 && this > limit) limit
else if (this < 0 && this < -limit) -limit else if (this < 0 && this < -limit) -limit
else this else this
fun Double.floorSpecial(): Int { fun absMax(left: Double, right: Double): Double {
val threshold = 1.1 / TSIZE.toDouble() if (left > 0 && right > 0)
// the idea is 321.0625 would rounded to 321, 320.9375 would rounded to 321, if (left > right) return left
// and regular flooring for otherwise. else return right
if (this % TSIZE.toDouble() <= threshold) // case: 321.0625 else if (left < 0 && right < 0)
return this.floorInt() if (left < right) return left
else if (1.0 - this.mod(TSIZE.toDouble()) <= threshold) // case: 320.9375 else return right
return this.floorInt() + 1 else {
else val absL = left.abs()
return this.floorInt() val absR = right.abs()
if (absL > absR) return left
else return right
}
} }
private fun assertInit() { private fun assertInit() {

View File

@@ -1,5 +1,6 @@
package net.torvald.terrarum.gameactors package net.torvald.terrarum.gameactors
import com.jme3.math.FastMath
import net.torvald.terrarum.gameactors.faction.Faction import net.torvald.terrarum.gameactors.faction.Faction
import net.torvald.terrarum.gamecontroller.EnumKeyFunc import net.torvald.terrarum.gamecontroller.EnumKeyFunc
import net.torvald.terrarum.gamecontroller.KeyMap import net.torvald.terrarum.gamecontroller.KeyMap
@@ -55,7 +56,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
internal var noClip = false internal var noClip = false
@Transient private val AXIS_POSMAX = 1.0f @Transient private val AXIS_POSMAX = 1.0f
@Transient private val GAMEPAD_JUMP = 5 @Transient private val GAMEPAD_JUMP = 7
@Transient private val TSIZE = MapDrawer.TILE_SIZE @Transient private val TSIZE = MapDrawer.TILE_SIZE
@@ -78,6 +79,12 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
override val lightBoxList: List<Hitbox> override val lightBoxList: List<Hitbox>
get() = arrayOf(Hitbox(0.0, 0.0, hitbox.width, hitbox.height)).toList() // use getter; dimension of the player may change by time. get() = arrayOf(Hitbox(0.0, 0.0, hitbox.width, hitbox.height)).toList() // use getter; dimension of the player may change by time.
var gamepad: Controller? = Controllers.getController(0)
var axisX = 0f
var axisY = 0f
var axisRX = 0f
var axisRY = 0f
companion object { companion object {
@Transient internal const val ACCEL_MULT_IN_FLIGHT: Double = 0.21 @Transient internal const val ACCEL_MULT_IN_FLIGHT: Double = 0.21
@Transient internal const val WALK_ACCEL_BASE: Double = 0.67 @Transient internal const val WALK_ACCEL_BASE: Double = 0.67
@@ -137,19 +144,19 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
*/ */
private fun walkHorizontal(left: Boolean, absAxisVal: Float) { private fun walkHorizontal(left: Boolean, absAxisVal: Float) {
if (true) {//(!walledLeft && left) || (!walledRight && !left)) { if (true) {//(!walledLeft && left) || (!walledRight && !left)) {
readonly_totalX = //veloX + readonly_totalX =
/*actorValue.getAsDouble(AVKey.ACCEL)!! * absMax( // keyboard
actorValue.getAsDouble(AVKey.ACCELMULT)!! * actorValue.getAsDouble(AVKey.ACCEL)!! *
Math.sqrt(scale) * actorValue.getAsDouble(AVKey.ACCELMULT)!! *
applyAccelRealism(walkPowerCounter) * Math.sqrt(scale) *
(if (left) -1 else 1).toFloat() * applyVelo(walkCounterX) *
absAxisVal*/ (if (left) -1f else 1f)
actorValue.getAsDouble(AVKey.ACCEL)!! * , // gamepad
actorValue.getAsDouble(AVKey.ACCELMULT)!! * actorValue.getAsDouble(AVKey.ACCEL)!! *
Math.sqrt(scale) * actorValue.getAsDouble(AVKey.ACCELMULT)!! *
applyVelo(walkCounterX) * Math.sqrt(scale) *
(if (left) -1 else 1).toFloat() * (if (left) -1f else 1f) * absAxisVal
absAxisVal )
//applyForce(Vector2(readonly_totalX, 0.0)) //applyForce(Vector2(readonly_totalX, 0.0))
walkX += readonly_totalX walkX += readonly_totalX
@@ -163,8 +170,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
else else
walkHeading = RIGHT walkHeading = RIGHT
// println("$walkCounter: ${readonly_totalX}") isWalkingH = true
isWalking = true
} }
} }
@@ -176,19 +182,26 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
*/ */
private fun walkVertical(up: Boolean, absAxisVal: Float) { private fun walkVertical(up: Boolean, absAxisVal: Float) {
readonly_totalY = readonly_totalY =
actorValue.getAsDouble(AVKey.ACCEL)!! * absMax( // keyboard
actorValue.getAsDouble(AVKey.ACCELMULT)!! * actorValue.getAsDouble(AVKey.ACCEL)!! *
Math.sqrt(scale) * actorValue.getAsDouble(AVKey.ACCELMULT)!! *
applyVelo(walkCounterY) * Math.sqrt(scale) *
(if (up) -1 else 1).toFloat() * applyVelo(walkCounterY) *
absAxisVal (if (up) -1f else 1f)
, // gamepad
actorValue.getAsDouble(AVKey.ACCEL)!! *
actorValue.getAsDouble(AVKey.ACCELMULT)!! *
Math.sqrt(scale) *
applyVelo(walkCounterY) *
(if (up) -1f else 1f) * absAxisVal
)
walkY += readonly_totalY walkY += readonly_totalY
walkY = absClamp(walkY, actorValue.getAsDouble(AVKey.SPEED)!! * actorValue.getAsDouble(AVKey.SPEEDMULT)!!) walkY = absClamp(walkY, actorValue.getAsDouble(AVKey.SPEED)!! * actorValue.getAsDouble(AVKey.SPEEDMULT)!!)
//if (walkCounterY <= WALK_FRAMES_TO_MAX_ACCEL) walkCounterY += 1 walkCounterY += 1
isWalking = true isWalkingV = true
} }
private fun applyAccel(x: Int): Double { private fun applyAccel(x: Int): Double {
@@ -226,7 +239,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
//veloX = 0f //veloX = 0f
walkCounterX = 0 walkCounterX = 0
isWalking = false isWalkingH = false
} }
// stops; let the friction kick in by doing nothing to the velocity here // stops; let the friction kick in by doing nothing to the velocity here
@@ -253,7 +266,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
///veloY = 0f ///veloY = 0f
walkCounterY = 0 walkCounterY = 0
isWalking = false isWalkingV = false
} }
/** /**
@@ -299,18 +312,14 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
} }
override fun processInput(input: Input) { override fun processInput(input: Input) {
var gamepad: Controller? = null
var axisX = 0f
var axisY = 0f
var axisRX = 0f
var axisRY = 0f
if (Terrarum.hasController) { if (Terrarum.hasController) {
gamepad = Controllers.getController(0) gamepad = Controllers.getController(0)
axisX = gamepad!!.getAxisValue(0) axisX = gamepad!!.getAxisValue(0)
axisY = gamepad.getAxisValue(1) axisY = gamepad!!.getAxisValue(1)
axisRX = gamepad.getAxisValue(2) axisRX = gamepad!!.getAxisValue(2)
axisRY = gamepad.getAxisValue(3) axisRY = gamepad!!.getAxisValue(3)
// deadzonning
if (Math.abs(axisX) < Terrarum.CONTROLLER_DEADZONE) axisX = 0f if (Math.abs(axisX) < Terrarum.CONTROLLER_DEADZONE) axisX = 0f
if (Math.abs(axisY) < Terrarum.CONTROLLER_DEADZONE) axisY = 0f if (Math.abs(axisY) < Terrarum.CONTROLLER_DEADZONE) axisY = 0f
if (Math.abs(axisRX) < Terrarum.CONTROLLER_DEADZONE) axisRX = 0f if (Math.abs(axisRX) < Terrarum.CONTROLLER_DEADZONE) axisRX = 0f
@@ -320,16 +329,15 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
/** /**
* L-R stop * L-R stop
*/ */
if (Terrarum.hasController) { if (Terrarum.hasController && !isWalkingH) {
if (axisX == 0f) { if (axisX == 0f) {
walkHStop() walkHStop()
} }
} else { }
// ↑F, ↑S // ↑F, ↑S
if (!isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && !isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) { if (isWalkingH && !isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && !isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) {
walkHStop() walkHStop()
prevHMoveKey = KEY_NULL prevHMoveKey = KEY_NULL
}
} }
/** /**
* U-D stop * U-D stop
@@ -338,15 +346,14 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
if (axisY == 0f) { if (axisY == 0f) {
walkVStop() walkVStop()
} }
} else { }
// ↑E // ↑E
// ↑D // ↑D
if (isNoClip() if (isNoClip()
&& !isFuncDown(input, EnumKeyFunc.MOVE_UP) && !isFuncDown(input, EnumKeyFunc.MOVE_UP)
&& !isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) { && !isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) {
walkVStop() walkVStop()
prevVMoveKey = KEY_NULL prevVMoveKey = KEY_NULL
}
} }
/** /**
@@ -355,28 +362,27 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
if (Terrarum.hasController) { if (Terrarum.hasController) {
if (axisX != 0f) { if (axisX != 0f) {
walkHorizontal(axisX < 0, AXIS_POSMAX) walkHorizontal(axisX < 0f, axisX.abs())
} }
} else {
// ↑F, ↓S
if (isFuncDown(input, EnumKeyFunc.MOVE_RIGHT) && !isFuncDown(input, EnumKeyFunc.MOVE_LEFT)) {
walkHorizontal(false, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)
} // ↓F, ↑S
else if (isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && !isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) {
walkHorizontal(true, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)
} // ↓F, ↓S
/*else if (isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) {
if (prevHMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)) {
walkHorizontal(false, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)
} else if (prevHMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)) {
walkHorizontal(true, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)
}
}*/
} }
// ↑F, ↓S
if (isFuncDown(input, EnumKeyFunc.MOVE_RIGHT) && !isFuncDown(input, EnumKeyFunc.MOVE_LEFT)) {
walkHorizontal(false, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)
} // ↓F, ↑S
else if (isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && !isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) {
walkHorizontal(true, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)
} // ↓F, ↓S
/*else if (isFuncDown(input, EnumKeyFunc.MOVE_LEFT) && isFuncDown(input, EnumKeyFunc.MOVE_RIGHT)) {
if (prevHMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)) {
walkHorizontal(false, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)
} else if (prevHMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_RIGHT)) {
walkHorizontal(true, AXIS_POSMAX)
prevHMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_LEFT)
}
}*/
/** /**
* Up/Down movement * Up/Down movement
@@ -384,28 +390,27 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
if (noClip) { if (noClip) {
if (Terrarum.hasController) { if (Terrarum.hasController) {
if (axisY != 0f) { if (axisY != 0f) {
walkVertical(axisY > 0, AXIS_POSMAX) walkVertical(axisY > 0, axisY.abs())
} }
} else { }
// ↑E, ↓D // ↑E, ↓D
if (isFuncDown(input, EnumKeyFunc.MOVE_DOWN) && !isFuncDown(input, EnumKeyFunc.MOVE_UP)) { if (isFuncDown(input, EnumKeyFunc.MOVE_DOWN) && !isFuncDown(input, EnumKeyFunc.MOVE_UP)) {
walkVertical(false, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN)
} // ↓E, ↑D
else if (isFuncDown(input, EnumKeyFunc.MOVE_UP) && !isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) {
walkVertical(true, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP)
} // ↓E, ↓D
/*else if (isFuncDown(input, EnumKeyFunc.MOVE_UP) && isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) {
if (prevVMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP)) {
walkVertical(false, AXIS_POSMAX) walkVertical(false, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN) prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN)
} // ↓E, ↑D } else if (prevVMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN)) {
else if (isFuncDown(input, EnumKeyFunc.MOVE_UP) && !isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) {
walkVertical(true, AXIS_POSMAX) walkVertical(true, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP) prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP)
} // ↓E, ↓D }
/*else if (isFuncDown(input, EnumKeyFunc.MOVE_UP) && isFuncDown(input, EnumKeyFunc.MOVE_DOWN)) { }*/
if (prevVMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP)) {
walkVertical(false, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN)
} else if (prevVMoveKey == KeyMap.getKeyCode(EnumKeyFunc.MOVE_DOWN)) {
walkVertical(true, AXIS_POSMAX)
prevVMoveKey = KeyMap.getKeyCode(EnumKeyFunc.MOVE_UP)
}
}*/
}
} }
/** /**
@@ -423,6 +428,7 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
} else { } else {
jumping = false jumping = false
jumpCounter = 0 jumpCounter = 0
jumpAcc = 0.0
} }
} }
@@ -485,4 +491,5 @@ class Player : ActorWithBody(), Controllable, Pocketed, Factionable, Luminous, L
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
fun Float.abs() = FastMath.abs(this)
} }