Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2023
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. Entity :: struct{
  2. version : u32;
  3. index: u32;
  4. }
  5.  
  6. ENTITY_MAX :: 4294967295;
  7.  
  8. SparseSet:: struct($T: Type){
  9.  
  10. reverse: [..]Entity;
  11. sparse: [..]u32; //todo change into a proper map
  12. dense: [..]T;
  13. }
  14.  
  15. get_component :: (using self: *$SparseSet, et:Entity) -> *self.T{
  16. assert(has_component(self, et));
  17.  
  18. return *dense[sparse[et.index]];
  19. }
  20.  
  21. set_component :: (using self: *$SparseSet,val : $T ,et:Entity){
  22. dense[sparse[et.index]] = val;
  23. }
  24.  
  25. has_component :: (using self: *$SparseSet, et:Entity) -> bool{
  26.  
  27. return sparse.count > et.index && sparse[et.index] != ENTITY_MAX;
  28. }
  29.  
  30. add_component :: (using self: *$SparseSet,comp: $T,et: Entity){
  31. assert(!has_component(self, et));
  32.  
  33. array_add(*dense,comp);
  34. array_add(*reverse,et);
  35.  
  36.  
  37. while et.index >= sparse.count {
  38. array_add(*sparse,ENTITY_MAX);
  39. }
  40.  
  41. print("added component at index % cmp %, dense is %", et.index, comp, dense.count);
  42.  
  43. sparse[et.index] = cast(u32)dense.count-1;
  44. }
  45.  
  46. Base_Registry:: struct($types: []Type){
  47.  
  48. versions: [..]u32;
  49.  
  50. //insert the sparse sets for each type at compile time
  51. #insert -> string {
  52. builder: String_Builder;
  53.  
  54. for types {
  55. //print("set_%: SparseSet(%);\n", it, it);
  56. print_to_builder(*builder, "set_%: SparseSet(%);\n", it, it);
  57. }
  58.  
  59. return builder_to_string(*builder);
  60. }
  61.  
  62. find_pool :: ($t: Type,reg : *$Base_Registry) -> *SparseSet(t) {
  63. set := #insert -> string {
  64. builder: String_Builder;
  65.  
  66. print( "reg.set_%;\n",t);
  67. print_to_builder(*builder, "*reg.set_%;\n",t);
  68.  
  69. return builder_to_string(*builder);
  70. };
  71. return set;
  72. }
  73.  
  74. get :: ($t: Type,reg : *$Base_Registry, et : Entity) -> t{
  75.  
  76. return get_component(find_pool(t,reg),et);
  77. }
  78.  
  79. set :: (cmp: $T,reg : *$Base_Registry, et : Entity) {
  80. set := find_pool(T,reg);
  81.  
  82. if has_component(set,et) {
  83. set_component(set,cmp,et);
  84. }
  85. else {
  86. add_component(set,cmp,et);
  87. }
  88. }
  89. }
  90.  
  91. tail :: (types: []Type) -> []Type{
  92. filter := types;
  93. filter.data += 1;
  94. filter.count -= 1;
  95.  
  96. return filter;
  97. }
  98.  
  99. View :: struct ($types: []Type){
  100.  
  101. #assert(types.count > 0);
  102. #if(types.count == 1){
  103. mypool : *SparseSet(types[0]);
  104. }
  105. else{
  106. mypool : *SparseSet(types[0]);
  107.  
  108. recursion : View(#run tail(types));
  109. }
  110. }
  111.  
  112. init_view :: (v : *$V/View, reg : *$Base_Registry){
  113.  
  114. v.mypool = reg.find_pool(V.types[0],reg);
  115. #if(v.types.count> 1){
  116. v.recursion = newv.recursion.new(reg);S
  117. }
  118. }
  119.  
  120. has :: (v : $V/View,eid: Entity) -> bool {
  121. hasSelf := has_component(v.mypool,entity);
  122.  
  123. #if v.types.count <= 1{
  124. return hasSelf;
  125. }
  126. else {
  127. return hasSelf && has(v.recursion);
  128. }
  129. }
  130. each :: (v : $V/View, fn : (eid: Entity)) {
  131. for v.mypool.reverse {
  132. #if v.types.count <= 1{
  133. fn(it);
  134. }
  135. else {
  136. if has(v.recursion, it_value) {
  137. fn(it);
  138. }
  139. }
  140. }
  141. }
  142.  
  143. for_expansion :: (v : $V/View, body: Code, flags: For_Flags) #expand {
  144. for eid : v.mypool.reverse {
  145. #if v.types.count > 1{
  146. if !has(v.recursion, it_value) continue;
  147. }
  148.  
  149. `it := eid;
  150. `it_index := 0;
  151.  
  152. #insert body;
  153. }
  154. }
  155.  
  156.  
  157. Registry :: struct {
  158. #as using base: Base_Registry(.[C1,C2,C3,C4] );
  159.  
  160. new :: (reg : *Registry) -> Entity {
  161. array_add(*reg.versions, 1);
  162.  
  163. return Entity.{version = 1, index = cast(u32)reg.versions.count-1};
  164. }
  165.  
  166. get :: ($t: Type,reg : *Registry, et : Entity) -> t{
  167. return reg.base.get(t,*reg,et);
  168. }
  169.  
  170. set :: (cmp: $T,reg : *Registry, et : Entity) -> T{
  171. return reg.base.set(cmp,*reg,et);
  172. }
  173. }
  174.  
  175. C1 :: struct {
  176. var : float64;
  177. }
  178.  
  179. C2 :: struct {
  180. stuff : float64;
  181. stuffother : s32;
  182. }
  183.  
  184. C3 :: struct {
  185. vars : [3]float64;
  186. }
  187.  
  188. C4 :: struct {
  189. var : bool;
  190. }
  191.  
  192. main :: () {
  193. reg : Registry;
  194. et : Entity = Registry.new(*reg);
  195.  
  196. test := C2.{stuff = 10, stuffother = 5};
  197.  
  198. Registry.base.set(test,*reg,et);
  199. Registry.base.set(C1.{7.0},*reg,et);
  200.  
  201. var := Registry.base.get(C2,*reg,et);
  202.  
  203. assert(var.stuff == test.stuff && var.stuffother == test.stuffother);
  204.  
  205. printe :: ( eid: Entity ){
  206. print("\n entity % \n", eid);
  207. }
  208.  
  209. testv : View(.[C1,C2]) ;
  210. init_view(*testv,*reg);
  211.  
  212. for testv {
  213. print("\n entity % \n", it);
  214. }
  215. }
  216.  
  217.  
  218. #import "Basic";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement