Skip to content

Instantly share code, notes, and snippets.

@Jire
Last active June 3, 2016 20:50
Show Gist options
  • Save Jire/f7224d1109af4ae4b555c767bf6a5925 to your computer and use it in GitHub Desktop.
Save Jire/f7224d1109af4ae4b555c767bf6a5925 to your computer and use it in GitHub Desktop.
package com.acelta.world.mob
import com.acelta.world.mob.player.Player
import com.acelta.world.region.lastRegionX
import com.acelta.world.region.lastRegionY
import com.acelta.packet.outgoing.rs186.sector
import it.unimi.dsi.fastutil.ints.IntArrayList
import java.lang.Math.abs
import java.lang.Math.max
class Movement(val mob: Mob) {
private var cursor = 0
private val xList = IntArrayList(128)
private val yList = IntArrayList(128)
var firstDirection = Direction.NONE
var secondDirection = Direction.NONE
var running = false
val moving: Boolean
get() = firstDirection != Direction.NONE
var teleporting = false
var regionChanging = false
fun reset() {
xList.clear()
yList.clear()
running = false
}
fun addFirstStep(nextX: Int, nextY: Int) {
reset()
addStep(nextX, nextY)
}
fun addStep(nextX: Int, nextY: Int) {
val currentX = if (cursor == 0) mob.position.x else xList.getInt(xList.size - 1)
val currentY = if (cursor == 0) mob.position.y else yList.getInt(yList.size - 1)
addStep(currentX, currentY, nextX, nextY)
}
private fun addStep(currentX: Int, currentY: Int, nextX: Int, nextY: Int) {
var deltaX = nextX - currentX
var deltaY = nextY - currentY
repeat(max(abs(deltaX), abs(deltaY))) {
if (deltaX < 0) deltaX++
else if (deltaX > 0) deltaX--
if (deltaY < 0) deltaY++
else if (deltaY > 0) deltaY--
val x = nextX - deltaX
val y = nextY - deltaY
xList.add(x)
yList.add(y)
}
}
fun tick() = with(mob.position) {
firstDirection = Direction.NONE
secondDirection = Direction.NONE
if (cursor >= xList.size) return
// poll //
var nextX = xList.getInt(cursor)
var nextY = yList.getInt(cursor)
cursor++
// end poll //
firstDirection = Direction.between(x, y, nextX, nextY)
x = nextX
y = nextY
if (running && cursor < xList.size) {
// poll //
nextX = xList.getInt(cursor)
nextY = yList.getInt(cursor)
cursor++
// end poll //
secondDirection = Direction.between(x, y, nextX, nextY)
x = nextX
y = nextY
}
val diffX = x - lastRegionX * 8
val diffY = y - lastRegionY * 8
if (diffX < 16 || diffX >= 88 || diffY < 16 || diffY >= 88) {
regionChanging = true
// TODO: set last region X and Y
if (mob is Player) mob.send.sector()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment