Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Component Interaction  (Read 1197 times)
0 Members and 1 Guest are viewing this topic.
Offline Harris6310

Senior Newbie





« Posted 2012-03-31 13:08:52 »

Hello, I'm trying to create a simple entity/component system for a game and I'm having trouble getting components to interact. Say I have an entity which has a position component and a render component. How would I be able to get the render component to receive  where to render it from the position component? Both components have a entity reference to their owner. Can anybody help me out with this?
Offline ReBirth
« Reply #1 - Posted 2012-03-31 13:21:54 »

To interact, put all entities into a class with List or what, usually a class that hold game logic. That class, should have access to render component, which to put it simple in java2d is Graphics object. Now every loop/tick, let all entity render them self through method inside them that receive render component parameter and use it along with position component.

And creating another class to hold position value is kinda overkill, just let entity class to have instance of Point2D or integers.

Offline Harris6310

Senior Newbie





« Reply #2 - Posted 2012-03-31 13:29:48 »

Well if I was to have components stored in an ArrayList in the entity, how would I be able to access them from that?

In the render component:
1  
owner.entitiesList.?


Each component has a unique index string, could I access it through that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-03-31 13:50:17 »

1  
2  
3  
4  
//you get Graphics object 'g' from JPanel, BufferedImage or Canvas
for (int i=0; i<entitiesList.size(); i++){
    entitiesList.get(i).render(g);
}

Offline Harris6310

Senior Newbie





« Reply #4 - Posted 2012-03-31 13:57:28 »

Thanks for your help but I have sorted this out. I created a method that will go through the list and get the needed component. Your method would of worked but I didn't have a render method for each component. Here is mine:

Quote
public Component getComponent(String index)
    {
        Component component = null;
       
        for(int i = 0; i < components.size(); i++)
        {
            if(components.get(i).getIndex().equals(index))
            {
                component = components.get(i);
            }
        }
       
        return component;
    }
Offline ReBirth
« Reply #5 - Posted 2012-03-31 14:06:28 »

Quote
but I didn't have a render method for each component
then create it Smiley

Offline actual

JGO Coder


Medals: 19



« Reply #6 - Posted 2012-03-31 16:10:03 »

I don't know how many components you have in your list typically, but could you do this in as a HashMap and then just say

1  
2  
3  
public Component getComponent(String index) {
   return components.get(index);
}
Offline tom
« Reply #7 - Posted 2012-03-31 18:07:33 »

I do it like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public <T> T getComponent(Class<T> type) {
        for (Component comp : this) {
            if (type.isAssignableFrom(comp.getClass())) {
                return (T) comp;
            }
        }

        return null;
    }


Then you can write code like:
1  
Renderer renderer = entity.getComponent(Renderer.class);

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #8 - Posted 2012-03-31 18:15:52 »

I do it like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public <T> T getComponent(Class<T> type) {
        for (Component comp : this) {
            if (type.isAssignableFrom(comp.getClass())) {
                return (T) comp;
            }
        }

        return null;
    }


Then you can write code like:
1  
Renderer renderer = entity.getComponent(Renderer.class);


Do it like this so you won't need to cast and get warnings:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public <T extends Component> T getComponent(Class<T> type) {
        for (Component comp : this) {
            if (type.isAssignableFrom(comp.getClass())) {
                return comp;
            }
        }

        return null;
    }

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (33 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (114 views)
2013-05-14 15:56:26

kutucuk (138 views)
2013-05-12 17:10:36

kutucuk (138 views)
2013-05-12 15:36:09

UnluckyDevil (146 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.138 seconds with 21 queries.