mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-09 18:14:06 +09:00
forEach codes for occupying and feet tiles
Former-commit-id: f8a719b262420d673b3672f7e21764a0dbc754b9 Former-commit-id: e09af27d09cb7ab1dbe08637b4a8690ab612466c
This commit is contained in:
@@ -7,11 +7,14 @@ import net.torvald.terrarum.gameworld.GameWorld
|
|||||||
import net.torvald.terrarum.mapdrawer.MapDrawer
|
import net.torvald.terrarum.mapdrawer.MapDrawer
|
||||||
import net.torvald.terrarum.tileproperties.TilePropCodex
|
import net.torvald.terrarum.tileproperties.TilePropCodex
|
||||||
import net.torvald.spriteanimation.SpriteAnimation
|
import net.torvald.spriteanimation.SpriteAnimation
|
||||||
|
import net.torvald.terrarum.mapdrawer.MapDrawer.TILE_SIZE
|
||||||
import net.torvald.terrarum.tileproperties.TileNameCode
|
import net.torvald.terrarum.tileproperties.TileNameCode
|
||||||
|
import net.torvald.terrarum.tileproperties.TileProp
|
||||||
import org.dyn4j.Epsilon
|
import org.dyn4j.Epsilon
|
||||||
import org.dyn4j.geometry.Vector2
|
import org.dyn4j.geometry.Vector2
|
||||||
import org.newdawn.slick.GameContainer
|
import org.newdawn.slick.GameContainer
|
||||||
import org.newdawn.slick.Graphics
|
import org.newdawn.slick.Graphics
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for every actor that has physical (or echo) body. This includes furnishings, paintings, gadgets, etc.
|
* Base class for every actor that has physical (or echo) body. This includes furnishings, paintings, gadgets, etc.
|
||||||
@@ -832,27 +835,9 @@ open class ActorWithBody : Actor() {
|
|||||||
internal val bodyFriction: Int
|
internal val bodyFriction: Int
|
||||||
get() {
|
get() {
|
||||||
var friction = 0
|
var friction = 0
|
||||||
val frictionCalcHitbox =
|
forEachFeetTile {
|
||||||
if (!isWalkingH)
|
if (it?.friction ?: 4 > friction) // 4: friction of the air
|
||||||
Hitbox(nextHitbox.posX,
|
friction = it?.friction ?: 4
|
||||||
nextHitbox.posY,
|
|
||||||
nextHitbox.width + 2.0,
|
|
||||||
nextHitbox.height + 2.0)
|
|
||||||
// when not walking, enlarge the hitbox for calculation so that
|
|
||||||
// feet tiles are also counted
|
|
||||||
else
|
|
||||||
nextHitbox.clone()
|
|
||||||
|
|
||||||
// take highest value
|
|
||||||
val tilePosXStart = (frictionCalcHitbox.posX / TILE_SIZE).floorInt()
|
|
||||||
val tilePosXEnd = (frictionCalcHitbox.hitboxEnd.x / TILE_SIZE).floorInt()
|
|
||||||
val tilePosY = (frictionCalcHitbox.pointedY / TILE_SIZE).floorInt()
|
|
||||||
|
|
||||||
for (x in tilePosXStart..tilePosXEnd) {
|
|
||||||
val tile = world.getTileFromTerrain(x, tilePosY)
|
|
||||||
val thisFriction = TilePropCodex[tile].friction
|
|
||||||
|
|
||||||
if (thisFriction > friction) friction = thisFriction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return friction
|
return friction
|
||||||
@@ -866,21 +851,13 @@ open class ActorWithBody : Actor() {
|
|||||||
private val tileDensityFluid: Int
|
private val tileDensityFluid: Int
|
||||||
get() {
|
get() {
|
||||||
var density = 0
|
var density = 0
|
||||||
|
forEachOccupyingTile {
|
||||||
// take highest value
|
// get max density for each tile
|
||||||
val tilePosXStart = (hitbox.posX / TILE_SIZE).roundInt()
|
if (it?.isFluid ?: false && it?.density ?: 0 > density) {
|
||||||
val tilePosXEnd = (hitbox.hitboxEnd.x / TILE_SIZE).roundInt()
|
density = it?.density ?: 0
|
||||||
val tilePosYStart = (hitbox.posY / TILE_SIZE).roundInt()
|
|
||||||
val tilePosYEnd = (hitbox.hitboxEnd.y / TILE_SIZE).roundInt()
|
|
||||||
for (y in tilePosXStart..tilePosYEnd) {
|
|
||||||
for (x in tilePosXStart..tilePosXEnd) {
|
|
||||||
val tile = world.getTileFromTerrain(x, y)
|
|
||||||
val prop = TilePropCodex[tile]
|
|
||||||
|
|
||||||
if (prop.isFluid && prop.density > density)
|
|
||||||
density = prop.density
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return density
|
return density
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -891,18 +868,10 @@ open class ActorWithBody : Actor() {
|
|||||||
private val tileDensity: Int
|
private val tileDensity: Int
|
||||||
get() {
|
get() {
|
||||||
var density = 0
|
var density = 0
|
||||||
|
forEachOccupyingTile {
|
||||||
//get highest fluid density
|
// get max density for each tile
|
||||||
val tilePosXStart = (nextHitbox.posX / TILE_SIZE).roundInt()
|
if (it?.density ?: 0 > density) {
|
||||||
val tilePosYStart = (nextHitbox.posY / TILE_SIZE).roundInt()
|
density = it?.density ?: 0
|
||||||
val tilePosXEnd = (nextHitbox.hitboxEnd.x / TILE_SIZE).roundInt()
|
|
||||||
val tilePosYEnd = (nextHitbox.hitboxEnd.y / TILE_SIZE).roundInt()
|
|
||||||
for (y in tilePosYStart..tilePosYEnd) {
|
|
||||||
for (x in tilePosXStart..tilePosXEnd) {
|
|
||||||
val tile = world.getTileFromTerrain(x, y)
|
|
||||||
val thisFluidDensity = TilePropCodex[tile].density
|
|
||||||
|
|
||||||
if (thisFluidDensity > density) density = thisFluidDensity
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1040,6 +1009,54 @@ open class ActorWithBody : Actor() {
|
|||||||
flagDespawn = true
|
flagDespawn = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun forEachOccupyingTileNum(consumer: (Int?) -> Unit) {
|
||||||
|
val tiles = ArrayList<Int?>()
|
||||||
|
for (y in tilewiseHitbox.posY.toInt()..tilewiseHitbox.endPointY.toInt()) {
|
||||||
|
for (x in tilewiseHitbox.posX.toInt()..tilewiseHitbox.endPointX.toInt()) {
|
||||||
|
tiles.add(world.getTileFromTerrain(x, y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tiles.forEach(consumer)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun forEachOccupyingTile(consumer: (TileProp?) -> Unit) {
|
||||||
|
val tileProps = ArrayList<TileProp?>()
|
||||||
|
for (y in tilewiseHitbox.posY.toInt()..tilewiseHitbox.endPointY.toInt()) {
|
||||||
|
for (x in tilewiseHitbox.posX.toInt()..tilewiseHitbox.endPointX.toInt()) {
|
||||||
|
tileProps.add(TilePropCodex[world.getTileFromTerrain(x, y)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tileProps.forEach(consumer)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun forEachFeetTileNum(consumer: (Int?) -> Unit) {
|
||||||
|
val tiles = ArrayList<Int?>()
|
||||||
|
|
||||||
|
// offset 1 pixel to the down so that friction would work
|
||||||
|
val y = nextHitbox.endPointY.plus(1.0).div(TILE_SIZE).floorInt()
|
||||||
|
|
||||||
|
for (x in tilewiseHitbox.posX.toInt()..tilewiseHitbox.endPointX.toInt()) {
|
||||||
|
tiles.add(world.getTileFromTerrain(x, y))
|
||||||
|
}
|
||||||
|
|
||||||
|
return tiles.forEach(consumer)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun forEachFeetTile(consumer: (TileProp?) -> Unit) {
|
||||||
|
val tileProps = ArrayList<TileProp?>()
|
||||||
|
|
||||||
|
// offset 1 pixel to the down so that friction would work
|
||||||
|
val y = nextHitbox.endPointY.plus(1.0).div(TILE_SIZE).floorInt()
|
||||||
|
|
||||||
|
for (x in tilewiseHitbox.posX.toInt()..tilewiseHitbox.endPointX.toInt()) {
|
||||||
|
tileProps.add(TilePropCodex[world.getTileFromTerrain(x, y)])
|
||||||
|
}
|
||||||
|
|
||||||
|
return tileProps.forEach(consumer)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
@Transient private val TILE_SIZE = MapDrawer.TILE_SIZE
|
@Transient private val TILE_SIZE = MapDrawer.TILE_SIZE
|
||||||
|
|||||||
Reference in New Issue
Block a user