33
О докладчике Якимец Максим C# на сервере

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

Embed Size (px)

DESCRIPTION

 

Citation preview

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

О докладчике

Якимец Максим

C# на сервере

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

Entity Component System

Использование

при разработке игр.

Page 3: Якимец Максим ECS in games
Page 4: Якимец Максим ECS in games

Структура доклада

• Что предлагает ООП• Различные Entity Component Systems• Выбор Artemis• Описание игрового мира. Примеры• AI, Scripting• Физика• Сеть• Tips & Tricks

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

Что предлагает ООП?

• Множественное наследование• Множество классов• Композиция• Типажи (traits)• АОП• Паттерн “Стратегия”

Множественное поведение

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

Entity Component Systems

UnityAshEmberArtemisApollo

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

Artemis

Entity System Framework

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

Entity Component System

Artemis• Entity - ID.• Components - данные• Systems - логика, поведение• Managers - управление данными, запросы

Data Oriented Design

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

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

Artemis

var frameTime = watch.Elapsed - currentTime;accumulator += frameTime;

while (accumulator >= minDelta){

entityWorld.Update(minDelta);accumulator -= minDelta;

}

currentTime = watch.Elapsed;

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

MovementSystem

[ArtemisEntitySystem]public class MovementSystem : EntityProcessingSystem{

public MovementSystem(): base(Aspect.All(typeof(Transform), typeof(Velocity)))

{}...

}

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

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

[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

Position (Transform)Velocity (Movement)Lifetime (Expires)Health (Destroyable)Attack (Damager)Dead (Killed)GoldBountyRewardBonusInputControllerAnimationColliderPhysics

MovementSystemExpirationSystemAttackSystemAfterlifeSystemBonusSystemBountyRewardSystemAnimationSystemRenderSystemPhysicsSystem

Описание игрового мира

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

CellPositionScreenPositionDamagerTimedEffectSpreadableDestroyableCollidableBombLayerPowerup

Описание игрового мира

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

Описание игрового мира

EnemyComponentExpiresComponentHealthComponentWeaponComponentTransformComponentVelocityComponentSpatialFormComponent

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

EnemyComponentExpiresComponentHealthComponentWeaponComponentTransformComponentVelocityComponentSpatialFormComponent

Описание игрового мира

EnemyShipMovementSystem ExpirationSystemEnemyShooterSystem PlayerShipControlSystemCollisionSystemMovementSystemRenderSystemHudRenderSystemEnemySpawnSystem

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

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

Scripting

ScriptComponent (IScript) + ScriptSystem

vs

BehaviorXComponent + BehaviorXSystemBehaviorYComponent + BehaviorYSystem

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

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

Физика

PhysicsComponent: Body

PhysicsSystem: OnAdded -> physicsWorld.Add(body) OnRemoved -> physicsWorld.Remove(body)

ProcessEntities -> physicsWorld.Step();

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

Физика: Position

PositionComponent

PhysicsComponent:Body (Position + Form)

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

Сеть

• ECS на сервере и на клиенте• ECS на сервере, ООП на клиенте

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

Сеть

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

Tips & tricks

Взаимодействиемежду частями Entity System

Entity-EntityComponent-ComponentEntity-Component

System-System

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

Tips & tricks

• Порядок срабатывания систем• Удаление/добавление Component• Удаление Entity

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

Tips & tricks

Pooling• entity pool• component pool

Проблема с переиспользованием Entity.Id

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

Tips & tricks

Карта - это Entity? Component?

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

Tips & tricks

Связи между Entity:• references• IDs• Managers• System data

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

Пример: инвентарь

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

Пример: Заклинания

• Удар молнии = 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

Пример: неуязвимость

Неуязвимость

HealthComponentDefenseComponent

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

[email protected]

“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.”