|
1 |
| -package kenny.jecs; |
| 1 | +package com.kenny.jecs; |
2 | 2 |
|
3 |
| -import static kenny.jecs.BaseJECS.Reflect.*; |
| 3 | +import static com.kenny.jecs.BaseJECS.Reflect.*; |
4 | 4 | import static java.lang.annotation.ElementType.*;
|
5 | 5 | import static java.lang.annotation.RetentionPolicy.*;
|
6 | 6 | import java.lang.annotation.Documented;
|
|
28 | 28 | import java.util.concurrent.ThreadLocalRandom;
|
29 | 29 | import java.util.Map.Entry;
|
30 | 30 |
|
31 |
| -import kenny.jecs.collection.ComponentSequence; |
32 |
| -import kenny.jecs.collection.ComponentSequenceImpl; |
33 |
| -import kenny.jecs.collection.Pair; |
34 |
| -import kenny.jecs.collection.RawPair; |
35 |
| -import kenny.jecs.collection.ReversedIterator; |
36 |
| -import kenny.jecs.collection.ReversedIteratorList; |
37 |
| -import kenny.jecs.funcs.Create; |
38 |
| -import kenny.jecs.funcs.ICreate; |
39 |
| -import kenny.jecs.funcs.Destroy; |
40 |
| -import kenny.jecs.funcs.IDestroy; |
41 |
| -import kenny.jecs.funcs.Each; |
42 |
| -import kenny.jecs.funcs.EachC; |
43 |
| -import kenny.jecs.funcs.IEachC; |
44 |
| -import kenny.jecs.funcs.EachE; |
45 |
| -import kenny.jecs.funcs.IEachE; |
46 |
| -import kenny.jecs.funcs.IEach; |
47 |
| -import kenny.jecs.funcs.Patch; |
48 |
| -import kenny.jecs.funcs.IPatch; |
49 |
| -import kenny.jecs.funcs.Sort; |
50 |
| -import kenny.jecs.funcs.SortC; |
51 |
| -import kenny.jecs.funcs.ISort; |
52 |
| -import kenny.jecs.funcs.ISortC; |
| 31 | +import com.kenny.jecs.collection.ComponentSequence; |
| 32 | +import com.kenny.jecs.collection.ComponentSequenceImpl; |
| 33 | +import com.kenny.jecs.collection.Pair; |
| 34 | +import com.kenny.jecs.collection.RawPair; |
| 35 | +import com.kenny.jecs.collection.ReversedIterator; |
| 36 | +import com.kenny.jecs.collection.ReversedIteratorList; |
| 37 | +import com.kenny.jecs.funcs.Create; |
| 38 | +import com.kenny.jecs.funcs.ICreate; |
| 39 | +import com.kenny.jecs.funcs.Destroy; |
| 40 | +import com.kenny.jecs.funcs.IDestroy; |
| 41 | +import com.kenny.jecs.funcs.Each; |
| 42 | +import com.kenny.jecs.funcs.EachC; |
| 43 | +import com.kenny.jecs.funcs.IEachC; |
| 44 | +import com.kenny.jecs.funcs.EachE; |
| 45 | +import com.kenny.jecs.funcs.IEachE; |
| 46 | +import com.kenny.jecs.funcs.IEach; |
| 47 | +import com.kenny.jecs.funcs.Patch; |
| 48 | +import com.kenny.jecs.funcs.IPatch; |
| 49 | +import com.kenny.jecs.funcs.Sort; |
| 50 | +import com.kenny.jecs.funcs.SortC; |
| 51 | +import com.kenny.jecs.funcs.ISort; |
| 52 | +import com.kenny.jecs.funcs.ISortC; |
53 | 53 |
|
54 | 54 | /**
|
55 | 55 | * <code>JECS</code> or <b>Java Entity-Component-System API</b> this is a small system that holds all entity identifiers in a single object
|
@@ -541,22 +541,41 @@ public <C> C get(EntityT entity, Class<C> componentT) {
|
541 | 541 | }
|
542 | 542 |
|
543 | 543 | /**
|
544 |
| - * See {@link #BaseJECS.get(Number, Class...)} |
| 544 | + * Pair components. See {@link #BaseJECS.getPair(Number, Class, Class)} |
545 | 545 | */
|
546 |
| - @SuppressWarnings("unchecked") |
547 | 546 | @JECSApi(since = "0.1.9")
|
548 |
| - public <C extends Component> C[] get(EntityT entity, Class<C>... componentTs) { |
549 |
| - return system.get(entity, componentTs); |
| 547 | + public <C extends Component, C1 extends Component> |
| 548 | + Pair<C, C1> getPair(EntityT entity, Class<C> a, Class<C1> b) { |
| 549 | + return system.getPair(entity, a, b); |
550 | 550 | }
|
551 | 551 |
|
552 | 552 | /**
|
553 |
| - * See {@link #BaseJECS.getPair(Number, Class, Class)} |
| 553 | + * Two components. See {@link #BaseJECS.getPair(Number, Class, Class)} |
554 | 554 | */
|
555 | 555 | @JECSApi(since = "0.1.9")
|
556 | 556 | public <C extends Component, C1 extends Component>
|
557 |
| - Pair<C, C1> getPair(EntityT entity, Class<C> a, Class<C1> b) { |
558 |
| - return system.getPair(entity, a, b); |
| 557 | + C[] get(EntityT entity, Class<C> a, Class<C1> b) { |
| 558 | + return system.get(entity, a, b); |
| 559 | + } |
| 560 | + |
| 561 | + /** |
| 562 | + * Three components. See {@link #BaseJECS.get(Number, Class...)} |
| 563 | + */ |
| 564 | + @JECSApi(since = "0.1.9") |
| 565 | + public <C extends Component, C1 extends Component, C2 extends Component> |
| 566 | + C[] get(EntityT entity, Class<C> a, Class<C1> b, Class<C2> c) { |
| 567 | + return system.get(entity, a, b ,c); |
| 568 | + } |
| 569 | + |
| 570 | + /** |
| 571 | + * Four components. See {@link #BaseJECS.get(Number, Class...)} |
| 572 | + */ |
| 573 | + @JECSApi(since = "0.1.9") |
| 574 | + public <C extends Component, C1 extends Component, C2 extends Component, C3 extends Component> |
| 575 | + C[] get(EntityT entity, Class<C> a, Class<C1> b, Class<C2> c, Class<C3> d) { |
| 576 | + return system.get(entity, a, b, c, d); |
559 | 577 | }
|
| 578 | + |
560 | 579 |
|
561 | 580 | @Override
|
562 | 581 | public Iterator<EntityT> iterator() {
|
@@ -618,6 +637,8 @@ public Iterator<EntityT> iterator() {
|
618 | 637 | View<EntityT> view = new View<>(this);
|
619 | 638 | /**Temparary component array stored by #push and released by #pop.*/
|
620 | 639 | Object[] arr = null;
|
| 640 | + /** Global pack index. */ |
| 641 | + int packIndex = 0; |
621 | 642 |
|
622 | 643 | /**Just null is already exist + im love C++.*/
|
623 | 644 | @JECSApi
|
@@ -1472,7 +1493,7 @@ else if(args[0] == NULL_ARGS)
|
1472 | 1493 | Constructor<?> ctor = componentT.getDeclaredConstructors()[0];
|
1473 | 1494 | ctor.setAccessible(true);
|
1474 | 1495 | if(isInnerClass && !isInnerStaticClass) {
|
1475 |
| - Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs); |
| 1496 | + Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs);; |
1476 | 1497 | return (C) ctor.newInstance(argss);
|
1477 | 1498 | } else
|
1478 | 1499 | return (C) ctor.newInstance((Object[])null);
|
@@ -1518,7 +1539,7 @@ private Object[] getInnerObjects(Object[] args, Class<?>[] finalCtorArgs, boolea
|
1518 | 1539 | argss[i] = args[i - 1];
|
1519 | 1540 | } else {
|
1520 | 1541 | argss = new Object[1];
|
1521 |
| - args[0] = inner; |
| 1542 | + argss[0] = inner; |
1522 | 1543 | }
|
1523 | 1544 | return argss;
|
1524 | 1545 | }
|
@@ -2946,6 +2967,33 @@ public <C extends Component> EntityT get(Class<C> component) {
|
2946 | 2967 |
|
2947 | 2968 | return null;
|
2948 | 2969 | }
|
| 2970 | + |
| 2971 | + /** |
| 2972 | + * Gets all entities that attached to this component. |
| 2973 | + * <p> |
| 2974 | + * If this method was called by constructor of some component on intitializaion or emplacing |
| 2975 | + * this also returns current emplaced entity or entity that will be emplaced, with this point of |
| 2976 | + * view is should be safe. |
| 2977 | + * |
| 2978 | + * @param <C> - component type. |
| 2979 | + * @param component - Class type of component. |
| 2980 | + */ |
| 2981 | + @JECSApi(since = "0.1.9") |
| 2982 | + public <C extends Component> List<EntityT> getAll(Class<C> component) { |
| 2983 | + var pairs = pool.get(component); |
| 2984 | + if(pairs == null) |
| 2985 | + return null; |
| 2986 | + |
| 2987 | + List<EntityT> ne = new ArrayList<EntityT>(); |
| 2988 | + for(var entity : entities) { |
| 2989 | + for(Pair<EntityT, Component> pair : pairs) { |
| 2990 | + if(pair.first == entity) |
| 2991 | + ne.add(entity); |
| 2992 | + } |
| 2993 | + } |
| 2994 | + |
| 2995 | + return ne; |
| 2996 | + } |
2949 | 2997 |
|
2950 | 2998 | /**
|
2951 | 2999 | * Return next avaliable component object of entity from index.
|
@@ -3106,22 +3154,21 @@ public <C extends Component> C getIfFamily(EntityT entity, Class<? extends Compo
|
3106 | 3154 | @SafeVarargs
|
3107 | 3155 | @JECSApi(since = "0.1.4")
|
3108 | 3156 | public final <C extends Component> void createPack(EntityT entity, Class<? extends Component>... componentTs) {
|
3109 |
| - for(int i = 0; i < packs.size(); i++) { |
3110 |
| - ComponentSequence<Component> sequence = packs.get(i); |
3111 |
| - if(sequence == null) |
3112 |
| - sequence = packs.put(i, new ComponentSequenceImpl<Component>()); |
3113 |
| - |
3114 |
| - if(sequence.isEmpty()) { |
3115 |
| - try { |
3116 |
| - Component[] component = get(entity, componentTs); |
3117 |
| - for(int c = 0; c < component.length; c++) |
3118 |
| - sequence.add(component[c]); |
3119 |
| - return; |
3120 |
| - } catch (JECSException e) { e.printStackTrace(); } |
3121 |
| - } |
3122 |
| - else |
3123 |
| - continue; |
| 3157 | + |
| 3158 | + ComponentSequence<Component> sequence = new ComponentSequenceImpl<Component>(); |
| 3159 | + |
| 3160 | + if(sequence.isEmpty()) { |
| 3161 | + try { |
| 3162 | + Component[] component = get(entity, componentTs); |
| 3163 | + for(int c = 0; c < component.length; c++) |
| 3164 | + sequence.add(component[c]); |
| 3165 | + packs.put(packIndex, sequence); |
| 3166 | + } catch (JECSException e) { e.printStackTrace(); } |
| 3167 | + } else { |
| 3168 | + createPack(entity, componentTs); |
3124 | 3169 | }
|
| 3170 | + |
| 3171 | + packIndex++; |
3125 | 3172 | }
|
3126 | 3173 |
|
3127 | 3174 | /**
|
|
0 commit comments