Skip to content

Commit

Permalink
Convert context to use generics/dynamic characters #466
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Jan 16, 2025
1 parent 8aca877 commit 7045e86
Show file tree
Hide file tree
Showing 139 changed files with 666 additions and 594 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package world.gregs.voidps.engine.client.ui

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.mode.interact.Interaction
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.entity.item.Item
import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events

data class InterfaceOption(
override val character: Character,
override val character: Player,
val id: String,
val component: String,
val optionIndex: Int,
val option: String,
val item: Item,
val itemSlot: Int,
val inventory: String
) : Interaction() {
) : Interaction<Player>() {
override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events

data class Command(
override val character: Character,
val player: Player,
val prefix: String,
val content: String
) : Interaction() {
) : Interaction<Player>() {
override val character = player

override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package world.gregs.voidps.engine.client.ui.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.mode.interact.Interaction
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.entity.item.Item
import world.gregs.voidps.engine.entity.item.floor.FloorItem
import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.suspend.arriveDelay

data class ItemOnFloorItem(
override val character: Character,
data class ItemOnFloorItem<C : Character>(
override val character: C,
val floorItem: FloorItem,
val id: String,
val component: String,
val item: Item,
val itemSlot: Int,
val inventory: String
) : Interaction() {
) : Interaction<C>() {
override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 5
Expand All @@ -31,17 +32,17 @@ data class ItemOnFloorItem(
}
}

fun itemOnFloorItemOperate(item: String = "*", floorItem: String = "*", id: String = "*", component: String = "*", arrive: Boolean = true, handler: suspend ItemOnFloorItem.() -> Unit) {
Events.handle<ItemOnFloorItem>("item_approach_floor_item", item, floorItem, id, component) {
fun itemOnFloorItemOperate(item: String = "*", floorItem: String = "*", id: String = "*", component: String = "*", arrive: Boolean = true, handler: suspend ItemOnFloorItem<Player>.() -> Unit) {
Events.handle<ItemOnFloorItem<Player>>("item_approach_floor_item", item, floorItem, id, component) {
if (arrive) {
arriveDelay()
}
handler.invoke(this)
}
}

fun itemOnFloorItemApproach(item: String = "*", floorItem: String = "*", id: String = "*", component: String = "*", handler: suspend ItemOnFloorItem.() -> Unit) {
Events.handle<ItemOnFloorItem>("item_approach_floor_item", item, floorItem, id, component) {
fun itemOnFloorItemApproach(item: String = "*", floorItem: String = "*", id: String = "*", component: String = "*", handler: suspend ItemOnFloorItem<Player>.() -> Unit) {
Events.handle<ItemOnFloorItem<Player>>("item_approach_floor_item", item, floorItem, id, component) {
handler.invoke(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package world.gregs.voidps.engine.client.ui.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.mode.interact.Interaction
import world.gregs.voidps.engine.entity.character.mode.interact.TargetNPCContext
import world.gregs.voidps.engine.entity.character.npc.NPC
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.entity.item.Item
import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.suspend.arriveDelay

data class ItemOnNPC(
override val character: Character,
val player: Player,
override val target: NPC,
val id: String,
val component: String,
val item: Item,
val itemSlot: Int,
val inventory: String
) : Interaction(), TargetNPCContext {
) : Interaction<Player>(), TargetNPCContext<Player> {

override val character = player

override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package world.gregs.voidps.engine.client.ui.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.mode.interact.Interaction
import world.gregs.voidps.engine.entity.character.mode.interact.TargetObjectContext
import world.gregs.voidps.engine.entity.character.player.Player
Expand All @@ -11,14 +10,16 @@ import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.suspend.arriveDelay

data class ItemOnObject(
override val character: Character,
val player: Player,
override val target: GameObject,
val id: String,
val component: String,
val item: Item,
val itemSlot: Int,
val inventory: String
) : Interaction(), TargetObjectContext {
) : Interaction<Player>(), TargetObjectContext<Player> {
override val character = player

override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.suspend.arriveDelay

data class ItemOnPlayer(
override val character: Character,
data class ItemOnPlayer<C: Character>(
override val character: C,
override val target: Player,
val id: String,
val component: String,
val item: Item,
val itemSlot: Int,
val inventory: String
) : Interaction(), TargetPlayerContext {
) : Interaction<C>(), TargetPlayerContext<C> {
override fun copy(approach: Boolean) = copy().apply { this.approach = approach }

override val size = 4
Expand All @@ -31,17 +31,17 @@ data class ItemOnPlayer(
}
}

fun itemOnPlayerOperate(item: String = "*", id: String = "*", component: String = "*", arrive: Boolean = true, override: Boolean = true, handler: suspend ItemOnPlayer.() -> Unit) {
Events.handle<ItemOnPlayer>("item_on_operate_player", item, id, component, override = override) {
fun itemOnPlayerOperate(item: String = "*", id: String = "*", component: String = "*", arrive: Boolean = true, override: Boolean = true, handler: suspend ItemOnPlayer<Player>.() -> Unit) {
Events.handle<ItemOnPlayer<Player>>("item_on_operate_player", item, id, component, override = override) {
if (arrive) {
arriveDelay()
}
handler.invoke(this)
}
}

fun itemOnPlayerApproach(item: String = "*", id: String = "*", component: String = "*", override: Boolean = true, handler: suspend ItemOnPlayer.() -> Unit) {
Events.handle<ItemOnPlayer>("item_on_approach_player", item, id, component, override = override) {
fun itemOnPlayerApproach(item: String = "*", id: String = "*", component: String = "*", override: Boolean = true, handler: suspend ItemOnPlayer<Player>.() -> Unit) {
Events.handle<ItemOnPlayer<Player>>("item_on_approach_player", item, id, component, override = override) {
handler.invoke(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import world.gregs.voidps.engine.entity.character.player.Player
/**
* Context of a queue or interaction action to access a [character] as either [player] or [npc] but which is only known implicitly
*/
interface CharacterContext {
val character: Character
val player: Player
get() = character as Player
val npc: NPC
get() = character as NPC
interface CharacterContext<C: Character> {
val character: C
var onCancel: (() -> Unit)?
val CharacterContext<Player>.player: Player
get() = character
val CharacterContext<NPC>.npc: NPC
get() = character
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ import world.gregs.voidps.engine.suspend.resumeSuspension
class Interact(
character: Character,
val target: Entity,
interaction: Interaction,
interaction: Interaction<*>,
strategy: TargetStrategy = TargetStrategy(target),
private var approachRange: Int? = null,
private val faceTarget: Boolean = true,
shape: Int? = null
) : Movement(character, strategy, shape) {

private var approach: Interaction = interaction.copy(true)
private var operate: Interaction = interaction.copy(false)
private var approach: Interaction<*> = interaction.copy(true)
private var operate: Interaction<*> = interaction.copy(false)
private var clearInteracted = false

fun updateInteraction(interaction: Interaction) {
fun updateInteraction(interaction: Interaction<*>) {
approach = interaction.copy(true)
operate = interaction.copy(false)
clearInteracted = true
Expand Down Expand Up @@ -151,7 +151,7 @@ class Interact(
/**
* Continue any suspended, clear any finished or start a new interaction
*/
private fun launch(event: Interaction): Boolean {
private fun launch(event: Interaction<*>): Boolean {
if (character.suspension != null) {
character.resumeSuspension()
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package world.gregs.voidps.engine.entity.character.mode.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.event.CancellableEvent
import world.gregs.voidps.engine.event.SuspendableEvent

abstract class Interaction : CancellableEvent(), SuspendableEvent, CharacterContext {
abstract class Interaction<C: Character> : CancellableEvent(), SuspendableEvent, CharacterContext<C> {
var approach = false
val operate: Boolean
get() = !approach
override var onCancel: (() -> Unit)? = null
var launched = false

abstract fun copy(approach: Boolean): Interaction
abstract fun copy(approach: Boolean): Interaction<C>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package world.gregs.voidps.engine.entity.character.mode.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.entity.item.floor.FloorItem

interface TargetFloorItemContext : CharacterContext {
interface TargetFloorItemContext<C: Character> : CharacterContext<C> {
val target: FloorItem
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package world.gregs.voidps.engine.entity.character.mode.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.entity.character.npc.NPC

interface TargetNPCContext : CharacterContext {
interface TargetNPCContext<C: Character> : CharacterContext<C> {
val target: NPC
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package world.gregs.voidps.engine.entity.character.mode.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.entity.obj.GameObject

interface TargetObjectContext : CharacterContext {
interface TargetObjectContext<C: Character> : CharacterContext<C> {
val target: GameObject
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package world.gregs.voidps.engine.entity.character.mode.interact

import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.entity.character.CharacterContext
import world.gregs.voidps.engine.entity.character.player.Player

interface TargetPlayerContext : CharacterContext {
interface TargetPlayerContext<C: Character> : CharacterContext<C> {
val target: Player
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.type.Area

data class AreaEntered(
override val character: Character,
data class AreaEntered<C: Character>(
override val character: C,
val name: String,
val tags: Set<String>,
val area: Area
) : SuspendableEvent, CharacterContext {
) : SuspendableEvent, CharacterContext<C> {
override var onCancel: (() -> Unit)? = null

override val size = 5
Expand All @@ -29,20 +29,20 @@ data class AreaEntered(
}
}

fun enterArea(area: String = "*", tag: String = "*", handler: suspend AreaEntered.() -> Unit) {
Events.handle<Player, AreaEntered>("player_enter", area, "player", tag, "*") {
fun enterArea(area: String = "*", tag: String = "*", handler: suspend AreaEntered<Player>.() -> Unit) {
Events.handle<Player, AreaEntered<Player>>("player_enter", area, "player", tag, "*") {
handler.invoke(this)
}
}

fun npcEnterArea(npc: String = "*", area: String = "*", tag: String = "*", handler: suspend AreaEntered.() -> Unit) {
Events.handle<NPC, AreaEntered>("npc_enter", area, npc, tag, "*") {
fun npcEnterArea(npc: String = "*", area: String = "*", tag: String = "*", handler: suspend AreaEntered<NPC>.() -> Unit) {
Events.handle<NPC, AreaEntered<NPC>>("npc_enter", area, npc, tag, "*") {
handler.invoke(this)
}
}

fun characterEnterArea(area: String = "*", tag: String = "*", handler: suspend AreaEntered.() -> Unit) {
val block: suspend AreaEntered.(EventDispatcher) -> Unit = {
fun characterEnterArea(area: String = "*", tag: String = "*", handler: suspend AreaEntered<Character>.() -> Unit) {
val block: suspend AreaEntered<Character>.(EventDispatcher) -> Unit = {
handler.invoke(this)
}
Events.handle("player_enter", area, "player", tag, "*", handler = block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.type.Area

data class AreaExited(
override val character: Character,
data class AreaExited<C: Character>(
override val character: C,
val name: String,
val tags: Set<String>,
val area: Area
) : SuspendableEvent, CharacterContext {
) : SuspendableEvent, CharacterContext<C> {
override var onCancel: (() -> Unit)? = null

override val size = 5
Expand All @@ -29,20 +29,20 @@ data class AreaExited(
}
}

fun exitArea(area: String = "*", tag: String = "*", handler: suspend AreaExited.() -> Unit) {
Events.handle<Player, AreaExited>("player_exit", area, "player", tag, "*") {
fun exitArea(area: String = "*", tag: String = "*", handler: suspend AreaExited<Player>.() -> Unit) {
Events.handle<Player, AreaExited<Player>>("player_exit", area, "player", tag, "*") {
handler.invoke(this)
}
}

fun npcExitArea(npc: String = "*", area: String = "*", tag: String = "*", handler: suspend AreaExited.() -> Unit) {
Events.handle<NPC, AreaExited>("npc_exit", area, npc, tag, "*") {
fun npcExitArea(npc: String = "*", area: String = "*", tag: String = "*", handler: suspend AreaExited<NPC>.() -> Unit) {
Events.handle<NPC, AreaExited<NPC>>("npc_exit", area, npc, tag, "*") {
handler.invoke(this)
}
}

fun characterExitArea(area: String = "*", tag: String = "*", handler: suspend AreaExited.() -> Unit) {
val block: suspend AreaExited.(EventDispatcher) -> Unit = {
fun characterExitArea(area: String = "*", tag: String = "*", handler: suspend AreaExited<Character>.() -> Unit) {
val block: suspend AreaExited<Character>.(EventDispatcher) -> Unit = {
handler.invoke(this)
}
Events.handle("player_exit", area, "player", tag, "*", handler = block)
Expand Down
Loading

0 comments on commit 7045e86

Please sign in to comment.