com.starsailor.util.box2d.Box2dRadiusProximity.java Source code

Java tutorial

Introduction

Here is the source code for com.starsailor.util.box2d.Box2dRadiusProximity.java

Source

/*******************************************************************************
 * Copyright 2014 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.starsailor.util.box2d;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.ai.steer.Proximity;
import com.badlogic.gdx.ai.steer.Steerable;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.World;
import com.starsailor.actors.bullets.Bullet;
import com.starsailor.components.SteerableComponent;

/**
 * A {@code Box2dRadiusProximity} is a {@link Proximity} that queries the world for all fixtures that potentially overlap the
 * circle having the specified detection radius and whose center is the owner position.
 *
 * @author davebaol
 */
public class Box2dRadiusProximity extends Box2dSquareAABBProximity {

    public Box2dRadiusProximity(Steerable<Vector2> owner, World world, float detectionRadius) {
        super(owner, world, detectionRadius);
    }

    @SuppressWarnings("unchecked")
    protected Steerable<Vector2> getSteerable(Fixture fixture) {
        Entity entity = (Entity) fixture.getBody().getUserData();
        return entity.getComponent(SteerableComponent.class);
    }

    @Override
    public boolean reportFixture(Fixture fixture) {
        Steerable<Vector2> steerable = getSteerable(fixture);
        if (steerable == null) {
            return false;
        }

        boolean accept = accept(steerable);
        if (!accept) {
            return false;
        }

        return super.reportFixture(fixture);
    }

    @Override
    protected boolean accept(Steerable<Vector2> steerable) {
        // The bounding radius of the current body is taken into account
        // by adding it to the radius proximity
        float range = detectionRadius + steerable.getBoundingRadius();

        // Make sure the current body is within the range.
        // Notice we're working in distance-squared space to avoid square root.
        float distanceSquare = steerable.getPosition().dst2(owner.getPosition());

        Entity entity = (Entity) ((SteerableComponent) steerable).getBody().getUserData();
        if (entity instanceof Bullet) {
            return false;
        }

        return distanceSquare <= range * range;
    }

}