Skip to content

Commit

Permalink
Fix duplicate enemies, rigidbody sync issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jun 29, 2024
1 parent 6ef53c5 commit b1e203a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
24 changes: 23 additions & 1 deletion HKMP/Game/Client/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,18 @@ public void MakeHost() {
component.IsControlled = false;
}

Logger.Debug(" Client object null, enabling host object and returning");
return;
}

if (_hasParent) {
Logger.Debug(" Entity has parent, only setting local transform");

Object.Host.transform.localPosition = _lastPosition = Object.Client.transform.localPosition;
Object.Host.transform.localScale = _lastScale = Object.Client.transform.localScale;
} else {
Logger.Debug(" Entity has no parent, calculating transform");

var clientPos = Object.Client.transform.localPosition;
var parentPos = Vector3.zero;
if (Object.Host.transform.parent != null) {
Expand Down Expand Up @@ -851,6 +856,17 @@ public void MakeHost() {
Object.Client.SetActive(false);
Object.Host.SetActive(clientActive);

Logger.Debug($" Set Active of host object to: {clientActive}, disabling client object");

// We need to set the isKinematic property of rigid bodies to ensure physics work again after enabling
// the host object. In Hornet 1 this is necessary because another state sets this property normally in the
// fight. See the "Wake" or "Refight Ready" state of the "Control" FSM on Hornet 1
var rigidBody = Object.Host.GetComponent<Rigidbody2D>();
if (rigidBody != null) {
Logger.Debug(" Resetting isKinematic of Rigidbody to ensure physics work for host object");
rigidBody.isKinematic = false;
}

_lastIsActive = _hasParent ? Object.Host.activeSelf : Object.Host.activeInHierarchy;

_isControlled = false;
Expand All @@ -865,15 +881,19 @@ public void MakeHost() {
var clientAnimation = currentClip.name;
var wrapMode = currentClip.wrapMode;

Logger.Debug($"MakeHost ({Id}, {Type}) animation: {clientAnimation}, {wrapMode}");
Logger.Debug($" Animator and current clip present, updating animation: {clientAnimation}, {wrapMode}");

LateUpdateAnimation(_animator.Host, clientAnimation, wrapMode);
}
}

Logger.Debug(" Restoring FSMs from snapshots");

for (var fsmIndex = 0; fsmIndex < _fsms.Host.Count; fsmIndex++) {
var fsm = _fsms.Host[fsmIndex];

Logger.Debug($" Restoring FSM: {fsm.Fsm.Name}");

// Force initialize the host FSM, since it might have been disabled before initializing
EntityInitializer.InitializeFsm(fsm);

Expand Down Expand Up @@ -912,6 +932,8 @@ public void MakeHost() {
var oldActions = state.Actions;
var newActions = oldActions.Where(ActionRegistry.IsActionContinuous).ToArray();

Logger.Debug($" Only using actions: {string.Join(", ", newActions.Select(a => a.GetType().ToString()))}");

// Replace the actions, set the state and reset the actions again
state.Actions = newActions;
fsm.SetState(snapshot.CurrentState);
Expand Down
7 changes: 5 additions & 2 deletions HKMP/Game/Client/Entity/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void SpawnEntity(ushort id, EntityType spawningType, EntityType spawnedTy
var processor = new EntityProcessor {
GameObject = spawnedObject,
IsSceneHost = IsSceneHost,
IsSceneHostDetermined = IsSceneHostDetermined,
LateLoad = true,
SpawnedId = id
}.Process();
Expand Down Expand Up @@ -238,6 +239,7 @@ private bool OnGameObjectSpawned(EntitySpawnDetails details) {
var processor = new EntityProcessor {
GameObject = details.GameObject,
IsSceneHost = IsSceneHost,
IsSceneHostDetermined = IsSceneHostDetermined,
LateLoad = true
}.Process();

Expand Down Expand Up @@ -329,13 +331,13 @@ private void OnSceneChanged(Scene oldScene, Scene newScene) {
return;
}

IsSceneHostDetermined = false;

FindEntitiesInScene(newScene, false);

// Since we have tried finding entities in the scene, we also check whether there are un-applied updates for
// those entities
CheckReceivedUpdates();

IsSceneHostDetermined = false;
}

/// <summary>
Expand Down Expand Up @@ -447,6 +449,7 @@ private void FindEntitiesInScene(Scene scene, bool lateLoad) {
new EntityProcessor {
GameObject = obj,
IsSceneHost = IsSceneHost,
IsSceneHostDetermined = IsSceneHostDetermined,
LateLoad = lateLoad
}.Process();
}
Expand Down
6 changes: 5 additions & 1 deletion HKMP/Game/Client/Entity/EntityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ internal class EntityProcessor {
/// </summary>
public bool IsSceneHost { get; init; }
/// <summary>
/// Whether the scene host is determined for this scene locally.
/// </summary>
public bool IsSceneHostDetermined { get; init; }
/// <summary>
/// Whether the processing of this entity should happen under the assumption that was a late load of the
/// game object.
/// </summary>
Expand Down Expand Up @@ -188,7 +192,7 @@ private void Process(
}
}

if (LateLoad) {
if (LateLoad && IsSceneHostDetermined) {
if (IsSceneHost) {
// Since this is a late load it needs to be initialized as host if we are the scene host
entity.InitializeHost();
Expand Down

0 comments on commit b1e203a

Please sign in to comment.