mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 03:24:06 +09:00
Issue #6 resolved
Former-commit-id: 643ca0b3757f329107a7677fe3bf5f43ffac7639 Former-commit-id: caa5582abb797485e90b8c3e47a6307b177ff456
This commit is contained in:
@@ -227,7 +227,7 @@ constructor(gamename: String) : StateBasedGame(gamename) {
|
|||||||
//var hasController = false
|
//var hasController = false
|
||||||
var controller: org.lwjgl.input.Controller? = null
|
var controller: org.lwjgl.input.Controller? = null
|
||||||
private set
|
private set
|
||||||
val CONTROLLER_DEADZONE = 0.2f
|
val CONTROLLER_DEADZONE = 0.1f
|
||||||
|
|
||||||
/** Available CPU threads */
|
/** Available CPU threads */
|
||||||
val THREADS = Runtime.getRuntime().availableProcessors()
|
val THREADS = Runtime.getRuntime().availableProcessors()
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
|
|||||||
protected var isRightDown = false
|
protected var isRightDown = false
|
||||||
protected var isJumpDown = false
|
protected var isJumpDown = false
|
||||||
protected val isGamer: Boolean
|
protected val isGamer: Boolean
|
||||||
get() = this is Player // FIXME true iff composed by PlayableActorDelegate
|
get() = this == Terrarum.ingame.player
|
||||||
|
|
||||||
|
|
||||||
private val nullItem = object : InventoryItem() {
|
private val nullItem = object : InventoryItem() {
|
||||||
@@ -387,23 +387,15 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
|
|||||||
private fun walkHorizontal(left: Boolean, absAxisVal: Float) {
|
private fun walkHorizontal(left: Boolean, absAxisVal: Float) {
|
||||||
if ((!walledLeft && left) || (!walledRight && !left)) {
|
if ((!walledLeft && left) || (!walledRight && !left)) {
|
||||||
readonly_totalX =
|
readonly_totalX =
|
||||||
absMax( // keyboard
|
if (absAxisVal == AXIS_KEYBOARD)
|
||||||
if (absAxisVal == AXIS_KEYBOARD)
|
avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f)
|
||||||
avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f)
|
else
|
||||||
else
|
avAcceleration * (if (left) -1f else 1f) * absAxisVal
|
||||||
0.0
|
|
||||||
,
|
|
||||||
// gamepad
|
|
||||||
if (absAxisVal != AXIS_KEYBOARD)
|
|
||||||
avAcceleration * (if (left) -1f else 1f) * absAxisVal
|
|
||||||
else
|
|
||||||
0.0
|
|
||||||
)
|
|
||||||
|
|
||||||
//applyForce(Vector2(readonly_totalX, 0.0))
|
if (absAxisVal != AXIS_KEYBOARD)
|
||||||
walkX += readonly_totalX
|
walkX = walkX.plus(readonly_totalX).bipolarClamp(avSpeedCap * absAxisVal)
|
||||||
walkX = readonly_totalX
|
else
|
||||||
walkX = walkX.bipolarClamp(avSpeedCap)
|
walkX = walkX.plus(readonly_totalX).bipolarClamp(avSpeedCap)
|
||||||
|
|
||||||
if (absAxisVal == AXIS_KEYBOARD) {
|
if (absAxisVal == AXIS_KEYBOARD) {
|
||||||
walkCounterX += 1
|
walkCounterX += 1
|
||||||
@@ -413,10 +405,7 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Heading flag
|
// Heading flag
|
||||||
if (left)
|
walkHeading = if (left) LEFT else RIGHT
|
||||||
walkHeading = LEFT
|
|
||||||
else
|
|
||||||
walkHeading = RIGHT
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -427,21 +416,15 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
|
|||||||
*/
|
*/
|
||||||
private fun walkVertical(up: Boolean, absAxisVal: Float) {
|
private fun walkVertical(up: Boolean, absAxisVal: Float) {
|
||||||
readonly_totalY =
|
readonly_totalY =
|
||||||
absMax( // keyboard
|
if (absAxisVal == AXIS_KEYBOARD)
|
||||||
if (absAxisVal == AXIS_KEYBOARD)
|
avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f)
|
||||||
avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f)
|
else
|
||||||
else
|
avAcceleration * (if (up) -1f else 1f) * absAxisVal
|
||||||
0.0
|
|
||||||
,
|
|
||||||
// gamepad
|
|
||||||
if (absAxisVal != AXIS_KEYBOARD)
|
|
||||||
avAcceleration * (if (up) -1f else 1f) * absAxisVal
|
|
||||||
else
|
|
||||||
0.0
|
|
||||||
)
|
|
||||||
|
|
||||||
walkY += readonly_totalY
|
if (absAxisVal != AXIS_KEYBOARD)
|
||||||
walkY = walkY.bipolarClamp(avSpeedCap)
|
walkY = walkY.plus(readonly_totalY).bipolarClamp(avSpeedCap * absAxisVal)
|
||||||
|
else
|
||||||
|
walkY = walkY.plus(readonly_totalY).bipolarClamp(avSpeedCap)
|
||||||
|
|
||||||
if (absAxisVal == AXIS_KEYBOARD) {
|
if (absAxisVal == AXIS_KEYBOARD) {
|
||||||
walkCounterY += 1
|
walkCounterY += 1
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.torvald.terrarum.gameactors
|
|||||||
|
|
||||||
import net.torvald.terrarum.gameactors.ActorHumanoid
|
import net.torvald.terrarum.gameactors.ActorHumanoid
|
||||||
import net.torvald.terrarum.gameactors.ai.AILuaAPI
|
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.EquipPosition
|
||||||
import net.torvald.terrarum.gameitem.InventoryItem
|
import net.torvald.terrarum.gameitem.InventoryItem
|
||||||
import org.luaj.vm2.*
|
import org.luaj.vm2.*
|
||||||
@@ -18,7 +19,7 @@ import java.io.Reader
|
|||||||
/**
|
/**
|
||||||
* Created by minjaesong on 16-01-31.
|
* 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 = ""
|
override val scriptPath: String = ""
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
aiLuaAPI = AILuaAPI(luag, this)
|
aiLuaAPI = AILuaAPI(luag, this)
|
||||||
luaInstance = luag.load(luaScript)
|
luaInstance = luag.load(luaScript.getString(), luaScript.javaClass.simpleName)
|
||||||
luaInstance.call()
|
luaInstance.call()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +79,7 @@ open class HumanoidNPC(val luaScript: String, born: GameDate) : ActorHumanoid(bo
|
|||||||
super.update(gc, delta)
|
super.update(gc, delta)
|
||||||
aiLuaAPI.update(delta)
|
aiLuaAPI.update(delta)
|
||||||
|
|
||||||
//runCommand(luaScript)
|
|
||||||
luag.get("update").call(delta.toLuaValue())
|
luag.get("update").call(delta.toLuaValue())
|
||||||
|
|
||||||
//moveRight()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun moveLeft(amount: Float) { // hit the buttons on the controller box
|
override fun moveLeft(amount: Float) { // hit the buttons on the controller box
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ object PlayerBuilderCynthia {
|
|||||||
|
|
||||||
operator fun invoke(): ActorWithBody {
|
operator fun invoke(): ActorWithBody {
|
||||||
//val p: Player = Player(GameDate(100, 143)) // random value thrown
|
//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")
|
InjectCreatureRaw(p.actorValue, "CreatureHuman.json")
|
||||||
|
|
||||||
p.actorValue[AVKey.__PLAYER_QUICKBARSEL] = 0
|
p.actorValue[AVKey.__PLAYER_QUICKBARSEL] = 0
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -5,52 +5,63 @@ package net.torvald.terrarum.gameactors.ai.scripts
|
|||||||
*
|
*
|
||||||
* Created by SKYHi14 on 2016-12-23.
|
* Created by SKYHi14 on 2016-12-23.
|
||||||
*/
|
*/
|
||||||
object PokemonNPCAI {
|
|
||||||
operator fun invoke(): String = """
|
|
||||||
|
|
||||||
|
|
||||||
|
object PokemonNPCAI : EncapsulatedString() {
|
||||||
|
override fun getString() = """
|
||||||
timeCounter = 0
|
timeCounter = 0
|
||||||
countMax = 0
|
countMax = 0
|
||||||
moveMode = math.random() >= 0.5 and "left" or "right"
|
moveMode = math.random() >= 0.5 and "left" or "right"
|
||||||
currentMode = "turn"
|
currentMode = "turn"
|
||||||
|
|
||||||
function generateTurn()
|
local function generateCountMax()
|
||||||
return 4600 + 1250 * math.random()
|
function generateTurn()
|
||||||
|
return 4600 + 1250 * math.random()
|
||||||
|
end
|
||||||
|
|
||||||
|
function generateWalk()
|
||||||
|
return 568 + 342 * math.random()
|
||||||
|
end
|
||||||
|
|
||||||
|
return (currentMode == "move") and generateWalk() or generateTurn()
|
||||||
end
|
end
|
||||||
|
|
||||||
function generateWalk()
|
local function moveToDirection()
|
||||||
return 568 + 342 * math.random()
|
if moveMode == "left" then
|
||||||
|
ai.moveLeft(0.5)
|
||||||
|
elseif moveMode == "right" then
|
||||||
|
ai.moveRight(0.5)
|
||||||
|
end
|
||||||
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)
|
function update(delta)
|
||||||
timeCounter = timeCounter + delta
|
timeCounter = timeCounter + delta
|
||||||
|
|
||||||
if currentMode == "turn" then
|
if currentMode == "move" then
|
||||||
-- wait
|
moveToDirection()
|
||||||
-- reset counter
|
end
|
||||||
if timeCounter >= countMax then
|
|
||||||
timeCounter = 0
|
if timeCounter >= countMax then
|
||||||
currentMode = "move"
|
timeCounter = 0
|
||||||
countMax = generateWalk()
|
toggleCurrentMode()
|
||||||
moveMode = (moveMode == "left") and "right" or "left"
|
countMax = generateCountMax()
|
||||||
end
|
if currentMode == "turn" then
|
||||||
elseif currentMode == "move" then
|
toggleMoveMode()
|
||||||
-- 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()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import net.torvald.terrarum.mapdrawer.MapDrawer
|
|||||||
import net.torvald.terrarum.Terrarum
|
import net.torvald.terrarum.Terrarum
|
||||||
import net.torvald.terrarum.blendNormal
|
import net.torvald.terrarum.blendNormal
|
||||||
import net.torvald.terrarum.blendScreen
|
import net.torvald.terrarum.blendScreen
|
||||||
|
import net.torvald.terrarum.gameactors.ActorHumanoid
|
||||||
import org.newdawn.slick.Color
|
import org.newdawn.slick.Color
|
||||||
import org.newdawn.slick.GameContainer
|
import org.newdawn.slick.GameContainer
|
||||||
import org.newdawn.slick.Graphics
|
import org.newdawn.slick.Graphics
|
||||||
@@ -80,9 +81,9 @@ class BasicDebugInfoWindow : UICanvas {
|
|||||||
|
|
||||||
printLine(g, 1, "posX "
|
printLine(g, 1, "posX "
|
||||||
+ ccG
|
+ ccG
|
||||||
+ "${hitbox.pointedX.toString()}"
|
+ "${hitbox.pointedX}"
|
||||||
+ " ("
|
+ " ("
|
||||||
+ "${(hitbox.pointedX / MapDrawer.TILE_SIZE).toInt().toString()}"
|
+ "${(hitbox.pointedX / MapDrawer.TILE_SIZE).toInt()}"
|
||||||
+ ")")
|
+ ")")
|
||||||
printLine(g, 2, "posY "
|
printLine(g, 2, "posY "
|
||||||
+ ccG
|
+ ccG
|
||||||
@@ -143,6 +144,14 @@ class BasicDebugInfoWindow : UICanvas {
|
|||||||
Terrarum.WIDTH - histogramW - 30,
|
Terrarum.WIDTH - histogramW - 30,
|
||||||
Terrarum.HEIGHT - histogramH - 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
|
* Top right
|
||||||
@@ -195,7 +204,7 @@ class BasicDebugInfoWindow : UICanvas {
|
|||||||
val histogramMax = histogram.screen_tiles.toFloat()
|
val histogramMax = histogram.screen_tiles.toFloat()
|
||||||
|
|
||||||
g.color = uiColour
|
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.color = Color.gray
|
||||||
g.drawString("0", x.toFloat(), y.toFloat() + h + 2)
|
g.drawString("0", x.toFloat(), y.toFloat() + h + 2)
|
||||||
g.drawString("255", x.toFloat() + w + 1 - 8*3, 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_h = FastMath.ceil(h / histogramMax * histogram_value.toFloat()).toFloat()
|
||||||
val bar_y = y + (h / histogramMax) - bar_h + h
|
val bar_y = y + (h / histogramMax) - bar_h + h
|
||||||
val bar_w = 1f
|
val bar_w = 1f
|
||||||
@@ -223,6 +232,29 @@ class BasicDebugInfoWindow : UICanvas {
|
|||||||
blendNormal()
|
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 line(i: Int): Float = i * 10f
|
||||||
|
|
||||||
private fun column(i: Int): Float = 300f * (i - 1)
|
private fun column(i: Int): Float = 300f * (i - 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user