Skip to content

Commit

Permalink
Synchronise dream platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jul 28, 2024
1 parent 8f47e1e commit 0e7de2e
Show file tree
Hide file tree
Showing 24 changed files with 257 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var type = data.Packet.ReadByte();

// If the player is a scene client we destroy the prompt, otherwise we start the fight by progressing the FSM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
if (!IsControlled) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/ClimberComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
}

/// <inheritdoc />
public override void Destroy() {
}
}
}
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/ColliderComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
Logger.Info($"Received collider update for {GameObject.Client.name}");

if (!IsControlled) {
Expand All @@ -80,4 +80,4 @@ public override void Update(EntityNetworkData data) {
public override void Destroy() {
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdateCollider;
}
}
}
2 changes: 2 additions & 0 deletions HKMP/Game/Client/Entity/Component/ComponentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ HostClientPair<GameObject> objects
return null;
case EntityComponentType.FlipPlatform:
return new FlipPlatformComponent(netClient, entityId, objects);
case EntityComponentType.DreamPlatform:
return new DreamPlatformComponent(netClient, entityId, objects);
default:
throw new ArgumentOutOfRangeException(nameof(type), type, $"Could not instantiate entity component for type: {type}");
}
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/DamageHeroComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var damageDealt = data.Packet.ReadByte();
_damageHero.Host.damageDealt = damageDealt;
_damageHero.Client.damageDealt = damageDealt;
Expand All @@ -70,4 +70,4 @@ public override void Update(EntityNetworkData data) {
public override void Destroy() {
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdate;
}
}
}
196 changes: 196 additions & 0 deletions HKMP/Game/Client/Entity/Component/DreamPlatformComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using Hkmp.Networking.Client;
using Hkmp.Networking.Packet.Data;
using UnityEngine;
using Logger = Hkmp.Logging.Logger;

namespace Hkmp.Game.Client.Entity.Component;

/// <inheritdoc />
/// This component manages the platforms that (dis)appear in dream sequences.
internal class DreamPlatformComponent : EntityComponent {
/// <summary>
/// Host-client pair of the DreamPlatform components.
/// </summary>
private readonly HostClientPair<DreamPlatform> _platform;

/// <summary>
/// The number of players currently in range of the platform.
/// </summary>
private ushort _numInRange;
/// <summary>
/// Whether the local player is in range of the platform.
/// </summary>
private bool _isInRange;

public DreamPlatformComponent(
NetClient netClient,
ushort entityId,
HostClientPair<GameObject> gameObject
) : base(netClient, entityId, gameObject) {
_platform = new HostClientPair<DreamPlatform> {
Client = gameObject.Client.GetComponent<DreamPlatform>(),
Host = gameObject.Host.GetComponent<DreamPlatform>()
};

if (!_platform.Client.showOnEnable) {
_platform.Client.outerCollider.OnTriggerExited += OuterColliderOnTriggerExited;
_platform.Host.outerCollider.OnTriggerExited += OuterColliderOnTriggerExited;

_platform.Client.innerCollider.OnTriggerEntered += InnerColliderOnTriggerEntered;
_platform.Host.innerCollider.OnTriggerEntered += InnerColliderOnTriggerEntered;

On.DreamPlatform.Start += DreamPlatformOnStart;
}
}

/// <summary>
/// Hook for the Start method of DreamPlatform. Used to prevent the original method from registering event
/// handlers to the trigger enter/exit.
/// </summary>
private void DreamPlatformOnStart(On.DreamPlatform.orig_Start orig, DreamPlatform self) {
if (self == _platform.Client || self == _platform.Host) {
return;
}

orig(self);
}

/// <summary>
/// Show the correct platform based on whether the entity is controlled or not.
/// </summary>
private void Show() {
if (IsControlled) {
_platform.Client.Show();
} else {
_platform.Host.Show();
}
}

/// <summary>
/// Hide the correct platform based on whether the entity is controlled or not.
/// </summary>
private void Hide() {
if (IsControlled) {
_platform.Client.Hide();
} else {
_platform.Host.Hide();
}
}

/// <summary>
/// Event handler for when the trigger for the outer collider of the platform is exited.
/// </summary>
private void OuterColliderOnTriggerExited(Collider2D collider, GameObject sender) {
// If we haven't been in range of the platform but trigger the exit, we do not want to update anything
if (!_isInRange) {
return;
}

_isInRange = false;

_numInRange--;

// If the number of players in range is now 0 (or lower), we can hide the platform
if (_numInRange == 0) {
Hide();
}

var data = new EntityNetworkData {
Type = EntityComponentType.DreamPlatform
};

data.Packet.Write(_numInRange);
data.Packet.Write(0);

SendData(data);
}

/// <summary>
/// Event handler for when the trigger for the inner collider of the platform is entered.
/// </summary>
private void InnerColliderOnTriggerEntered(Collider2D collider, GameObject sender) {
_isInRange = true;

EnterPlatform();

var data = new EntityNetworkData {
Type = EntityComponentType.DreamPlatform
};

data.Packet.Write(_numInRange);
data.Packet.Write(1);

SendData(data);
}

/// <summary>
/// Exit the platform and decrease the number of players in range. If the number hits zero, the platform will be
/// hidden.
/// </summary>
private void ExitPlatform() {
_numInRange--;

// If the number of players in range is now 0 (or lower), we can hide the platform
if (_numInRange == 0) {
Hide();
}
}

/// <summary>
/// Enter the platform and increase the number of players in range. If the number is exactly 1, the platform will
/// be shown.
/// </summary>
private void EnterPlatform() {
_numInRange++;

// If the number of players in range is exactly 1 now, we show the platform
if (_numInRange == 1) {
Show();
}
}

/// <inheritdoc />
public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var numInRange = data.Packet.ReadUShort();
var action = data.Packet.ReadByte();

if (alreadyInSceneUpdate) {
_numInRange = numInRange;

if (_numInRange > 0) {
Show();
}

return;
}

if (action == 0) {
// Action 0 is exited collider
ExitPlatform();
} else if (action == 1) {
// Action 1 is entered collider
EnterPlatform();
} else {
Logger.Error($"Could not process unknown action for DreamPlatformComponent update: {action}");
}
}

