Block gravity and collision handling.

concurrentModificationException1
Joshua Sigona 3 years ago
parent 24e26b852c
commit b2d2701c9a
  1. 8
      src/sig/BlockClump.java
  2. 66
      src/sig/Board.java
  3. 4
      src/sig/Meteo.java

@ -1,6 +1,7 @@
package sig;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -9,7 +10,11 @@ public class BlockClump {
double x,y; //the lower-left origin of this block clump. Every block positions relative to this.
double yspd;
int[][] collisionColumnRanges;
boolean launched=false; //Blocks do not fall when landing (by gravity).
boolean re_sort=false; //Set to true when block clumps are divided into smaller columns for re-sorting.
public BlockClump(List<Block> blockList, double x, double y, double startspd, int width) {
this.blocks = new ArrayList<Block>();
this.blocks.addAll(blockList);
collisionColumnRanges = new int[width][];
for (int i=0;i<width;i++) {
@ -20,6 +25,7 @@ public class BlockClump {
this.x=x;
this.y=y;
this.yspd=startspd;
System.out.println(Arrays.deepToString(collisionColumnRanges));
}
public void updateBlockCollision() {
//Call this whenever the block structure changes. This will define what the top and bottom positions
@ -44,7 +50,7 @@ public class BlockClump {
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;
}

@ -3,6 +3,7 @@ package sig;
import java.awt.Graphics;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Board {
@ -34,25 +35,68 @@ public class Board {
List<Block> initialBlocks = new ArrayList<Block>();
for (int x=0;x<boardWidth;x++) {
for (int y=0;y<3;y++) {
initialBlocks.add(new Block(x,y));
for (int y=0;y<(int)(Math.random()*12);y++) {
initialBlocks.add(new Block(x,y));
}
}
BlockClump defaultClump = new BlockClump(initialBlocks,0,260,0,width);
List<Block> initialBlocks2 = new ArrayList<Block>();
for (int x=0;x<boardWidth;x++) {
for (int y=0;y<(int)(Math.random()*12);y++) {
initialBlocks2.add(new Block(x,y));
}
}
BlockClump defaultClump2 = new BlockClump(initialBlocks2,0,540,0,width);
blockData.add(defaultClump);
blockData.add(defaultClump2);
}
public void run() {
public void run(long frames) {
if (frames%100==0) {
blockData.add(new BlockClump(Arrays.asList(new Block((int)(Math.random()*width),0)),0,590,0,width));
}
for (BlockClump blocks : blockData) {
if (blocks.y+blocks.yspd+gravity>0) {
blocks.yspd=Math.max(blocks.yspd+gravity,max_fall_spd);
blocks.y+=blocks.yspd;
} else {
//We have hit the bottom.
blocks.yspd=0;
blocks.y=0;
}
double FUTURE_FALL_POSITION = blocks.y+blocks.yspd+gravity;
boolean landed=false;
outerloop:
for (int x=0;x<width;x++) {
if (blocks.collisionColumnRanges[x][0]!=-1) {
for (BlockClump blocks2 : blockData) {
if (!blocks.equals(blocks2)&&blocks2.collisionColumnRanges[x][1]!=-1) {
if (FUTURE_FALL_POSITION<blocks2.y) {
if (FUTURE_FALL_POSITION+blocks.collisionColumnRanges[x][1]*block_height>blocks2.y+(blocks2.collisionColumnRanges[x][0]+1)*block_height) {
blocks.yspd=0;
blocks.y=blocks2.y+(blocks2.collisionColumnRanges[x][0]+1)*block_height;
landed=true;
break outerloop;
}
} else {
if (FUTURE_FALL_POSITION+blocks.collisionColumnRanges[x][0]*block_height<blocks2.y+(blocks2.collisionColumnRanges[x][1]+1)*block_height) {
blocks.yspd=0;
blocks.y=blocks2.y+(blocks2.collisionColumnRanges[x][1]+1)*block_height;
landed=true;
break outerloop;
}
}
}
}
}
}
if (!landed) {
if (FUTURE_FALL_POSITION>0) {
blocks.yspd=Math.max(blocks.yspd+gravity,max_fall_spd);
blocks.y+=blocks.yspd;
} else {
//We have hit the bottom.
blocks.yspd=0;
blocks.y=0;
}
}
//System.out.println(blocks.y);
}
}
public void drawBoard(Graphics g) {

@ -15,12 +15,12 @@ public class Meteo {
public static void runGameLoop() {
FRAMECOUNT++;
b.run();
b.run(FRAMECOUNT);
}
public static void main(String[] args) {
double[] val = {0,0,};
b = new Board(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,16,16,8,14,-0.065,1,4,-2,val);
b = new Board(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,24,24,8,14,-0.065,1,4,-2,val);
JFrame f = new JFrame("Meteo Engine");
Panel p = new Panel();

Loading…
Cancel
Save