Skip to content

Commit a3f3ba6

Browse files
feat: Allow sending spawnData when spawning entities using the MultiplayerService
1 parent 59d580e commit a3f3ba6

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/multiplayer/MultiplayerService.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,18 @@ class MultiplayerService : EngineService() {
104104
}
105105

106106
fun spawn(connection: Connection<Bundle>, entity: Entity, entityName: String) {
107+
spawn(connection, entity, entityName, SpawnData(entity.x, entity.y, entity.z))
108+
}
109+
110+
fun spawn(connection: Connection<Bundle>, entity: Entity, entityName: String, spawnData: SpawnData) {
107111
if (!entity.hasComponent(NetworkComponent::class.java)) {
108112
log.warning("Attempted to network-spawn entity $entityName, but it does not have NetworkComponent")
109113
return
110114
}
111115

112116
val networkComponent = entity.getComponent(NetworkComponent::class.java)
113117

114-
val event = EntitySpawnEvent(networkComponent.id, entityName, entity.x, entity.y, entity.z)
118+
val event = EntitySpawnEvent(networkComponent.id, entityName, NetworkSpawnData(spawnData))
115119

116120
// TODO: if not available
117121
val data = replicatedEntitiesMap[connection]!!
@@ -129,7 +133,12 @@ class MultiplayerService : EngineService() {
129133
val id = event.networkID
130134
val entityName = event.entityName
131135

132-
val e = gameWorld.spawn(entityName, SpawnData(event.x, event.y, event.z))
136+
val networkSpawnData = event.networkSpawnData
137+
val data = networkSpawnData.bundle.data
138+
val spawnData = SpawnData(networkSpawnData.x, networkSpawnData.y, networkSpawnData.z)
139+
data.forEach(spawnData::put)
140+
141+
val e = gameWorld.spawn(entityName, spawnData)
133142

134143
// TODO: show warning if not present
135144
e.getComponentOptional(NetworkComponent::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB ([email protected]).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.multiplayer
8+
9+
import com.almasb.fxgl.core.serialization.Bundle
10+
import com.almasb.fxgl.entity.SpawnData
11+
import java.io.Serializable
12+
13+
/**
14+
* Wrapper around SpawnData to be sent over network.
15+
*
16+
* @author Jonas Andersen ([email protected])
17+
*/
18+
class NetworkSpawnData(spawnData: SpawnData) : Serializable {
19+
20+
val bundle = Bundle("NetworkSpawnData")
21+
var x: Double = 0.0
22+
var y: Double = 0.0
23+
var z: Double = 0.0
24+
25+
init {
26+
x = spawnData.x
27+
y = spawnData.y
28+
z = spawnData.z
29+
30+
spawnData.data.forEach { (key, value) ->
31+
if (value is Serializable) {
32+
bundle.put(key, value)
33+
}
34+
};
35+
}
36+
}

fxgl/src/main/kotlin/com/almasb/fxgl/multiplayer/ReplicationEvent.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.almasb.fxgl.multiplayer
88

9+
import com.almasb.fxgl.entity.SpawnData
910
import javafx.event.Event
1011
import javafx.event.EventType
1112
import javafx.scene.input.KeyCode
@@ -36,13 +37,10 @@ abstract class ReplicationEvent(eventType: EventType<out ReplicationEvent>) : Ev
3637
}
3738
}
3839

39-
// TODO: consider SpawnData properties in the future
4040
class EntitySpawnEvent(
4141
val networkID: Long,
4242
val entityName: String,
43-
val x: Double,
44-
val y: Double,
45-
val z: Double
43+
val networkSpawnData: NetworkSpawnData,
4644
) : ReplicationEvent(ENTITY_SPAWN)
4745

4846
class EntityUpdateEvent(

0 commit comments

Comments
 (0)