-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.nim
38 lines (28 loc) · 943 Bytes
/
entity.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import tables, hashes
type
Entity* = ref object
x*, y*: int
id*: string
Velocity = ref object
x*, y*: int
Collider = ref object
halfW*, halfH*: int
var
entities*: seq[Entity]
velocityEntities*: seq[Entity]
colliderEntities*: seq[Entity]
velocities = initTable[Entity, Velocity]()
colliders = initTable[Entity, Collider]()
proc hash(entity: Entity): Hash =
result = cast[Hash](entity)
proc newEntity*(x, y: int, id: string): Entity =
result = Entity(x: x, y: y, id: id)
entities.add(result)
proc addVelocity*(entity: Entity) =
velocities[entity] = Velocity(x: 0, y: 0)
velocityEntities.add(entity)
proc addCollider*(entity: Entity, halfW, halfH: int) =
colliders[entity] = Collider(halfW: halfW, halfH: halfH)
colliderEntities.add(entity)
proc vel*(entity: Entity): Velocity {.inline.} = return velocities[entity]
proc col*(entity: Entity): Collider {.inline.} = return colliders[entity]