Slide 1

Slide 1 text

Developing Games Using Data not Trees Drew Petersen @KirbySaysHi

Slide 2

Slide 2 text

ONE MAN

Slide 3

Slide 3 text

VS HIS OWN MIND

Slide 4

Slide 4 text

24 HOURS

Slide 5

Slide 5 text

THIS

Slide 6

Slide 6 text

IS

Slide 7

Slide 7 text

A GAME JAM

Slide 8

Slide 8 text

There is just one problem…

Slide 9

Slide 9 text

ASTEROIDS:
 TOTALLY DIFFERENT THIS TIME

Slide 10

Slide 10 text

is not actually different.

Slide 11

Slide 11 text

Prepare for IMMEDIATE Internal Monologue

Slide 12

Slide 12 text

Oh no! It’s not different at all! But it uses typed arrays and verlet physics and all this stuff that I had a lot of fun making!

Slide 13

Slide 13 text

But the players don’t actually care about that… What am I going to do? There are only a few hours left before the deadline!

Slide 14

Slide 14 text

–Every programmer under a deadline, ever. “Maybe I can quickly change it.”

Slide 15

Slide 15 text

Maybe if the ship is huge?

Slide 16

Slide 16 text

Maybe if the asteroids are tiny?

Slide 17

Slide 17 text

–Me, to myself “Boring.”

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

What if…

Slide 20

Slide 20 text

you…

Slide 21

Slide 21 text

…control the asteroids?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

How will I have time?

Slide 26

Slide 26 text

Oh right. I modeled everything as data.

Slide 27

Slide 27 text

Wait, what does that mean?

Slide 28

Slide 28 text

Is that like a database?

Slide 29

Slide 29 text

Databases are fun!

Slide 30

Slide 30 text

SELECT * FROM boring LIMIT fun

Slide 31

Slide 31 text

Databases are not a game.

Slide 32

Slide 32 text

–Me in middle school “I heard you can capture a Databasei with a Master Ball”

Slide 33

Slide 33 text

How does one data?

Slide 34

Slide 34 text

Does data you?

Slide 35

Slide 35 text

Data Matthews Band really replicates.

Slide 36

Slide 36 text

Base into data without remorse

Slide 37

Slide 37 text

OH GAWD I’VE DEVOLVED TO MEME

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

But seriously, how?

Slide 42

Slide 42 text

Approach: Object Oriented

Slide 43

Slide 43 text

Objects • Ship • Asteroid • Bullet

Slide 44

Slide 44 text

├ WorldEntity ├ PhysicsEntity ├ CollidableEntity ├ RenderableEntity ├ AsteroidEntity ├ WorldEntity ├ PhysicsEntity ├ CollidableEntity ├ RenderableEntity ├ BulletEntity

Slide 45

Slide 45 text

├ WorldEntity ├ PhysicsEntity ├ CollidableEntity ├ RenderableEntity ├ KeyboardCtrlEntity ├ ShipEntity update() (WorldE) handleCollisions() (CE) draw() (RenderableE) handleInput() (KBCE) shoot()

Slide 46

Slide 46 text

What about controllable asteroids?

Slide 47

Slide 47 text

├ WorldEntity ├ PhysicsEntity ├ CollidableEntity ├ RenderableEntity ├ KeyboardCtrlEntity ├ ShipEntity update() (WorldE) handleCollisions() (CE) draw() (RenderableE) handleInput() (KBCE) shoot() AsteroidEntity

Slide 48

Slide 48 text

DISCLAIMER: THE REAL WORLD IS WORSE

Slide 49

Slide 49 text

ProCon: Professional Conventions for Professionals • Logic appears to be local • State is spread throughout the hierarchy • Hard to optimize (batch all physics, batch draw, etc)

Slide 50

Slide 50 text

Approach: Data Oriented

Slide 51

Slide 51 text

• Functions operate on data, not Objects. • There will always be more than one. • Don't inhibit optimization (batching).

Slide 52

Slide 52 text

DISCLAIMER: IT’S NOT PERFECT But Might Be Better.

Slide 53

Slide 53 text

Starts with a Pocket

Slide 54

Slide 54 text

var pkt = new Pocket() pkt.tick(16); // ms

Slide 55

Slide 55 text

We add Component Types to the Pocket

Slide 56

Slide 56 text

–Ben Newman, probably “Components like in ReactJS?”

Slide 57

Slide 57 text

NOPE

Slide 58

Slide 58 text

pkt.componentType('rotation', function(cmp, opts) { cmp.angle = opts.angle || 0; cmp.rate = opts.rate || 0; }) ! // shortcut / alias pkt.cmpType('rotation', function(cmp, opts) { cmp.angle = opts.angle || 0; cmp.rate = opts.rate || 0; })

Slide 59

Slide 59 text

pkt.cmpType('drag', function(cmp, opts) { cmp.percentage = opts.percentage || 0.99; })

Slide 60

Slide 60 text

We request a Key from the Pocket

Slide 61

Slide 61 text

var key = pkt.key({ 'rotation': { rate: 0.9 }, 'drag': null }) ! console.log(key); // 1 console.log(typeof key); // number

Slide 62

Slide 62 text

pkt.key({ 'ship': null, 'human-controlled-01': null, 'verlet-position': { x: pkt.firstData('ctx-2d').center.x, y: pkt.firstData('ctx-2d').center.y }, 'rotation': { rate: 0.1 }, 'thrust': null, 'drag': null, 'projectile-launcher': { launchForce: 10 }, 'point-shape': { points: [ { x: size, y: 0 }, { x: -size, y: -size / 2 }, { x: -size, y: size / 2 } ]}, 'bbox': null })

Slide 63

Slide 63 text

We write Systems to modify Component Data

Slide 64

Slide 64 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } })

Slide 65

Slide 65 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } }) Name

Slide 66

Slide 66 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } }) Name Component Type Requirements

Slide 67

Slide 67 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } }) Name Component Type Requirements Action

Slide 68

Slide 68 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } }) Name Component Type Requirements Action Array of Keys that each have the required Component Datas

Slide 69

Slide 69 text

pkt.system('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, keys, positions, rotations, thrusts) { for (var i = 0; i < keys.length; i++) { // Do something } }) Name Component Type Requirements Action Global Collection of Component Data by Key Array of Keys that each have the required Component Datas

Slide 70

Slide 70 text

pkt.system( 'render-point-shape', ['verlet-position', 'point-shape', 'rotation'], function(pkt, keys, positions, shapes, rotations) { var ctx2d = pkt.firstData('ctx-2d') ! for (var i = 0; i < keys.length; i++) { var k = keys[i]; var position = positions[k]; var shape = shapes[k]; var rotation = rotations[k]; } } )

Slide 71

Slide 71 text

pkt.system( 'render-point-shape', ['verlet-position', 'point-shape', 'rotation'], function(pkt, keys, positions, shapes, rotations) { var ctx2d = pkt.firstData('ctx-2d') ! for (var i = 0; i < keys.length; i++) { var k = keys[i]; var position = positions[k]; var shape = shapes[k]; var rotation = rotations[k]; } } ) Manually grab each component data from the global collections.

Slide 72

Slide 72 text

pkt.systemForEach('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, key, position, rotation, thrust) { // Do Something })

Slide 73

Slide 73 text

pkt.systemForEach('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, key, position, rotation, thrust) { // Do Something }) Individual Key

Slide 74

Slide 74 text

pkt.systemForEach('input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, key, position, rotation, thrust) { // Do Something }) Component Data Matching the Key Individual Key

Slide 75

Slide 75 text

–Maybe one person in the audience? “But what about player controlled asteroids?”

Slide 76

Slide 76 text

Oh right.

Slide 77

Slide 77 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 78

Slide 78 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 79

Slide 79 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } ) ?

Slide 80

Slide 80 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } ) ? Why no human-controlled-01 component data?

Slide 81

Slide 81 text

THERE IS NO human-controlled-01 COMPONENT

Slide 82

Slide 82 text

We use Labels as anonymous components

Slide 83

Slide 83 text

pkt.key({ 'asteroid': null, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': null }) There is no 'asteroid' component

Slide 84

Slide 84 text

pkt.system( 'asteroid-ship-collider', ['point-shape', 'verlet-position', 'rotation', 'asteroid'], function(pkt, keys, shapes, positions, rotations) { })

Slide 85

Slide 85 text

pkt.system( 'asteroid-ship-collider', ['point-shape', 'verlet-position', 'rotation', 'asteroid'], function(pkt, keys, shapes, positions, rotations) { }) We only want asteroids in here. Ship also has point-shape, verlet-position, and rotation

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 88

Slide 88 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 89

Slide 89 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } ) ?

Slide 90

Slide 90 text

What happens if we add that label to the asteroids?

Slide 91

Slide 91 text

