Якимец Максим ECS in games
-
Upload
forkconf -
Category
Technology
-
view
214 -
download
6
description
Transcript of Якимец Максим ECS in games
![Page 1: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/1.jpg)
О докладчике
Якимец Максим
C# на сервере
![Page 2: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/2.jpg)
Entity Component System
Использование
при разработке игр.
![Page 3: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/3.jpg)
![Page 4: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/4.jpg)
Структура доклада
• Что предлагает ООП• Различные Entity Component Systems• Выбор Artemis• Описание игрового мира. Примеры• AI, Scripting• Физика• Сеть• Tips & Tricks
![Page 5: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/5.jpg)
Что предлагает ООП?
• Множественное наследование• Множество классов• Композиция• Типажи (traits)• АОП• Паттерн “Стратегия”
Множественное поведение
![Page 6: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/6.jpg)
Entity Component Systems
UnityAshEmberArtemisApollo
![Page 7: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/7.jpg)
Artemis
Entity System Framework
![Page 8: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/8.jpg)
Entity Component System
Artemis• Entity - ID.• Components - данные• Systems - логика, поведение• Managers - управление данными, запросы
Data Oriented Design
![Page 9: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/9.jpg)
Artemis
var world = new EntityWorld(initializeAll: true);
Entity entity = world.CreateEntity();entity.Add(new Transform(100, 100));entity.Add(new Velocity(1, 1));
![Page 10: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/10.jpg)
Artemis
var frameTime = watch.Elapsed - currentTime;accumulator += frameTime;
while (accumulator >= minDelta){
entityWorld.Update(minDelta);accumulator -= minDelta;
}
currentTime = watch.Elapsed;
![Page 11: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/11.jpg)
MovementSystem
[ArtemisEntitySystem]public class MovementSystem : EntityProcessingSystem{
public MovementSystem(): base(Aspect.All(typeof(Transform), typeof(Velocity)))
{}...
}
![Page 12: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/12.jpg)
MovementSystem
public override void Process(Entity entity){
var velocity = entity.Get<Velocity>();var transform = entity.Get<Transform>();
var seconds = this.EntityWorld.Delta.TotalSeconds;
transform.X += velocity.X * seconds;transform.Y += velocity.Y * seconds;
}
![Page 13: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/13.jpg)
[ArtemisEntitySystem]public class AfterlifeSystem : EntityProcessingSystem{
public event Action<Entity> EntityVanishing;
public AfterlifeSystem(): base(Aspect.All(typeof(KilledComponent)))
{}
public override void Process(Entity entity){
var expiresComponent = entity.Get<ExpiresComponent>();if (expiresComponent == null || expiresComponent.IsExpired){
this.OnEntityVanishing(entity);entity.Delete();
}}
private void OnEntityVanishing(Entity entity){
var handler = this.EntityVanishing;if (handler != null){
handler(entity);}
}}
![Page 14: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/14.jpg)
Position (Transform)Velocity (Movement)Lifetime (Expires)Health (Destroyable)Attack (Damager)Dead (Killed)GoldBountyRewardBonusInputControllerAnimationColliderPhysics
MovementSystemExpirationSystemAttackSystemAfterlifeSystemBonusSystemBountyRewardSystemAnimationSystemRenderSystemPhysicsSystem
Описание игрового мира
![Page 15: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/15.jpg)
CellPositionScreenPositionDamagerTimedEffectSpreadableDestroyableCollidableBombLayerPowerup
Описание игрового мира
![Page 16: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/16.jpg)
Описание игрового мира
EnemyComponentExpiresComponentHealthComponentWeaponComponentTransformComponentVelocityComponentSpatialFormComponent
![Page 17: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/17.jpg)
EnemyComponentExpiresComponentHealthComponentWeaponComponentTransformComponentVelocityComponentSpatialFormComponent
Описание игрового мира
EnemyShipMovementSystem ExpirationSystemEnemyShooterSystem PlayerShipControlSystemCollisionSystemMovementSystemRenderSystemHudRenderSystemEnemySpawnSystem
![Page 18: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/18.jpg)
Scripting
public class ScriptComponent: IComponent{ public IScript Script { get; set; }}
public interface IScript{ void Init(EntityWorld world, Entity entity); void Update();}
![Page 19: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/19.jpg)
Scripting
ScriptComponent (IScript) + ScriptSystem
vs
BehaviorXComponent + BehaviorXSystemBehaviorYComponent + BehaviorYSystem
![Page 20: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/20.jpg)
AI: BehaviorLibrary
public class AIComponent: IComponent{ public Behavior Behavior { get; set; }}
public class AISystem : EntityProcessingSystem{ public override void Process(Entity entity) { entity.Get<AIComponent>().Behavior.Behave(); }}
![Page 21: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/21.jpg)
Физика
PhysicsComponent: Body
PhysicsSystem: OnAdded -> physicsWorld.Add(body) OnRemoved -> physicsWorld.Remove(body)
ProcessEntities -> physicsWorld.Step();
![Page 22: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/22.jpg)
Физика: Position
PositionComponent
PhysicsComponent:Body (Position + Form)
![Page 23: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/23.jpg)
Сеть
• ECS на сервере и на клиенте• ECS на сервере, ООП на клиенте
![Page 24: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/24.jpg)
Сеть
foreach (Entity player in this.players){ float gold = player.Get<Gold>().Value;
if (this.playersGold[player.Id] != gold) { this.playersGold[player.Id] = gold; this.RaiseGoldChangedEvent(gold, player); }}
![Page 25: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/25.jpg)
Tips & tricks
Взаимодействиемежду частями Entity System
Entity-EntityComponent-ComponentEntity-Component
System-System
![Page 26: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/26.jpg)
Tips & tricks
• Порядок срабатывания систем• Удаление/добавление Component• Удаление Entity
![Page 27: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/27.jpg)
Tips & tricks
Pooling• entity pool• component pool
Проблема с переиспользованием Entity.Id
![Page 28: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/28.jpg)
Tips & tricks
Карта - это Entity? Component?
![Page 29: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/29.jpg)
Tips & tricks
Связи между Entity:• references• IDs• Managers• System data
![Page 30: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/30.jpg)
Пример: инвентарь
public class InventoryComponent: IComponent{ public List<Entity> Items { get; set; }}
public class InventoryItemComponent: IComponent{ public Entity Inventory { get; set; }}
public class InventoryManager{ private Dictionary<Entity, List<Entity>> inventories;}
![Page 31: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/31.jpg)
Пример: Заклинания
• Удар молнии = Position + Damage + Attacking• Камнепад = Position + Damage + AoE + Attacking• Мина = Position + Damage + AoE• Бомба с фитилем = Position + Damage + AoE +
Expires + AttackOnExpires• Болото = Position + AoE + Expires + BuffSource• Усиление = Position + AoE + Expires(0) + BuffSource
![Page 32: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/32.jpg)
Пример: неуязвимость
Неуязвимость
HealthComponentDefenseComponent
![Page 33: Якимец Максим ECS in games](https://reader033.fdocument.pub/reader033/viewer/2022051514/548713f8b4af9fa00d8b537c/html5/thumbnails/33.jpg)
“Build games, not engines”
“Build games, not elaborate component systems!”
“Entity Systems are the future of MMOs”
“A game is just a real-time database with a pretty graphical front end.”
“Do not try and bend the spoon - that's impossible. Instead, only try to realize the truth… There is no spoon.”