Skip to content

Commit

Permalink
Add examples, new getAll method and fix pack storing.
Browse files Browse the repository at this point in the history
  • Loading branch information
KennyProgrammer committed Mar 6, 2022
1 parent 961977b commit cc748b2
Show file tree
Hide file tree
Showing 60 changed files with 482 additions and 1,047 deletions.
143 changes: 95 additions & 48 deletions src/kenny/jecs/BaseJECS.java → src/com/kenny/jecs/BaseJECS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kenny.jecs;
package com.kenny.jecs;

import static kenny.jecs.BaseJECS.Reflect.*;
import static com.kenny.jecs.BaseJECS.Reflect.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Documented;
Expand Down Expand Up @@ -28,28 +28,28 @@
import java.util.concurrent.ThreadLocalRandom;
import java.util.Map.Entry;

import kenny.jecs.collection.ComponentSequence;
import kenny.jecs.collection.ComponentSequenceImpl;
import kenny.jecs.collection.Pair;
import kenny.jecs.collection.RawPair;
import kenny.jecs.collection.ReversedIterator;
import kenny.jecs.collection.ReversedIteratorList;
import kenny.jecs.funcs.Create;
import kenny.jecs.funcs.ICreate;
import kenny.jecs.funcs.Destroy;
import kenny.jecs.funcs.IDestroy;
import kenny.jecs.funcs.Each;
import kenny.jecs.funcs.EachC;
import kenny.jecs.funcs.IEachC;
import kenny.jecs.funcs.EachE;
import kenny.jecs.funcs.IEachE;
import kenny.jecs.funcs.IEach;
import kenny.jecs.funcs.Patch;
import kenny.jecs.funcs.IPatch;
import kenny.jecs.funcs.Sort;
import kenny.jecs.funcs.SortC;
import kenny.jecs.funcs.ISort;
import kenny.jecs.funcs.ISortC;
import com.kenny.jecs.collection.ComponentSequence;
import com.kenny.jecs.collection.ComponentSequenceImpl;
import com.kenny.jecs.collection.Pair;
import com.kenny.jecs.collection.RawPair;
import com.kenny.jecs.collection.ReversedIterator;
import com.kenny.jecs.collection.ReversedIteratorList;
import com.kenny.jecs.funcs.Create;
import com.kenny.jecs.funcs.ICreate;
import com.kenny.jecs.funcs.Destroy;
import com.kenny.jecs.funcs.IDestroy;
import com.kenny.jecs.funcs.Each;
import com.kenny.jecs.funcs.EachC;
import com.kenny.jecs.funcs.IEachC;
import com.kenny.jecs.funcs.EachE;
import com.kenny.jecs.funcs.IEachE;
import com.kenny.jecs.funcs.IEach;
import com.kenny.jecs.funcs.Patch;
import com.kenny.jecs.funcs.IPatch;
import com.kenny.jecs.funcs.Sort;
import com.kenny.jecs.funcs.SortC;
import com.kenny.jecs.funcs.ISort;
import com.kenny.jecs.funcs.ISortC;

