Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
add unitTest sample for haxe API
Browse files Browse the repository at this point in the history
  • Loading branch information
LeHaine committed Dec 7, 2020
1 parent 2ed7fd9 commit 3f275de
Show file tree
Hide file tree
Showing 4 changed files with 2,345 additions and 10 deletions.
62 changes: 52 additions & 10 deletions samples/src/main/kotlin/com/lehaine/samples/GdxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,87 @@ package com.lehaine.samples

import com.badlogic.gdx.ApplicationListener
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.badlogic.gdx.utils.viewport.FitViewport
import com.lehaine.ldtk.LayerIntGrid

class GdxApp : ApplicationListener {

private lateinit var spriteBatch: SpriteBatch
private lateinit var tiles: Texture
private lateinit var shapeRenderer: ShapeRenderer
private lateinit var worldTiles: Texture
private lateinit var unitTestWorldTiles: Texture
private lateinit var camera: OrthographicCamera
private lateinit var viewport: FitViewport
private val world = World()
private val testLevel = world.allLevels[0]
private val unitTestWorld = UnitTestWorld()
private val worldLevel = world.allLevels[0]
private val unitTestWorldLevel = unitTestWorld.allLevels[0]
private var showWorld = true

override fun create() {
spriteBatch = SpriteBatch()
tiles = Texture(Gdx.files.internal("Cavernas_by_Adam_Saltsman.png"))
shapeRenderer = ShapeRenderer()
worldTiles = Texture(Gdx.files.internal("Cavernas_by_Adam_Saltsman.png"))
unitTestWorldTiles = Texture(Gdx.files.internal("Minecraft_texture_pack.gif"))
camera = OrthographicCamera()
viewport = PixelPerfectViewport(480f, 270f, camera)
camera.translate(testLevel.pxWidth / 2f, testLevel.pxHeight / -2f)
camera.translate(worldLevel.pxWidth / 2f, worldLevel.pxHeight / -2f + 20f)
}

override fun resize(width: Int, height: Int) {
viewport.update(width, height, false)
}

override fun render() {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
val bgColorHex = if (showWorld) world.bgColorHex else unitTestWorld.bgColorHex
val bgColor = Color.valueOf(bgColorHex)
Gdx.gl.glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
if (Gdx.input.isKeyJustPressed(Input.Keys.S)) {
showWorld = !showWorld
}
camera.update()
spriteBatch.projectionMatrix = camera.combined
spriteBatch.begin()
testLevel.layerBackground.render(spriteBatch, tiles)
testLevel.layerCollisions.render(spriteBatch, tiles)
testLevel.layerCustom_tiles.render(spriteBatch, tiles)
spriteBatch.end()
shapeRenderer.projectionMatrix = camera.combined
if (showWorld) {
spriteBatch.begin()
worldLevel.layerBackground.render(spriteBatch, worldTiles)
worldLevel.layerCollisions.render(spriteBatch, worldTiles)
worldLevel.layerCustom_tiles.render(spriteBatch, worldTiles)
spriteBatch.end()
} else {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
renderIntGrid(shapeRenderer, unitTestWorldLevel.layerIntGrid8)
renderIntGrid(shapeRenderer, unitTestWorldLevel.layerIntGridTest)
shapeRenderer.end()
spriteBatch.begin()
unitTestWorldLevel.layerTileTest.render(spriteBatch, unitTestWorldTiles)
unitTestWorldLevel.layerPure_AutoLayer.render(spriteBatch, worldTiles)
unitTestWorldLevel.layerIntGrid_AutoLayer.render(spriteBatch, worldTiles)
spriteBatch.end()
}
}

private fun renderIntGrid(shapeRenderer: ShapeRenderer, layerIntGrid: LayerIntGrid) {
for (cx in 0..layerIntGrid.cWidth) {
for (cy in 0..layerIntGrid.cHeight) {
if (layerIntGrid.hasValue(cx, cy)) {
val colorHex = layerIntGrid.getColorHex(cx, cy)
val gridSize = layerIntGrid.gridSize.toFloat()
shapeRenderer.color = Color.valueOf(colorHex)
shapeRenderer.rect(cx * gridSize, -cy * gridSize, gridSize, gridSize)
}
}
}
}

override fun pause() {
Expand Down
2 changes: 2 additions & 0 deletions samples/src/main/kotlin/com/lehaine/samples/Sample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.lehaine.ldtk.Point
@LDtkProject(ldtkFileLocation = "sample.ldtk", name = "World")
class _World

@LDtkProject(ldtkFileLocation = "unitTest.ldtk", name = "UnitTestWorld")
class _UnitTestWorld
fun main(args: Array<String>) {
// create new LDtk world
val world = World()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3f275de

Please sign in to comment.