All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mostlyoriginal.api.operation.common.Operation Maven / Gradle / Ivy

package net.mostlyoriginal.api.operation.common;

import com.artemis.Entity;
import com.artemis.World;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pools;
import net.mostlyoriginal.api.component.Schedule;

import java.io.Serializable;

/**
 * Scriptable operation.
 *
 * @author Daan van Yperen
 * @see Schedule
 */
public abstract class Operation implements Pool.Poolable, Serializable {

	private transient Pool pool;
	protected boolean completed = false;

	protected Operation() {
	}

	/**
	 * Rewind operation as if it has never been processed yet.
	 * Unlike {@see #reset} this should only clear temporal/completion state.
	 */
	public void rewind() {
		completed = false;
	}

	public void reset() {
		completed=false;
	}

	/**
	 * Perform operation.
	 *
	 * Will do nothing if completed.
	 *
	 * @param delta world delta since last invocation.
	 * @param e subject of operation.
	 * @return {@code true} if completed, {@code false} if not.
	 */
	abstract public boolean process(float delta, Entity e);

	@SuppressWarnings("unchecked")
	public void release() {
		if (pool != null) {
			pool.free(this);
			pool = null;
		}
	}

	/**
	 * Returns a new or pooled action of the specified type.
	 */
	public static  T prepare(Class type) {
		final Pool pool = Pools.get(type);
		T node = pool.obtain();
		node.setPool(pool);
		return node;
	}

	/**
	 * @return {@code true} if operation completed, {@code false} if operation not completed
	 */
	public final boolean isCompleted() {
		return completed;
	}

	public final Pool getPool() {
		return pool;
	}

	public final void setPool(Pool pool) {
		this.pool = pool;
	}

	public void setCompleted(boolean completed) {
		this.completed = completed;
	}

	/**
	 * Add operation to empty entity in world.
	 * @param world
	 */
	public void on(World world) {
		world.createEntity().edit().create(Schedule.class).add(this);
	}

	/**
	 * Add operation to entity.
	 * @param e Apply script to passed entity.
	 * @see net.mostlyoriginal.api.operation.flow.ParallelOperation
	 * @see net.mostlyoriginal.api.operation.flow.SequenceOperation
	 */
	public void on(Entity e) {
		e.edit().create(Schedule.class).add(this);
	}

	/**
	 * Add operation to entity by id.
	 */
	public void on(World w, int entityId) {
		on(w.getEntity(entityId));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy