Role switching implemented, starting team configuration

This commit is contained in:
xeon 2024-02-11 01:28:35 +03:00
parent 50d47136ba
commit c820b71e9f
13 changed files with 133 additions and 60 deletions

View file

@ -24,7 +24,7 @@ internal class CreatureController : Controller
public async Task JoinScene(int instanceId) public async Task JoinScene(int instanceId)
{ {
_modelManager.Creature.SetSceneLoadingData(instanceId); _modelManager.Creature.SetSceneLoadingData(instanceId);
CreateInitialPlayer(); CreateTeamPlayerEntities();
await Session.Push(MessageId.JoinSceneNotify, new JoinSceneNotify await Session.Push(MessageId.JoinSceneNotify, new JoinSceneNotify
{ {
@ -73,7 +73,7 @@ internal class CreatureController : Controller
[GameEvent(GameEventType.VisionSkillChanged)] [GameEvent(GameEventType.VisionSkillChanged)]
public async Task OnVisionSkillChanged() public async Task OnVisionSkillChanged()
{ {
PlayerEntity? playerEntity = GetPlayerEntity(_modelManager.Player.Id); PlayerEntity? playerEntity = GetPlayerEntity();
if (playerEntity == null) return; if (playerEntity == null) return;
EntityVisionSkillComponent visionSkillComponent = playerEntity.ComponentSystem.Get<EntityVisionSkillComponent>(); EntityVisionSkillComponent visionSkillComponent = playerEntity.ComponentSystem.Get<EntityVisionSkillComponent>();
@ -84,9 +84,25 @@ internal class CreatureController : Controller
await Session.Push(MessageId.VisionSkillChangeNotify, skillChangeNotify); await Session.Push(MessageId.VisionSkillChangeNotify, skillChangeNotify);
} }
public PlayerEntity? GetPlayerEntity(int playerId) public PlayerEntity? GetPlayerEntity()
{ {
return _entitySystem.EnumerateEntities().FirstOrDefault(entity => entity is PlayerEntity p && p.PlayerId == playerId) as PlayerEntity; return _entitySystem.EnumerateEntities().FirstOrDefault(entity => entity.Id == _modelManager.Creature.PlayerEntityId) as PlayerEntity;
}
public async Task SwitchPlayerEntity(int roleId)
{
PlayerEntity? prevEntity = GetPlayerEntity();
if (prevEntity == null) return;
prevEntity.IsCurrentRole = false;
PlayerEntity? newEntity = _entitySystem.EnumerateEntities().FirstOrDefault(e => e is PlayerEntity playerEntity && playerEntity.ConfigId == roleId) as PlayerEntity;
if (newEntity == null) return;
_modelManager.Creature.PlayerEntityId = newEntity.Id;
newEntity.IsCurrentRole = true;
await OnVisionSkillChanged();
} }
private SceneInformation CreateSceneInfo() private SceneInformation CreateSceneInfo()
@ -111,37 +127,44 @@ internal class CreatureController : Controller
Y = -2000, Y = -2000,
Z = 260 Z = 260
}, },
FightRoleInfos =
{
new FightRoleInformation
{
EntityId = 1,
CurHp = 1000,
MaxHp = 1000,
IsControl = true,
RoleId = _modelManager.Player.CharacterId,
RoleLevel = 1,
}
},
PlayerName = _modelManager.Player.Name PlayerName = _modelManager.Player.Name
} }
} }
}; };
for (int i = 0; i < _modelManager.Player.Characters.Length; i++)
{
scene.PlayerInfos[0].FightRoleInfos.Add(new FightRoleInformation
{
EntityId = i + 1,
CurHp = 1000,
MaxHp = 1000,
IsControl = i == 0,
RoleId = _modelManager.Player.Characters[i],
RoleLevel = 1,
});
}
scene.AoiData.Entities.AddRange(_entitySystem.Pb); scene.AoiData.Entities.AddRange(_entitySystem.Pb);
return scene; return scene;
} }
private void CreateInitialPlayer() private void CreateTeamPlayerEntities()
{ {
PlayerEntity entity = _entityFactory.CreatePlayer(1601, _modelManager.Player.Id); for (int i = 0; i < _modelManager.Player.Characters.Length; i++)
entity.Pos = new()
{ {
X = 4000, PlayerEntity entity = _entityFactory.CreatePlayer(_modelManager.Player.Characters[i], _modelManager.Player.Id);
Y = -2000, entity.Pos = new()
Z = 260 {
}; X = 4000,
Y = -2000,
Z = 260
};
entity.IsCurrentRole = i == 0;
_entitySystem.Create(entity); _entitySystem.Create(entity);
if (i == 0) _modelManager.Creature.PlayerEntityId = entity.Id;
}
} }
} }

View file

@ -21,10 +21,10 @@ internal class FormationController : Controller
{ {
new FightFormation new FightFormation
{ {
CurRole = _modelManager.Player.CharacterId, CurRole = _modelManager.Player.Characters[0],
FormationId = 1, FormationId = 1,
IsCurrent = true, IsCurrent = true,
RoleIds = { _modelManager.Player.CharacterId }, RoleIds = { _modelManager.Player.Characters },
} }
}, },
}); });

View file

@ -17,7 +17,7 @@ internal class PlayerInfoController : Controller
{ {
PlayerModel player = modelManager.Player; PlayerModel player = modelManager.Player;
await Session.Push(MessageId.BasicInfoNotify, new BasicInfoNotify BasicInfoNotify basicInfo = new()
{ {
RandomSeed = 1337, RandomSeed = 1337,
Id = player.Id, Id = player.Id,
@ -36,15 +36,18 @@ internal class PlayerInfoController : Controller
ValueType = (int)PlayerAttrType.Int32, ValueType = (int)PlayerAttrType.Int32,
Int32Value = 10 Int32Value = 10
} }
}, }
RoleShowList = };
for (int i = 0; i < player.Characters.Length; i++)
{
basicInfo.RoleShowList.Add(new RoleShowEntry
{ {
new RoleShowEntry Level = 1,
{ RoleId = player.Characters[i]
Level = 1, });
RoleId = player.CharacterId }
}
}, await Session.Push(MessageId.BasicInfoNotify, basicInfo);
});
} }
} }

View file

@ -22,15 +22,24 @@ internal class RoleController : Controller
{ {
RoleList = RoleList =
{ {
new roleInfo player.Characters.Select(i => new roleInfo
{ {
RoleId = player.CharacterId, RoleId = i,
Level = 1, Level = 1
} })
} }
}); });
} }
[NetEvent(MessageId.SwitchRoleRequest)]
public ResponseMessage OnSwitchRoleRequest(SwitchRoleRequest request)
{
return Response(MessageId.SwitchRoleResponse, new SwitchRoleResponse
{
RoleId = request.RoleId
});
}
[NetEvent(MessageId.RoleFavorListRequest)] [NetEvent(MessageId.RoleFavorListRequest)]
public ResponseMessage OnRoleFavorListRequest() => Response(MessageId.RoleFavorListResponse, new RoleFavorListResponse()); public ResponseMessage OnRoleFavorListRequest() => Response(MessageId.RoleFavorListResponse, new RoleFavorListResponse());
} }

View file

