Thread: Component Based Entity Design

Results 1 to 3 of 3
  1. #1 Component Based Entity Design 
    Registered Member
    Join Date
    Apr 2016
    Posts
    26
    Thanks given
    6
    Thanks received
    15
    Rep Power
    71
    Component based design was originally pioneered in order to avoid annoying class hierarchies that inheritance introduces. The idea in this scenario is that our entity is simply an aggregation of multiple components. These components then define that entity's behavior and attributes. I wrote a small implementation to exemplify how this could be used in an emulation context.

    Entity.class
    Code:
    package org.glasscastle.model.entity;
    
    import java.util.HashMap;
    
    public class Entity {
    
    	private int index;
    	
    	private final HashMap<Class<? extends EntityComponent>, EntityComponent> components = new HashMap<>();
    
    	public EntityComponent get(Class<? extends EntityComponent> clazz) {
    		EntityComponent component = components.get(clazz);
    		if (component == null)
    			throw new InvalidComponentException("Component for " + clazz + " could not be located.");
    		return component;
    	}
    
    	public EntityComponent add(EntityComponent component) {
    		return components.put(component.getClass(), component);
    	}
    
    	public EntityComponent remove(Class<? extends EntityComponent> clazz) {
    		return components.remove(clazz);
    	}
    	
    	public boolean has(Class<? extends EntityComponent> clazz) {
    		return components.containsKey(clazz);
    	}
    
    	public int getIndex() {
    		return index;
    	}
    
    	public void setIndex(int index) {
    		this.index = index;
    	}
    
    	public HashMap<Class<? extends EntityComponent>, EntityComponent> getComponents() {
    		return components;
    	}
    }
    EntityComponent.class
    Code:
    package org.glasscastle.model.entity;
    
    public abstract class EntityComponent {
    
    }
    InvalidComponentException.class
    Code:
    package org.glasscastle.model.entity;
    
    public class InvalidComponentException extends RuntimeException {
    
    	public InvalidComponentException(String message) {
    		super(message);
    	}
    }
    We could use this type of pattern as follows. As you can see, we're not explicitly defining an entity. Instead, we're building it from multiple different components.
    Code:
    Entity player = new Entity();
    player.add(new MutablePositionComponent(3200, 3200, 0));
    player.add(new LoginDetailsComponent("Username", "Password", "0.0.0.0"));
    		
    MutablePositionComponent position = (MutablePositionComponent) player.get(MutablePositionComponent.class);
    LoginDetailsComponent details = (LoginDetailsComponent) player.get(LoginDetailsComponent.class);
    		
    System.out.println(details.getUsername() + " is standing at " + position + ".");
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Worth noting you can do away with the Entity class altogether and then you'll have an Entity Component System. Which is what my current project is using, feel free to poke around the code https://www.rune-server.ee/runescape...94-hestia.html
    Attached imageAttached image
    Reply With Quote  
     

  4. Thankful users:


  5. #3  
    Erethium Developer
    Code Kiwi's Avatar
    Join Date
    Jan 2015
    Posts
    432
    Thanks given
    179
    Thanks received
    23
    Rep Power
    72
    Quote Originally Posted by Greg View Post
    Worth noting you can do away with the Entity class altogether and then you'll have an Entity Component System. Which is what my current project is using, feel free to poke around the code https://www.rune-server.ee/runescape...94-hestia.html
    That's pretty neat greg, gj
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Entity handling designs
    By gnarly in forum RS2 Server
    Replies: 112
    Last Post: 06-19-2012, 07:11 PM
  2. Replies: 0
    Last Post: 12-19-2011, 05:12 AM
  3. Decentish Entity based combat
    By Seb Bruce in forum Snippets
    Replies: 6
    Last Post: 11-21-2011, 09:05 PM
  4. Character Design Base
    By AlexMason in forum Tutorials
    Replies: 42
    Last Post: 05-16-2009, 05:16 AM
  5. Dophert's entity based combat system
    By WH:II:DOW in forum Tutorials
    Replies: 22
    Last Post: 01-11-2009, 12:17 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •