Top Banner
Entity sys tem architecture with Unity
56

Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Apr 16, 2017

Download

Technology

Wooga
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Entity system architecturewith Unity

Page 2: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Maxim Zaks | @icex33 | github.com/mzaksSimon Schmid | @s_schmid | github.com/sschmid

Page 3: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Wooga

Page 4: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 5: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Unity pain points4 Testability

4 Code sharing

4 Co-dependent logic

4 Querying

4 Deleting code

Page 6: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Testability

Page 7: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Code sharing

Page 8: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Co-dependent logic

Page 9: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Co-dependent logic

Page 10: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Co-dependent logic

Page 11: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Querying

Page 12: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Deleting code

Page 13: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 14: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

EntitasMatch One Demo

Page 15: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 16: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Components are just data

4 no Methods, no Start(), no Update()

4 no inheritance

+-----------+ | Component | |-----------| | Data | +-----------+

Page 17: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

PositionComponent

using Entitas;

public class PositionComponent : IComponent{ public int x; public int y;}

Page 18: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

GameBoardElementComponent

using Entitas;

public class GameBoardElementComponent : IComponent{

}

Page 19: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Entity is a container for components

+-----------+ | Entity | |-----------| | Component | | | | Component | | | | Component | +-----------+

Page 20: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Entity4 Add Components

4 Replace Components

4 Remove Components

Page 21: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Create Blocker Entity

public static Entity CreateBlocker(this Pool pool, int x, int y){ return pool.CreateEntity() .IsGameBoardElement(true) .AddPosition(x, y) .AddResource(Res.Blocker);}

Page 22: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Pool contains all entities

+------------------+ | Pool | |------------------| | e e | | e e | | e e | | e e e | | e e | | e e | | e e e | | e e e | +------------------+

Page 23: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Pool4 Create Entity

4 Destroy Entity

4 Get all Entities

4 Get Group

Page 24: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Groups are subsets of entities

4 Performance optimization for querying

4 Matcher is a filter description +-------------+ Groups: | e | Subsets of entities in the pool | e e | for blazing fast querying | +------------+ | e | | | | e | e | e | +--------|----+ e | | e | | e e | +------------+

Page 25: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Get Group from pool

_pool.GetGroup( Matcher.AllOf( Matcher.GameBoardElement, Matcher.Position ));

Page 26: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

+------------------+ | Pool | Entitas in a nutshell |------------------| | e e | +-----------+ | e e---|----> | Entity | | e e | |-----------| | e e e | | Component | | e e | | | +-----------+ | e e | | Component-|----> | Component | | e e e | | | |-----------| | e e e | | Component | | Data | +------------------+ +-----------+ +-----------+ | | | +-------------+ Groups: | | e | Subsets of entities in the pool | | e e | for blazing fast querying +---> | +------------+ | e | | | | e | e | e | +--------|----+ e | | e | | e e | +------------+

Page 27: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Behaviour

Page 28: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

System4 Start / Execute

4 No State!!!

Page 29: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

+------------------+ +-----------------------------+ | Pool | | System | |------------------| |-----------------------------| | e e | | - Execute | | e e | | | | e e | | +-------------+ | | e e e | | | e | Groups | | e e |+----|->| e e | | | e e | | | +------------+ | | e e e | | | e | | | | | e e e | | | e | e | e | | +------------------+ | +--------|----+ e | | | | e | | | | e e | | | +------------+ | +-----------------------------+

Page 30: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

MoveSystem

public void Execute() { var movables = _pool.GetGroup( Matcher.AllOf( Matcher.Move, Matcher.Position ));

foreach (var e in movables.GetEntities()) { var move = e.move; var pos = e.position; e.ReplacePosition(pos.x, pos.y + move.speed, pos.z); }}

Page 31: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Chain of Responsibility| ||-------------------------------- Game Loop --------------------------------|| |

+------------+ +------------+ +------------+ +------------+| | | | | | | || System | +---> | System | +---> | System | +---> | System || | | | | | | |+------------+ +------------+ +------------+ +------------+

Page 32: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

return new Systems() .Add( pool.CreateGameBoardSystem ()) .Add( pool.CreateCreateGameBoardCacheSystem ()) .Add( pool.CreateFallSystem ()) .Add( pool.CreateFillSystem ())

.Add( pool.CreateProcessInputSystem ())

.Add( pool.CreateRemoveViewSystem ()) .Add( pool.CreateAddViewSystem ()) .Add( pool.CreateRenderPositionSystem ())

.Add( pool.CreateDestroySystem ()) .Add( pool.CreateScoreSystem ());

Page 33: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Reacting to changes in a Group4 On Entity added

4 On Entity removed

Page 34: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

ScoreLabelController

void Start() { _pool.GetGroup(Matcher.Score).OnEntityAdded += (group, entity) => updateScore(entity.score.value);

updateScore(_pool.score.value);}

void updateScore(int score) { _label.text = "Score " + score;}

Page 35: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Reactive System4 Executed only when entities in a group have changed

4 Aggregate and process changes

Page 36: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Render Position Systempublic class RenderPositionSystem : IReactiveSystem { public IMatcher trigger { get { return Matcher.AllOf(Matcher.Position, Matcher.View); } }

public GroupEventType eventType { get { return GroupEventType.OnEntityAdded; } }

public void Execute(Entity[] entities) { foreach (var e in entities) { var pos = e.position; e.view.gameObject.transform.position = new Vector3(pos.x, pos.y); } }}

Page 37: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Optimizations

Page 38: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Componentsmutable vs immutable

Page 39: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 40: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 41: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

EntityDictionary vs Array

Page 42: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Dictionarye.AddComponent(new PositionComponent());

var component = e.GetComponent<PositionComponent>();

Arraye.AddComponent(new PositionComponent(), 5);

var component = (PositionComponent)e.GetComponent(5);

Page 43: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 44: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 45: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Code Generator

Page 46: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Before Code Generation

PositionComponent component;if (e.HasComponent(ComponentIds.Position)) { e.WillRemoveComponent(ComponentIds.Position); component = (PositionComponent)e.GetComponent(ComponentIds.Position);} else { component = new PositionComponent();}component.x = 10;component.y = 10;e.ReplaceComponent(ComponentIds.Position, component);

Page 47: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

After Code Generation

e.ReplacePosition(10, 10);

Page 48: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Code Generator Demo

Page 49: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

var pool = Pools.pool;var e = pool.CreateEntity();

e.AddPosition(1, 2, 3);e.ReplacePosition(4, 5, 6);e.RemovePosition();

var posX = e.position.x;var hasPos = e.hasPosition;

e.isMovable = true;e.isMovable = false;var isMovable = e.isMovable;

Page 50: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Visual Debugging Demo

Page 51: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid
Page 52: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Entitas is

open sourcegithub.com/sschmid/Entitas-CSharp

Page 53: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Recap

Page 54: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Unity pain points4 Testability

4 Code sharing

4 Co-dependent logic

4 Querying

4 Deleting code

Page 55: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Advantages4 Straightforward to achieve Determinism and

therefore Replay

4 Simulation Speed (2x, 4x)

4 Headless Simulation, just remove systems which rely on GameObjects (render systems)

4 Save Game (Serialization / Deserialization) send data to backend on change

Page 56: Entitas System Architecture with Unity - Maxim Zaks and Simon Schmid

Q&AMaxim Zaks | @icex33 | github.com/mzaks

Simon Schmid | @s_schmid | github.com/sschmid

tinyurl.com/entitas