KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt

223 lines
5.8 KiB
Kotlin

package ru.dbotthepony.kstarbound
import org.apache.logging.log4j.LogManager
import org.lwjgl.Version
import org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose
import ru.dbotthepony.kbox2d.api.*
import ru.dbotthepony.kbox2d.collision.DistanceProxy
import ru.dbotthepony.kbox2d.collision.b2TimeOfImpact
import ru.dbotthepony.kbox2d.collision.shapes.CircleShape
import ru.dbotthepony.kbox2d.collision.shapes.PolygonShape
import ru.dbotthepony.kstarbound.client.StarboundClient
import ru.dbotthepony.kstarbound.defs.TileDefinition
import ru.dbotthepony.kstarbound.world.CHUNK_SIZE_FF
import ru.dbotthepony.kstarbound.world.Chunk
import ru.dbotthepony.kstarbound.world.ChunkPos
import ru.dbotthepony.kstarbound.world.entities.Move
import ru.dbotthepony.kstarbound.world.entities.PlayerEntity
import ru.dbotthepony.kstarbound.world.entities.Projectile
import ru.dbotthepony.kvector.vector.ndouble.Vector2d
import java.io.File
import java.util.*
private val LOGGER = LogManager.getLogger()
fun main() {
LOGGER.info("Running LWJGL ${Version.getVersion()}")
if (true) {
//val pak = StarboundPak(File("J:\\SteamLibrary\\steamapps\\common\\Starbound\\assets\\packed.pak"))
//val json = JsonParser.parseReader(pak.getReader("/projectiles/traps/lowgravboostergas/lowgravboostergas.projectile"))
//val obj = Gson().fromJson(json, ProjectileDefinitionBuilder::class.java)
//println(obj.build())
//return
}
val client = StarboundClient()
//Starbound.addFilePath(File("./unpacked_assets/"))
Starbound.addPakPath(File("J:\\SteamLibrary\\steamapps\\common\\Starbound\\assets\\packed.pak"))
Starbound.initializeGame { finished, replaceStatus, status ->
client.putDebugLog(status, replaceStatus)
}
client.onTermination {
Starbound.terminateLoading = true
}
var chunkA: Chunk<*, *>? = null
val ent = PlayerEntity(client.world!!)
Starbound.onInitialize {
chunkA = client.world!!.computeIfAbsent(ChunkPos(0, 0)).chunk
val chunkB = client.world!!.computeIfAbsent(ChunkPos(-1, 0)).chunk
val chunkC = client.world!!.computeIfAbsent(ChunkPos(-2, 0)).chunk
val tile = Starbound.getTileDefinition("alienrock")
for (x in -6 .. 6) {
for (y in 0 .. 4) {
val chnk = client.world!!.computeIfAbsent(ChunkPos(x, y))
if (y == 0) {
for (bx in 0 .. 31) {
for (by in 0 .. 3) {
chnk.chunk.foreground[bx, by] = tile
}
}
}
}
}
run {
val chunk = client.world!!.computeIfAbsent(ChunkPos(-2, 0)).chunk
for (y in 0 .. CHUNK_SIZE_FF) {
for (x in 0 .. (CHUNK_SIZE_FF - y)) {
chunk.foreground[x, y] = tile
}
}
}
run {
val chunk = client.world!!.computeIfAbsent(ChunkPos(-3, 0)).chunk
for (y in 0 .. CHUNK_SIZE_FF) {
for (x in 0 .. (CHUNK_SIZE_FF - y * 2)) {
chunk.foreground[x, y] = tile
}
}
}
for (x in 0 .. 31) {
for (y in 0 .. 3) {
chunkA!!.foreground[x, y] = tile
}
}
for (x in 0 .. 31) {
for (y in 8 .. 9) {
chunkA!!.foreground[x, y] = tile
}
}
for (x in 0 .. 31) {
for (y in 0 .. 0) {
chunkB.foreground[x, y] = tile
}
}
for (x in 4 .. 8) {
for (y in 4 .. 8) {
chunkA!!.foreground[x, y] = null as TileDefinition?
}
}
chunkA!!.foreground[18, 14] = tile
/*val rand = Random()
for (i in 0 .. 400) {
chunkA!!.foreground[rand.nextInt(0, CHUNK_SIZE_FF), rand.nextInt(0, CHUNK_SIZE_FF)] = tile
}*/
// ent.movement.dropToFloor()
for ((i, proj) in Starbound.projectilesAccess.values.withIndex()) {
val projEnt = Projectile(client.world!!, proj)
projEnt.position = Vector2d(i * 2.0, 10.0)
projEnt.spawn()
}
run {
val stripes = 0
for (stripe in 0 until stripes) {
for (x in 0 .. (stripes - stripe)) {
val movingBody = client.world!!.physics.createBody(BodyDef(
type = BodyType.DYNAMIC,
position = Vector2d(x = (-stripes + stripe) * 1.0 + x * 2.1, y = 8.0 + stripe * 2.1),
gravityScale = 1.1
))
val dynamicBox: IShape<*>
if (false) {
dynamicBox = PolygonShape()
dynamicBox.setAsBox(1.0, 1.0)
} else {
dynamicBox = CircleShape(1.0)
}
movingBody.createFixture(FixtureDef(
shape = dynamicBox,
density = 1.0,
friction = 0.3
))
}
}
}
}
ent.position += Vector2d(y = 36.0, x = -10.0)
client.onDrawGUI {
client.gl.font.render("${ent.position}", y = 100f, scale = 0.25f)
client.gl.font.render("${ent.movement.velocity}", y = 120f, scale = 0.25f)
}
client.onPreDrawWorld {
//client.camera.pos.x = ent.pos.x.toFloat()
//client.camera.pos.y = ent.pos.y.toFloat()
}
client.camera.pos.y = 10f
client.gl.box2dRenderer.drawShapes = true
client.gl.box2dRenderer.drawPairs = true
client.gl.box2dRenderer.drawAABB = false
client.gl.box2dRenderer.drawJoints = false
ent.spawn()
while (client.renderFrame()) {
Starbound.pollCallbacks()
//ent.think(client.frameRenderTime)
client.camera.pos.x = ent.position.x.toFloat()
client.camera.pos.y = ent.position.y.toFloat()
//println(client.camera.velocity.toDoubleVector() * client.frameRenderTime * 0.1)
//if (ent.onGround)
//ent.velocity += client.camera.velocity.toDoubleVector() * client.frameRenderTime * 0.1
if (client.input.KEY_LEFT_DOWN) {
ent.movement.moveDirection = Move.MOVE_LEFT
} else if (client.input.KEY_RIGHT_DOWN) {
ent.movement.moveDirection = Move.MOVE_RIGHT
} else {
ent.movement.moveDirection = Move.STAND_STILL
}
if (client.input.KEY_SPACE_PRESSED && ent.movement.onGround) {
ent.movement.requestJump()
} else if (client.input.KEY_SPACE_RELEASED) {
ent.movement.recallJump()
}
if (client.input.KEY_ESCAPE_PRESSED) {
glfwSetWindowShouldClose(client.window, true)
}
ent.wantsToDuck = client.input.KEY_DOWN_DOWN
//if (chunkA != null && glfwGetTime() < 10.0) {
// val tile = Starbound.getTileDefinition("alienrock")
//chunkA!!.foreground[rand.nextInt(0, CHUNK_SIZE_FF), rand.nextInt(0, CHUNK_SIZE_FF)] = tile
//}
}
}