Skip to content

Commit d8e7f78

Browse files
adam-aroldAdam Arold
authored andcommitted
Integrate Amethyst and add the Player
1 parent 0a6ea78 commit d8e7f78

18 files changed

Lines changed: 289 additions & 26 deletions

File tree

gradle.properties

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ org.gradle.jvmargs=-Xmx2048M
44
org.gradle.daemon=true
55

66
group=org.hexworks.cavesofzircon
7-
version=2018.1.0-PREVIEW
7+
version=2019.0.1-PREVIEW
88

9-
kotlin_version=1.3.11
9+
kotlin_version=1.3.21
1010
shadow_version=4.0.2
1111

12-
cobalt_version=2018.1.0-PREVIEW
13-
amethyst_version=2019.0.1-PREVIEW
14-
zircon_version=2019.0.9-PREVIEW
12+
cobalt_version=2018.1.1-PREVIEW
13+
amethyst_version=2019.0.4-PREVIEW
14+
zircon_version=2019.0.14-PREVIEW
15+
1516
junit_version=4.12
1617
mockito_version=1.10.19
1718
assertj_version=3.6.2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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.zircon.api.data.impl.Position3D
6+
7+
class EntityPosition(initialPosition: Position3D = Position3D.unknown()) : Attribute { // 1
8+
private val positionProperty = createPropertyFrom(initialPosition) // 2
9+
10+
var position: Position3D by positionProperty.asDelegate() // 3
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hexworks.cavesofzircon.attributes
2+
3+
import org.hexworks.amethyst.api.Attribute
4+
import org.hexworks.zircon.api.Tiles
5+
import org.hexworks.zircon.api.data.Tile
6+
7+
/**
8+
* Contains the tile of an entity.
9+
*/
10+
data class EntityTile(val tile: Tile = Tiles.empty()) : Attribute
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.hexworks.cavesofzircon.attributes.types
2+
3+
import org.hexworks.amethyst.api.base.BaseEntityType
4+
5+
object Player : BaseEntityType(
6+
name = "player")

src/main/kotlin/org/hexworks/cavesofzircon/blocks/GameBlock.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.hexworks.cavesofzircon.blocks
22

3+
import org.hexworks.amethyst.api.entity.EntityType
34
import org.hexworks.cavesofzircon.builders.GameTileRepository
5+
import org.hexworks.cavesofzircon.extensions.GameEntity
6+
import org.hexworks.cavesofzircon.extensions.tile
47
import org.hexworks.zircon.api.data.BlockSide
58
import org.hexworks.zircon.api.data.Tile
69
import org.hexworks.zircon.api.data.base.BlockBase
710

8-
class GameBlock(private var defaultTile: Tile = GameTileRepository.FLOOR)
11+
class GameBlock(private var defaultTile: Tile = GameTileRepository.FLOOR,
12+
private val currentEntities: MutableList<GameEntity<EntityType>> = mutableListOf())
913
: BlockBase<Tile>() {
1014

1115
val isFloor: Boolean
@@ -15,10 +19,30 @@ class GameBlock(private var defaultTile: Tile = GameTileRepository.FLOOR)
1519
val isWall: Boolean
1620
get() = defaultTile == GameTileRepository.WALL
1721

22+
val isEmptyFloor: Boolean
23+
get() = currentEntities.isEmpty()
24+
25+
val entities: Iterable<GameEntity<EntityType>>
26+
get() = currentEntities.toList()
27+
28+
override val layers: MutableList<Tile>
29+
get() {
30+
val entityTiles = currentEntities.map { it.tile }
31+
val tile = when {
32+
entityTiles.contains(GameTileRepository.PLAYER) -> GameTileRepository.PLAYER
33+
entityTiles.isNotEmpty() -> entityTiles.first()
34+
else -> defaultTile
35+
}
36+
return mutableListOf(tile)
37+
}
38+
39+
fun addEntity(entity: GameEntity<EntityType>) {
40+
currentEntities.add(entity)
41+
}
1842

19-
override val layers
20-
get() = mutableListOf(defaultTile)
21-
43+
fun removeEntity(entity: GameEntity<EntityType>) {
44+
currentEntities.remove(entity)
45+
}
2246

2347
override fun fetchSide(side: BlockSide): Tile {
2448
return GameTileRepository.EMPTY
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.hexworks.cavesofzircon.builders
2+
3+
import org.hexworks.amethyst.api.Entities
4+
import org.hexworks.amethyst.api.builder.EntityBuilder
5+
import org.hexworks.amethyst.api.entity.EntityType
6+
import org.hexworks.cavesofzircon.attributes.EntityPosition
7+
import org.hexworks.cavesofzircon.attributes.EntityTile
8+
import org.hexworks.cavesofzircon.attributes.types.Player
9+
import org.hexworks.cavesofzircon.world.GameContext
10+
11+
fun <T : EntityType> newGameEntityOfType(type: T, init: EntityBuilder<T, GameContext>.() -> Unit) =
12+
Entities.newEntityOfType(type, init)
13+
14+
object EntityFactory {
15+
16+
fun newPlayer() = newGameEntityOfType(Player) {
17+
attributes(EntityPosition(), EntityTile(GameTileRepository.PLAYER))
18+
behaviors()
19+
facets()
20+
}
21+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ object GameColors {
88

99
val FLOOR_FOREGROUND = TileColors.fromString("#75715E")
1010
val FLOOR_BACKGROUND = TileColors.fromString("#1e2320")
11+
12+
val ACCENT_COLOR = TileColors.fromString("#FFCD22")
1113
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ object GameTileRepository {
2020
.withBackgroundColor(GameColors.WALL_BACKGROUND)
2121
.buildCharacterTile()
2222

23+
val PLAYER = Tiles.newBuilder()
24+
.withCharacter('@')
25+
.withBackgroundColor(GameColors.FLOOR_BACKGROUND)
26+
.withForegroundColor(GameColors.ACCENT_COLOR)
27+
.buildCharacterTile()
2328
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.hexworks.cavesofzircon.extensions
2+
3+
import org.hexworks.amethyst.api.Attribute
4+
import org.hexworks.cavesofzircon.attributes.EntityPosition
5+
import org.hexworks.cavesofzircon.attributes.EntityTile
6+
import org.hexworks.cobalt.datatypes.extensions.map
7+
import org.hexworks.cobalt.datatypes.extensions.orElseThrow
8+
import org.hexworks.zircon.api.data.Tile
9+
import kotlin.reflect.KClass
10+
11+
var AnyGameEntity.position
12+
get() = tryToFindAttribute(EntityPosition::class).position
13+
set(value) {
14+
findAttribute(EntityPosition::class).map {
15+
it.position = value
16+
}
17+
}
18+
19+
val AnyGameEntity.tile: Tile
20+
get() = this.tryToFindAttribute(EntityTile::class).tile
21+
22+
fun <T : Attribute> AnyGameEntity.tryToFindAttribute(klass: KClass<T>): T = findAttribute(klass).orElseThrow {
23+
NoSuchElementException("Entity '$this' has no property with type '${klass.simpleName}'.")
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.hexworks.cavesofzircon.extensions
2+
3+
import org.hexworks.amethyst.api.entity.Entity
4+
import org.hexworks.amethyst.api.entity.EntityType
5+
import org.hexworks.cavesofzircon.world.GameContext
6+
7+
/**
8+
* Fits any [Entity] type we use.
9+
*/
10+
typealias AnyGameEntity = Entity<EntityType, GameContext>
11+
12+
/**
13+
* Specializes [Entity] with our [GameContext] type.
14+
*/
15+
typealias GameEntity<T> = Entity<T, GameContext>

0 commit comments

Comments
 (0)