Code example

The following code example might look very rudimentary, but it can easily be expanded to implement more complex use cases. In a way, it's a foundation that we will be able to build on:

  1. Let's start by implementing the core element of our pattern, the Grid:
using System;using System.Linq;using UnityEngine;public class Grid: MonoBehaviour{    private int m_SquareSize;    private int m_NumberOfSquares;    public Grid(int squareSize, int numberOfSquares)    {        // The size can represent anything (meters, km)        m_SquareSize = squareSize;        // Squares permits to subdivide the grid granulary        m_NumberOfSquares = numberOfSquares;    }    public void AddToRandomnPosition(IUnit unit)    {        unit.AddToGrid(UnityEngine.Random.Range(0, m_NumberOfSquares));    } public int ...

Get Hands-On Game Development Patterns with Unity 2019 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.