alecthomas / entityx

EntityX - A fast, type-safe C++ Entity-Component system
MIT License
2.22k stars 296 forks source link

Example request #51

Closed sabotage3d closed 10 years ago

sabotage3d commented 10 years ago

Hello ,

I have read the tutorial on the main page but some of the stuff are changed in the latest release and can't compile. Can I request an example where an Entity is inflicting damage on a few monsters let say, throwing a bomb that will subtract their health. I just can't figure some stuff on how to combine your system with timed events like bomb for example. Something similar like in the Trefall's entity system: https://xp-dev.com/svn/Trefall-ComponentSystem/TestEntity/main.cpp

Thanks in advance,

Alex

alecthomas commented 10 years ago

There are several examples listed in the README, including an asteroids clone. Is that not sufficient?

Also if there are examples in the README that don't work I'd appreciate a heads up on which ones.

Virspace commented 10 years ago

Alec, I can confirm that the examples are a bit rough. The Azteroids clone is using a different version of EntityX, and I actually found your README examples to be outdated as well. Then again, this is before you released 1.0.0 ten days ago so I haven't looked at anything newer than that. I just know that I've been having a hard time myself trying to get something going between the two. Trial and error has been the best teacher thus far. I'm taking another crack at it this weekend and will document what I can.

alecthomas commented 10 years ago

Hmm, okay. I think what I might do is extract the code from the README into an example, maybe flesh it out some more, then selectively merge it back in.

sabotage3d commented 10 years ago

Sounds good just some full working code of communication between entities and timed events would be great. As I am having hard time wrapping Ogre3d into the system as I don't fully understand what is happening :)

Thanks,

Alex

alecthomas commented 10 years ago

Okay. I've written up an example. It illustrates all of EntityX's concepts, and general entity-component principles.

sabotage3d commented 10 years ago

Awesome :)

sabotage3d commented 10 years ago

I am getting like 4-7 fps on this example on OSX is that normal ?

alecthomas commented 10 years ago

That's likely. The draw calls are mind bogglingly inefficient. Try modifying the code to use a smaller resolution and less initial circles (~ line 278).

alecthomas commented 10 years ago

Also, in practice you probably wouldn't use EntityX for a particle emitter :)

sabotage3d commented 10 years ago

Yeah just noticed the collisions are brute force they would be slow anyway :)

alecthomas commented 10 years ago

Yeah, exactly. 500^2 (250K) entity accesses per frame!

sabotage3d commented 10 years ago

Just a quick question is it currently supported to add processes like time events. If we create a separate process class how would that fit best with the entity system ? Something like:

DelayProcess::DelayProcess(unsigned long timeToDelay)
{
m_timeToDelay = timeToDelay;
m_timeDelayedSoFar = 0;
}
void DelayProcess::OnUpdate(unsigned long deltaMs)
{
m_timeDelayedSoFar += deltaMs;
if (m_timeDelayedSoFar >= m_timeToDelay)
Succeed();
}

DelayProcessr* pDelay(new DelayProcess(5000)); 
processManager.AttachProcess(pDelay);
alecthomas commented 10 years ago

That's not really in the scope of an entity-component system, directly. But you could integrate it into a system, and emit an event when a timer expires. Another option would be to have a Timeout component with a corresponding TimeoutSystem. Add the component to a system, then the TimeoutSystem manages emitting timer events for particular entities.

That said, using entity-component systems requires a different way of thinking about logic and data, so you might find that an alternate approach will work better. I'm not sure what, it will depend on what you're trying to achieve.

sabotage3d commented 10 years ago

I am trying to make a simple bomb entity with Timeout component. I just noticed that Fadeable is close to what I am trying to do I will go from there.

alecthomas commented 10 years ago

Reworked the example (507ae22c5b74d2f558cbd8bd2a204f02b43a66cd) to use a (slightly) more efficient spatial partitioning schema for collision. I also found a bit of a performance bottleneck in the EntityX itself, during iteration, which I've fixed (585a2835442cef94f1ca0c2290fb17f660ee1641).

Rendering now dominates the profile, which is what I would hope for.

sreich commented 10 years ago

btw what bottleneck does (585a283) address? didn't quite understand what the changes involve.

alecthomas commented 10 years ago

The View was generic, allowing for multiple predicates to be used to filter out entities. This caused some performance loss and in practice the only predicate ever used was one that filtered by component mask, so I just hard-coded it.