Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created May 2, 2017 11:18
Show Gist options
  • Select an option

  • Save copygirl/e7968106b8220e72b57f9cd4b45cfeec to your computer and use it in GitHub Desktop.

Select an option

Save copygirl/e7968106b8220e72b57f9cd4b45cfeec to your computer and use it in GitHub Desktop.
import options, tables
import ../entity
template simpleTableComponent*(Type, name) =
let table = newTable[Entity, Type]()
proc name*(entity: Entity): Option[Type] =
try: table[entity].some
except KeyError: Type.none
proc `name=`*(entity: Entity, value: Type) =
table[entity] = value
proc `name=`*(entity: Entity, value: Option[Type]) =
if value.isSome: table[entity] = value.get
else: table.del(entity)
import options, strutils, tables
import component, ../entity
type
Location* = object
x*: int
y*: int
simpleTableComponent(Location, location)
converter toLocation*(value: tuple[x: int, y: int]): Location =
Location(x: value.x, y: value.y)
proc `$`*(loc: Location): string =
"($1, $2)" % [$loc.x, $loc.y]
import options
import ./entity
import ./components/location
let me = newEntity()
echo $me.location
me.location = (1, 1)
echo $me.location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment