Skip to content

Commit

Permalink
FIX: Implement try catch blocks to handle ArrayIndexOutOfBoundsException
Browse files Browse the repository at this point in the history
I notice that the player doesn't slope go off the solid tiles like you would expect

https://www.w3schools.com/java/java_try_catch.asp
  • Loading branch information
SonicGDX committed Dec 29, 2022
1 parent 0e33fcc commit 8b75ef7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion SOURCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ https://www.geeksforgeeks.org/singleton-class-java \
https://en.wikipedia.org/wiki/Singleton_pattern \
https://stackoverflow.com/a/12697861 \
https://www.w3schools.com/java/java_enums.asp \
https://www.baeldung.com/a-guide-to-java-enums
https://www.baeldung.com/a-guide-to-java-enums \
https://www.w3schools.com/java/java_try_catch.asp

## Graphics
https://libgdx.com/wiki/graphics/2d/spritebatch-textureregions-and-sprites \
https://libgdx.com/wiki/articles/coordinate-systems#game-coordinates \
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/sonicgdx/sonicswirl/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SensorReturn downSensorCheck(float xPosition, float yPosition) //TODO imp

//Gdx.app.debug("gridValue", String.valueOf(TileMap.map[chunkX][chunkY][tileX][tileY].height[grid]));

height = TileMap.map[chunkX][chunkY][tileX][tileY].getHeight(grid);
height = TileMap.getTile(chunkX,chunkY,tileX,tileY).getHeight(grid);

distance = ((chunkY * 128) + (tileY * 16) + height) - yPosition;

Expand All @@ -74,7 +74,7 @@ public SensorReturn downSensorCheck(float xPosition, float yPosition) //TODO imp
tempTileY = 0;
}

height = TileMap.map[chunkX][tempChunkY][tileX][tempTileY].getHeight(grid);
height = TileMap.getTile(chunkX,tempChunkY,tileX,tempTileY).getHeight(grid);
if (height > 0) //TODO outline conditions in comment
{
chunkY = tempChunkY;
Expand All @@ -99,7 +99,7 @@ else if (height == 0)
tileY--;
}

height = TileMap.map[chunkX][chunkY][tileX][tileY].getHeight(grid);
height = TileMap.getTile(chunkX,chunkY,tileX,tileY).getHeight(grid);

if (height == 0)
{
Expand All @@ -117,7 +117,7 @@ else if (height == 0)

// Classes are reference types so modifying a value would affect all the tiles that are the same.

return new SensorReturn(TileMap.map[chunkX][chunkY][tileX][tileY],distance);
return new SensorReturn(TileMap.getTile(chunkX,chunkY,tileX,tileY),distance);
}

/*
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/sonicgdx/sonicswirl/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ else if (Gdx.input.isKeyPressed(Input.Keys.A) || (Gdx.input.isKeyPressed(Input.K
}

// "Invisible walls" - prevent players from going beyond borders to simplify calculations. TODO stop collision errors when going outside index bounds
xPos = Math.min(xPos,1750);
/*xPos = Math.min(xPos,1750);
xPos = Math.max(xPos,0);
yPos = Math.max(yPos,0);
yPos = Math.max(yPos,0);*/

sprite.setPosition(xPos, yPos);

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/sonicgdx/sonicswirl/SonicGDX.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public void render(float delta) {

//frameLog.log();


TileMap.TILE_MAP.getTile(122,122,122,122);

ScreenUtils.clear(Color.DARK_GRAY); // clears the screen and sets the background to a certain colour

player.move(delta);
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/com/sonicgdx/sonicswirl/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public class Tile {
public byte getHeight(int block)
{
if (empty) return 0;
else return height[block];
try{
return height[block];
}
catch(Exception e){
//Gdx.app.error("getHeight() error", String.valueOf(e));
return 0;
}

}

public byte getWidth(int block)
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/com/sonicgdx/sonicswirl/TileMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ public byte getWidth(int chunkX, int chunkY, int tileX, int tileY, int block)
else return map[chunkX][chunkY][tileX][tileY].width[block];
}

@Deprecated
public Tile getTile(int chunkX, int chunkY, int tileX, int tileY)
public static Tile getTile(int chunkX, int chunkY, int tileX, int tileY)
{
return map[chunkX][chunkY][tileX][tileY];
try {
return map[chunkX][chunkY][tileX][tileY];
}
catch (Exception e){
//Gdx.app.error("getTile() Error",String.valueOf(e));
return TILE_MAP.EMPTY;
}

}

@Deprecated
Expand Down

0 comments on commit 8b75ef7

Please sign in to comment.