diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index d4431b648..2b5b957bb 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -227,7 +227,7 @@ constructor(gamename: String) : StateBasedGame(gamename) { //var hasController = false var controller: org.lwjgl.input.Controller? = null private set - val CONTROLLER_DEADZONE = 0.2f + val CONTROLLER_DEADZONE = 0.1f /** Available CPU threads */ val THREADS = Runtime.getRuntime().availableProcessors() diff --git a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt index 7475cab35..a88748778 100644 --- a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt @@ -127,7 +127,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) protected var isRightDown = false protected var isJumpDown = false protected val isGamer: Boolean - get() = this is Player // FIXME true iff composed by PlayableActorDelegate + get() = this == Terrarum.ingame.player private val nullItem = object : InventoryItem() { @@ -387,23 +387,15 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) private fun walkHorizontal(left: Boolean, absAxisVal: Float) { if ((!walledLeft && left) || (!walledRight && !left)) { readonly_totalX = - absMax( // keyboard - if (absAxisVal == AXIS_KEYBOARD) - avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f) - else - 0.0 - , - // gamepad - if (absAxisVal != AXIS_KEYBOARD) - avAcceleration * (if (left) -1f else 1f) * absAxisVal - else - 0.0 - ) + if (absAxisVal == AXIS_KEYBOARD) + avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f) + else + avAcceleration * (if (left) -1f else 1f) * absAxisVal - //applyForce(Vector2(readonly_totalX, 0.0)) - walkX += readonly_totalX - walkX = readonly_totalX - walkX = walkX.bipolarClamp(avSpeedCap) + if (absAxisVal != AXIS_KEYBOARD) + walkX = walkX.plus(readonly_totalX).bipolarClamp(avSpeedCap * absAxisVal) + else + walkX = walkX.plus(readonly_totalX).bipolarClamp(avSpeedCap) if (absAxisVal == AXIS_KEYBOARD) { walkCounterX += 1 @@ -413,10 +405,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) } // Heading flag - if (left) - walkHeading = LEFT - else - walkHeading = RIGHT + walkHeading = if (left) LEFT else RIGHT } /** @@ -427,21 +416,15 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) */ private fun walkVertical(up: Boolean, absAxisVal: Float) { readonly_totalY = - absMax( // keyboard - if (absAxisVal == AXIS_KEYBOARD) - avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f) - else - 0.0 - , - // gamepad - if (absAxisVal != AXIS_KEYBOARD) - avAcceleration * (if (up) -1f else 1f) * absAxisVal - else - 0.0 - ) + if (absAxisVal == AXIS_KEYBOARD) + avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f) + else + avAcceleration * (if (up) -1f else 1f) * absAxisVal - walkY += readonly_totalY - walkY = walkY.bipolarClamp(avSpeedCap) + if (absAxisVal != AXIS_KEYBOARD) + walkY = walkY.plus(readonly_totalY).bipolarClamp(avSpeedCap * absAxisVal) + else + walkY = walkY.plus(readonly_totalY).bipolarClamp(avSpeedCap) if (absAxisVal == AXIS_KEYBOARD) { walkCounterY += 1 diff --git a/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt b/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt index 5b3d1fa9d..014a66fb6 100644 --- a/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt +++ b/src/net/torvald/terrarum/gameactors/HumanoidNPC.kt @@ -2,6 +2,7 @@ package net.torvald.terrarum.gameactors import net.torvald.terrarum.gameactors.ActorHumanoid import net.torvald.terrarum.gameactors.ai.AILuaAPI +import net.torvald.terrarum.gameactors.ai.scripts.EncapsulatedString import net.torvald.terrarum.gameitem.EquipPosition import net.torvald.terrarum.gameitem.InventoryItem import org.luaj.vm2.* @@ -18,7 +19,7 @@ import java.io.Reader /** * Created by minjaesong on 16-01-31. */ -open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(born), AIControlled, CanBeAnItem { +open class HumanoidNPC(luaScript: EncapsulatedString, born: GameDate) : ActorHumanoid(born), AIControlled, CanBeAnItem { override val scriptPath: String = "" @@ -34,7 +35,7 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo init { aiLuaAPI = AILuaAPI(luag, this) - luaInstance = luag.load(luaScript) + luaInstance = luag.load(luaScript.getString(), luaScript.javaClass.simpleName) luaInstance.call() } @@ -78,10 +79,7 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo super.update(gc, delta) aiLuaAPI.update(delta) - //runCommand(luaScript) luag.get("update").call(delta.toLuaValue()) - - //moveRight() } override fun moveLeft(amount: Float) { // hit the buttons on the controller box diff --git a/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt b/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt index 40fd15299..153ee68f8 100644 --- a/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt +++ b/src/net/torvald/terrarum/gameactors/PlayerBuilderCynthia.kt @@ -12,7 +12,7 @@ object PlayerBuilderCynthia { operator fun invoke(): ActorWithBody { //val p: Player = Player(GameDate(100, 143)) // random value thrown - val p: HumanoidNPC = HumanoidNPC(PokemonNPCAI(), GameDate(100, 143)) // random value thrown + val p: HumanoidNPC = HumanoidNPC(PokemonNPCAI, GameDate(100, 143)) // random value thrown InjectCreatureRaw(p.actorValue, "CreatureHuman.json") p.actorValue[AVKey.__PLAYER_QUICKBARSEL] = 0 diff --git a/src/net/torvald/terrarum/gameactors/ai/scripts/EncapsulatedString.kt b/src/net/torvald/terrarum/gameactors/ai/scripts/EncapsulatedString.kt new file mode 100644 index 000000000..4cb1e0a46 --- /dev/null +++ b/src/net/torvald/terrarum/gameactors/ai/scripts/EncapsulatedString.kt @@ -0,0 +1,9 @@ +package net.torvald.terrarum.gameactors.ai.scripts + +/** + * Created by SKYHi14 on 2016-12-28. + */ +abstract class EncapsulatedString { + abstract fun getString(): String + override fun toString() = getString() +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/ai/scripts/PokemonNPCAI.kt b/src/net/torvald/terrarum/gameactors/ai/scripts/PokemonNPCAI.kt index d0a2a6f7b..367d2afb2 100644 --- a/src/net/torvald/terrarum/gameactors/ai/scripts/PokemonNPCAI.kt +++ b/src/net/torvald/terrarum/gameactors/ai/scripts/PokemonNPCAI.kt @@ -5,52 +5,63 @@ package net.torvald.terrarum.gameactors.ai.scripts * * Created by SKYHi14 on 2016-12-23. */ -object PokemonNPCAI { - operator fun invoke(): String = """ + +object PokemonNPCAI : EncapsulatedString() { + override fun getString() = """ timeCounter = 0 countMax = 0 moveMode = math.random() >= 0.5 and "left" or "right" currentMode = "turn" -function generateTurn() - return 4600 + 1250 * math.random() +local function generateCountMax() + function generateTurn() + return 4600 + 1250 * math.random() + end + + function generateWalk() + return 568 + 342 * math.random() + end + + return (currentMode == "move") and generateWalk() or generateTurn() end -function generateWalk() - return 568 + 342 * math.random() +local function moveToDirection() + if moveMode == "left" then + ai.moveLeft(0.5) + elseif moveMode == "right" then + ai.moveRight(0.5) + end end +local function toggleCurrentMode() + currentMode = (currentMode == "move") and "turn" or "move" +end +local function toggleMoveMode() + moveMode = (moveMode == "left") and "right" or "left" +end -countMax = generateTurn() +------------------------------------------------------------------------------- + +countMax = generateCountMax() function update(delta) timeCounter = timeCounter + delta - if currentMode == "turn" then - -- wait - -- reset counter - if timeCounter >= countMax then - timeCounter = 0 - currentMode = "move" - countMax = generateWalk() - moveMode = (moveMode == "left") and "right" or "left" - end - elseif currentMode == "move" then - -- move - if moveMode == "left" then - ai.moveLeft(0.5) - elseif moveMode == "right" then - ai.moveRight(0.5) - end - -- reset counter - if timeCounter >= countMax then - timeCounter = 0 - currentMode = "turn" - countMax = generateTurn() + if currentMode == "move" then + moveToDirection() + end + + if timeCounter >= countMax then + timeCounter = 0 + toggleCurrentMode() + countMax = generateCountMax() + if currentMode == "turn" then + toggleMoveMode() end end end + """ -} \ No newline at end of file +} diff --git a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt index 3960dc7ba..97628f895 100644 --- a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt +++ b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt @@ -10,6 +10,7 @@ import net.torvald.terrarum.mapdrawer.MapDrawer import net.torvald.terrarum.Terrarum import net.torvald.terrarum.blendNormal import net.torvald.terrarum.blendScreen +import net.torvald.terrarum.gameactors.ActorHumanoid import org.newdawn.slick.Color import org.newdawn.slick.GameContainer import org.newdawn.slick.Graphics @@ -80,9 +81,9 @@ class BasicDebugInfoWindow : UICanvas { printLine(g, 1, "posX " + ccG - + "${hitbox.pointedX.toString()}" + + "${hitbox.pointedX}" + " (" - + "${(hitbox.pointedX / MapDrawer.TILE_SIZE).toInt().toString()}" + + "${(hitbox.pointedX / MapDrawer.TILE_SIZE).toInt()}" + ")") printLine(g, 2, "posY " + ccG @@ -143,6 +144,14 @@ class BasicDebugInfoWindow : UICanvas { Terrarum.WIDTH - histogramW - 30, Terrarum.HEIGHT - histogramH - 30 ) + if (Terrarum.controller != null) { + drawGamepadAxis(g, + Terrarum.controller!!.getAxisValue(3), + Terrarum.controller!!.getAxisValue(2), + Terrarum.WIDTH - 135, + 40 + ) + } /** * Top right @@ -195,7 +204,7 @@ class BasicDebugInfoWindow : UICanvas { val histogramMax = histogram.screen_tiles.toFloat() g.color = uiColour - g.fillRect(x.toFloat(), y.toFloat(), w.plus(1).toFloat(), h.toFloat()) + g.fillRect(x.toFloat(), y.toFloat(), w.plus(1), h) g.color = Color.gray g.drawString("0", x.toFloat(), y.toFloat() + h + 2) g.drawString("255", x.toFloat() + w + 1 - 8*3, y.toFloat() + h + 2) @@ -211,7 +220,7 @@ class BasicDebugInfoWindow : UICanvas { } } - val bar_x = x + (w / w.minus(1).toFloat()) * i.toFloat() + val bar_x = x + (w / w.minus(1f)) * i.toFloat() val bar_h = FastMath.ceil(h / histogramMax * histogram_value.toFloat()).toFloat() val bar_y = y + (h / histogramMax) - bar_h + h val bar_w = 1f @@ -223,6 +232,29 @@ class BasicDebugInfoWindow : UICanvas { blendNormal() } + private fun drawGamepadAxis(g: Graphics, axisX: Float, axisY: Float, uiX: Int, uiY: Int) { + val uiColour = Color(0xAA000000.toInt()) + val w = 128f + val h = 128f + val halfW = w / 2f + val halfH = h / 2f + + val pointDX = axisX * halfW + val pointDY = axisY * halfH + + val padName = if (Terrarum.controller!!.name.isEmpty()) "Gamepad" + else Terrarum.controller!!.name + + blendNormal() + + g.color = uiColour + g.fillRect(uiX.toFloat(), uiY.toFloat(), w, h) + g.color = Color.white + g.drawLine(uiX + halfW, uiY + halfH, uiX + halfW + pointDX, uiY + halfH + pointDY) + g.color = Color.gray + g.drawString(padName, uiX + w / 2 - (padName.length) * 4, uiY.toFloat() + h + 2) + } + private fun line(i: Int): Float = i * 10f private fun column(i: Int): Float = 300f * (i - 1)