2024-02-12 06:26:32 +00:00
|
|
|
using Core.Config;
|
2024-02-11 22:23:13 +00:00
|
|
|
using GameServer.Controllers.Attributes;
|
2024-02-10 16:04:03 +00:00
|
|
|
using GameServer.Models;
|
|
|
|
using GameServer.Network;
|
|
|
|
using GameServer.Network.Messages;
|
|
|
|
using GameServer.Systems.Entity;
|
|
|
|
using GameServer.Systems.Entity.Component;
|
|
|
|
using GameServer.Systems.Event;
|
|
|
|
using Protocol;
|
|
|
|
|
|
|
|
namespace GameServer.Controllers;
|
|
|
|
internal class CreatureController : Controller
|
|
|
|
{
|
|
|
|
private readonly EntitySystem _entitySystem;
|
|
|
|
private readonly EntityFactory _entityFactory;
|
|
|
|
private readonly ModelManager _modelManager;
|
2024-02-12 00:23:27 +00:00
|
|
|
private readonly ConfigManager _configManager;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
2024-02-12 00:23:27 +00:00
|
|
|
public CreatureController(PlayerSession session, EntitySystem entitySystem, EntityFactory entityFactory, ModelManager modelManager, ConfigManager configManager) : base(session)
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
|
|
|
_entitySystem = entitySystem;
|
|
|
|
_entityFactory = entityFactory;
|
|
|
|
_modelManager = modelManager;
|
2024-02-12 00:23:27 +00:00
|
|
|
_configManager = configManager;
|
2024-02-10 16:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task JoinScene(int instanceId)
|
|
|
|
{
|
|
|
|
_modelManager.Creature.SetSceneLoadingData(instanceId);
|
2024-02-10 22:28:35 +00:00
|
|
|
CreateTeamPlayerEntities();
|
2024-02-14 21:22:21 +00:00
|
|
|
CreateWorldEntities();
|
2024-02-10 16:04:03 +00:00
|
|
|
|
|
|
|
await Session.Push(MessageId.JoinSceneNotify, new JoinSceneNotify
|
|
|
|
{
|
2024-02-12 20:15:14 +00:00
|
|
|
MaxEntityId = 10000000,
|
2024-02-10 16:04:03 +00:00
|
|
|
TransitionOption = new TransitionOptionPb
|
|
|
|
{
|
|
|
|
TransitionType = (int)TransitionType.Empty
|
|
|
|
},
|
|
|
|
SceneInfo = CreateSceneInfo()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[NetEvent(MessageId.EntityActiveRequest)]
|
|
|
|
public async Task<ResponseMessage> OnEntityActiveRequest(EntityActiveRequest request)
|
|
|
|
{
|
|
|
|
EntityActiveResponse response;
|
|
|
|
|
|
|
|
EntityBase? entity = _entitySystem.Get<EntityBase>(request.EntityId);
|
|
|
|
if (entity != null)
|
|
|
|
{
|
|
|
|
_entitySystem.Activate(entity);
|
|
|
|
response = new EntityActiveResponse
|
|
|
|
{
|
|
|
|
ErrorCode = (int)ErrorCode.Success
|
|
|
|
};
|
|
|
|
|
|
|
|
response.ComponentPbs.AddRange(entity.ComponentSystem.Pb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = new EntityActiveResponse { ErrorCode = (int)ErrorCode.ErrActionEntityNoExist };
|
|
|
|
}
|
|
|
|
|
|
|
|
await OnVisionSkillChanged();
|
|
|
|
return Response(MessageId.EntityActiveResponse, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
[NetEvent(MessageId.SceneLoadingFinishRequest)]
|
|
|
|
public ResponseMessage OnSceneLoadingFinishRequest()
|
|
|
|
{
|
|
|
|
_modelManager.Creature.OnWorldDone();
|
|
|
|
|
|
|
|
return Response(MessageId.SceneLoadingFinishResponse, new SceneLoadingFinishResponse());
|
|
|
|
}
|
|
|
|
|
2024-02-11 22:23:13 +00:00
|
|
|
[GameEvent(GameEventType.FormationUpdated)]
|
|
|
|
public async Task OnFormationUpdated()
|
|
|
|
{
|
|
|
|
// Remove old entities
|
|
|
|
|
|
|
|
IEnumerable<PlayerEntity> oldEntities = _entitySystem.EnumerateEntities()
|
|
|
|
.Where(e => e is PlayerEntity entity && entity.PlayerId == _modelManager.Player.Id)
|
2024-02-12 06:26:32 +00:00
|
|
|
.Cast<PlayerEntity>().ToArray();
|
2024-02-11 22:23:13 +00:00
|
|
|
|
|
|
|
foreach (PlayerEntity oldEntity in oldEntities)
|
|
|
|
{
|
|
|
|
_entitySystem.Destroy(oldEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Session.Push(MessageId.EntityRemoveNotify, new EntityRemoveNotify
|
|
|
|
{
|
|
|
|
IsRemove = true,
|
|
|
|
RemoveInfos =
|
|
|
|
{
|
|
|
|
oldEntities.Select(entity => new EntityRemoveInfo
|
|
|
|
{
|
|
|
|
EntityId = entity.Id,
|
|
|
|
Type = (int)entity.Type
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Spawn new entities
|
|
|
|
|
|
|
|
CreateTeamPlayerEntities();
|
|
|
|
|
|
|
|
IEnumerable<PlayerEntity> newEntities = _entitySystem.EnumerateEntities()
|
|
|
|
.Where(e => e is PlayerEntity entity && entity.PlayerId == _modelManager.Player.Id)
|
|
|
|
.Cast<PlayerEntity>();
|
|
|
|
|
|
|
|
await Session.Push(MessageId.EntityAddNotify, new EntityAddNotify
|
|
|
|
{
|
|
|
|
IsAdd = true,
|
|
|
|
EntityPbs =
|
|
|
|
{
|
|
|
|
newEntities.Select(entity => entity.Pb)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_modelManager.Creature.PlayerEntityId = newEntities.First().Id;
|
|
|
|
await Session.Push(MessageId.UpdatePlayerAllFightRoleNotify, new UpdatePlayerAllFightRoleNotify
|
|
|
|
{
|
|
|
|
PlayerId = _modelManager.Player.Id,
|
2024-02-12 01:03:50 +00:00
|
|
|
FightRoleInfos = { GetFightRoleInfos() }
|
2024-02-11 22:23:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:04:03 +00:00
|
|
|
[GameEvent(GameEventType.VisionSkillChanged)]
|
|
|
|
public async Task OnVisionSkillChanged()
|
|
|
|
{
|
2024-02-10 22:28:35 +00:00
|
|
|
PlayerEntity? playerEntity = GetPlayerEntity();
|
2024-02-10 16:04:03 +00:00
|
|
|
if (playerEntity == null) return;
|
|
|
|
|
|
|
|
EntityVisionSkillComponent visionSkillComponent = playerEntity.ComponentSystem.Get<EntityVisionSkillComponent>();
|
|
|
|
|
|
|
|
VisionSkillChangeNotify skillChangeNotify = new() { EntityId = playerEntity.Id };
|
|
|
|
skillChangeNotify.VisionSkillInfos.AddRange(visionSkillComponent.Skills);
|
|
|
|
|
|
|
|
await Session.Push(MessageId.VisionSkillChangeNotify, skillChangeNotify);
|
|
|
|
}
|
|
|
|
|
2024-02-10 22:28:35 +00:00
|
|
|
public PlayerEntity? GetPlayerEntity()
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-11 22:23:13 +00:00
|
|
|
return _entitySystem.Get<PlayerEntity>(_modelManager.Creature.PlayerEntityId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerEntity? GetPlayerEntityByRoleId(int roleId)
|
|
|
|
{
|
|
|
|
return _entitySystem.EnumerateEntities()
|
|
|
|
.FirstOrDefault(e => e is PlayerEntity playerEntity && playerEntity.ConfigId == roleId && playerEntity.PlayerId == _modelManager.Player.Id) as PlayerEntity;
|
2024-02-10 22:28:35 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 21:22:21 +00:00
|
|
|
public IEnumerable<PlayerEntity> GetPlayerEntities()
|
|
|
|
{
|
|
|
|
return _entitySystem.EnumerateEntities()
|
|
|
|
.Where(e => e is PlayerEntity entity && entity.PlayerId == _modelManager.Player.Id)
|
|
|
|
.Cast<PlayerEntity>();
|
|
|
|
}
|
|
|
|
|
2024-02-10 22:28:35 +00:00
|
|
|
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();
|
2024-02-10 16:04:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 01:03:50 +00:00
|
|
|
private SceneInformation CreateSceneInfo() => new()
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-12 01:03:50 +00:00
|
|
|
InstanceId = _modelManager.Creature.InstanceId,
|
|
|
|
OwnerId = _modelManager.Creature.OwnerId,
|
|
|
|
CurContextId = _modelManager.Player.Id,
|
|
|
|
TimeInfo = new(),
|
|
|
|
AoiData = new PlayerSceneAoiData
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-12 01:03:50 +00:00
|
|
|
Entities = { _entitySystem.Pb }
|
|
|
|
},
|
|
|
|
PlayerInfos =
|
|
|
|
{
|
|
|
|
new ScenePlayerInformation
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-12 01:03:50 +00:00
|
|
|
PlayerId = _modelManager.Player.Id,
|
|
|
|
Level = 1,
|
|
|
|
IsOffline = false,
|
2024-02-15 18:33:05 +00:00
|
|
|
Location = _modelManager.Player.Position,
|
2024-02-12 01:03:50 +00:00
|
|
|
PlayerName = _modelManager.Player.Name,
|
|
|
|
FightRoleInfos = { GetFightRoleInfos() }
|
2024-02-10 16:04:03 +00:00
|
|
|
}
|
2024-02-10 22:28:35 +00:00
|
|
|
}
|
2024-02-12 01:03:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private IEnumerable<FightRoleInformation> GetFightRoleInfos()
|
|
|
|
{
|
|
|
|
IEnumerable<PlayerEntity> playerEntities = _entitySystem.EnumerateEntities()
|
|
|
|
.Where(e => e is PlayerEntity entity && entity.PlayerId == _modelManager.Player.Id)
|
|
|
|
.Cast<PlayerEntity>();
|
2024-02-10 22:28:35 +00:00
|
|
|
|
2024-02-12 01:03:50 +00:00
|
|
|
return playerEntities.Select(playerEntity => new FightRoleInformation
|
|
|
|
{
|
|
|
|
EntityId = playerEntity.Id,
|
|
|
|
CurHp = playerEntity.Health,
|
|
|
|
MaxHp = playerEntity.HealthMax,
|
|
|
|
IsControl = playerEntity.Id == _modelManager.Creature.PlayerEntityId,
|
|
|
|
RoleId = playerEntity.ConfigId,
|
|
|
|
RoleLevel = 1,
|
|
|
|
});
|
2024-02-10 16:04:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 22:28:35 +00:00
|
|
|
private void CreateTeamPlayerEntities()
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-11 22:23:13 +00:00
|
|
|
for (int i = 0; i < _modelManager.Formation.RoleIds.Length; i++)
|
2024-02-10 16:04:03 +00:00
|
|
|
{
|
2024-02-11 22:23:13 +00:00
|
|
|
PlayerEntity entity = _entityFactory.CreatePlayer(_modelManager.Formation.RoleIds[i], _modelManager.Player.Id);
|
2024-02-15 18:33:05 +00:00
|
|
|
entity.Pos = _modelManager.Player.Position.Clone();
|
2024-02-10 22:28:35 +00:00
|
|
|
entity.IsCurrentRole = i == 0;
|
2024-02-10 16:04:03 +00:00
|
|
|
|
2024-02-10 22:28:35 +00:00
|
|
|
_entitySystem.Create(entity);
|
2024-02-12 20:15:14 +00:00
|
|
|
entity.InitProps(_configManager.GetConfig<BasePropertyConfig>(entity.ConfigId)!);
|
2024-02-10 22:28:35 +00:00
|
|
|
|
2024-02-12 00:23:27 +00:00
|
|
|
// Give weapon to entity
|
|
|
|
RoleInfoConfig roleConfig = _configManager.GetConfig<RoleInfoConfig>(entity.ConfigId)!;
|
2024-02-12 11:33:01 +00:00
|
|
|
WeaponConfig weaponConfig = _configManager.GetConfig<WeaponConfig>(roleConfig.InitWeaponItemId)!;
|
2024-02-12 00:23:27 +00:00
|
|
|
entity.WeaponId = weaponConfig.ItemId;
|
|
|
|
|
2024-02-10 22:28:35 +00:00
|
|
|
if (i == 0) _modelManager.Creature.PlayerEntityId = entity.Id;
|
|
|
|
}
|
2024-02-14 21:22:21 +00:00
|
|
|
}
|
2024-02-12 20:15:14 +00:00
|
|
|
|
2024-02-14 21:22:21 +00:00
|
|
|
private void CreateWorldEntities()
|
|
|
|
{
|
2024-02-15 18:33:05 +00:00
|
|
|
Vector playerPos = _modelManager.Player.Position;
|
|
|
|
|
2024-02-12 20:15:14 +00:00
|
|
|
// Test monster
|
2024-02-15 18:33:05 +00:00
|
|
|
MonsterEntity monster = _entityFactory.CreateMonster(106003002); // Turtle.
|
2024-02-12 20:15:14 +00:00
|
|
|
monster.Pos = new()
|
|
|
|
{
|
2024-02-15 18:33:05 +00:00
|
|
|
X = playerPos.X + 250,
|
|
|
|
Y = playerPos.Y + 250,
|
|
|
|
Z = playerPos.Z
|
2024-02-12 20:15:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_entitySystem.Create(monster);
|
|
|
|
monster.InitProps(_configManager.GetConfig<BasePropertyConfig>(600000100)!);
|
2024-02-10 16:04:03 +00:00
|
|
|
}
|
|
|
|
}
|