Overhaul the collision grid system.

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent ed96d43935
commit eabf5d6f7d
  1. 12
      src/sig/map/Map.java
  2. 34
      src/sig/objects/LevelRenderer.java
  3. 30
      src/sig/objects/Player.java

@ -45,7 +45,15 @@ public class Map {
while (stream.available()!=0) { while (stream.available()!=0) {
switch (readingData) { switch (readingData) {
case MAP_DATA: case MAP_DATA:
newMap.tiles[marker++]=stream.readChar(); char tileValue = stream.readChar();
newMap.tiles[marker++]=tileValue;
int ypos=marker/Map.MAP_WIDTH;
int xpos=marker%Map.MAP_WIDTH;
for (int y=0;y<Tile.TILE_HEIGHT;y++) {
for (int x=0;x<Tile.TILE_WIDTH;x++) {
RabiClone.COLLISION[(ypos*Tile.TILE_HEIGHT)*(Map.MAP_WIDTH*Tile.TILE_WIDTH)+xpos*Tile.TILE_WIDTH]=Tile.values()[tileValue].getCollision()==CollisionType.SOLID;
}
}
break; break;
case VIEW_DATA: case VIEW_DATA:
newMap.views[marker++]=stream.readByte(); newMap.views[marker++]=stream.readByte();
@ -106,7 +114,7 @@ public class Map {
if (prevIsCollisionTile!=newIsCollisionTile) { if (prevIsCollisionTile!=newIsCollisionTile) {
for (int yy=0;yy<Tile.TILE_HEIGHT;yy++) { for (int yy=0;yy<Tile.TILE_HEIGHT;yy++) {
for (int xx=0;xx<Tile.TILE_WIDTH;xx++) { for (int xx=0;xx<Tile.TILE_WIDTH;xx++) {
RabiClone.COLLISION[(y*Tile.TILE_HEIGHT*Map.MAP_HEIGHT+yy)*Map.MAP_WIDTH+(x*Tile.TILE_WIDTH*Map.MAP_WIDTH+xx)]=newIsCollisionTile; RabiClone.COLLISION[(y*Tile.TILE_HEIGHT+yy)*(Map.MAP_WIDTH*Tile.TILE_WIDTH)+(x*Tile.TILE_WIDTH+xx)]=newIsCollisionTile;
} }
} }
} }

@ -13,7 +13,6 @@ import sig.engine.objects.AnimatedObject;
import sig.engine.objects.Object; import sig.engine.objects.Object;
import sig.engine.String; import sig.engine.String;
import sig.map.Background; import sig.map.Background;
import sig.map.CollisionType;
import sig.map.Map; import sig.map.Map;
import sig.map.Tile; import sig.map.Tile;
@ -27,35 +26,6 @@ public class LevelRenderer extends Object{
@Override @Override
public void update(double updateMult) { public void update(double updateMult) {
Arrays.fill(RabiClone.COLLISION, false); Arrays.fill(RabiClone.COLLISION, false);
CreateCollisionGrid();
}
private void CreateCollisionGrid() {
for (int y=(int)(this.getY()/Tile.TILE_HEIGHT);y<(int)(RabiClone.BASE_HEIGHT/Tile.TILE_HEIGHT+this.getY()/Tile.TILE_HEIGHT+1);y++) {
if (y<0||y>Map.MAP_HEIGHT) {
continue;
}
for (int x=(int)(0+this.getX()/Tile.TILE_WIDTH);x<(int)(RabiClone.BASE_WIDTH/Tile.TILE_WIDTH+this.getX()/Tile.TILE_WIDTH+1);x++) {
if (x<0||x>Map.MAP_WIDTH) {
continue;
}
if (RabiClone.CURRENT_MAP.getTile(x,y).getCollision()==CollisionType.BLOCK||RabiClone.CURRENT_MAP.getTile(x,y).getCollision()==CollisionType.SLOPE) {
byte[] spritesheet = Sprite.TILE_SHEET.getBi_array();
int tileX = RabiClone.CURRENT_MAP.getTile(x,y).getSpriteSheetX()*Tile.TILE_WIDTH;
int tileY = RabiClone.CURRENT_MAP.getTile(x,y).getSpriteSheetY()*Tile.TILE_HEIGHT;
for (int yy=0;yy<Tile.TILE_HEIGHT;yy++) {
for (int xx=0;xx<Tile.TILE_WIDTH;xx++) {
if (spritesheet[(tileY+yy)*Sprite.TILE_SHEET.getCanvasWidth()+(tileX+xx)]!=(byte)32) {
int xpos=(int)(x*Tile.TILE_WIDTH-this.getX()+xx),ypos=(int)(y*Tile.TILE_HEIGHT-this.getY()+yy);
if (xpos>=0&&xpos<RabiClone.BASE_WIDTH&&ypos>=0&&ypos<RabiClone.BASE_HEIGHT) {
RabiClone.COLLISION[ypos*RabiClone.BASE_WIDTH+xpos]=true;
}
}
}
}
}
}
}
} }
@Override @Override
@ -79,13 +49,13 @@ public class LevelRenderer extends Object{
Draw_Text(4,4,new String(RabiClone.player.y_velocity),Font.PROFONT_12); Draw_Text(4,4,new String(RabiClone.player.y_velocity),Font.PROFONT_12);
Draw_Text(4,4+Font.PROFONT_12.getGlyphHeight(),new String(RabiClone.player.slide_time3),Font.PROFONT_12); Draw_Text(4,4+Font.PROFONT_12.getGlyphHeight(),new String(RabiClone.player.slide_time3),Font.PROFONT_12);
} }
//RenderCollisionGrid(p); RenderCollisionGrid(p);
} }
private void RenderCollisionGrid(byte[] p) { private void RenderCollisionGrid(byte[] p) {
for (int y=0;y<RabiClone.BASE_HEIGHT;y++) { for (int y=0;y<RabiClone.BASE_HEIGHT;y++) {
for (int x=0;x<RabiClone.BASE_WIDTH;x++) { for (int x=0;x<RabiClone.BASE_WIDTH;x++) {
if (RabiClone.COLLISION[y*RabiClone.BASE_WIDTH+x]) { if (RabiClone.COLLISION[((int)getY()+y)*RabiClone.BASE_WIDTH+(x+(int)getX())]) {
p[y*RabiClone.BASE_WIDTH+x]=(byte)PaletteColor.CRIMSON.ordinal(); p[y*RabiClone.BASE_WIDTH+x]=(byte)PaletteColor.CRIMSON.ordinal();
} }
} }

@ -7,7 +7,6 @@ import sig.engine.Rectangle;
import sig.engine.Sprite; import sig.engine.Sprite;
import sig.engine.objects.AnimatedObject; import sig.engine.objects.AnimatedObject;
import sig.engine.objects.CollisionEntity; import sig.engine.objects.CollisionEntity;
import sig.map.CollisionType;
import sig.map.Map; import sig.map.Map;
import sig.map.Tile; import sig.map.Tile;
import sig.map.View; import sig.map.View;
@ -369,8 +368,8 @@ public class Player extends AnimatedObject implements CollisionEntity {
if (x==getX()) { if (x==getX()) {
continue; continue;
} }
if (checkCollision(x-RabiClone.level_renderer.getX()+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()))|| if (checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()))||
checkCollision(x-RabiClone.level_renderer.getX()+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()))) { checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()))) {
x_acceleration = 0; x_acceleration = 0;
x_velocity = 0.000001; x_velocity = 0.000001;
sideCollision=true; sideCollision=true;
@ -384,8 +383,8 @@ public class Player extends AnimatedObject implements CollisionEntity {
if (x==getX()) { if (x==getX()) {
continue; continue;
} }
if (checkCollision((x-RabiClone.level_renderer.getX()+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY2())|| if (checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2())||
checkCollision((x-RabiClone.level_renderer.getX()+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY())) { checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY())) {
x_acceleration = 0; x_acceleration = 0;
x_velocity = -0.000001; x_velocity = -0.000001;
sideCollision=true; sideCollision=true;
@ -395,8 +394,8 @@ public class Player extends AnimatedObject implements CollisionEntity {
} }
} }
if (y_velocity==0) { if (y_velocity==0) {
if (!(checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX2(),getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1)|| if (!(checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1)||
checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX(),getY()-RabiClone.level_renderer.getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1))) { checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1))) {
groundCollision=false; groundCollision=false;
} else { } else {
groundCollision=true; groundCollision=true;
@ -410,8 +409,8 @@ public class Player extends AnimatedObject implements CollisionEntity {
if (y==getY()) { if (y==getY()) {
continue; continue;
} }
if (checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y-RabiClone.level_renderer.getY()+getCollisionBox().getY2()-getSprite().getHeight()/2)|| if (checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y+getCollisionBox().getY2()-getSprite().getHeight()/2)||
checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y-RabiClone.level_renderer.getY()+getCollisionBox().getY2()-getSprite().getHeight()/2)) { checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y+getCollisionBox().getY2()-getSprite().getHeight()/2)) {
setY(y-0.1); setY(y-0.1);
//System.out.println("Running"+System.currentTimeMillis()); //System.out.println("Running"+System.currentTimeMillis());
y_acceleration = 0; y_acceleration = 0;
@ -428,8 +427,8 @@ public class Player extends AnimatedObject implements CollisionEntity {
if (y==getY()) { if (y==getY()) {
continue; continue;
} }
if (checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y-RabiClone.level_renderer.getY()+getCollisionBox().getY()-getSprite().getHeight()/2)|| if (checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y+getCollisionBox().getY()-getSprite().getHeight()/2)||
checkCollision(getX()-RabiClone.level_renderer.getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y-RabiClone.level_renderer.getY()+getCollisionBox().getY()-getSprite().getHeight()/2)) { checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y+getCollisionBox().getY()-getSprite().getHeight()/2)) {
setY(y+1); setY(y+1);
y_acceleration = 0; y_acceleration = 0;
y_velocity = 0; y_velocity = 0;
@ -457,6 +456,15 @@ public class Player extends AnimatedObject implements CollisionEntity {
} }
} }
private boolean checkCollision(double x,double y) {
int index = (int)y*RabiClone.BASE_WIDTH+(int)x;
if (index>=0&&index<RabiClone.COLLISION.length) {
return RabiClone.COLLISION[index];
} else {
return false;
}
}
private void handleKeyboardMovement(double updateMult, int movement, double friction, double drag) { private void handleKeyboardMovement(double updateMult, int movement, double friction, double drag) {
if (movement != 0 && Math.abs(x_velocity) < WALKING_SPEED_LIMIT) { if (movement != 0 && Math.abs(x_velocity) < WALKING_SPEED_LIMIT) {
x_acceleration = drag * (movement); x_acceleration = drag * (movement);

Loading…
Cancel
Save