Android Open Source - killingspree World Body Utils






From Project

Back to project page killingspree.

License

The source code is released under:

MIT License

If you think the Android project killingspree listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sillygames.killingSpree.managers;
/*from w w  w .  j a va  2 s .co  m*/
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.sillygames.killingSpree.categories.LivingCategory;
import com.sillygames.killingSpree.managers.physics.Body;
import com.sillygames.killingSpree.managers.physics.Body.BodyType;
import com.sillygames.killingSpree.managers.physics.Ray;
import com.sillygames.killingSpree.managers.physics.World;
import com.sillygames.killingSpree.networking.messages.AudioMessage;
import com.sillygames.killingSpree.serverEntities.ServerArrow;
import com.sillygames.killingSpree.serverEntities.ServerBomb;
import com.sillygames.killingSpree.serverEntities.ServerBullet;
import com.sillygames.killingSpree.serverEntities.ServerEntity;
import com.sillygames.killingSpree.serverEntities.ServerPlayer;

public class WorldBodyUtils {
    
    public WorldManager worldManager;
    public ArrayList<ServerEntity> entities;
    private Circle circle;
    public AudioMessage audio;
    private World world;
    private Vector2 tempPlayerPosition;
    
    public WorldBodyUtils(WorldManager worldManager) {
        circle = new Circle();
        this.worldManager = worldManager;
        entities = new ArrayList<ServerEntity>();
        audio = worldManager.audio;
        this.world = worldManager.getWorld();
        tempPlayerPosition = new Vector2();
    }
    
    public Body addBox(float w, float h, float x, float y, BodyType type){
        Body body = new Body(x - w/2, y - h/2, w, h, type);
        body.setWorld(world);
        world.bodies.add(body);
        return body;
    }

    public void createWorldObject(MapObject object) {
        if (object instanceof RectangleMapObject) {
            Rectangle rectangle = ((RectangleMapObject) object).getRectangle();
            Body body = new Body(rectangle);
            world.bodies.add(body);
            
            if (rectangle.x < 20) {
                rectangle = new Rectangle(rectangle);
                rectangle.x += WorldRenderer.VIEWPORT_WIDTH;
                body = new Body(rectangle);
                world.bodies.add(body);
            }
            
            if (rectangle.x + rectangle.width > WorldRenderer.VIEWPORT_WIDTH - 20) {
                rectangle = new Rectangle(rectangle);
                rectangle.x -= WorldRenderer.VIEWPORT_WIDTH;
                body = new Body(rectangle);
                world.bodies.add(body);
            }
            
            if (rectangle.y < 20) {
                rectangle = new Rectangle(rectangle);
                rectangle.y += WorldRenderer.VIEWPORT_HEIGHT;
                body = new Body(rectangle);
                world.bodies.add(body);
            }
            if (rectangle.y > WorldRenderer.VIEWPORT_HEIGHT - 20) {
                rectangle = new Rectangle(rectangle);
                rectangle.y -= WorldRenderer.VIEWPORT_WIDTH;
                body = new Body(rectangle);
                world.bodies.add(body);
            }
        }
    }
    
    public ServerArrow AddArrow(float x, float y) {
        ServerArrow arrow = new ServerArrow(worldManager.id++, x, y, this);
        arrow.body.setUserData(arrow);
        entities.add(arrow);
        return arrow;
    }
    
    public ServerBullet AddBullet(float x, float y, ServerPlayer shooter) {
        ServerBullet bullet = new ServerBullet(worldManager.id++, x, y, this);
        bullet.shooter = shooter;
        bullet.body.setUserData(bullet);
        entities.add(bullet);
        return bullet;
    }
    
    public ServerBomb AddBomb(float x, float y, ServerPlayer bomber) {
        for (Body body: world.bodies) {
            if (body.rectangle.contains(x, y)) {
                return null;
            }
        }
        ServerBomb bomb = new ServerBomb(worldManager.id++, x, y, this);
        bomb.bomber = bomber;
        bomb.body.setUserData(bomb);
        entities.add(bomb);
        return bomb;
    }

    public void destroyBody(Body body) {
        body.toDestroy = true;
    }

    public ArrayList<Vector2> getPlayers(Vector2 point, float distance) {
        ArrayList<Vector2> playersPosition = new ArrayList<Vector2>();
        distance *= distance;
        for (ServerPlayer player: worldManager.playerList.values()) {
            Vector2 position = player.body.getPosition();
            if (point.dst2(position.x, position.y) < distance) {
                playersPosition.add(tempPlayerPosition.set(position.x, position.y));
            }
            else if (point.dst2(position.x + WorldRenderer.VIEWPORT_WIDTH, position.y) < distance) {
                playersPosition.add(tempPlayerPosition.set(position.x + WorldRenderer.VIEWPORT_WIDTH, position.y));
            }
            else if (point.dst2(position.x - WorldRenderer.VIEWPORT_WIDTH, position.y) < distance) {
                playersPosition.add(tempPlayerPosition.set(position.x - WorldRenderer.VIEWPORT_WIDTH, position.y));
                
            }
            else if (point.dst2(position.x, position.y + WorldRenderer.VIEWPORT_HEIGHT) < distance) {
                playersPosition.add(tempPlayerPosition.set(position.x, position.y + WorldRenderer.VIEWPORT_HEIGHT));
            }
            else if (point.dst2(position.x, position.y - WorldRenderer.VIEWPORT_HEIGHT) < distance) {
                playersPosition.add(tempPlayerPosition.set(position.x, position.y - WorldRenderer.VIEWPORT_HEIGHT));
            }
        }
        return playersPosition;
    }