/**
* <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
Expand Down Expand Up @@ -541,22 +541,41 @@ public <C> C get(EntityT entity, Class<C> componentT) {
}

/**
* See {@link #BaseJECS.get(Number, Class...)}
* Pair components. See {@link #BaseJECS.getPair(Number, Class, Class)}
*/
@SuppressWarnings("unchecked")
@JECSApi(since = "0.1.9")
public <C extends Component> C[] get(EntityT entity, Class<C>... componentTs) {
return system.get(entity, componentTs);
public <C extends Component, C1 extends Component>
Pair<C, C1> getPair(EntityT entity, Class<C> a, Class<C1> b) {
return system.getPair(entity, a, b);
}

/**
* See {@link #BaseJECS.getPair(Number, Class, Class)}
* Two components. See {@link #BaseJECS.getPair(Number, Class, Class)}
*/
@JECSApi(since = "0.1.9")
public <C extends Component, C1 extends Component>
Pair<C, C1> getPair(EntityT entity, Class<C> a, Class<C1> b) {
return system.getPair(entity, a, b);
C[] get(EntityT entity, Class<C> a, Class<C1> b) {
return system.get(entity, a, b);
}

/**
* Three components. See {@link #BaseJECS.get(Number, Class...)}
*/
@JECSApi(since = "0.1.9")
public <C extends Component, C1 extends Component, C2 extends Component>
C[] get(EntityT entity, Class<C> a, Class<C1> b, Class<C2> c) {
return system.get(entity, a, b ,c);
}

/**
* Four components. See {@link #BaseJECS.get(Number, Class...)}
*/
@JECSApi(since = "0.1.9")
public <C extends Component, C1 extends Component, C2 extends Component, C3 extends Component>
C[] get(EntityT entity, Class<C> a, Class<C1> b, Class<C2> c, Class<C3> d) {
return system.get(entity, a, b, c, d);
}


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

/**Just null is already exist + im love C++.*/
@JECSApi
Expand Down Expand Up @@ -1472,7 +1493,7 @@ else if(args[0] == NULL_ARGS)
Constructor<?> ctor = componentT.getDeclaredConstructors()[0];
ctor.setAccessible(true);
if(isInnerClass && !isInnerStaticClass) {
Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs);
Object[] argss = getInnerObjects(args, finalCtorArgs, isNullArgs);;
return (C) ctor.newInstance(argss);
} else
return (C) ctor.newInstance((Object[])null);
Expand Down Expand Up @@ -1518,7 +1539,7 @@ private Object[] getInnerObjects(Object[] args, Class<?>[] finalCtorArgs, boolea
argss[i] = args[i - 1];
} else {
argss = new Object[1];
args[0] = inner;
argss[0] = inner;
}
return argss;
}
Expand Down Expand Up @@ -2946,6 +2967,33 @@ public <C extends Component> EntityT get(Class<C> component) {

return null;
}

/**
* Gets all entities that attached to this component.
* <p>
* If this method was called by constructor of some component on intitializaion or emplacing
* this also returns current emplaced entity or entity that will be emplaced, with this point of
* view is should be safe.
*
* @param <C> - component type.
* @param component - Class type of component.
*/
@JECSApi(since = "0.1.9")
public <C extends Component> List<EntityT> getAll(Class<C> component) {
var pairs = pool.get(component);
if(pairs == null)
return null;

List<EntityT> ne = new ArrayList<EntityT>();
for(var entity : entities) {
for(Pair<EntityT, Component> pair : pairs) {
if(pair.first == entity)
ne.add(entity);
}
}

return ne;
}

/**
* Return next avaliable component object of entity from index.
Expand Down Expand Up @@ -3106,22 +3154,21 @@ public <C extends Component> C getIfFamily(EntityT entity, Class<? extends Compo
@SafeVarargs
@JECSApi(since = "0.1.4")
public final <C extends Component> void createPack(EntityT entity, Class<? extends Component>... componentTs) {
for(int i = 0; i < packs.size(); i++) {
ComponentSequence<Component> sequence = packs.get(i);
if(sequence == null)
sequence = packs.put(i, new ComponentSequenceImpl<Component>());

if(sequence.isEmpty()) {
try {
Component[] component = get(entity, componentTs);
for(int c = 0; c < component.length; c++)
sequence.add(component[c]);
return;
} catch (JECSException e) { e.printStackTrace(); }
}
else
continue;

ComponentSequence<Component> sequence = new ComponentSequenceImpl<Component>();

if(sequence.isEmpty()) {
try {
Component[] component = get(entity, componentTs);
for(int c = 0; c < component.length; c++)
sequence.add(component[c]);
packs.put(packIndex, sequence);
} catch (JECSException e) { e.printStackTrace(); }
} else {
createPack(entity, componentTs);
}

packIndex++;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs;
package com.kenny.jecs;

public class JECSException extends RuntimeException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs;
package com.kenny.jecs;

public class JECSUndifiendBehaviourError extends RuntimeException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.io.Serializable;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kenny.jecs.collection;

package com.kenny.jecs.collection;

/**
* Pair is container/collection type that can hold two objects with different types.
*/
public class Pair<A, B> implements Comparable<A>
{
public A first;
Expand Down Expand Up @@ -31,23 +33,21 @@ public void setSecond(B second) {
this.second = second;
}

//@Override
//public Iterator<A> iterator() {
// return null;
//}
//
//public Iterator<B> iteratorB() {
// return null;
//}////

@Override
public String toString() {
return "[" + first + ", " + second + "]";
}

@Override
public int compareTo(A o) {
// TODO Auto-generated method stub
if(o == this || o.getClass() == o.getClass())
return 1;

@SuppressWarnings("unchecked")
B b = (B) new Object();
if(b.getClass() == o.getClass() || b.getClass() == this.getClass())
return 1;

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

/**
* Non generic version of {@link Pair}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.util.Iterator;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kenny.jecs.collection;
package com.kenny.jecs.collection;

import java.util.List;
import java.util.ListIterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kenny.jecs.funcs;
package com.kenny.jecs.funcs;

/**
* Implementation for {@link ICreate}. Implements a single {@link #invoke(int)}
* function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
public abstract class Create<EntityT> implements ICreate<EntityT>
{
/**
Expand All @@ -21,7 +21,7 @@ public abstract class Create<EntityT> implements ICreate<EntityT>
/**
* Represent a single {@link IEach#invoke(int)} function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
private static final class Function<EntityT> extends Create<EntityT>
{
private final ICreate<EntityT> createI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kenny.jecs.funcs;
package com.kenny.jecs.funcs;

/**
* Implementation for {@link IDestroy}. Implements a single {@link #invoke(int)}
* function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
public abstract class Destroy<EntityT> implements IDestroy<EntityT>
{
/**
Expand All @@ -21,7 +21,7 @@ public abstract class Destroy<EntityT> implements IDestroy<EntityT>
/**
* Represent a single {@link IDestroy#invoke(int)} function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
private static final class Function<EntityT> extends Destroy<EntityT>
{
private final IDestroy<EntityT> destroyI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kenny.jecs.funcs;
package com.kenny.jecs.funcs;

/**
* Implementation for {@link IEach}. Implements a single {@link #invoke(int, C)}
* function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
public abstract class Each<EntityT, Component> implements IEach<EntityT, Component>
{
/**
Expand All @@ -21,7 +21,7 @@ public abstract class Each<EntityT, Component> implements IEach<EntityT, Compone
/**
* Represent a single {@link IEach#invoke(int, Object)} function.
*/
@kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEach.JECSApi(since = "0.1.5")
private static final class Function<EntityT, Component> extends Each<EntityT, Component>
{
private final IEach<EntityT, Component> eachI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kenny.jecs.funcs;
package com.kenny.jecs.funcs;

/**
* Implementation for {@link IEachC}. Implements a single {@link #invoke(int, C)}
* function.
*/
@kenny.jecs.funcs.IEachC.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEachC.JECSApi(since = "0.1.5")
public abstract class EachC<EntityT, Component> implements IEachC<EntityT, Component>
{
/**
Expand All @@ -21,7 +21,7 @@ public abstract class EachC<EntityT, Component> implements IEachC<EntityT, Compo
/**
* Represent a single {@link IEachC#invoke(int, Object)} function.
*/
@kenny.jecs.funcs.IEachC.JECSApi(since = "0.1.5")
@com.kenny.jecs.funcs.IEachC.JECSApi(since = "0.1.5")
private static final class Function<EntityT, Component> extends EachC<EntityT, Component>
{
private final IEachC<EntityT, Component> eachCI;
Expand Down
Loading

0 comments on commit cc748b2

Please sign in to comment.