Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions CharacterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,45 @@
using System;
using System.Text.Json.Serialization;

/// <summary>
/// Handles local player movement and basic player node initialization.
/// Movement is applied only when this node is the multiplayer authority.
/// </summary>
public partial class CharacterController : CharacterBody2D
{
/// <summary>
/// Horizontal movement speed.
/// </summary>
public const float Speed = 300.0f;

/// <summary>
/// Upward impulse used for jump.
/// </summary>
public const float JumpVelocity = -400.0f;

public PlayerInfo Info;

// Get the gravity from the project settings to be synced with RigidBody nodes.
public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();

public override void _Ready()
{
base._Ready();
NakamaClient.Client.PlayerDataSync += onPlayerDataSync;
}
public override void _Ready()
{
base._Ready();
}

/// <summary>
/// Sets the player's visible name and initial spawn position.
/// </summary>
/// <param name="name">Player display identifier.</param>
/// <param name="position">Spawn world position.</param>
public void SetupPlayer(string name, Vector2 position){
GlobalPosition = position;
GetNode<Label>("Label").Text = name;
}

private void onPlayerDataSync(string data)
{
var playerData = JsonConvert.DeserializeObject<PlayerSyncData>(data);

if(playerData.Id == Name){
GlobalPosition = playerData.Position;
RotationDegrees = playerData.RotationDegrees;
}
}


public override void _PhysicsProcess(double delta)
public override void _PhysicsProcess(double delta)
{
if(Name == NakamaClient.Session.Username){
if (IsMultiplayerAuthority()){
Vector2 velocity = Velocity;

// Add the gravity.
Expand All @@ -64,16 +68,6 @@ public override void _PhysicsProcess(double delta)
Velocity = velocity;
MoveAndSlide();

syncData();
}
}

private void syncData(){
PlayerSyncData playerSyncData = new PlayerSyncData(){
Position = GlobalPosition,
RotationDegrees = RotationDegrees,
Id = Name
};
NakamaClient.SyncData(JsonConvert.SerializeObject(playerSyncData), 1);
}
}
1 change: 1 addition & 0 deletions CharacterController.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://caxbbb8xkusye
10 changes: 10 additions & 0 deletions ChatChannel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/// <summary>
/// Represents a chat tab/channel binding used by the UI.
/// </summary>
public class ChatChannel
{
/// <summary>
/// Nakama channel identifier.
/// </summary>
public string ID;

/// <summary>
/// Human-readable label shown in the tab container.
/// </summary>
public string Label;
}
1 change: 1 addition & 0 deletions ChatChannel.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://byjfha1fb6nlf
18 changes: 18 additions & 0 deletions ChatMessage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
/// <summary>
/// Serializable chat payload exchanged through Nakama chat channels.
/// </summary>
public class ChatMessage
{
/// <summary>
/// Message body content.
/// </summary>
public string Message;

/// <summary>
/// Sender username.
/// </summary>
public string User;

/// <summary>
/// Logical room/channel identifier used by the sample.
/// </summary>
public string ID;

/// <summary>
/// Message type marker used by the UI router.
/// </summary>
public int Type;
}
1 change: 1 addition & 0 deletions ChatMessage.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://b57selabvodv1
6 changes: 6 additions & 0 deletions GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Godot;
using System;

/// <summary>
/// Coordinates the transition from UI/lobby to gameplay level.
/// </summary>
public partial class GameManager : Node2D
{

/// <summary>
/// Level scene instantiated when the start-game signal is received.
/// </summary>
[Export]
public PackedScene LevelToLoad;
// Called when the node enters the scene tree for the first time.
Expand Down
1 change: 1 addition & 0 deletions GameManager.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://s4oj34akvow5
8 changes: 3 additions & 5 deletions Nakama cSharp Tutorial.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<Project Sdk="Godot.NET.Sdk/4.2.0">
<Project Sdk="Godot.NET.Sdk/4.6.0">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>NakamacSharpTutorial</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NakamaClient" Version="3.12.1" />
<PackageReference Include="NakamaClient" Version="3.21.2" />
<PackageReference Include="newtonsoft.json" Version="13.0.3" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions Nakama cSharp Tutorial.csproj.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Godot.NET.Sdk/4.2.0">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>NakamacSharpTutorial</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NakamaClient" Version="3.12.1" />
<PackageReference Include="newtonsoft.json" Version="13.0.3" />
</ItemGroup>
</Project>
Loading