Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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 2306 times)
0 Members and 1 Guest are viewing this topic.
Offline Harris6310

Senior Newbie





« Posted 2012-03-31 11: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 11: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 11: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 11: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 11: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 12:06:28 »

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

Offline actual

JGO Coder


Medals: 24



« Reply #6 - Posted 2012-03-31 14: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 16: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: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-03-31 16: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.

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!