Skip to content

Commit

Permalink
Merge pull request #10 from Jump-King-Multiplayer/development
Browse files Browse the repository at this point in the history
Merge development into main
  • Loading branch information
Skippeh authored Jan 15, 2022
2 parents fa03f7a + a23a16c commit 88af268
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public FollowingTextRenderer(SpriteFont font)
textRenderer = new(font);
}

protected override void OnOwnerDestroy()
{
textRenderer.Destroy();
}

protected override void Init()
{
transform = GetComponent<Transform>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class PlayerStateTransmitter : Component

private BodyComp? body;
private float timeSinceTransmission;
private PlayerState lastState;

private readonly P2PManager p2p;

Expand Down Expand Up @@ -64,11 +65,13 @@ protected override void LateUpdate(float delta)
{
timeSinceTransmission += delta;

if (timeSinceTransmission >= TransmissionInterval)
if (timeSinceTransmission >= TransmissionInterval || listener!.CurrentState != lastState)
{
timeSinceTransmission = 0;
SendState();
}

lastState = listener!.CurrentState;
}

private void SendState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JKMP.Core.Logging;
using JKMP.Plugin.Multiplayer.Game.Entities;
using JKMP.Plugin.Multiplayer.Game.Player;
using JKMP.Plugin.Multiplayer.Game.Player.Animations;
using JKMP.Plugin.Multiplayer.Game.Sound;
using JKMP.Plugin.Multiplayer.Networking.Messages;
using JumpKing;
Expand All @@ -24,15 +25,42 @@ public class RemotePlayerInterpolator : Component

private FakePlayer FakePlayer => (FakePlayer)gameObject;

private static readonly SpriteAnimation StretchAnimation = new(CreateStretchAnimationFrames());

private static IEnumerable<SpriteFrame> CreateStretchAnimationFrames()
{
for (int i = 0; i < 7; ++i)
{
yield return new SpriteFrame(JKContentManager.PlayerSprites.stretch_one, 0.5f);
yield return new SpriteFrame(JKContentManager.PlayerSprites.stretch_smear, 0.25f);
yield return new SpriteFrame(JKContentManager.PlayerSprites.stretch_two, 0.5f);
yield return new SpriteFrame(JKContentManager.PlayerSprites.stretch_smear, 0.25f);
}
}

private static readonly SpriteAnimation IdleAnimation = new(
new SpriteFrame(JKContentManager.PlayerSprites.idle, 7f, 15f),
new SpriteFrame(JKContentManager.PlayerSprites.look_up, 5f, 7f),
new SpriteFrame(JKContentManager.PlayerSprites.idle, 7f, 15f),
new SpriteFrame(StretchAnimation, 10.5f)
);

private static readonly SpriteAnimation WalkAnimation = new(
new SpriteFrame(JKContentManager.PlayerSprites.walk_one, 0.225f),
new SpriteFrame(JKContentManager.PlayerSprites.walk_smear, 0.05f),
new SpriteFrame(JKContentManager.PlayerSprites.walk_two, 0.225f),
new SpriteFrame(JKContentManager.PlayerSprites.walk_smear, 0.05f)
);

private static readonly Dictionary<PlayerState, Sprite> StateSprites = new()
{
{ PlayerState.Idle, JKContentManager.PlayerSprites.idle },
{ PlayerState.Idle, IdleAnimation },
{ PlayerState.Falling, JKContentManager.PlayerSprites.jump_fall },
{ PlayerState.StartJump, JKContentManager.PlayerSprites.jump_charge },
{ PlayerState.Jump, JKContentManager.PlayerSprites.jump_up },
{ PlayerState.Knocked, JKContentManager.PlayerSprites.jump_bounce },
{ PlayerState.Land, JKContentManager.PlayerSprites.idle },
{ PlayerState.Walk, JKContentManager.PlayerSprites.walk_one },
{ PlayerState.Land, IdleAnimation },
{ PlayerState.Walk, WalkAnimation },
{ PlayerState.Splat, JKContentManager.PlayerSprites.splat },
};

Expand All @@ -57,6 +85,12 @@ protected override void Update(float delta)
{
if (lastState != null && nextState != null)
{
// Reset animation if we're changing states
if (FakePlayer.Sprite is SpriteAnimation spriteAnimation && lastState.State != nextState.State)
{
spriteAnimation.Reset();
}

FakePlayer.SetSprite(StateSprites[lastState.State]);
FakePlayer.SetDirection(nextState.WalkDirection);

Expand All @@ -68,6 +102,12 @@ protected override void Update(float delta)

elapsedTimeSinceLastState += delta;
}

if (FakePlayer.Sprite is SpriteAnimation)
{
var spriteAnimation = (SpriteAnimation)FakePlayer.Sprite;
spriteAnimation.Update(delta);
}
}

private void UpdateAudioEmitter()
Expand Down
2 changes: 2 additions & 0 deletions JKMP.Plugin.Multiplayer/Game/Entities/FakePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace JKMP.Plugin.Multiplayer.Game.Entities
{
public class FakePlayer : Entity
{
public Sprite Sprite => sprite;

private Sprite sprite;
private bool flip;

Expand Down
124 changes: 124 additions & 0 deletions JKMP.Plugin.Multiplayer/Game/Player/Animations/SpriteAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JKMP.Core.Logging;
using JumpKing;
using JumpKing.XnaWrappers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace JKMP.Plugin.Multiplayer.Game.Player.Animations
{
public class SpriteFrame
{
public Sprite Sprite { get; set; }
public float MinDuration { get; set; }
public float MaxDuration { get; set; }

private static readonly Random Random = new();

public SpriteFrame(Sprite sprite, float minDuration, float? maxDuration = null)
{
Sprite = sprite;
MinDuration = minDuration;
MaxDuration = maxDuration ?? minDuration;
}

public float GetRandomDuration()
{
if (MinDuration.Equals(MaxDuration))
return MinDuration;

return MinDuration + (float)Random.NextDouble() * (MaxDuration - MinDuration);
}

public void Update(float dt)
{
if (Sprite is SpriteAnimation spriteAnimation)
{
spriteAnimation.Update(dt);
}
}
}

public class SpriteAnimation : Sprite
{
private SpriteFrame[] frames;
private float currentDuration;

private int currentFrameIndex;
private float currentFrameDuration;

private SpriteFrame CurrentFrame => frames[currentFrameIndex];
private Sprite CurrentSprite => CurrentFrame.Sprite;

public SpriteAnimation(IEnumerable<SpriteFrame> frames)
{
this.frames = frames.ToArray();

if (this.frames.Length == 0)
throw new ArgumentException("Animation must have at least one frame", nameof(frames));

currentFrameDuration = CurrentFrame.GetRandomDuration();
}

public SpriteAnimation(params SpriteFrame[] frames) : this(frames.AsEnumerable())
{
}

public SpriteAnimation(ICollection<SpriteFrame> frames) : this(frames.AsEnumerable())
{
}

public void Reset()
{
currentFrameIndex = 0;
currentFrameDuration = CurrentFrame.GetRandomDuration();
currentDuration = 0;

foreach (var frame in frames)
{
if (frame.Sprite is SpriteAnimation spriteAnim)
spriteAnim.Reset();
}
}

public void Update(float dt)
{
currentDuration += dt;

if (currentDuration >= currentFrameDuration)
{
currentFrameIndex += 1;

if (currentFrameIndex >= frames.Length)
currentFrameIndex = 0;

currentFrameDuration = CurrentFrame.GetRandomDuration();
currentDuration = 0;
}

CurrentFrame.Update(dt);
}

public override void Draw(Vector2 pos, SpriteEffects effect = SpriteEffects.None)
{
CurrentSprite.Draw(pos, effect);
}

public override void Draw(Rectangle dst, SpriteEffects effect = SpriteEffects.None)
{
CurrentSprite.Draw(dst, effect);
}

public override void Draw(float x, float y, SpriteEffects effect = SpriteEffects.None)
{
CurrentSprite.Draw(x, y, effect);
}

public override void Draw(Point pos, SpriteEffects effect = SpriteEffects.None)
{
CurrentSprite.Draw(pos, effect);
}
}
}
11 changes: 9 additions & 2 deletions JKMP.Plugin.Multiplayer/Game/Player/LocalPlayerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public LocalPlayerListener()

public void Update(float delta)
{
if (localBody.LastVelocity.Y <= 0 && localBody.velocity.Y > 0)
if (localBody.LastVelocity.Y <= PlayerValues.GRAVITY && localBody.velocity.Y > PlayerValues.GRAVITY)
{
CurrentState = PlayerState.Falling;
StartedFalling?.Invoke(localBody.IsKnocked);
Expand Down Expand Up @@ -96,7 +96,7 @@ public void Update(float delta)
if (walkDirection != lastWalkDirection)
{
Walk?.Invoke(walkDirection);
CurrentState = PlayerState.Walk;
CurrentState = walkDirection != 0 ? PlayerState.Walk : PlayerState.Idle;
}

WalkDirection = (sbyte)walkDirection;
Expand Down Expand Up @@ -141,6 +141,13 @@ private void OnJump()
{
CurrentState = PlayerState.Jump;
Jump?.Invoke();

WalkDirection = localBody.velocity.X switch
{
< 0 => -1,
> 0 => 1,
_ => 0
};
}
}
}
2 changes: 1 addition & 1 deletion JKMP.Plugin.Multiplayer/JKMP.Plugin.Multiplayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JKMP.Facepunch.Steamworks.Win32" Version="2.3.4" />
<PackageReference Include="JKMP.Facepunch.Steamworks.Win32" Version="2.3.5" />
<PackageReference Include="JKMP.Core" Version="0.8.3" />
<PackageReference Include="JKMP.GameDependencies" Version="1.0.0" />
<PackageReference Include="JKMP.Myra" Version="2.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion JKMP.Plugin.Multiplayer/Steam/SteamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static bool InitializeSteam()
{
try
{
SteamClient.Init(1061090, false);
SteamClient.Init(1061090, true);
}
catch (Exception ex)
{
Expand Down
11 changes: 2 additions & 9 deletions JKMP.Plugin.Multiplayer/Steam/SteamUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JKMP.Core.Logging;
using Steamworks;

namespace JKMP.Plugin.Multiplayer.Steam
Expand Down Expand Up @@ -29,15 +30,7 @@ private static void OnPersonaStateChange(Friend friend)
{
if (!SteamFriends.RequestUserInformation(steamId))
{
Friend friend = SteamFriends.GetFriends().FirstOrDefault(f => f.Id == steamId);

if (!friend.Id.IsValid)
friend = SteamFriends.GetFriendsOnGameServer().FirstOrDefault(f => f.Id == steamId);

if (!friend.Id.IsValid)
return null;

return friend;
return new Friend(steamId);
}

var tcs = new TaskCompletionSource<Friend>();
Expand Down

0 comments on commit 88af268

Please sign in to comment.