Slide 1

Slide 1 text

Creating games with entities and components Denis Defreyne / RUG::B / December 4, 2014 1

Slide 2

Slide 2 text

I make games*! 2 * Or at least I try.

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

Less about games. More about paradigms. 7

Slide 8

Slide 8 text

I am not a professional game developer. 8 DISCLAIMER

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

Put your data first. 10

Slide 11

Slide 11 text

If all you have is the data,
 you know what it’s about.
 If all you have is the behavior,
 you’re still in the dark. 11

Slide 12

Slide 12 text

In object orientation, objects encapsulate data. 12

Slide 13

Slide 13 text

Objects are defined by their methods, not by their data. 13

Slide 14

Slide 14 text

Encapsulation is not data-driven. 14

Slide 15

Slide 15 text

Object orientation is not data-driven. 15

Slide 16

Slide 16 text

You can be data-driven in an object-oriented language. 16

Slide 17

Slide 17 text

Objects combine data with behavior. 17

Slide 18

Slide 18 text

Games have data position, velocity, health, … Games have behaviors movement, combat, AI, … 18

Slide 19

Slide 19 text

How does the game data get its behavior? 19

Slide 20

Slide 20 text

class Spaceship end 20

Slide 21

Slide 21 text

class Spaceship def update # TODO: Write me end end 21

Slide 22

Slide 22 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # … end end 22

Slide 23

Slide 23 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # … end end 23

Slide 24

Slide 24 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # Rotate if Keyboard.key_down?('a') @rotation -= 5 elsif Keyboard.key_down?('d') @rotation += 5 end # TODO: Add rotational velocity # … end end 24

Slide 25

Slide 25 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # Rotate if Keyboard.key_down?('a') @rotation -= 5 elsif Keyboard.key_down?('d') @rotation += 5 end # TODO: Add rotational velocity # Accelerate @velocity_x += Math.cos(@rotation) * @acceleration @velocity_y += Math.sin(@rotation) * @acceleration # … end end 25

Slide 26

Slide 26 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # Rotate if Keyboard.key_down?('a') @rotation -= 5 elsif Keyboard.key_down?('d') @rotation += 5 end # TODO: Add rotational velocity # Accelerate @velocity_x += Math.cos(@rotation) * @acceleration @velocity_y += Math.sin(@rotation) * @acceleration # Cap speed @velocity_x = [@velocity_x, @max_velocity_x].min @velocity_y = [@velocity_y, @max_velocity_y].min # … end end 26

Slide 27

Slide 27 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # Rotate if Keyboard.key_down?('a') @rotation -= 5 elsif Keyboard.key_down?('d') @rotation += 5 end # TODO: Add rotational velocity # Accelerate @velocity_x += Math.cos(@rotation) * @acceleration @velocity_y += Math.sin(@rotation) * @acceleration # Cap speed @velocity_x = [@velocity_x, @max_velocity_x].min @velocity_y = [@velocity_y, @max_velocity_y].min # Render Graphics.translate(@position_x, @position_y) do Graphics.rotate(@rotation) do @sprite.draw end end end end 27

Slide 28

Slide 28 text

class Spaceship def update # Move @position_x += @velocity_x @position_y += @velocity_y # Calculate acceleration @acceleration = 0.0 if Keyboard.key_down?('w') @acceleration = 10.0 # TODO: Play accelerate sound elsif Keyboard.key_down?('s') @acceleration = -3.0 end # Rotate if Keyboard.key_down?('a') @rotation -= 5 elsif Keyboard.key_down?('d') @rotation += 5 end # TODO: Add rotational velocity # Accelerate @velocity_x += Math.cos(@rotation) * @acceleration @velocity_y += Math.sin(@rotation) * @acceleration # Cap speed @velocity_x = [@velocity_x, @max_velocity_x].min @velocity_y = [@velocity_y, @max_velocity_y].min # Render Graphics.translate(@position_x, @position_y) do Graphics.rotate(@rotation) do if @acceleration > 0.0 @flame_animation.step @flame_sprite = @flame_animation.sprite @flame_sprite.draw end @sprite.draw end end end end 28

Slide 29

Slide 29 text

class Spaceship < Movable … end 29 ?

Slide 30

Slide 30 text

class Spaceship include Movable include Decoration … end 30 ?

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

How can you be data-driven in an object oriented language? 32

Slide 33

Slide 33 text

A better approach: entities, components, systems. 33

Slide 34

Slide 34 text

Objects can have logic. That doesn’t mean they should. 34

Slide 35

Slide 35 text

class Spaceship attr_reader :position_x, :position_y attr_reader :velocity_x, :velocity_y attr_reader :acceleration_x, :acceleration_y attr_reader :rotation attr_reader :shield_cur, :shield_max, :shield_rate attr_reader :armor_cur, :armor_max def initialize(params = {}) … end end 35

Slide 36

Slide 36 text

Entities are stupid objects; they’re just structs. 36

Slide 37

Slide 37 text

How do these entities get their behaviors? 37

Slide 38

Slide 38 text

Behaviors are handled by systems. 38

Slide 39

Slide 39 text

A system is essentially a procedure* that is called thirty times per second. 39 * A function with only side effects

Slide 40

Slide 40 text

If you mutate state, you might as well be explicit about it. 40

Slide 41

Slide 41 text

spaceship = Spaceship.new(
 position_x: 200,
 position_y: 150,
 velocity_x: 10,
 velocity_y: -5,
 armor_cur: 100,
 armor_max: 100)
 movement_system = MovementSystem.new movement_system.update(spaceship)
 p [spaceship.position_x, spaceship.position_y] # => [210, 145] 41

