-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.odin
147 lines (122 loc) · 3.39 KB
/
entity.odin
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package ecs
import "base:intrinsics"
// Entity represents the Entity :) It holds it's ID and flags of
// components that it has
Entity :: struct {
id: int,
components: ComponentSet,
}
// ComponentSet is a set of flags of component types that entity owns
ComponentSet :: map[typeid]struct {}
has_components :: proc(e: Entity, types: ..typeid) -> bool {
for t in types {
if t not_in e.components {
return false
}
}
return true
}
// create_entity creates new entity with specified components, adds it to world and returns it
create_entity :: proc {
create_entity_slice,
create_entity_0,
create_entity_1,
create_entity_2,
create_entity_3,
create_entity_4,
create_entity_5,
create_entity_6,
create_entity_7,
create_entity_8,
create_entity_9,
create_entity_10,
}
@(private)
create_entity_slice :: proc(
world: ^World($T),
components: []T,
) -> Entity where intrinsics.type_is_union(T) {
e := new_entity()
for comp in components {
set_component(world, &e, comp)
}
append(&world.entities, e)
return e
}
@(private)
create_entity_0 :: proc(world: ^World($T)) -> Entity {
return create_entity_slice(world, []T{})
}
@(private)
create_entity_1 :: proc(world: ^World($T), cmp1: T) -> Entity {
return create_entity_slice(world, []T{cmp1})
}
@(private)
create_entity_2 :: proc(world: ^World($T), cmp1, cmp2: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2})
}
@(private)
create_entity_3 :: proc(world: ^World($T), cmp1, cmp2, cmp3: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3})
}
@(private)
create_entity_4 :: proc(world: ^World($T), cmp1, cmp2, cmp3, cmp4: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4})
}
@(private)
create_entity_5 :: proc(world: ^World($T), cmp1, cmp2, cmp3, cmp4, cmp5: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4, cmp5})
}
@(private)
create_entity_6 :: proc(world: ^World($T), cmp1, cmp2, cmp3, cmp4, cmp5, cmp6: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4, cmp5, cmp6})
}
@(private)
create_entity_7 :: proc(world: ^World($T), cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7: T) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7})
}
@(private)
create_entity_8 :: proc(
world: ^World($T),
cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8: T,
) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8})
}
@(private)
create_entity_9 :: proc(
world: ^World($T),
cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8, cmp9: T,
) -> Entity {
return create_entity_slice(world, []T{cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8, cmp9})
}
@(private)
create_entity_10 :: proc(
world: ^World($T),
cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8, cmp9, cmp10: T,
) -> Entity {
return create_entity_slice(
world,
[]T{cmp1, cmp2, cmp3, cmp4, cmp5, cmp6, cmp7, cmp8, cmp9, cmp10},
)
}
@(private)
new_entity :: proc() -> Entity {
@(static)id := 0
id += 1
e := Entity {
id = id,
components = make(ComponentSet),
}
return e
}
// `remove_entity` removes the entity from the world and returns true if succeded.
// If that id doesn't exists returns false.
remove_entity :: proc(world: ^World($T), id: int) -> bool {
for e, i in world.entities {
if e.id == id {
unordered_remove(&world.entities, i)
return true
}
}
return false
}