Files
CavesOfJolk-libgdx/src/main/kotlin/Game.kt

167 lines
5.5 KiB
Kotlin
Raw Normal View History

2023-08-16 13:10:48 +10:00
package gay.pizza.CavesOfJolk
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.audio.Sound
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g3d.*
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
2023-08-16 16:26:05 +10:00
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute
2023-08-16 13:10:48 +10:00
import com.badlogic.gdx.graphics.g3d.attributes.IntAttribute
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
import com.badlogic.gdx.graphics.g3d.utils.DefaultShaderProvider
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder
import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor
import com.badlogic.gdx.math.*
import com.badlogic.gdx.utils.ScreenUtils
import gay.pizza.CavesOfJolk.Resources.Companion.assetManager
class Game: ApplicationAdapter()
{
private lateinit var texJolk: Texture
private lateinit var fntComic: BitmapFont
private lateinit var nut: Sound
private lateinit var cube: Model
private lateinit var floor: Model
private lateinit var spriteBatch: SpriteBatch
private lateinit var modelBatch: ModelBatch
private lateinit var env: Environment
private lateinit var colin: Colin
private var jolkRot = 0.0f
private lateinit var cubeInstance: ModelInstance
private lateinit var floorInstance: ModelInstance
private lateinit var suzanneInstance: ModelInstance
private fun makeCube(texture: Texture): Model
{
val modelBuilder = ModelBuilder()
val size = 2.0f
2023-08-16 16:26:05 +10:00
val material = Material(
ColorAttribute.createDiffuse(XnaColor.White),
TextureAttribute(TextureAttribute.Diffuse,
TextureDescriptor(texture,
Texture.TextureFilter.Linear,
Texture.TextureFilter.Linear,
Texture.TextureWrap.ClampToEdge,
Texture.TextureWrap.ClampToEdge)),
ColorAttribute.createSpecular(XnaColor.Gray),
FloatAttribute.createShininess(20.0f))
2023-08-16 13:10:48 +10:00
val attribs = VertexAttributes.Usage.Position or VertexAttributes.Usage.TextureCoordinates or VertexAttributes.Usage.Normal
return modelBuilder.createBox(size, size, size, material, attribs.toLong())
}
private fun makeFloor(texture: Texture): Model
{
val modelBuilder = ModelBuilder()
2023-08-16 16:26:05 +10:00
val material = Material(
ColorAttribute.createDiffuse(XnaColor.BlanchedAlmond.lighten(0.01)),
ColorAttribute.createSpecular(XnaColor.BlanchedAlmond.mix(XnaColor.Black, 0.4f)),
FloatAttribute.createShininess(65.0f),
TextureAttribute.createDiffuse(texture))
2023-08-16 13:10:48 +10:00
val attribs = VertexAttributes.Usage.Position or VertexAttributes.Usage.TextureCoordinates or VertexAttributes.Usage.Normal
val size = 10.0f
return modelBuilder.createRect(
0.0f, 0.0f, 0.0f,
size, 0.0f, 0.0f,
size, 0.0f, -size,
0.0f, 0.0f, -size,
0.0f, 1.0f, 0.0f,
material, attribs.toLong())
}
override fun create()
{
Resources.instance.loadAssets()
assetManager.finishLoading()
texJolk = assetManager.get("jolkmeup.jpg")
fntComic = assetManager.get("Comic Sans MS.ttf")
2023-08-16 16:26:05 +10:00
val suzanne = assetManager.get("suzanne.g3db", Model::class.java)
2023-08-16 13:10:48 +10:00
2023-08-16 16:26:05 +10:00
// Override ridiculous shininess because i cbf to fix the model
for (i in suzanne.materials)
i.set(FloatAttribute.createShininess(30.0f))
2023-08-16 13:10:48 +10:00
cube = makeCube(texJolk)
floor = makeFloor(assetManager.get("cobblestone.png"))
spriteBatch = SpriteBatch()
env = Environment()
env.set(IntAttribute.createCullFace(GL20.GL_BACK))
env.set(ColorAttribute.createAmbientLight(XnaColor.DarkSlateGray.lighten(-0.6)))
env.set(ColorAttribute.createFog(XnaColor.CornflowerBlue))
env.add(DirectionalLight().set(XnaColor.White, Vector3(1.0f, -1.0f, -1.0f).nor()))
val shaderConfig = DefaultShader.Config(
Gdx.files.internal("lit.vert.glsl").readString(),
Gdx.files.internal("lit.frag.glsl").readString())
shaderConfig.numDirectionalLights = 1
shaderConfig.numPointLights = 0
shaderConfig.numBones = 0
modelBatch = ModelBatch(DefaultShaderProvider(shaderConfig))
colin = Colin()
cubeInstance = ModelInstance(cube)
floorInstance = ModelInstance(floor)
2023-08-16 16:26:05 +10:00
suzanneInstance = ModelInstance(suzanne)
2023-08-16 13:10:48 +10:00
suzanneInstance.transform = Matrix4().translate(3.0f, 1.0f, -3.5f)
}
private fun update(deltaTime: Float)
{
colin.update(deltaTime)
jolkRot += 15.0f * deltaTime
}
override fun render()
{
val deltaTime = Gdx.graphics.deltaTime
update(deltaTime)
val fogColour = XnaColor.CornflowerBlue.lighten(Math.sin(0.056 * jolkRot.toDouble()) * 0.15 - 0.15)
ScreenUtils.clear(fogColour, true)
env.set(ColorAttribute.createFog(fogColour))
val jolkPos = Vector3(0.0f, 1.0f + MathUtils.sin(jolkRot * 0.25f) * 0.25f, -4.0f)
val world = Matrix4()
.setTranslation(jolkPos)
.rotateRad(1.0f, 0.0f, 0.0f, jolkRot * 0.25f)
.rotate(0.0f, 1.0f, 0.0f, jolkRot)
.scale(0.25f, 0.25f, 0.25f)
cubeInstance.transform = world
modelBatch.begin(colin.camera)
modelBatch.render(floorInstance, env)
modelBatch.render(cubeInstance, env)
modelBatch.render(suzanneInstance, env)
modelBatch.end()
spriteBatch.begin()
colin.draw(spriteBatch)
val textPos = colin.camera.project(jolkPos)
if (textPos.z < 1.0)
fntComic.draw(spriteBatch, "I am filled with jolk", textPos.x, textPos.y)
fntComic.draw(spriteBatch, "${colin.position.x}\n${colin.position.y}", 10.0f, Gdx.graphics.height - 10.0f)
spriteBatch.end()
}
override fun dispose()
{
floor.dispose()
cube.dispose()
modelBatch.dispose()
spriteBatch.dispose()
assetManager.dispose()
}
}