156 lines
5.1 KiB
Kotlin
156 lines
5.1 KiB
Kotlin
|
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
|
||
|
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
|
||
|
//val material = Material(ColorAttribute.createDiffuse(Color.WHITE))
|
||
|
val material = Material(TextureAttribute(TextureAttribute.Diffuse,
|
||
|
TextureDescriptor(texture,
|
||
|
Texture.TextureFilter.Linear,
|
||
|
Texture.TextureFilter.Linear,
|
||
|
Texture.TextureWrap.ClampToEdge,
|
||
|
Texture.TextureWrap.ClampToEdge)))
|
||
|
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()
|
||
|
//val material = Material(ColorAttribute.createDiffuse(XnaColor.BlanchedAlmond))
|
||
|
val material = Material(TextureAttribute.createDiffuse(texture))
|
||
|
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")
|
||
|
|
||
|
|
||
|
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)
|
||
|
suzanneInstance = ModelInstance(assetManager.get("suzanne.g3db", Model::class.java))
|
||
|
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()
|
||
|
}
|
||
|
}
|