Slide 42

Slide 42 text

class MovementSystem
 def update(entity)
 entity.position_x += entity.velocity_x entity.position_y += entity.velocity_y end end 42

Slide 43

Slide 43 text

class MovementSystem class HealthSystem class InputSystem class AISteeringSystem class RenderingSystem class … 43

Slide 44

Slide 44 text

Systems are decoupled. 44

Slide 45

Slide 45 text

Data is stored in components. 45

Slide 46

Slide 46 text

class Spaceship attr_reader :position_x, :position_y attr_reader :velocity_x, :velocity_y attr_reader :acceleration_x, :acceleration_y attr_reader :rotation attr_reader :shield_cur, :shield_max, :shield_rate attr_reader :armor_cur, :armor_max def initialize(params = {}) … end end 46

Slide 47

Slide 47 text

class Spaceship attr_reader :position attr_reader :velocity attr_reader :acceleration attr_reader :rotation attr_reader :shield attr_reader :armor def initialize(params = {}) … end end 47

Slide 48

Slide 48 text

Position = Struct.new(:x, :y) Velocity = Struct.new(:x, :y) Acceleration = Struct.new(:x, :y) Rotation = Struct.new(:rad) Shield = Struct.new(:cur, :max, :rate) Armor = Struct.new(:cur, :max) 48

Slide 49

Slide 49 text

Components have no behavior. 49

Slide 50

Slide 50 text

Entities are essentially just collections of components. 50

Slide 51

Slide 51 text

class Spaceship attr_reader :position attr_reader :velocity attr_reader :acceleration attr_reader :rotation attr_reader :shield attr_reader :armor def initialize(params = {}) … end end 51

Slide 52

Slide 52 text

spaceship = Entity.new 52

Slide 53

Slide 53 text

spaceship = Entity.new spaceship.add(Position.new(400, 200)) spaceship.add(Velocity.new(0, 0)) spaceship.add(Acceleration.new(0, 0)) spaceship.add(Rotation.new(Math::PI / 4)) spaceship.add(Armor.new(100, 100)) spaceship.add(Shield.new(100, 100, 10)) 53

Slide 54

Slide 54 text

class MovementSystem def update(entity) entity[Position] += entity[Velocity] end end 54

Slide 55

Slide 55 text

Entities group components. Components contain data. Systems update components. 55

Slide 56

Slide 56 text

What are the advantages being data-driven? 56

Slide 57

Slide 57 text

It gives us the power to change behavior at runtime! 57 WIN #1

Slide 58

Slide 58 text

# Become invisible spaceship.remove(Sprite) # Become mortal angel.add(Health.new(100)) # Become a ghost asteroid.remove(CollisionShape) # Mind control enemy enemy.remove(AISteering) enemy.add(PlayerSteering) 58

Slide 59

Slide 59 text

It gives us data locality*! 59 * Not so much in Ruby. WIN #2

Slide 60

Slide 60 text

60 HD RAM Cache ~ 1 000 000 ns ~ 1-10 ns ~ 500 ns

Slide 61

Slide 61 text

61

Slide 62

Slide 62 text

62

Slide 63

Slide 63 text

To avoid cache misses, keep similar data together in memory. 63

Slide 64

Slide 64 text

64 position velocity rotation armor shield

Slide 65

Slide 65 text

65 position velocity 14 used bytes / 32 total bytes = 44% efficiency (for movement system) rotation armor shield

Slide 66

Slide 66 text

66

Slide 67

Slide 67 text

67

Slide 68

Slide 68 text

Store components in contiguous arrays. 68

Slide 69

Slide 69 text

positions[47] = Position.new(400, 200) velocities[47] = Velocity.new(0, 0) shields[47] = Shield.new(cur: 100, max: 100, rate: 2) armors[47] = Armor.new(cur: 50, max: 50) 69

Slide 70

Slide 70 text

Smart use of the CPU cache can lead to a 50x speedup! 70 * Source: Game Programming Patterns by Robert Nystrom

Slide 71

Slide 71 text

Saving and loading becomes trivial! 71 WIN #3

Slide 72

Slide 72 text

# Save File.write('save.json', JSON.dump(@world)) # Load @world = JSON.parse(File.read('save.json')) 72

Slide 73

Slide 73 text

Level data becomes plaintext! 73 WIN #4

Slide 74

Slide 74 text

{ "position": [400, 200], "velocity": [0, 0], "shield": {"cur": 100, "max": 100, "rate": 2}, "armor": {"cur": 50, "max": 50} } 74

Slide 75

Slide 75 text

In-game editors become easy to write! 75 WIN #5

Slide 76

Slide 76 text

76

Slide 77

Slide 77 text

Entities group components. Components contain data. Systems update components. 77

Slide 78

Slide 78 text

DEMO 78

Slide 79

Slide 79 text

79 @ddfreyne denis@stoneship.org Denis Defreyne, expert game dev newbie.


Slide 80

Slide 80 text

80 This talk would not have been the same without some great assets that I could use, either for free or for a well-deserved donation. The fonts in this presentation are Clear Sans by Intel (01.org/clear-sans) and Ubuntu Mono by Canonical Ltd (font.ubuntu.com). Most of the sprites are by Kenney Vleugels (kenney.nl) and are part of the Kenney Donation Pack (kenney.itch.io/kenney-donation). The planet sprite in the 2D space shooter example is by Justin Nichol. Assets in the initial screenshot are by Sven Ahlgrimm.