Setup column collision ranges for collision checking.
This commit is contained in:
parent
7c70ccb76a
commit
24e26b852c
BIN
Meteo_Engine.jar
BIN
Meteo_Engine.jar
Binary file not shown.
@ -1,25 +1,57 @@
|
||||
package sig;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockClump {
|
||||
List<Block> blocks;
|
||||
private List<Block> blocks;
|
||||
double x,y; //the lower-left origin of this block clump. Every block positions relative to this.
|
||||
double yspd;
|
||||
public BlockClump(List<Block> blockList, double x, double y, double startspd) {
|
||||
this.blocks = blockList;
|
||||
int[][] collisionColumnRanges;
|
||||
public BlockClump(List<Block> blockList, double x, double y, double startspd, int width) {
|
||||
collisionColumnRanges = new int[width][];
|
||||
|
||||
for (int i=0;i<width;i++) {
|
||||
collisionColumnRanges[i] = new int[]{-1,-1};
|
||||
}
|
||||
|
||||
addBlock(blockList.toArray(new Block[blockList.size()]));
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
this.yspd=startspd;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlockClump [blocks=" + blocks + ", x=" + x + ", y=" + y + ", yspd=" + yspd + "]";
|
||||
public void updateBlockCollision() {
|
||||
//Call this whenever the block structure changes. This will define what the top and bottom positions
|
||||
//of each vertical column are for faster collision checking.
|
||||
collisionColumnRanges = new int[collisionColumnRanges.length][];
|
||||
|
||||
for (int i=0;i<collisionColumnRanges.length;i++) {
|
||||
collisionColumnRanges[i] = new int[]{-1,-1};
|
||||
}
|
||||
for (Block b : blocks) {updateBlockCollisionRangeWithBlock(b);}
|
||||
}
|
||||
public void addBlock(Block...blocks) {
|
||||
//Adds the block to the strucutre. Update collision column ranges to reflect the new bounds.
|
||||
for (Block b : blocks) {updateBlockCollisionRangeWithBlock(b);}
|
||||
}
|
||||
public void drawBlocks(Graphics g, int originX, int originY, int block_width, int block_height) {
|
||||
for (Block b : blocks) {
|
||||
b.draw(g,originX+x*block_width,originY-y,block_width,block_height);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBlockCollisionRangeWithBlock(Block b) {
|
||||
if (collisionColumnRanges[b.x][0]==-1||collisionColumnRanges[b.x][0]>b.y) {
|
||||
collisionColumnRanges[b.x][0]=b.y;
|
||||
} else
|
||||
if (collisionColumnRanges[b.x][1]==-1||collisionColumnRanges[b.x][1]<b.y) {
|
||||
collisionColumnRanges[b.x][1]=b.y;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlockClump [blocks=" + blocks + ", x=" + x + ", y=" + y + ", yspd=" + yspd + "]";
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class Board {
|
||||
}
|
||||
}
|
||||
|
||||
BlockClump defaultClump = new BlockClump(initialBlocks,0,260,0);
|
||||
BlockClump defaultClump = new BlockClump(initialBlocks,0,260,0,width);
|
||||
|
||||
blockData.add(defaultClump);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user