mirror of
https://github.com/jlengrand/korge-samples.git
synced 2026-03-10 08:31:18 +00:00
Box2D
This commit is contained in:
@@ -1,66 +1,106 @@
|
||||
import com.soywiz.korge.*
|
||||
import com.soywiz.korge.admob.*
|
||||
import com.soywiz.korge.box2d.*
|
||||
import com.soywiz.korge.input.*
|
||||
import com.soywiz.korge.view.*
|
||||
import com.soywiz.korgw.*
|
||||
import com.soywiz.korim.color.*
|
||||
import com.soywiz.korim.format.*
|
||||
import com.soywiz.korim.vector.*
|
||||
import com.soywiz.korio.async.*
|
||||
import com.soywiz.korio.file.std.*
|
||||
import org.jbox2d.callbacks.*
|
||||
import org.jbox2d.collision.*
|
||||
import com.soywiz.korio.lang.*
|
||||
import com.soywiz.korma.geom.vector.*
|
||||
import org.jbox2d.collision.shapes.*
|
||||
import org.jbox2d.dynamics.*
|
||||
import org.jbox2d.dynamics.contacts.*
|
||||
|
||||
suspend fun main() = Korge(quality = GameWindow.Quality.PERFORMANCE, title = "My Awesome Box2D Game!") {
|
||||
val admob = AdmobCreate(testing = true)
|
||||
|
||||
println("STARTED!")
|
||||
|
||||
addUpdatable {
|
||||
//println("FRAME!")
|
||||
}
|
||||
|
||||
launchImmediately {
|
||||
try {
|
||||
admob.bannerPrepareAndShow(Admob.Config("ca-app-pub-xxx/xxx"))
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
views.clearColor = Colors.DARKGREEN
|
||||
solidRect(300, 200, Colors.DARKCYAN)
|
||||
sgraphics {
|
||||
fill(Colors.DARKCYAN) {
|
||||
rect(-1, -1, 3, 2)
|
||||
}
|
||||
fill(Colors.AQUAMARINE) {
|
||||
circle(0, 0, 1)
|
||||
}
|
||||
fill(Colors.AQUAMARINE) {
|
||||
circle(1, 0, 1)
|
||||
}
|
||||
position(100, 100)
|
||||
}.scale(100, 100).interactive()
|
||||
worldView {
|
||||
position(400, 400).scale(20)
|
||||
|
||||
createBody {
|
||||
setPosition(0, -10)
|
||||
}.fixture {
|
||||
shape = BoxShape(100, 20)
|
||||
density = 0f
|
||||
}.setView(solidRect(100, 20, Colors.RED).position(-50, -10))
|
||||
}.setViewWithContainer(solidRect(100, 20, Colors.RED).position(-50, -10).interactive())
|
||||
|
||||
val ball = createBody {
|
||||
// Dynamic Body
|
||||
createBody {
|
||||
type = BodyType.DYNAMIC
|
||||
setPosition(0, 0)
|
||||
setPosition(0, 7)
|
||||
}.fixture {
|
||||
shape = BoxShape(4f, 4f)
|
||||
density = 985f
|
||||
friction = 0f
|
||||
userData = "ball"
|
||||
}.setView(container {
|
||||
// [...]
|
||||
})
|
||||
shape = BoxShape(2f, 2f)
|
||||
density = 0.5f
|
||||
friction = 0.2f
|
||||
}.setView(solidRect(2f, 2f, Colors.GREEN).anchor(.5, .5).interactive())
|
||||
|
||||
val enemy = createBody {
|
||||
createBody {
|
||||
type = BodyType.DYNAMIC
|
||||
setPosition(5, 0)
|
||||
setPosition(0.75, 13)
|
||||
}.fixture {
|
||||
shape = BoxShape(10, 20)
|
||||
}.setView(container {
|
||||
image(resourcesVfs["korge-ui.png"].readBitmap())
|
||||
.size(4f, 4f)
|
||||
.position(-2f, -2f)
|
||||
})
|
||||
}.apply {
|
||||
world.setContactListener(contactListener)
|
||||
shape = BoxShape(2f, 2f)
|
||||
density = 1f
|
||||
friction = 0.2f
|
||||
}.setView(sgraphics {
|
||||
fill(Colors.BLUE) {
|
||||
rect(-1f, -1f, 2f, 2f)
|
||||
}
|
||||
}.interactive())
|
||||
|
||||
createBody {
|
||||
type = BodyType.DYNAMIC
|
||||
setPosition(0.5, 15)
|
||||
}.fixture {
|
||||
shape = CircleShape().apply { m_radius = 2f }
|
||||
density = 22f
|
||||
friction = 3f
|
||||
}.setView(sgraphics {
|
||||
fillStroke(Context2d.Color(Colors.BLUE), Context2d.Color(Colors.RED), Context2d.StrokeInfo(thickness = 0.3)) {
|
||||
circle(0, 0, 2)
|
||||
//rect(0, 0, 400, 20)
|
||||
}
|
||||
fill(Colors.DARKCYAN) {
|
||||
circle(1, 1, 0.2)
|
||||
}
|
||||
hitTestUsingShapes = true
|
||||
}.interactive())
|
||||
}
|
||||
image(resourcesVfs["korge.png"].readBitmap())
|
||||
}
|
||||
|
||||
val contactListener = object : ContactListener {
|
||||
override fun beginContact(contact: Contact) {
|
||||
println("beginContact")
|
||||
}
|
||||
|
||||
override fun endContact(contact: Contact) {
|
||||
println("endContact")
|
||||
}
|
||||
|
||||
override fun postSolve(contact: Contact, impulse: ContactImpulse) {
|
||||
println("postSolve")
|
||||
}
|
||||
|
||||
override fun preSolve(contact: Contact, oldManifold: Manifold) {
|
||||
println("preSolve")
|
||||
}
|
||||
fun <T : View> T.interactive(): T = this.apply {
|
||||
alpha = 0.5
|
||||
onOver { alpha = 1.0 }
|
||||
onOut { alpha = 0.5 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user