Якимец Максим ECS in games

  • 139 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
139
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. О докладчике Якимец Максим C# на сервере
  • 2. Использование Entity Component System при разработке игр.
  • 3. Структура доклада • • • • • • • • Что предлагает ООП Различные Entity Component Systems Выбор Artemis Описание игрового мира. Примеры AI, Scripting Физика Сеть Tips & Tricks
  • 4. Что предлагает ООП? • • • • • • Множественное наследование Множество классов Композиция Типажи (traits) АОП Паттерн “Стратегия” Множественное поведение
  • 5. Entity Component Systems Unity Ash Ember Artemis Apollo
  • 6. Artemis Entity System Framework
  • 7. Entity Component System Artemis • • • • Entity - ID. Components - данные Systems - логика, поведение Managers - управление данными, запросы Data Oriented Design
  • 8. Artemis var world = new EntityWorld(initializeAll: true); Entity entity = world.CreateEntity(); entity.Add(new Transform(100, 100)); entity.Add(new Velocity(1, 1));
  • 9. Artemis var frameTime = watch.Elapsed - currentTime; accumulator += frameTime; while (accumulator >= minDelta) { entityWorld.Update(minDelta); accumulator -= minDelta; } currentTime = watch.Elapsed;
  • 10. MovementSystem [ArtemisEntitySystem] public class MovementSystem : EntityProcessingSystem { public MovementSystem() : base(Aspect.All(typeof(Transform), typeof(Velocity))) { } ... }
  • 11. 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; }
  • 12. [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); } }
  • 13. Описание игрового мира Position (Transform) Velocity (Movement) Lifetime (Expires) Health (Destroyable) Attack (Damager) Dead (Killed) Gold BountyReward Bonus InputController Animation Collider Physics MovementSystem ExpirationSystem AttackSystem AfterlifeSystem BonusSystem BountyRewardSystem AnimationSystem RenderSystem PhysicsSystem
  • 14. Описание игрового мира CellPosition ScreenPosition Damager TimedEffect Spreadable Destroyable Collidable BombLayer Powerup
  • 15. Описание игрового мира EnemyComponent ExpiresComponent HealthComponent WeaponComponent TransformComponent VelocityComponent SpatialFormComponent
  • 16. Описание игрового мира EnemyComponent ExpiresComponent HealthComponent WeaponComponent TransformComponent VelocityComponent SpatialFormComponent EnemyShipMovementSystem ExpirationSystem EnemyShooterSystem PlayerShipControlSystem CollisionSystem MovementSystem RenderSystem HudRenderSystem EnemySpawnSystem
  • 17. Scripting public class ScriptComponent: IComponent { public IScript Script { get; set; } } public interface IScript { void Init(EntityWorld world, Entity entity); void Update(); }
  • 18. Scripting ScriptComponent (IScript) + ScriptSystem vs BehaviorXComponent + BehaviorXSystem BehaviorYComponent + BehaviorYSystem
  • 19. 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(); } }
  • 20. Физика PhysicsComponent: Body PhysicsSystem: OnAdded -> physicsWorld.Add(body) OnRemoved -> physicsWorld.Remove(body) ProcessEntities -> physicsWorld.Step();
  • 21. Физика: Position PositionComponent PhysicsComponent: Body (Position + Form)
  • 22. Сеть • ECS на сервере и на клиенте • ECS на сервере, ООП на клиенте
  • 23. Сеть 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); } }
  • 24. Tips & tricks Взаимодействие между частями Entity System Entity-Entity Component-Component Entity-Component System-System
  • 25. Tips & tricks • Порядок срабатывания систем • Удаление/добавление Component • Удаление Entity
  • 26. Tips & tricks Pooling • • entity pool component pool Проблема с переиспользованием Entity.Id
  • 27. Tips & tricks Карта - это Entity? Component?
  • 28. Tips & tricks Связи между Entity: references IDs Managers System data • • • •
  • 29. Пример: инвентарь 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; }
  • 30. Пример: Заклинания • • • • • • Удар молнии = Position + Damage + Attacking Камнепад = Position + Damage + AoE + Attacking Мина = Position + Damage + AoE Бомба с фитилем = Position + Damage + AoE + Expires + AttackOnExpires Болото = Position + AoE + Expires + BuffSource Усиление = Position + AoE + Expires(0) + BuffSource
  • 31. Пример: неуязвимость Неуязвимость HealthComponent DefenseComponent
  • 32. “Build games, not engines” “A game is just a real-time database with a pretty graphical front end.” “Entity Systems are the future of MMOs” “Do not try and bend the spoon - that's impossible. Instead, only try to realize the truth… There is no spoon.” “Build games, not elaborate component systems!” maximusya@tut.by