@ -41,9 +41,9 @@ internal class RouletteController : Controller
} }
[NetEvent(MessageId.VisionExploreSkillSetRequest)] [NetEvent(MessageId.VisionExploreSkillSetRequest)]
public async Task<ResponseMessage> OnVisionExploreSkillSetRequest(VisionExploreSkillSetRequest request, CreatureController creatureController, ModelManager modelManager, EventSystem eventSystem) public async Task<ResponseMessage> OnVisionExploreSkillSetRequest(VisionExploreSkillSetRequest request, CreatureController creatureController, EventSystem eventSystem)
{ {
PlayerEntity? playerEntity = creatureController.GetPlayerEntity(modelManager.Player.Id); PlayerEntity? playerEntity = creatureController.GetPlayerEntity();
if (playerEntity == null) return Response(MessageId.VisionExploreSkillSetResponse, new VisionExploreSkillSetResponse { ErrCode = (int)ErrorCode.PlayerNotInAnyScene }); if (playerEntity == null) return Response(MessageId.VisionExploreSkillSetResponse, new VisionExploreSkillSetResponse { ErrCode = (int)ErrorCode.PlayerNotInAnyScene });
EntityVisionSkillComponent visionSkillComponent = playerEntity.ComponentSystem.Get<EntityVisionSkillComponent>(); EntityVisionSkillComponent visionSkillComponent = playerEntity.ComponentSystem.Get<EntityVisionSkillComponent>();

View file

@ -20,6 +20,9 @@
<None Update="appsettings.json"> <None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="gameplay.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,16 +1,25 @@
using GameServer.Controllers.Attributes; using GameServer.Controllers.Attributes;
using GameServer.Settings;
using GameServer.Systems.Event; using GameServer.Systems.Event;
using Microsoft.Extensions.Options;
namespace GameServer.Models; namespace GameServer.Models;
internal class ModelManager internal class ModelManager
{ {
private readonly IOptions<PlayerStartingValues> _playerStartingValues;
private PlayerModel? _playerModel; private PlayerModel? _playerModel;
private CreatureModel? _creatureModel; private CreatureModel? _creatureModel;
public ModelManager(IOptions<PlayerStartingValues> playerStartingValues)
{
_playerStartingValues = playerStartingValues;
}
[GameEvent(GameEventType.Login)] [GameEvent(GameEventType.Login)]
public void OnLogin() public void OnLogin()
{ {
_playerModel = PlayerModel.CreateDefaultPlayer(); _playerModel = PlayerModel.CreateDefaultPlayer(_playerStartingValues.Value);
_creatureModel = new CreatureModel(_playerModel.Id); _creatureModel = new CreatureModel(_playerModel.Id);
} }

View file

@ -1,22 +1,25 @@
namespace GameServer.Models; using GameServer.Settings;
namespace GameServer.Models;
internal class PlayerModel internal class PlayerModel
{ {
public int Id { get; private set; } public int Id { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
public int CharacterId { get; set; } public int[] Characters { get; private set; }
public PlayerModel() public PlayerModel()
{ {
Name = string.Empty; Name = string.Empty;
Characters = [];
} }
public static PlayerModel CreateDefaultPlayer() public static PlayerModel CreateDefaultPlayer(PlayerStartingValues startingValues)
{ {
return new PlayerModel return new PlayerModel
{ {
Id = 1337, Id = 1337,
Name = "ReversedRooms", Name = startingValues.Name,
CharacterId = 1601 Characters = startingValues.Characters
}; };
} }
} }

View file

@ -23,8 +23,7 @@ internal static class Program
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(); builder.Logging.AddConsole();
builder.Services.Configure<GatewaySettings>(builder.Configuration.GetRequiredSection("Gateway")); builder.SetupConfiguration();
builder.Services.AddControllers() builder.Services.AddControllers()
.AddSingleton<KcpGateway>().AddScoped<PlayerSession>() .AddSingleton<KcpGateway>().AddScoped<PlayerSession>()
.AddScoped<MessageManager>().AddSingleton<EventHandlerFactory>() .AddScoped<MessageManager>().AddSingleton<EventHandlerFactory>()
@ -36,4 +35,11 @@ internal static class Program
await builder.Build().RunAsync(); await builder.Build().RunAsync();
} }
private static void SetupConfiguration(this HostApplicationBuilder builder)
{
builder.Configuration.AddJsonFile("gameplay.json");
builder.Services.Configure<GatewaySettings>(builder.Configuration.GetRequiredSection("Gateway"));
builder.Services.Configure<PlayerStartingValues>(builder.Configuration.GetRequiredSection("StartingValues"));
}
} }

View file

@ -0,0 +1,6 @@
namespace GameServer.Settings;
internal class PlayerStartingValues
{
public required string Name { get; set; }
public required int[] Characters { get; set; }
}

View file

@ -29,14 +29,9 @@ internal abstract class EntityBase
State = EntityState.Born; State = EntityState.Born;
} }
public void Activate() public virtual void Activate()
{ {
AddComponents(); // Activate.
}
public virtual void AddComponents()
{
// AddComponents.
} }
public virtual LivingStatus LivingStatus => LivingStatus.Alive; public virtual LivingStatus LivingStatus => LivingStatus.Alive;

View file

@ -13,15 +13,23 @@ internal class PlayerEntity : EntityBase
public int ConfigId { get; } public int ConfigId { get; }
public int PlayerId { get; } public int PlayerId { get; }
public override void AddComponents() public bool IsCurrentRole { get; set; }
{
base.AddComponents();
public override void OnCreate()
{
base.OnCreate();
// Should be created immediately
EntityConcomitantsComponent concomitantsComponent = ComponentSystem.Create<EntityConcomitantsComponent>(); EntityConcomitantsComponent concomitantsComponent = ComponentSystem.Create<EntityConcomitantsComponent>();
concomitantsComponent.CustomEntityIds.Add(Id); concomitantsComponent.CustomEntityIds.Add(Id);
EntityVisionSkillComponent visionSkillComponent = ComponentSystem.Create<EntityVisionSkillComponent>(); EntityVisionSkillComponent visionSkillComponent = ComponentSystem.Create<EntityVisionSkillComponent>();
visionSkillComponent.SetExploreTool(1001); visionSkillComponent.SetExploreTool(1001);
}
public override void Activate()
{
base.Activate();
_ = ComponentSystem.Create<EntityAttributeComponent>(); _ = ComponentSystem.Create<EntityAttributeComponent>();
InitAttributes(); InitAttributes();
@ -40,6 +48,8 @@ internal class PlayerEntity : EntityBase
public override EEntityType Type => EEntityType.Player; public override EEntityType Type => EEntityType.Player;
public override EntityConfigType ConfigType => EntityConfigType.Character; public override EntityConfigType ConfigType => EntityConfigType.Character;
public override bool IsVisible => IsCurrentRole;
public override EntityPb Pb public override EntityPb Pb
{ {
get get

6
GameServer/gameplay.json Normal file
View file

@ -0,0 +1,6 @@
{
"StartingValues": {
"Name": "ReversedRooms",
"Characters": [ 1601, 1302, 1203 ]
}
}