buesingniklas

Untitled

Dec 2nd, 2021
972
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package army.warfare.holo.util.misc
  2.  
  3. import army.warfare.holo.events.UpdateEvent
  4. import com.darkmagician6.eventapi.EventManager
  5. import com.darkmagician6.eventapi.EventTarget
  6. import kotlinx.coroutines.*
  7. import kotlin.coroutines.Continuation
  8. import kotlin.coroutines.resume
  9. import kotlin.coroutines.suspendCoroutine
  10.  
  11. class SequenceList {
  12.     private val sequences = mutableListOf<Sequence>()
  13.  
  14.     fun add(sequence: Sequence) {
  15.         sequences.add(sequence)
  16.     }
  17.  
  18.     fun register() {
  19.         EventManager.register(this)
  20.     }
  21.  
  22.     fun unregister() {
  23.         EventManager.unregister(this)
  24.     }
  25.  
  26.     @EventTarget
  27.     fun updateSequences(event: UpdateEvent.Pre) {
  28.         for (seq in sequences) {
  29.             val angle = seq.tick() ?: continue
  30.             if (angle.first != null) {
  31.                 event.yaw = angle.first!!
  32.             }
  33.             if (angle.second != null) {
  34.                 event.pitch = angle.second!!
  35.             }
  36.         }
  37.     }
  38. }
  39.  
  40. class Sequence(val handler: suspend Sequence.() -> Unit) {
  41.  
  42.     private var continuation: Continuation<Unit>? = null
  43.     private var remainingTicks: Int = 0
  44.     private var coroutine: Job = buildCoroutine()
  45.  
  46.     private var rotationYaw: Float? = null
  47.     private var rotationPitch: Float? = null
  48.  
  49.  
  50.     private fun buildCoroutine(): Job {
  51.         return GlobalScope.launch(Dispatchers.Unconfined) {
  52.             sync()
  53.             while (isActive) {
  54.                 handler()
  55.                 sync()
  56.             }
  57.         }
  58.     }
  59.  
  60.     fun reset() {
  61.         remainingTicks = 0
  62.         continuation = null
  63.         coroutine = buildCoroutine()
  64.     }
  65.  
  66.     fun tick(): Pair<Float?, Float?>? {
  67.         if (remainingTicks > 0) {
  68.             remainingTicks--
  69.         } else {
  70.             if (coroutine.isActive) {
  71.                 continuation?.resume(Unit)
  72.  
  73.                 if (rotationYaw != null || rotationPitch != null) {
  74.                     val angle = Pair(rotationYaw, rotationPitch)
  75.                     rotationYaw = null
  76.                     rotationPitch = null
  77.                     return angle
  78.                 }
  79.             }
  80.         }
  81.  
  82.         return null
  83.     }
  84.  
  85.     suspend fun wait(ticks: Int) {
  86.         remainingTicks = ticks
  87.         suspendCoroutine<Unit> { continuation = it }
  88.     }
  89.  
  90.     fun rotate(yaw: Float?, pitch: Float?) {
  91.         rotationYaw = yaw
  92.         rotationPitch = pitch
  93.     }
  94.  
  95.     suspend fun sync() {
  96.         wait(0)
  97.     }
  98.  
  99. }
RAW Paste Data