Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I access all entities with a certain component? #740

Closed
eboatwright opened this issue Nov 13, 2021 · 5 comments
Closed

How do I access all entities with a certain component? #740

eboatwright opened this issue Nov 13, 2021 · 5 comments

Comments

@eboatwright
Copy link

To learn specs, I'm working on making a breakout clone. So, for the Ball component, I need to be able to access the Paddle to get it's position (for collisions). And, I can't figure out how to do it :(

@zesterer
Copy link
Contributor

Have you read through the SPECS book?

@eboatwright
Copy link
Author

Yeah I've read through it :( Basically, what I need is a way to just do like "Entities.get_entity_with_tag("paddle")" or something like that. Because, I need to access the Transform component on the paddle Entity from the ball System to check for collisions.

@eboatwright
Copy link
Author

I figured out a temporary solution. Is this good?
struct BallSystem;

impl<'a> System<'a> for BallSystem {
type SystemData = (
ReadStorage<'a, Transform>,
WriteStorage<'a, RigidBody>,
ReadStorage<'a, Ball>,
ReadStorage<'a, Paddle>,
);

fn run(&mut self, (transform, mut rigidbody, ball, paddle): Self::SystemData) {
    for (e_transform, e_rigidbody, e_ball) in (&transform, &mut rigidbody, &ball).join() {
        if is_key_pressed(KeyCode::Space)
        && e_rigidbody.velocity == Vec2::ZERO {
            e_rigidbody.velocity = vec2(e_ball.move_speed, -e_ball.move_speed);
        }

        if e_transform.position.x < 0.0
        || e_transform.position.x > SCREEN_WIDTH as f32 - 13.0 {
            e_rigidbody.velocity.x *= -1.0;
        }

        if e_transform.position.y < 0.0 {
            e_rigidbody.velocity.y *= -1.0;
        }

        for (p_transform, _p_paddle) in (&transform, &paddle).join() {
            if e_transform.position.y + 13.0 > p_transform.position.y
            && e_transform.position.x + 13.0 > p_transform.position.x
            && e_transform.position.x < p_transform.position.x + 80.0
            && e_rigidbody.velocity.y > 0.0 {
                e_rigidbody.velocity.y *= -1.0;
            }
        }
    }
}

}

@zesterer
Copy link
Contributor

I figured out a temporary solution. Is this good?

Yes. SPECS does not support querying for specific entities with certain components, only the set of all entities with a particular set of components.

If specific entities have a special meaning in your project then I'd advise storing their Entity in some global resource to make finding them faster.

@eboatwright
Copy link
Author

Ok! Thank you for your help! I'm not very experienced with Rust if you couldn't tell XD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants