Skip to content

Latest commit

 

History

History
125 lines (100 loc) · 3.11 KB

README.md

File metadata and controls

125 lines (100 loc) · 3.11 KB

GitHub Workflow Status Sonatype Nexus (Releases) PIXI.js Version

PIXI-Kotlin

This is a simple example of what a PIXI.js transcription in Kotlin could look like.

For now, there are all the classes, interfaces, enums, functions, type aliases, and objects. The private members are not present.

The types are from PIXI 6.5.2.

Usage

To use it in a project, just add this to your dependencies:

repositories {
	...
	mavenLocal()
}

dependencies {
	implementation("io.github.ayfri:PIXI-Kotlin:VERSION")
}

If you want to add optional PIXI modules, you have to implement them in your project.

const val PIXI_KOTLIN_VERSION = "VERSION"

dependencies {
	implementation("io.github.ayfri:PIXI-Kotlin:PIXI_KOTLIN_VERSION")
	implementation("io.github.ayfri:PIXI-Kotlin-unsafe-eval:PIXI_KOTLIN_VERSION")
}

Example

Simple application with a sprite, size change when clicking.

fun main() {
	val app = application {
		backgroundColor = Color(120, 200, 230)
		resizeTo = window
	}
	app.addToBody()
	
	val bunny = sprite("bunny.png") {
		setPositionFromWindow(0.5, 0.5)
		anchor.set(0.5)
		interactive = true
		addToApplication(app)
	}
	
	bunny.on(DisplayObjectEvents.mousedown) {
		bunny.scale.set(1.1)
	}
	
	bunny.on(DisplayObjectEvents.mouseup) {
		bunny.scale.set(1.0)
	}
}

Application with keymap and test if sprite sticks out of area.

fun main() {
	val app = application {
		resizeTo = window
	}
	app.addToBody()
	
	val speed = 10.0
	val sprite = sprite(generateBlankTexture(app) {
		width = 300.0
		height = 300.0
		color = Color(255, 0, 0)
	}) {
		addToApplication(app)
		anchor.set(0.5)
		setPositionFromApplication(app, 0.5, 0.5)
		window["sprite"] = this
	}
	
	val area = Rectangle(0.0, 0.0, app.screen.width, app.screen.height)
	
	app.renderer.on(AbstractRendererEvents.resize) {
		area.setSize(app.screen.width, app.screen.height)
	}
	
	
	val english = "en" in window.navigator.languages.elementAtOrElse(0) { window.navigator.language }
	KeyMap(
		mapOf(
			"forward" to setOf(if (english) "W" else "Z", "ArrowUp"),
			"backward" to setOf("S", "ArrowDown"),
			"left" to setOf(if (english) "A" else "Q", "ArrowLeft"),
			"right" to setOf("D", "ArrowRight"),
			"power" to setOf(" ")
		),
		ignoreCase = true
	).apply {
		onKeep("forward") {
			if ((sprite.hitBox.top + speed * 2) > area.top) sprite.y -= speed
		}
		onKeep("backward") {
			if ((sprite.hitBox.bottom - speed * 2) < area.bottom) sprite.y += speed
		}
		onKeep("left") {
			if ((sprite.hitBox.left + speed * 2) > area.left) sprite.x -= speed
		}
		onKeep("right") {
			if ((sprite.hitBox.right - speed * 2) < area.right) sprite.x += speed
		}
		
		onPress("power") {
			sprite.alpha = if (sprite.alpha == 0.1) 1.0 else 0.1
		}
	}
}