Android Open Source - Ready-Set-Rogue B S P Rectangle






From Project

Back to project page Ready-Set-Rogue.

License

The source code is released under:

GNU General Public License

If you think the Android project Ready-Set-Rogue 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.warsheep.scamp.adt;
/*from   w  w w.j av a 2  s  . c  o m*/
import com.badlogic.gdx.math.Vector2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BSPRectangle implements Container {

    private final boolean DISCARD_BY_RATIO = true;
    private final double H_RATIO = 0.35;
    private final double W_RATIO = 0.35;

    private static Random rnd = new Random();

    private int x, y, width, height;
    private Vector2 center;
    private BSPRectangle leftChild;
    private BSPRectangle rightChild;

    public BSPRectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.center = new Vector2(this.x + this.width / 2, this.y + this.height / 2);
    }

    public BSPRectangle[] split(int timeout) {
        if (rnd.nextBoolean()) {
            // Vertical
            leftChild = new BSPRectangle(x, y, rnd.nextInt(width - 1) + 1, height);
            rightChild = new BSPRectangle(x + leftChild.width, y, width - leftChild.width, height);

            if (DISCARD_BY_RATIO) {
                double r1_w_ratio = (float) leftChild.width / leftChild.height;
                double r2_w_ratio = (float) rightChild.width / rightChild.height;

                if ((r1_w_ratio < W_RATIO || r2_w_ratio < W_RATIO) && timeout > 0) {
                    return split(timeout--);
                }
            }
        } else {
            // Horizontal
            leftChild = new BSPRectangle(x, y, width, rnd.nextInt(height - 1) + 1);
            rightChild = new BSPRectangle(x, y + leftChild.height, width, height - leftChild.height);

            if (DISCARD_BY_RATIO) {
                double r1_h_ratio = (float) leftChild.height / leftChild.width;
                double r2_h_ratio = (float) rightChild.height / rightChild.width;
                if ((r1_h_ratio < H_RATIO || r2_h_ratio < H_RATIO) && timeout > 0) {
                    return split(timeout--);
                }
            }
        }
        BSPRectangle[] children = {leftChild, rightChild};

        return children;
    }

    public BSPRectangle partition(BSPRectangle root, int iterations) {
        if (iterations != 0) {
            BSPRectangle[] leaves = root.split(2);
            root.leftChild = partition(leaves[0], iterations - 1);
            root.rightChild = partition(leaves[1], iterations - 1);
        }
        return root;
    }

    public BSPRectangle getLeftChild() {
        return this.leftChild;
    }

    public BSPRectangle getRightChild() {
        return this.rightChild;
    }

    public List<BSPRectangle> getLeaves() {
        List<BSPRectangle> children = new ArrayList<>();

        BSPRectangle left;
        BSPRectangle right;
        left = this.getLeftChild();
        right = this.getRightChild();
        if (left == null || right == null) {
            children.add(this);
            return children;
        } else {
            children.add(left);
            children.add(right);
            children.addAll(left.getLeaves());
            children.addAll(right.getLeaves());
            return children;
        }
    }

    public List<BSPRectangle> getLevel(int level) {
        List<BSPRectangle> list = new ArrayList<>();
        if (level == 1) {
            list.add(this);
        } else {
            if (this.getLeftChild() != null) {
                list.addAll(this.getLeftChild().getLevel(level - 1));
            }
            if (this.getRightChild() != null) {
                list.addAll(this.getRightChild().getLevel(level - 1));
            }
        }
        return list;
    }

    @Override
    public int x() {
        return this.x;
    }

    @Override
    public int y() {
        return this.y;
    }

    @Override
    public int width() {
        return this.width;
    }

    @Override
    public int height() {
        return this.height;
    }

    @Override
    public Vector2 center() {
        return this.center;
    }
}




Java Source Code List

com.warsheep.scamp.AssetDepot.java
com.warsheep.scamp.IOSLauncher.java
com.warsheep.scamp.MapImporter.java
com.warsheep.scamp.PrefabFactory.java
com.warsheep.scamp.Scamp.java
com.warsheep.scamp.adt.BSPRectangle.java
com.warsheep.scamp.adt.Container.java
com.warsheep.scamp.adt.Pair.java
com.warsheep.scamp.adt.Room.java
com.warsheep.scamp.algorithms.BSPMapGenerator.java
com.warsheep.scamp.algorithms.Compositor.java
com.warsheep.scamp.android.AndroidLauncher.java
com.warsheep.scamp.client.HtmlLauncher.java
com.warsheep.scamp.components.AIControllableComponent.java
com.warsheep.scamp.components.AttackerComponent.java
com.warsheep.scamp.components.CameraComponent.java
com.warsheep.scamp.components.CollidableComponent.java
com.warsheep.scamp.components.ControllableComponent.java
com.warsheep.scamp.components.DamageableComponent.java
com.warsheep.scamp.components.DropComponent.java
com.warsheep.scamp.components.ECSMapper.java
com.warsheep.scamp.components.FactionComponent.java
com.warsheep.scamp.components.InventoryComponent.java
com.warsheep.scamp.components.LevelComponent.java
com.warsheep.scamp.components.MovementComponent.java
com.warsheep.scamp.components.StateComponent.java
com.warsheep.scamp.components.TileComponent.java
com.warsheep.scamp.components.TransformComponent.java
com.warsheep.scamp.components.VisibleComponent.java
com.warsheep.scamp.desktop.AssetPacker.java
com.warsheep.scamp.desktop.DesktopLauncher.java
com.warsheep.scamp.processors.AIProcessor.java
com.warsheep.scamp.processors.CameraProcessor.java
com.warsheep.scamp.processors.CollisionProcessor.java
com.warsheep.scamp.processors.CombatProcessor.java
com.warsheep.scamp.processors.ControlProcessor.java
com.warsheep.scamp.processors.DeathProcessor.java
com.warsheep.scamp.processors.LevelingProcessor.java
com.warsheep.scamp.processors.MovementProcessor.java
com.warsheep.scamp.processors.StateProcessor.java
com.warsheep.scamp.processors.TileProcessor.java
com.warsheep.scamp.processors.VisibilityProcessor.java
com.warsheep.scamp.screens.MainGameScreen.java
com.warsheep.scamp.screens.MainMenuScreen.java