Android Open Source - SimpleGame General Grid






From Project

Back to project page SimpleGame.

License

The source code is released under:

GNU General Public License

If you think the Android project SimpleGame 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 ru.rs.gameobjects;
// w w w. j a  va2  s. c o  m
import ru.rs.interfaces.Game;

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

/**
 * Created by Vadim Suslov on 30.05.14.
 * 1D Grid for 1D games
 */
public class GeneralGrid {

    private List<SimpleObject> objects;
    private Game game;
    private int cols;
    private List<SimpleObject>[] cells;


    /**
     * Constructor for grid
     * @param game game interface
     * @param i number of cells per row;
     */
    public GeneralGrid(Game game, int i) {
        this.game=game;
        cols=i;
        cells=new List[cols];
        for(int j=0;j<cols;j++) {
            cells[j]=new ArrayList<SimpleObject>();
        }

    }

    /**
     * insert object into Grid
     * @param object object to insert
     */
    public void insertObject(SimpleObject object) {
        int x1=convertX(object.getBounds().lowerLeft.x);
        int x2=convertX(object.getBounds().lowerLeft.x+object.getBounds().width);

        if(x1!=x2) {
            cells[x2].add(object);
        }
        cells[x1].add(object);
    }

    /**
     * Getting X coordinate as integer cell-coordinate
     * @param x double original coordinate
     * @return integer cell-coordinate
     */
    private int convertX(double x) {
       int colSize=game.getGraphics().getWidth()/cols;
       int result=(int) Math.floor(x/colSize);
       return result;
    }
}




Java Source Code List

ru.rs.Resources.java
ru.rs.SimpleGame.java
ru.rs.gameobjects.Castle.java
ru.rs.gameobjects.DynamicObject.java
ru.rs.gameobjects.GeneralGrid.java
ru.rs.gameobjects.Side.java
ru.rs.gameobjects.SimpleGameWorld.java
ru.rs.gameobjects.SimpleObject.java
ru.rs.gameobjects.Unit.java
ru.rs.screens.CollisionScreen.java
ru.rs.screens.LoadingScreen.java
ru.rs.screens.MenuScreen.java