lightning bolt WIP, player sprite declaration macro

Former-commit-id: d4999cda37bff2aa27615183aa125bfbc8cfa975
Former-commit-id: 224aee51d4fb7e5fe9d11bf3c0cc758a5604251d
This commit is contained in:
Song Minjae
2016-10-31 13:26:26 +09:00
parent e4886acc92
commit 5e82f87ee5
8 changed files with 92 additions and 108 deletions

View File

@@ -2,6 +2,8 @@ package net.torvald.terrarum
import com.jme3.math.FastMath
import net.torvald.point.Point2d
import net.torvald.random.HQRNG
import net.torvald.terrarum.gameactors.floorInt
import net.torvald.terrarum.gameactors.roundInt
import net.torvald.terrarum.virtualcomputer.terminal.ALException
@@ -9,6 +11,7 @@ import org.apache.commons.csv.CSVRecord
import org.lwjgl.BufferUtils
import org.lwjgl.openal.AL
import org.lwjgl.openal.AL10
import org.newdawn.slick.Color
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Graphics
import org.newdawn.slick.state.BasicGameState
@@ -27,117 +30,57 @@ class StateTestingSandbox : BasicGameState() {
override fun init(container: GameContainer?, game: StateBasedGame?) {
playTone()
}
private val sampleRate = 22050
private var beepSource: Int? = null
private var beepBuffer: Int? = null
val lightning_start = Point2d(50.0, 200.0)
val lightning_end = Point2d(750.0, 200.0)
/**
* @param duration : milliseconds
*/
private fun makeAudioData(duration: Int, freq: Float): ByteBuffer {
val audioData = BufferUtils.createByteBuffer(duration.times(sampleRate).div(1000))
val realDuration = duration * sampleRate / 1000
val chopSize = freq * 2f / sampleRate
val amp = Math.max(4600f / freq, 1f)
val nHarmonics = 4
val transitionThre = 1150f
if (freq < transitionThre) { // chopper generator (for low freq)
for (x in 0..realDuration - 1) {
var sine: Float = amp * FastMath.cos(FastMath.PI * x * chopSize)
if (sine > 1f) sine = 1f
else if (sine < -1f) sine = -1f
audioData.put(
(0.5f + 0.5f * sine).times(0xFF).toByte()
)
}
}
else { // harmonics generator (for high freq)
for (x in 0..realDuration - 1) {
var sine: Float = 0f
for (k in 0..nHarmonics) { // mix only odd harmonics to make squarewave
sine += (1f / (2*k + 1)) *
FastMath.sin((2*k + 1) * FastMath.PI * x * chopSize)
}
audioData.put(
(0.5f + 0.5f * sine).times(0xFF).toByte()
)
}
}
audioData.rewind()
return audioData
}
var audioData: ByteBuffer? = null
private fun playTone() {
if (audioData == null) audioData = makeAudioData(5000, 27.5f)
if (!AL.isCreated()) AL.create()
// Clear error stack.
AL10.alGetError()
beepBuffer = AL10.alGenBuffers()
checkALError()
try {
AL10.alBufferData(beepBuffer!!, AL10.AL_FORMAT_MONO8, audioData, sampleRate)
checkALError()
beepSource = AL10.alGenSources()
checkALError()
try {
AL10.alSourceQueueBuffers(beepSource!!, beepBuffer!!)
checkALError()
AL10.alSource3f(beepSource!!, AL10.AL_POSITION, 0f, 0f, 1f)
AL10.alSourcef(beepSource!!, AL10.AL_REFERENCE_DISTANCE, 1f)
AL10.alSourcef(beepSource!!, AL10.AL_MAX_DISTANCE, 1f)
AL10.alSourcef(beepSource!!, AL10.AL_GAIN, 0.3f)
checkALError()
AL10.alSourcePlay(beepSource!!)
checkALError()
}
catch (e: ALException) {
AL10.alDeleteSources(beepSource!!)
}
}
catch (e: ALException) {
if (beepSource != null) AL10.alDeleteSources(beepSource!!)
}
}
val bolt = LightingBolt(lightning_start, lightning_end, 20)
override fun update(container: GameContainer?, game: StateBasedGame?, delta: Int) {
}
// Custom implementation of Util.checkALError() that uses our custom exception.
private fun checkALError() {
val errorCode = AL10.alGetError()
if (errorCode != AL10.AL_NO_ERROR) {
throw ALException(errorCode)
override fun getID() = Terrarum.STATE_ID_TEST_SHIT
override fun render(container: GameContainer, game: StateBasedGame, g: Graphics) {
g.color = Color.white
g.lineWidth = 3f
//g.drawLine(lightning_start, lightning_end)
bolt.draw(g)
}
}
fun Graphics.drawLine(p1: Point2d, p2: Point2d) {
drawLine(p1.x.toFloat(), p1.y.toFloat(), p2.x.toFloat(), p2.y.toFloat())
}
class LightingBolt(val start: Point2d, val end: Point2d, val segments: Int) {
val mainBolt = LinkedList<Point2d>() //Pair<Length, Y-Pos>
val boltYDev = 20.0
init {
val length = start.length(end)
for (i in 0..segments - 1) {
mainBolt.add(
Point2d(
start.x + length / segments * i,
start.y + HQRNG().nextFloat().times(2.0).minus(1.0).times(boltYDev)
)
)
}
}
fun draw(g: Graphics) {
for (i in 0..segments - 1) {
val startpoint = mainBolt[i]
val endpoint = if (i == segments - 1) end else mainBolt[i + 1]
override fun getID() = Terrarum.STATE_ID_TEST_SHIT
override fun render(container: GameContainer?, game: StateBasedGame?, g: Graphics?) {
g.drawLine(startpoint, endpoint)
}
}
}