These slides are depricated. Instead go to this github repository

Game Programming Patterns in JS

part of 3d tech for the web course

Aims and Objectives

Recognise Patterns

Code Them Up

Understand Their Motivations

Audience

Novice JS programmers who want to make 3d web games or similar systems

What is a Pattern?

Architectural Patterns

Software Design Pattern?

What is a Game Programming Pattern?

Some Patterns

  • Game Loop
  • Update Method
  • Entity
  • Singleton
  • Object Pool
  • Prototype
  • Component
  • Service

Types of Patterns

StructuralArrangement or structure of the code, or the software objects
SequenceControlling the order of execution
DecouplingSeparation of software objects and types so they can be manipulated independantly
OptimisationReducing computation, speeding up execution, reducing memory use.

Game Loop

WhatRuns continuously, processes user input without blocking, updates game state, and renders the game. Tracks the passage of time to control the rate of gameplay
WhyDecouple game time from user input and processor speed. Real Time systems run continuously and very quickly to create an illusion of continuous reality
var animate = function () {
    requestAnimationFrame( animate );

    processInput();
    updateEntities();
    renderScene(scene, camera);
};

Game Object

WhatMany (most) of the objects in your system will have something in common which is necessary for them to participate in the simulation.
WhyBecause they all are part of the same simulation you dont want to re-write the code each time.
class GameObject {
    constructor(x, y, z){
        this.x = x;
        this.y = y;
        this.z = z;
    };
    //...
}

class Player extends GameObject { constructor(){}; }
class Enemy extends GameObject { constructor(){}; }
class Nazi extends Enemy { constructor(){}; }
        

Update Method

WhatThe game world maintains a collection of objects. Each object implements an update method that simulates one frame of the object’s behavior. Each frame, the game updates every object in the collection.
WhyYour game has a number of objects or systems that need to run simultaneously. Each object’s behavior is mostly independent of the others. The objects need to be simulated over time.
class GameObject {
    constructor(x, y) {this.x = x; this.y=y}
    Update() {
        //...
    }
}

while (gameOn)
{
    handleInput();
    for (int i = 0; i < numEntities; i++)
    {
      entities[i]->update();
    }
    renderScene(camera, scene);
}
        

(pseudocode)

Object Pool

WhatEfficiently use memory for a large set of very similar objects likely with a short lifetime.
WhyReducing memory fragmentation through reusing the same object again and again.
class Footprint {
    constructor() {this.initialise(0, 0, 0);}
    initialise(x, z, colour) {this.x = x; this.z=z; this.colour = colour;}
}

class FootprintPool {
    constructor(size) {
        this.size = size; this.new = 0;
        for (var n=0; n < this.size; n++) {
            this.poolArray.push(new Footprint(0,0,0));
        }
    };

    NewFootprint(x, y, z, c) {
        this.poolArray[new].initialise(x, z, c);
        this.new = this.new + 1;
        if (this.new>=this.size) {this.new = 0;}
    }
}
        

Singeton

WhatEnsure a class has only one instance, and provide a global point of access to it
WhyUseful when exactly one object is needed to coordinate actions across the entire system
var Singleton = (function () {
    var instance;

    function createInstance() {
        var object = new Object("I am the instance");
        return object;
    }

    return {
        getInstance: function () {
            if (!instance) {
                instance = createInstance();
            }
            return instance;
        }
    };
})();

Prototype Spawner

class Spawner
{
    constructor(){
        this.proto = prototype;
    }

    SpawnMonster() {
        return proto.clone();
    }
    Monster proto;
};

More Patterns

ComponentSeparate generic functions into sub-objects and composite together
ServiceLike a Game Loop, but instead of handling Entities, handles specific behaviours within components
FlyweightMove all the common logic and shared data of a class into a single larger object. Maintain separate simpler objects only for the unique features.
Spatial PartitionCut the space of the world into smaller parts and deal with them only when necessary
Scene GraphLinked Structure of every Entity/Object in your world

Further Reading

http://gameprogrammingpatterns.com/

https://en.wikipedia.org/wiki/Software_design_pattern

https://en.wikipedia.org/wiki/A_Pattern_Language