    public void destroyEntities(ServerBomb bomb, float radius, Vector2 position) {
        Body body = bomb.body;
        circle.set(position, radius);
        for (ServerEntity entity: worldManager.entities) {
            if (entity.body == body || entity.body.toDestroy) {
                continue;
            }
            if (Intersector.overlaps(circle, entity.body.rectangle)) {
                Vector2 step = entity.body.getPosition();
                float length = position.dst(step);
                step.sub(position);
                float max = Math.max(step.x, step.y);
                step.scl(4 / max);
                Body otherBody = Ray.findBody(world,
                        body, step, length, true);
                if (otherBody == null) {
                    if (entity instanceof LivingCategory) {
                        if (((LivingCategory)entity.body.getUserData()).kill()) {
                            if (bomb.bomber != entity.body.getUserData())
                                bomb.bomber.addKill();
                            else {
                                bomb.bomber.reduceKill();
                            }
                        }
                    }
                }
            }
        }
    }
}




Java Source Code List

com.sillygames.killingSpree.AndroidLauncher.java
com.sillygames.killingSpree.KillingSpree.java
com.sillygames.killingSpree.PlatformServices.java
com.sillygames.killingSpree.categories.EnemyCategory.java
com.sillygames.killingSpree.categories.ExplodingWeaponCategory.java
com.sillygames.killingSpree.categories.LivingCategory.java
com.sillygames.killingSpree.categories.NonExplodingWeaponCategory.java
com.sillygames.killingSpree.clientEntities.ClientArrow.java
com.sillygames.killingSpree.clientEntities.ClientBlob.java
com.sillygames.killingSpree.clientEntities.ClientBomb.java
com.sillygames.killingSpree.clientEntities.ClientBullet.java
com.sillygames.killingSpree.clientEntities.ClientEntity.java
com.sillygames.killingSpree.clientEntities.ClientFly.java
com.sillygames.killingSpree.clientEntities.ClientFrog.java
com.sillygames.killingSpree.clientEntities.ClientPlayer.java
com.sillygames.killingSpree.clientEntities.ClientTestPlayer.java
com.sillygames.killingSpree.controls.InputController.java
com.sillygames.killingSpree.controls.onScreenControls.java
com.sillygames.killingSpree.desktop.DesktopLauncher.java
com.sillygames.killingSpree.desktop.DesktopServices.java
com.sillygames.killingSpree.helpers.EntityUtils.java
com.sillygames.killingSpree.helpers.Event.java
com.sillygames.killingSpree.helpers.MyButton.java
com.sillygames.killingSpree.helpers.MyConnection.java
com.sillygames.killingSpree.helpers.Utils.java
com.sillygames.killingSpree.managers.LevelLoader.java
com.sillygames.killingSpree.managers.WorldBodyUtils.java
com.sillygames.killingSpree.managers.WorldManager.java
com.sillygames.killingSpree.managers.WorldRenderer.java
com.sillygames.killingSpree.managers.physics.Body.java
com.sillygames.killingSpree.managers.physics.CollisionProcessor.java
com.sillygames.killingSpree.managers.physics.Ray.java
com.sillygames.killingSpree.managers.physics.WorldDebugRenderer.java
com.sillygames.killingSpree.managers.physics.World.java
com.sillygames.killingSpree.networking.ControlsSender.java
com.sillygames.killingSpree.networking.NetworkRegisterer.java
com.sillygames.killingSpree.networking.StateProcessor.java
com.sillygames.killingSpree.networking.messages.AudioMessage.java
com.sillygames.killingSpree.networking.messages.ClientDetailsMessage.java
com.sillygames.killingSpree.networking.messages.ConnectMessage.java
com.sillygames.killingSpree.networking.messages.ControlsMessage.java
com.sillygames.killingSpree.networking.messages.EntityState.java
com.sillygames.killingSpree.networking.messages.GameStateMessage.java
com.sillygames.killingSpree.networking.messages.PlayerNamesMessage.java
com.sillygames.killingSpree.networking.messages.ServerStatusMessage.java
com.sillygames.killingSpree.pool.AssetLoader.java
com.sillygames.killingSpree.pool.EntityPool.java
com.sillygames.killingSpree.pool.MessageObjectPool.java
com.sillygames.killingSpree.pool.Pool.java
com.sillygames.killingSpree.pool.Poolable.java
com.sillygames.killingSpree.renderers.HUDRenderer.java
com.sillygames.killingSpree.screens.AbstractScreen.java
com.sillygames.killingSpree.screens.ClientDiscoveryScreen.java
com.sillygames.killingSpree.screens.GameScreen.java
com.sillygames.killingSpree.screens.LobbyScreen.java
com.sillygames.killingSpree.screens.MainMenuScreen.java
com.sillygames.killingSpree.screens.OptionsScreen.java
com.sillygames.killingSpree.screens.SplashScreen.java
com.sillygames.killingSpree.screens.settings.Constants.java
com.sillygames.killingSpree.serverEntities.ServerArrow.java
com.sillygames.killingSpree.serverEntities.ServerBlob.java
com.sillygames.killingSpree.serverEntities.ServerBomb.java
com.sillygames.killingSpree.serverEntities.ServerBullet.java
com.sillygames.killingSpree.serverEntities.ServerEntity.java
com.sillygames.killingSpree.serverEntities.ServerFly.java
com.sillygames.killingSpree.serverEntities.ServerFrog.java
com.sillygames.killingSpree.serverEntities.ServerPlayer.java
com.sillygames.killingSpree.sound.SFXPlayer.java