-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename Pickup to Take, and add Taken events to remove overrides from …
…FloorItemOption #582
- Loading branch information
Showing
9 changed files
with
142 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
29 changes: 9 additions & 20 deletions
29
game/src/main/kotlin/content/area/asgarnia/port_sarim/PortSarim.kts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,19 @@ | ||
package content.area.asgarnia.port_sarim | ||
|
||
import content.entity.player.inv.item.take.canTake | ||
import content.entity.player.inv.item.take.taken | ||
import world.gregs.voidps.engine.client.message | ||
import world.gregs.voidps.engine.entity.character.player.chat.inventoryFull | ||
import world.gregs.voidps.engine.entity.item.floor.FloorItems | ||
import world.gregs.voidps.engine.entity.item.floor.floorItemOperate | ||
import world.gregs.voidps.engine.inject | ||
import world.gregs.voidps.engine.inv.add | ||
import world.gregs.voidps.engine.inv.holdsItem | ||
import world.gregs.voidps.engine.inv.inventory | ||
import content.entity.sound.playSound | ||
|
||
val items: FloorItems by inject() | ||
|
||
floorItemOperate("Take", "white_apron_port_sarim", override = false) { | ||
canTake("white_apron_port_sarim") { player -> | ||
if (player.holdsItem("white_apron")) { | ||
player.message("You already have one of those.") | ||
cancel() | ||
} else if (player.inventory.isFull() && (!player.inventory.stackable(target.id) || !player.inventory.contains(target.id))) { | ||
player.inventoryFull() | ||
cancel() | ||
} else { | ||
player.anim("take") | ||
player.playSound("pickup_item") | ||
items.remove(target) | ||
player.inventory.add("white_apron") | ||
player.message("You take an apron. It feels freshly starched and smells of laundry.") | ||
cancel() | ||
} | ||
item = "white_apron" | ||
} | ||
|
||
taken("white_apron_port_sarim") { player -> | ||
player.anim("take") | ||
player.message("You take an apron. It feels freshly starched and smells of laundry.") | ||
} |
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
game/src/main/kotlin/content/entity/player/inv/item/take/Takeable.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package content.entity.player.inv.item.take | ||
|
||
import world.gregs.voidps.engine.entity.character.player.Player | ||
import world.gregs.voidps.engine.event.CancellableEvent | ||
import world.gregs.voidps.engine.event.EventDispatcher | ||
import world.gregs.voidps.engine.event.Events | ||
|
||
/** | ||
* Checks that an item can be taken off of the floor. Continues unless [cancelled]. | ||
*/ | ||
data class Takeable(var item: String) : CancellableEvent() { | ||
|
||
override val size = 2 | ||
|
||
override fun parameter(dispatcher: EventDispatcher, index: Int) = when (index) { | ||
0 -> "can_take" | ||
1 -> item | ||
else -> null | ||
} | ||
} | ||
|
||
fun canTake(vararg items: String = arrayOf("*"), handler: Takeable.(Player) -> Unit) { | ||
for (item in items) { | ||
Events.handle("can_take", item, handler = handler) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
game/src/main/kotlin/content/entity/player/inv/item/take/Taken.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package content.entity.player.inv.item.take | ||
|
||
import world.gregs.voidps.engine.entity.character.player.Player | ||
import world.gregs.voidps.engine.entity.item.floor.FloorItem | ||
import world.gregs.voidps.engine.event.CancellableEvent | ||
import world.gregs.voidps.engine.event.EventDispatcher | ||
import world.gregs.voidps.engine.event.Events | ||
|
||
/** | ||
* @param floorItem The item which was taken off of the floor | ||
* @param item The item which was given to the player | ||
*/ | ||
data class Taken(val floorItem: FloorItem, val item: String) : CancellableEvent() { | ||
|
||
override val size = 2 | ||
|
||
override fun parameter(dispatcher: EventDispatcher, index: Int) = when (index) { | ||
0 -> "taken" | ||
1 -> floorItem.id | ||
else -> null | ||
} | ||
} | ||
|
||
fun taken(vararg items: String = arrayOf("*"), handler: Taken.(Player) -> Unit) { | ||
for (item in items) { | ||
Events.handle("taken", item, handler = handler) | ||
} | ||
} |
This file contains 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
45 changes: 45 additions & 0 deletions
45
game/src/test/kotlin/content/entity/player/inv/TakeTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package content.entity.player.inv | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import world.gregs.voidps.engine.entity.item.Item | ||
import world.gregs.voidps.engine.inv.add | ||
import world.gregs.voidps.engine.inv.inventory | ||
import world.gregs.voidps.type.Tile | ||
import WorldTest | ||
import floorItemOption | ||
import interfaceOption | ||
import itemOnObject | ||
import kotlin.test.assertFalse | ||
|
||
internal class TakeTest : WorldTest() { | ||
|
||
@Test | ||
fun `Take item off the floor`() { | ||
val tile = emptyTile | ||
val player = createPlayer("player", tile) | ||
val item = floorItems.add(tile.add(0, 2), "bronze_sword") | ||
|
||
player.floorItemOption(item, "Take") | ||
tick(5) | ||
|
||
assertTrue(player.inventory.contains("bronze_sword")) | ||
assertTrue(floorItems[tile.add(0, 2)].isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `Take item up off a table`() { | ||
val tile = Tile(3212, 3218, 1) | ||
val player = createPlayer("player", tile) | ||
val item = floorItems.add(tile.add(1, 0), "bronze_sword") | ||
|
||
player.floorItemOption(item, "Take") | ||
tick(5) | ||
|
||
assertTrue(player.inventory.contains("bronze_sword")) | ||
assertTrue(floorItems[tile.add(1, 0)].isEmpty()) | ||
assertEquals(tile, player.tile) | ||
} | ||
|
||
} |