Skip to content

Commit

Permalink
feat: Allow sending spawnData when spawning entities using the Multip…
Browse files Browse the repository at this point in the history
…layerService
  • Loading branch information
NotBjoggisAtAll authored Feb 1, 2025
1 parent 59d580e commit a3f3ba6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ class MultiplayerService : EngineService() {
}

fun spawn(connection: Connection<Bundle>, entity: Entity, entityName: String) {
spawn(connection, entity, entityName, SpawnData(entity.x, entity.y, entity.z))
}

fun spawn(connection: Connection<Bundle>, entity: Entity, entityName: String, spawnData: SpawnData) {
if (!entity.hasComponent(NetworkComponent::class.java)) {
log.warning("Attempted to network-spawn entity $entityName, but it does not have NetworkComponent")
return
}

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

val event = EntitySpawnEvent(networkComponent.id, entityName, entity.x, entity.y, entity.z)
val event = EntitySpawnEvent(networkComponent.id, entityName, NetworkSpawnData(spawnData))

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

val e = gameWorld.spawn(entityName, SpawnData(event.x, event.y, event.z))
val networkSpawnData = event.networkSpawnData
val data = networkSpawnData.bundle.data
val spawnData = SpawnData(networkSpawnData.x, networkSpawnData.y, networkSpawnData.z)
data.forEach(spawnData::put)

val e = gameWorld.spawn(entityName, spawnData)

// TODO: show warning if not present
e.getComponentOptional(NetworkComponent::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.multiplayer

import com.almasb.fxgl.core.serialization.Bundle
import com.almasb.fxgl.entity.SpawnData
import java.io.Serializable

/**
* Wrapper around SpawnData to be sent over network.
*
* @author Jonas Andersen ([email protected])
*/
class NetworkSpawnData(spawnData: SpawnData) : Serializable {

val bundle = Bundle("NetworkSpawnData")
var x: Double = 0.0
var y: Double = 0.0
var z: Double = 0.0

init {
x = spawnData.x
y = spawnData.y
z = spawnData.z

spawnData.data.forEach { (key, value) ->
if (value is Serializable) {
bundle.put(key, value)
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.almasb.fxgl.multiplayer

import com.almasb.fxgl.entity.SpawnData
import javafx.event.Event
import javafx.event.EventType
import javafx.scene.input.KeyCode
Expand Down Expand Up @@ -36,13 +37,10 @@ abstract class ReplicationEvent(eventType: EventType<out ReplicationEvent>) : Ev
}
}

// TODO: consider SpawnData properties in the future
class EntitySpawnEvent(
val networkID: Long,
val entityName: String,
val x: Double,
val y: Double,
val z: Double
val networkSpawnData: NetworkSpawnData,
) : ReplicationEvent(ENTITY_SPAWN)

class EntityUpdateEvent(
Expand Down

0 comments on commit a3f3ba6

Please sign in to comment.