pkt.key({ 'asteroid': null, 'human-controlled-01': null, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': null })

Slide 92

Slide 92 text

Nothing?

Slide 93

Slide 93 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 94

Slide 94 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 95

Slide 95 text

pkt.systemForEach( 'input-rotation', ['rotation', 'human-controlled-01'], function(pkt, key, rotation) { var input = pkt.firstData('keyboard-state'); ! if (input.down.RIGHT) { rotation.angle += rotation.rate; } else if (input.down.LEFT) { rotation.angle -= rotation.rate; } } )

Slide 96

Slide 96 text

pkt.cmpType('rotation', function(cmp, opts) { cmp.angle = opts.angle || 0; cmp.rate = opts.rate || 0; })

Slide 97

Slide 97 text

pkt.cmpType('rotation', function(cmp, opts) { cmp.angle = opts.angle || 0; cmp.rate = opts.rate || 0; })

Slide 98

Slide 98 text

pkt.key({ 'asteroid': null, 'human-controlled-01': null, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': null })

Slide 99

Slide 99 text

pkt.key({ 'asteroid': null, 'human-controlled-01': null, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': { rate: 0.1 } })

Slide 100

Slide 100 text

They rotate!

Slide 101

Slide 101 text

Let's make them thrust.

Slide 102

Slide 102 text

pkt.systemForEach( 'input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, key, position, rotation, thrust) { var input = pkt.firstData('keyboard-state'); ! if (input.down.UP) { var x = Math.cos(rotation.angle) * thrust.force; var y = Math.sin(rotation.angle) * thrust.force; position.acel.x += x; position.acel.y += y; } } )

Slide 103

Slide 103 text

pkt.systemForEach( 'input-thrust', ['verlet-position', 'rotation', 'thrust', 'human-controlled-01'], function(pkt, key, position, rotation, thrust) { var input = pkt.firstData('keyboard-state'); ! if (input.down.UP) { var x = Math.cos(rotation.angle) * thrust.force; var y = Math.sin(rotation.angle) * thrust.force; position.acel.x += x; position.acel.y += y; } } )

Slide 104

Slide 104 text

'human-controlled-01': null, 'verlet-position': { x: pkt.firstData('ctx-2d').center.x, y: pkt.firstData('ctx-2d').center.y }, 'rotation': { rate: 0.1 }, 'thrust': null, 'drag': null, 'projectile-launcher': { launchForce: 10 }, 'point-shape': { points: [ { x: size, y: 0 }, { x: -size, y: -size / 2 }, { x: -size, y: size / 2 } ]},

Slide 105

Slide 105 text

pkt.key({ 'asteroid': null, 'human-controlled-01': null, 'thrust': null, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': { rate: 0.1 }, })

Slide 106

Slide 106 text

Let's make them shoot!

Slide 107

Slide 107 text

pkt.key({ 'asteroid': null, 'human-controlled-01': null, 'thrust': null, 'projectile-launcher': { launchForce: 10 }, 'verlet-position': { x: x, y: y, acel: v2(acelX, acelY) }, 'point-shape': { points: points }, 'bbox': null, 'rotation': { rate: 0.1 }, })

Slide 108

Slide 108 text

Let's make the ship get hit!

Slide 109

Slide 109 text

pkt.system( 'asteroid-projectile-collider', ['point-shape', 'verlet-position', 'rotation', 'asteroid'], function(pkt, keys, shapes, positions, rotations) { var projectiles = pkt.keysMatching( 'point-shape', 'verlet-position', 'rotation', 'projectile'); // Actual collision code } ) This label is the only "asteroidish" thing about this system

Slide 110

Slide 110 text

pkt.system( 'asteroid-projectile-collider', ['point-shape', 'verlet-position', 'rotation', 'ship'], function(pkt, keys, shapes, positions, rotations) { var projectiles = pkt.keysMatching( 'point-shape', 'verlet-position', 'rotation', 'projectile'); // Actual collision code } )

Slide 111

Slide 111 text

THE HUNTER HAS BECOME THE HUNTED

Slide 112

Slide 112 text

There is still more work to do!

Slide 113

Slide 113 text

But not today.

Slide 114

Slide 114 text

ProCon: Professional Conferences for Professionals • Logic is spread out • State is concentrated in components • Easier to optimize for batch drawing, batch collisions, etc. • Extremely modular

Slide 115

Slide 115 text

I mentioned databases a long time ago.

Slide 116

Slide 116 text

• ComponentTypes — Tables (properties are columns) • Components / Component Data — Rows • Keys — Primary Keys • Systems — Stored Procedures? (not really)

Slide 117

Slide 117 text

• ComponentTypes — Tables (properties are columns) • Components / Component Data — Rows • Keys — Primary Keys • Systems — Stored Procedures? (not really)

Slide 118

Slide 118 text

• ComponentTypes — Tables (properties are columns) • Components / Component Data — Rows • Keys — Primary Keys • Systems — Queries + Business Logic

Slide 119

Slide 119 text

console.log(JSON.stringify(pkt.components))

Slide 120

Slide 120 text

{ "ctx-2d": { "1": { "cvs": {}, "ctx": {}, "center": { "x": 719.5, "y": 200.5 }, "width": 1439, "height": 401 } }, "game-config": {

Slide 121

Slide 121 text

}, "verlet-position": { "4": { "cpos": { "x": 719.5, "y": 397.5 }, "ppos": { "x": 719.5, "y": 397.5 }, "acel": { "x": 0, "y": 0 }

Slide 122

Slide 122 text

} } }, "rotation": { "4": { "angle": 0, "rate": 0.1 }, "5": { "angle": 0, "rate": 0.1 }, "6": { "angle": 0, "rate": 0.1

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

–Everyone? “But did you finish the game jam?”

Slide 125

Slide 125 text

Prepare for IMMEDIATE Truth

Slide 126

Slide 126 text

Sorry.

Slide 127

Slide 127 text

There was no jam.

Slide 128

Slide 128 text

It was a conceit of the talk.

Slide 129

Slide 129 text

I hope that's alright.

Slide 130

Slide 130 text

Thank You! ! @KirbySaysHi http://github.com/ kirbysayshi/pocket-ces

Slide 131

Slide 131 text

Thank You! ! @KirbySaysHi http://github.com/ kirbysayshi/pocket-ces