Advertisement

Implementing a component-based entity system?

Started by September 01, 2013 03:13 AM
9 comments, last by stein102 11 years, 5 months ago

I'm still having a hard time figure out how to implement this and how it could work. Here is what I have right now, where did I go wrong? This is all jumbled up and feels really confusing. I seriously feel like I don't get this at all =/

ItemCreator:


package jesse.rscl.Items;


import java.io.IOException;
import java.util.ArrayList;

import jesse.rscl.Items.Components.IComponentGeneral;
import jesse.rscl.Items.Systems.ISystem;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;

public class ItemCreator {

	XmlReader reader;
	Element root;
	ISystem system;
	ArrayList<Item> items;
	ArrayList<Integer> GUIDS;
	//For storing all base items
	ArrayList<Integer> itemList;
	
	public ItemCreator(){
		items = new ArrayList<Item>();
		reader = new XmlReader();
		system = new ISystem();
		GUIDS = new ArrayList<Integer>();

		try {
			root = reader.parse(Gdx.files.internal("items/Items.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Array<Element> items = root.getChildrenByName("item");
		
		//Loop through each "Item"
		for (Element child : items){
			int currentGUID = newGUID();
			//Loop through each Item's components
			int numberOfChildren = child.getChildCount();
			Array<Element> components = new Array<Element>();
			for(int i = 0; i < numberOfChildren; i++){
				components.add(child.getChild(i));
				createComponent(currentGUID,child.getChild(i));
				GUIDS.add(currentGUID);
			}
		}
		
	}
	

	public void createComponent(int GUID,Element component){
		//General Component
		if(component.getName().equals("general")){
			int id = Integer.parseInt(component.getAttribute("id"));
			String name = component.getAttribute("name");
			system.general.add(new IComponentGeneral(id,name,GUID), GUID);
		}
		
		//WieldableComponent
	}
	
	
	public int newGUID(){
		int guid = GUIDS.size();
		return guid;
	}	
}

ISystem


package jesse.rscl.Items.Systems;

public class ISystem {

	public ISystemGeneral general;
	public ISystemWieldable wieldable;
	
	
	public ISystem() {
		general = new ISystemGeneral();
		wieldable = new ISystemWieldable();
	}
}

ISystemGeneral:


package jesse.rscl.Items.Systems;

import java.util.ArrayList;

import jesse.rscl.Items.Components.IComponentGeneral;

public class ISystemGeneral {

	ArrayList<IComponentGeneral> members;
	
	public ISystemGeneral() {
		members = new ArrayList<IComponentGeneral>();
	}
	
	public void add(IComponentGeneral member,int GUID){
		members.add(GUID, member);
	}
	
	public String getName(int GUID){
		return members.get(GUID).name;
	}
	
	public int getID(int GUID){
		return members.get(GUID).ID;
	}
}

IComponentGeneral:


package jesse.rscl.Items.Components;

public class IComponentGeneral {

	//GUID to match with specific entity
	int GUID;
	//Properties of component
	public int ID;
	public String name;
	
	public IComponentGeneral(int id,String name,int guid) {
		GUID = guid;
		ID = id;
		this.name = name;
	}
}

This topic is closed to new replies.

Advertisement