Strategy Injection
Without too much “game” to the Game of Life and with the simulation reasonably fast, I turned what was left of interest: runtime adjusting the rules of the game.
I started with some of the specific choices I’d pondering while making it: should the player draw cells with mouse clicks (more precise) or mouse movement (much faster)? Should the edge of the grid block cells from progression or should they wrap around?
The answer was now: both!
From there I quickly moved on to tweaking the rules of simulation itself: at what neighbor-counts do living cells die and dead cells live?
In both cases, with 71*71 cells, something like call_group("cells").adjust_min_life
seemed silly. Enter what’s essentially a flyweight strategy. On instantiation of each cell, I continue to provide the x/y coords, but also pass a Strategy: var c = cell.instantiate().new(i, j, Strategy)
.
What is Strategy
? It’s basically a bundle of functions which can be hot-swapped around, with all cell
s completely clueless about this tinkering. (If this was javascript, it would literally be a function that calls other functions, or a dictionary of functions. In GDscript, I’ll have to settle for a class with all static
members.)
class Strategy:
static var funcs = {}
static var min_live = 3
static var max_live = 3
static var min_die = 2
static var max_die = 3
static func death_to_life(count):
return count >= min_live and count <= max_live
static func life_to_death(count):
return count < min_die or count > max_die
static func toggle(e):
return funcs["toggle"].call(e)
static func edge(p, d):
return funcs["edge"].call(p, d)
Rule changes are now as simple as Strategy.funcs["edge"] = ...
. All the cells refer to the same static object, so they all adjust immediately:
if strategy.toggle(event):
if state == 0:
_update_neighbors(1, pos, true)
live()
Likewise for neighbor count evaluations:
if strategy.death_to_life(now_count):
next = 1
Files
Game of Life
Status | In development |
Author | frenata |
Genre | Simulation |
Tags | 20-game-challenge |
More posts
- Mature OptimizationNov 25, 2023
Leave a comment
Log in with itch.io to leave a comment.