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

com.artemis.factory.ShipImpl Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.artemis.factory;

import com.artemis.Archetype;
import com.artemis.ArchetypeBuilder;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.World;
import com.artemis.annotations.Wire;
import com.artemis.managers.GroupManager;
import com.artemis.managers.TagManager;
import com.artemis.utils.Bag;
import com.artemis.component.Asset;
import com.artemis.component.Cullible;
import com.artemis.component.Size;
import com.artemis.component.Velocity;
import com.artemis.component.Position;
import com.artemis.component.Sprite;
import com.artemis.component.HitPoints;

@Wire(failOnNull=false)
public class ShipImpl implements Ship {
	// default fields
	private World world;
	private boolean _tag;
	private String _tag_tag;
	private boolean _group;
	private Bag _groups = new Bag();
	
	boolean _sealed;
	
	private TagManager tagManager;
	private GroupManager groupManager;
	private Archetype archetype;
	
	// component parameter fields
	private boolean _cullible_culled;
	private boolean _id_asset_path_;
	private boolean _id_culled_culled_;
	private boolean _id_position_x_y_;
	private boolean _id_size_width_height_;
	private boolean _id_velocity_x_;
	private boolean _id_velocity_x_y_;
	private float _position_x;
	private float _position_y;
	private float _size_height;
	private float _size_width;
	private float _velocity_x;
	private float _velocity_y;
	private int _hitPoints_current;
	private java.lang.String _asset_path;
	
	// mappers
	private ComponentMapper hitPointsMapper;
	private ComponentMapper assetMapper;
	private ComponentMapper velocityMapper;
	private ComponentMapper positionMapper;
	private ComponentMapper cullibleMapper;
	private ComponentMapper sizeMapper;
	
	public ShipImpl(World world) {
		this.world = world;
		world.inject(this);
		
		archetype = new ArchetypeBuilder()
			.add(Asset.class)
			.add(Cullible.class)
			.add(Size.class)
			.add(Velocity.class)
			.add(Position.class)
			.add(Sprite.class)
			.add(HitPoints.class)
			.build(world);
	}
	
	private ShipImpl() {}
	
	@Override
	public Ship copy() {
		ShipImpl copy = new ShipImpl();
		copy.world = world;
		copy.archetype = archetype;
		copy._hitPoints_current = _hitPoints_current;
		
		world.inject(copy);
		
		return copy;
	}

	@Override
	public Ship tag(String tag) {
		_tag = true;
		this._tag_tag = tag;
		return this;
	}

	@Override
	public Ship group(String group) {
		_group = true;
		_groups.add(group);
		return this;
	}

	@Override
	public Ship group(String groupA, String... groups) {
		_groups.add(groupA);
		for (int i = 0; groups.length > i; i++) {
			_groups.add(groups[i]);
		}
		return this;
	}

	@Override
	public Ship group(String groupA, String groupB, String... groups) {
		_group = true;
		_groups.add(groupA);
		_groups.add(groupB);
		for (int i = 0; groups.length > i; i++) {
			_groups.add(groups[i]);
		}
		return this;
	}
	
	@Override
	public Entity create() {
		_sealed = true;
		
		Entity e = world.createEntity(archetype);

		if (_id_position_x_y_) {
			Position c = positionMapper.get(e);
			c.x = _position_x;
			c.y = _position_y;
			_id_position_x_y_ = false;
		}
		
		if (_id_velocity_x_y_) {
			Velocity c = velocityMapper.get(e);
			c.x = _velocity_x;
			c.y = _velocity_y;
			_id_velocity_x_y_ = false;
		}
		
		if (_id_velocity_x_) {
			Velocity c = velocityMapper.get(e);
			c.x = _velocity_x;
			_id_velocity_x_ = false;
		}
		
		if (_id_asset_path_) {
			Asset c = assetMapper.get(e);
			c.path = _asset_path;
			_id_asset_path_ = false;
		}
		
		if (_id_size_width_height_) {
			Size c = sizeMapper.get(e);
			c.width = _size_width;
			c.height = _size_height;
			_id_size_width_height_ = false;
		}
		
		if (_id_culled_culled_) {
			Cullible c = cullibleMapper.get(e);
			c.culled = _cullible_culled;
			_id_culled_culled_ = false;
		}
		
		{
			HitPoints c = hitPointsMapper.get(e);
			c.current = _hitPoints_current;
		}
		
		if (_tag) {
			tagManager.register(_tag_tag, e);
			_tag = false;
		}
		
		if (_group) {
			for (int i = 0, s = _groups.size(); s > i; i++) {
				groupManager.add(e, _groups.get(i));
			}
			_group = false;
		}
		
		return e;
	}


	@Override
	public Ship hitPoints(int current) {
		if (_sealed) {
			String err = "hitPoints are stickied, unable to change after creating " +
					"first entity. See copy().";
			throw new IllegalArgumentException(err);
		}
		_hitPoints_current = current;
		return this;
	}

	@Override
	public Ship position(float x, float y) {
		_id_position_x_y_ = true;
		_position_x = x;
		_position_y = y;
		return this;
	}
	@Override
	public Ship velocity(float x, float y) {
		_id_velocity_x_y_ = true;
		_velocity_x = x;
		_velocity_y = y;
		return this;
	}
	@Override
	public Ship velocity(float x) {
		_id_velocity_x_ = true;
		_velocity_x = x;
		return this;
	}
	@Override
	public Ship asset(java.lang.String path) {
		_id_asset_path_ = true;
		_asset_path = path;
		return this;
	}
	@Override
	public Ship size(float width, float height) {
		_id_size_width_height_ = true;
		_size_width = width;
		_size_height = height;
		return this;
	}
	@Override
	public Ship culled(boolean culled) {
		_id_culled_culled_ = true;
		_cullible_culled = culled;
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy