Skip to content

Commit cc748b2

Browse files
Add examples, new getAll method and fix pack storing.
1 parent 961977b commit cc748b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+482
-1047
lines changed

src/kenny/jecs/BaseJECS.java renamed to src/com/kenny/jecs/BaseJECS.java

Lines changed: 95 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package kenny.jecs;
1+
package com.kenny.jecs;
22

3-
import static kenny.jecs.BaseJECS.Reflect.*;
3+
import static com.kenny.jecs.BaseJECS.Reflect.*;
44
import static java.lang.annotation.ElementType.*;
55
import static java.lang.annotation.RetentionPolicy.*;
66
import java.lang.annotation.Documented;
@@ -28,28 +28,28 @@
2828
import java.util.concurrent.ThreadLocalRandom;
2929
import java.util.Map.Entry;
3030

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;
5353

5454
/**
5555
* <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) {
541541
}
542542

543543
/**
544-
* See {@link #BaseJECS.get(Number, Class...)}
544+
* Pair components. See {@link #BaseJECS.getPair(Number, Class, Class)}
545545
*/
546-
@SuppressWarnings("unchecked")
547546
@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);
550550
}
551551

552552
/**
553-
* See {@link #BaseJECS.getPair(Number, Class, Class)}
553+
* Two components. See {@link #BaseJECS.getPair(Number, Class, Class)}
554554
*/
555555
@JECSApi(since = "0.1.9")
556556
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);
559577
}
578+
560579

561580
@Override
562581
public Iterator<EntityT> iterator() {
@@ -618,6 +637,8 @@ public Iterator<EntityT> iterator() {
618637
View<EntityT> view = new View<>(this);
619638
/**Temparary component array stored by #push and released by #pop.*/
620639
Object[] arr = null;
640+
/** Global pack index. */
641+
int packIndex = 0;
621642

622643
/**Just null is already exist + im love C++.*/
623644
@JECSApi
@@ -1472,7 +1493,7 @@ else if(args[0] == NULL_ARGS)
14721493
Constructor<?> ctor = componentT.getDeclaredConstructors()[0];
14731494
ctor.setAccessible(true);
14741495
if(isInnerClass && !isInnerStaticClass) {
1475-
Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs);
1496+
Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs);;
14761497
return (C) ctor.newInstance(argss);
14771498
} else
14781499
return (C) ctor.newInstance((Object[])null);
@@ -1518,7 +1539,7 @@ private Object[] getInnerObjects(Object[] args, Class<?>[] finalCtorArgs, boolea
15181539
argss[i] = args[i - 1];
15191540
} else {
15201541
argss = new Object[1];
1521-
args[0] = inner;
1542+
argss[0] = inner;
15221543
}
15231544
return argss;
15241545
}
@@ -2946,6 +2967,33 @@ public <C extends Component> EntityT get(Class<C> component) {
29462967

29472968
return null;
29482969
}
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+
}
29492997

29502998
/**
29512999
* Return next avaliable component object of entity from index.
@@ -3106,22 +3154,21 @@ public <C extends Component> C getIfFamily(EntityT entity, Class<? extends Compo
31063154
@SafeVarargs
31073155
@JECSApi(since = "0.1.4")
31083156
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);
31243169
}
3170+
3171+
packIndex++;
31253172
}
31263173

31273174
/**

src/kenny/jecs/JECSException.java renamed to src/com/kenny/jecs/JECSException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs;
1+
package com.kenny.jecs;
22

33
public class JECSException extends RuntimeException
44
{

src/kenny/jecs/JECSUndifiendBehaviourError.java renamed to src/com/kenny/jecs/JECSUndifiendBehaviourError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs;
1+
package com.kenny.jecs;
22

33
public class JECSUndifiendBehaviourError extends RuntimeException
44
{

src/kenny/jecs/collection/ComponentSequence.java renamed to src/com/kenny/jecs/collection/ComponentSequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
import java.util.List;
44

src/kenny/jecs/collection/ComponentSequenceImpl.java renamed to src/com/kenny/jecs/collection/ComponentSequenceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
import java.util.ArrayList;
44
import java.util.Collection;

src/kenny/jecs/collection/EntityContainer.java renamed to src/com/kenny/jecs/collection/EntityContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
import java.util.Map;
44

src/kenny/jecs/collection/EntityContainerImpl.java renamed to src/com/kenny/jecs/collection/EntityContainerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
import java.io.Serializable;
44
import java.util.Iterator;

src/kenny/jecs/collection/Pair.java renamed to src/com/kenny/jecs/collection/Pair.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package kenny.jecs.collection;
2-
1+
package com.kenny.jecs.collection;
32

3+
/**
4+
* Pair is container/collection type that can hold two objects with different types.
5+
*/
46
public class Pair<A, B> implements Comparable<A>
57
{
68
public A first;
@@ -31,23 +33,21 @@ public void setSecond(B second) {
3133
this.second = second;
3234
}
3335

34-
//@Override
35-
//public Iterator<A> iterator() {
36-
// return null;
37-
//}
38-
//
39-
//public Iterator<B> iteratorB() {
40-
// return null;
41-
//}////
42-
4336
@Override
4437
public String toString() {
4538
return "[" + first + ", " + second + "]";
4639
}
4740

4841
@Override
4942
public int compareTo(A o) {
50-
// TODO Auto-generated method stub
43+
if(o == this || o.getClass() == o.getClass())
44+
return 1;
45+
46+
@SuppressWarnings("unchecked")
47+
B b = (B) new Object();
48+
if(b.getClass() == o.getClass() || b.getClass() == this.getClass())
49+
return 1;
50+
5151
return 0;
5252
}
5353

src/kenny/jecs/collection/RawPair.java renamed to src/com/kenny/jecs/collection/RawPair.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
/**
44
* Non generic version of {@link Pair}.

src/kenny/jecs/collection/ReversedIterator.java renamed to src/com/kenny/jecs/collection/ReversedIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kenny.jecs.collection;
1+
package com.kenny.jecs.collection;
22

33
import java.util.Iterator;
44
import java.util.List;

0 commit comments

Comments
 (0)