/// <inheritdoc />
public override void Destroy() {
if (_platform.Client != null) {
_platform.Client.outerCollider.OnTriggerExited -= OuterColliderOnTriggerExited;
_platform.Client.innerCollider.OnTriggerEntered -= InnerColliderOnTriggerEntered;
}

if (_platform.Host != null) {
_platform.Host.outerCollider.OnTriggerExited -= OuterColliderOnTriggerExited;
_platform.Host.innerCollider.OnTriggerEntered -= InnerColliderOnTriggerEntered;
}

On.DreamPlatform.Start -= DreamPlatformOnStart;
}
}
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/EnemySpawnerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
iTween.MoveBy(_spawner.Client.gameObject, new Hashtable {
{
"amount",
Expand All @@ -89,4 +89,4 @@ public override void Destroy() {
_spawner.Host.OnEnemySpawned -= OnEnemySpawned;
}
}
}
}
7 changes: 5 additions & 2 deletions HKMP/Game/Client/Entity/Component/EntityComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ protected void SendData(EntityNetworkData data) {
/// Initializes the entity component when the client user is the scene host.
/// </summary>
public abstract void InitializeHost();

/// <summary>
/// Update the entity component with the given data.
/// </summary>
/// <param name="data">The data to update with.</param>
public abstract void Update(EntityNetworkData data);
/// <param name="alreadyInSceneUpdate">Whether this data is from an already in scene packet.</param>
public abstract void Update(EntityNetworkData data, bool alreadyInSceneUpdate);
/// <summary>
/// Destroy the entity component.
/// </summary>
Expand Down Expand Up @@ -87,5 +89,6 @@ internal enum EntityComponentType : ushort {
SpriteRenderer,
ChallengePrompt,
Music,
FlipPlatform
FlipPlatform,
DreamPlatform
}
2 changes: 1 addition & 1 deletion HKMP/Game/Client/Entity/Component/FlipPlatformComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var platform = IsControlled ? _platform.Client : _platform.Host;

var type = data.Packet.ReadByte();
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/GravityScaleComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
if (!IsControlled) {
return;
}
Expand All @@ -83,4 +83,4 @@ public override void Update(EntityNetworkData data) {
public override void Destroy() {
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdate;
}
}
}
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/HealthManagerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
Logger.Info("Received health manager update");

if (!IsControlled) {
Expand Down Expand Up @@ -173,4 +173,4 @@ public override void Destroy() {
On.HealthManager.Die -= HealthManagerOnDie;
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdate;
}
}
}
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/MeshRendererComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var enabled = data.Packet.ReadBool();
_meshRenderer.Host.enabled = enabled;
_meshRenderer.Client.enabled = enabled;
Expand All @@ -73,4 +73,4 @@ public override void Update(EntityNetworkData data) {
public override void Destroy() {
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdate;
}
}
}
2 changes: 1 addition & 1 deletion HKMP/Game/Client/Entity/Component/MusicComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
Logger.Debug("Update MusicComponent");

if (!IsControlled) {
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Game/Client/Entity/Component/RotationComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
public override void Update(EntityNetworkData data, bool alreadyInSceneUpdate) {
var rotation = data.Packet.ReadFloat();

SetRotation(GameObject.Host);
Expand All @@ -74,4 +74,4 @@ void SetRotation(GameObject obj) {
public override void Destroy() {
MonoBehaviourUtil.Instance.OnUpdateEvent -= OnUpdateRotation;
}
}
}
Loading

0 comments on commit 0e7de2e

Please sign in to comment.