Created
March 9, 2021 09:57
-
-
Save Sven65/0006e242e477c00d9a82206c045e5719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.mackan.rpggame.events | |
interface Event { | |
val eventName: String | |
} | |
data class GameObjectSelectEvent(val instanceId: Int): Event { | |
override val eventName: String = "GameObjectSelectEvent" | |
companion object : Event { | |
override val eventName: String = "GameObjectSelectEvent" | |
} | |
} | |
data class GameObjectDeselectEvent(val instanceId: Int): Event { | |
override val eventName: String = "GameObjectDeselectEvent" | |
companion object : Event { | |
override val eventName: String = "GameObjectDeselectEvent" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.mackan.rpggame.events | |
open class EventEmitter { | |
var handlerMap = mutableMapOf<String, MutableList<(Any) -> Unit>>() | |
/** | |
* Adds the [handler] to the subscribers for the event [event] | |
*/ | |
fun <T : Event> on (event: Event, handler: (T) -> Unit) { | |
handlerMap.putIfAbsent(event.eventName, mutableListOf<(T: Any) -> Unit>()) | |
handlerMap[event.eventName]!!.add(handler as (Any) -> Unit) | |
} | |
/** | |
* Emits the [event] to it's subscribers | |
*/ | |
fun <T: Event> emit (event: T) { | |
if (!handlerMap.containsKey(event.eventName)) return | |
for (subscriber in handlerMap[event.eventName]!!) { | |
subscriber(event) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.mackan.rpggame | |
import com.badlogic.gdx.InputProcessor | |
import com.badlogic.gdx.math.Vector3 | |
import cookiedragon.eventsystem.EventDispatcher | |
import xyz.mackan.rpggame.events.* | |
import xyz.mackan.rpggame.utils.GameObject | |
class Manager : EventEmitter() { | |
init { | |
this.on<GameObjectSelectEvent>(GameObjectSelectEvent) { | |
println("Selected ${it.instanceId}") | |
} | |
} | |
fun doEmit () { | |
this.emit(GameObjectSelectEvent(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment