Skip to content

Commit 34bca7a

Browse files
author
adam-arold
committed
Add real combat
1 parent e906f03 commit 34bca7a

13 files changed

Lines changed: 156 additions & 25 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hexworks.cavesofzircon.attributes
2+
3+
import org.hexworks.amethyst.api.Attribute
4+
import org.hexworks.cobalt.databinding.api.createPropertyFrom
5+
import org.hexworks.cobalt.databinding.api.property.Property
6+
7+
data class CombatStats(val maxHpProperty: Property<Int>,
8+
val hpProperty: Property<Int> = createPropertyFrom(maxHpProperty.value),
9+
val attackValueProperty: Property<Int>,
10+
val defenseValueProperty: Property<Int>) : Attribute {
11+
val maxHp: Int by maxHpProperty.asDelegate()
12+
var hp: Int by hpProperty.asDelegate()
13+
val attackValue: Int by attackValueProperty.asDelegate()
14+
val defenseValue: Int by defenseValueProperty.asDelegate()
15+
16+
companion object {
17+
18+
fun create(maxHp: Int, hp: Int = maxHp, attackValue: Int, defenseValue: Int) =
19+
CombatStats(
20+
maxHpProperty = createPropertyFrom(maxHp),
21+
hpProperty = createPropertyFrom(hp),
22+
attackValueProperty = createPropertyFrom(attackValue),
23+
defenseValueProperty = createPropertyFrom(defenseValue))
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hexworks.cavesofzircon.attributes.types
2+
3+
import org.hexworks.amethyst.api.entity.EntityType
4+
import org.hexworks.cavesofzircon.attributes.CombatStats
5+
import org.hexworks.cavesofzircon.extensions.GameEntity
6+
7+
interface Combatant : EntityType
8+
9+
val GameEntity<Combatant>.combatStats: CombatStats
10+
get() = findAttribute(CombatStats::class).get()

src/main/kotlin/org/hexworks/cavesofzircon/attributes/types/EntityTypes.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package org.hexworks.cavesofzircon.attributes.types
33
import org.hexworks.amethyst.api.base.BaseEntityType
44

55
object Player : BaseEntityType(
6-
name = "player")
6+
name = "player"), Combatant
77

88
object Wall : BaseEntityType(
99
name = "wall")
1010

1111
object Fungus : BaseEntityType(
12-
name = "fungus")
12+
name = "fungus"), Combatant

src/main/kotlin/org/hexworks/cavesofzircon/builders/EntityFactory.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.hexworks.cavesofzircon.builders
33
import org.hexworks.amethyst.api.Entities
44
import org.hexworks.amethyst.api.builder.EntityBuilder
55
import org.hexworks.amethyst.api.entity.EntityType
6+
import org.hexworks.cavesofzircon.attributes.CombatStats
67
import org.hexworks.cavesofzircon.attributes.EntityActions
78
import org.hexworks.cavesofzircon.attributes.EntityPosition
89
import org.hexworks.cavesofzircon.attributes.EntityTile
@@ -13,37 +14,51 @@ import org.hexworks.cavesofzircon.attributes.types.Player
1314
import org.hexworks.cavesofzircon.attributes.types.Wall
1415
import org.hexworks.cavesofzircon.commands.Attack
1516
import org.hexworks.cavesofzircon.commands.Dig
16-
import org.hexworks.cavesofzircon.systems.*
17+
import org.hexworks.cavesofzircon.systems.Attackable
18+
import org.hexworks.cavesofzircon.systems.CameraMover
19+
import org.hexworks.cavesofzircon.systems.Destructible
20+
import org.hexworks.cavesofzircon.systems.Diggable
21+
import org.hexworks.cavesofzircon.systems.FungusGrowth
22+
import org.hexworks.cavesofzircon.systems.InputReceiver
23+
import org.hexworks.cavesofzircon.systems.Movable
1724
import org.hexworks.cavesofzircon.world.GameContext
1825

1926
fun <T : EntityType> newGameEntityOfType(type: T, init: EntityBuilder<T, GameContext>.() -> Unit) =
2027
Entities.newEntityOfType(type, init)
2128

