Skip to content

Commit

Permalink
Add doc strings & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jul 22, 2024
1 parent 1f5d4be commit 485e626
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 75 deletions.
103 changes: 59 additions & 44 deletions src/games/stendhal/common/CollisionDetection.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003 - Marauroa *
* (C) Copyright 2003-2024 - Marauroa *
***************************************************************************
***************************************************************************
* *
Expand All @@ -12,7 +12,6 @@
***************************************************************************/
package games.stendhal.common;


import java.awt.geom.Rectangle2D;

import games.stendhal.common.tiled.LayerDefinition;
Expand All @@ -22,10 +21,10 @@
* not with any of the non trespasable areas of the world.
*/
public class CollisionDetection {

private CollisionMap map;

private int width;

private int height;

/**
Expand All @@ -40,8 +39,10 @@ public void clear() {
/**
* Initialize the collision map to desired size.
*
* @param width width of the map
* @param height height of the map
* @param width
* Width of the map.
* @param height
* Height of the map.
*/
public void init(final int width, final int height) {
if (this.width != width || this.height != height) {
Expand All @@ -59,8 +60,10 @@ public void init(final int width, final int height) {
/**
* Set a position in the collision map to static collision.
*
* @param x x coordinate
* @param y y coordinate
* @param x
* X coordinate.
* @param y
* Y coordinate.
*/
public void setCollide(final int x, final int y) {
if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {
Expand All @@ -72,7 +75,8 @@ public void setCollide(final int x, final int y) {
/**
* Fill the collision map from layer data.
*
* @param collisionLayer static collision information
* @param collisionLayer
* Static collision information.
*/
public void setCollisionData(final LayerDefinition collisionLayer) {
// First we build the int array.
Expand All @@ -83,7 +87,7 @@ public void setCollisionData(final LayerDefinition collisionLayer) {
for (int x = 0; x < width; x++) {
/*
* NOTE: Right now our collision detection system is binary, so
* something or is blocked or is not.
* something is blocked or is not.
*/
if (collisionLayer.getTileAt(x, y) != 0) {
map.set(x, y);
Expand Down Expand Up @@ -119,9 +123,10 @@ public void printaround(final int x, final int y, final int size) {
/**
* Check if a rectangle is at least partially outside the map.
*
* @param shape area to be checked
* @return <code>true</code> if shape is at least partially outside the map,
* <code>false</code> otherwise
* @param shape
* Area to be checked.
* @return
* {@code true} if shape is at least partially outside the map, {@code false} otherwise.
*/
public boolean leavesZone(final Rectangle2D shape) {
final double x = shape.getX();
Expand All @@ -132,12 +137,34 @@ public boolean leavesZone(final Rectangle2D shape) {
return (x < 0) || (x + w > width) || (y < 0) || (y + h > height);
}

/**
* Get the width of the collision map.
*
* @return
* Map width.
*/
public int getWidth() {
return width;
}

/**
* Get the height of the collision map.
*
* @return
* Map height.
*/
public int getHeight() {
return height;
}

/**
* Check if a rectangle overlaps colliding areas.
*
* @param shape checked area
* @return <code>true</code> if the shape enters in any of the non
* trespassable areas of the map, <code>false</code> otherwise
* @param shape
* Checked Area
* @return
* {@codetrue} if the shape enters in any of the non-trespassable areas of the map,
* {@code false} otherwise.
*/
public boolean collides(final Rectangle2D shape) {
final double x = shape.getX();
Expand All @@ -147,16 +174,20 @@ public boolean collides(final Rectangle2D shape) {
return collides(x, y, w, h);
}


/**
* Check if a rectangle overlaps colliding areas.
*
* @param x x-position
* @param y y-position
* @param w width
* @param h height
* @return <code>true</code> if the shape enters in any of the non
* trespassable areas of the map, <code>false</code> otherwise
* @param x
* Rectangle X position.
* @param y
* Rectangle Y position.
* @param w
* Rectangle width.
* @param h
* Rectangle height.
* @return
* {@code true} if the shape enters in any of the non-trespassable areas of the map,
* {@code false} otherwise.
*/
public boolean collides(final double x, final double y, final double w, final double h) {
/*
Expand All @@ -178,10 +209,12 @@ public boolean collides(final double x, final double y, final double w, final do
/**
* Check if a location is marked with collision.
*
* @param x x coordinate
* @param y y coordinate
* @return <code>true</code> if the map position is a collision tile,
* otherwise <code>false</code>
* @param x
* X coordinate.
* @param y
* Y coordinate.
* @return
* {@code true} if the map position is a collision tile, otherwise {@code false}.
*/
public boolean collides(final int x, final int y) {
if ((x < 0) || (x >= width)) {
Expand All @@ -193,22 +226,4 @@ public boolean collides(final int x, final int y) {
}
return map.get(x, y);
}

/**
* Get the width of the collision map.
*
* @return width
*/
public int getWidth() {
return width;
}

/**
* Get the height of the collision map.
*
* @return height
*/
public int getHeight() {
return height;
}
}
126 changes: 100 additions & 26 deletions src/games/stendhal/common/CollisionMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2023 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand All @@ -19,12 +19,24 @@

import games.stendhal.common.tiled.LayerDefinition;


/**
* A map containing collision information.
*/
public class CollisionMap {

private final int width;
private final int height;
private final BitSet[] colls;

/**
* Creates a new empty collision map.
*
* @param width
* Node width of new map.
* @param height.
* Node height of new map.
*/
public CollisionMap(final int width, final int height) {
this.width = width;
this.height = height;
Expand All @@ -35,6 +47,12 @@ public CollisionMap(final int width, final int height) {

}

/**
* Creates a new collision map using layers definition.
*
* @param layer
* Definition used to create map.
*/
public CollisionMap(final LayerDefinition layer) {
this(layer.getWidth(), layer.getHeight());
for (int x = 0; x < width; x++) {
Expand All @@ -46,23 +64,94 @@ public CollisionMap(final LayerDefinition layer) {
}
}

/**
* Retrieves width of collision map.
*/
public int getWidth() {
return width;
}

/**
* Retrieves height of collision map.
*/
public int getHeight() {
return height;
}

/**
* Checks if node is a collision tile.
*
* @param i
* Node X coordinate.
* @param j
* Node Y coordinate.
* @return
* {@code true} if node has collision.
*/
public boolean get(final int i, final int j) {
return colls[i].get(j);
}

/**
* Sets a collision node.
*
* @param i
* Node X coordinate.
* @param j
* Node Y coordinate.
*/
public void set(final int i, final int j) {

colls[i].set(j);
}

/**
* Sets collision for a rectangle area.
*
* @param shape
* Area to be set.
*/
public void set(final Rectangle2D shape) {
int y = (int) shape.getY();
for (int x = (int) shape.getX(); x < shape.getX() + shape.getWidth(); x++) {
colls[x].set(y, (int) (y + shape.getHeight()));
}
}

/**
* Removes collision from a node.
*
* @param i
* Node X coordinate.
* @param k
* Node Y coordinate.
*/
public void unset(final int i, final int k) {
colls[i].clear(k);
}

/**
* Removes all collision from the map.
*/
public void clear() {
for (int i = 0; i < this.width; i++) {
colls[i].clear();
}
}

/**
* Checks for collision in a rectangle area.
*
* @param x
* Beginning node X coordinate.
* @param y
* Beginning node Y coordinate.
* @param width
* Width of area to be checked.
* @param height
* Height of area to be checked.
* @return
* {@code true} if collision is found in area.
*/
public boolean collides(final int x, final int y, final int width, final int height) {
if (x < 0 || x - 1 + width >= this.width) {
return true;
Expand All @@ -80,16 +169,16 @@ public boolean collides(final int x, final int y, final int width, final int hei
return !result.get(y, y + height).isEmpty();
}

public void clear() {
for (int i = 0; i < this.width; i++) {
colls[i].clear();
}

}
/**
* Creates a new collision map.
*
* @param layer
* Layer definition.
* @return
* Resulting collision map.
*/
public static CollisionMap create(final LayerDefinition layer) {

CollisionMap collissionMap = new CollisionMap(layer.getWidth(), layer
.getHeight());
CollisionMap collissionMap = new CollisionMap(layer.getWidth(), layer.getHeight());
for (int x = 0; x < layer.getWidth(); x++) {
for (int y = 0; y < layer.getHeight(); y++) {
if (layer.getTileAt(x, y) != 0) {
Expand All @@ -99,19 +188,4 @@ public static CollisionMap create(final LayerDefinition layer) {
}
return collissionMap;
}

public void unset(final int i, final int k) {
colls[i].clear(k);

}

public void set(final Rectangle2D shape) {
int y = (int) shape.getY();
for (int x = (int) shape.getX(); x < shape.getX() + shape.getWidth(); x++) {

colls[x].set(y, (int) (y + shape.getHeight()));
}

}

}
3 changes: 0 additions & 3 deletions src/games/stendhal/server/core/config/ZonesXMLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

//
//

import games.stendhal.common.tiled.LayerDefinition;
import games.stendhal.common.tiled.StendhalMapStructure;
import games.stendhal.server.core.config.zone.AttributesXMLReader;
Expand Down
Loading

0 comments on commit 485e626

Please sign in to comment.