Created
May 2, 2017 11:18
-
-
Save copygirl/e7968106b8220e72b57f9cd4b45cfeec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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