2229
object EntityFactory {
2330

31+
fun newWall() = newGameEntityOfType(Wall) {
32+
attributes(
33+
EntityPosition(),
34+
BlockOccupier,
35+
EntityTile(GameTileRepository.WALL))
36+
facets(Diggable)
37+
}
38+
2439
fun newPlayer() = newGameEntityOfType(Player) {
2540
attributes(
2641
EntityPosition(),
42+
CombatStats.create(
43+
maxHp = 100,
44+
attackValue = 10,
45+
defenseValue = 5),
2746
EntityTile(GameTileRepository.PLAYER),
2847
EntityActions(Dig::class, Attack::class))
2948
behaviors(InputReceiver)
3049
facets(Movable, CameraMover)
3150
}
3251

33-
fun newWall() = newGameEntityOfType(Wall) {
34-
attributes(
35-
EntityPosition(),
36-
BlockOccupier,
37-
EntityTile(GameTileRepository.WALL))
38-
facets(Diggable)
39-
}
40-
4152
fun newFungus(fungusSpread: FungusSpread = FungusSpread()) = newGameEntityOfType(Fungus) {
4253
attributes(BlockOccupier,
4354
EntityPosition(),
55+
CombatStats.create(
56+
maxHp = 10,
57+
attackValue = 0,
58+
defenseValue = 0),
4459
EntityTile(GameTileRepository.FUNGUS),
4560
fungusSpread)
46-
facets(Attackable)
61+
facets(Attackable, Destructible)
4762
behaviors(FungusGrowth)
4863
}
4964
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.hexworks.cavesofzircon.commands
22

3-
import org.hexworks.amethyst.api.entity.EntityType
3+
import org.hexworks.cavesofzircon.attributes.types.Combatant
44
import org.hexworks.cavesofzircon.extensions.GameEntity
55
import org.hexworks.cavesofzircon.world.GameContext
66

77
data class Attack(override val context: GameContext,
8-
override val source: GameEntity<EntityType>,
9-
override val target: GameEntity<EntityType>) : EntityAction<EntityType, EntityType>
8+
override val source: GameEntity<Combatant>,
9+
override val target: GameEntity<Combatant>) : EntityAction<Combatant, Combatant>

src/main/kotlin/org/hexworks/cavesofzircon/commands/CameraMoveDirection.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hexworks.cavesofzircon.commands
2+
3+
import org.hexworks.amethyst.api.entity.EntityType
4+
import org.hexworks.cavesofzircon.extensions.GameCommand
5+
import org.hexworks.cavesofzircon.extensions.GameEntity
6+
import org.hexworks.cavesofzircon.world.GameContext
7+
8+
data class Destroy(override val context: GameContext,
9+
override val source: GameEntity<EntityType>,
10+
val target: GameEntity<EntityType>,
11+
val cause: String = "natural causes.") : GameCommand<EntityType>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.hexworks.cavesofzircon.events
2+
3+
import org.hexworks.cobalt.events.api.Event
4+
5+
data class GameLogEvent(val text: String) : Event

src/main/kotlin/org/hexworks/cavesofzircon/extensions/EntityExtensions.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import org.hexworks.cavesofzircon.attributes.EntityActions
88
import org.hexworks.cavesofzircon.attributes.EntityPosition
99
import org.hexworks.cavesofzircon.attributes.EntityTile
1010
import org.hexworks.cavesofzircon.attributes.flags.BlockOccupier
11+
import org.hexworks.cavesofzircon.attributes.types.Combatant
12+
import org.hexworks.cavesofzircon.attributes.types.Player
13+
import org.hexworks.cavesofzircon.attributes.types.combatStats
1114
import org.hexworks.cavesofzircon.world.GameContext
1215
import org.hexworks.cobalt.datatypes.extensions.map
1316
import org.hexworks.cobalt.datatypes.extensions.orElseThrow
@@ -28,6 +31,15 @@ val AnyGameEntity.occupiesBlock: Boolean
2831
val AnyGameEntity.tile: Tile
2932
get() = this.tryToFindAttribute(EntityTile::class).tile
3033

34+
val AnyGameEntity.isPlayer: Boolean
35+
get() = this.type == Player
36+
37+
fun GameEntity<Combatant>.whenHasNoHealthLeft(fn: () -> Unit) {
38+
if (combatStats.hp <= 0) {
39+
fn()
40+
}
41+
}
42+
3143
fun <T : Attribute> AnyGameEntity.tryToFindAttribute(klass: KClass<T>): T = findAttribute(klass).orElseThrow {
3244
NoSuchElementException("Entity '$this' has no property with type '${klass.simpleName}'.")
3345
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.hexworks.cavesofzircon.functions
2+
3+
import org.hexworks.cavesofzircon.events.GameLogEvent
4+
import org.hexworks.zircon.internal.Zircon
5+
6+
fun logGameEvent(text: String) {
7+
Zircon.eventBus.publish(GameLogEvent(text))
8+
}

0 commit comments

Comments
 (0)