Package nl.saxion.gameapp.gameobject
Class GameObject
java.lang.Object
nl.saxion.gameapp.gameobject.GameObject
- Direct Known Subclasses:
RotatableGameObject
Represents a generic game object in a 2D world.
Each game object has a position (x, y), a size (width, height), and can have multiple hitboxes of different shapes (rectangles or circles). Hitboxes are defined relative to the GameObject's position.
Subclasses must implement the render(float) method to define
how the object is drawn on the screen.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected floatThe height of the object.The list of hitboxes attached to this object.protected floatThe width of the object.protected floatThe x-coordinate of the object.protected floatThe y-coordinate of the object. -
Constructor Summary
ConstructorsConstructorDescriptionGameObject(float x, float y, float width, float height) Creates a new game object with the specified position and size. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCircleHitbox(float relCenterX, float relCenterY, float relRadius) Adds a circular hitbox defined in relative percentages.voidaddHitbox(float relOffsetX, float relOffsetY, float relWidth, float relHeight) Adds a rectangular hitbox defined in relative percentages (0–1).voidAdds a custom hitbox to this GameObject.voidaddPolygonHitbox(float[] relVertices) Adds a polygon hitbox defined in relative percentages.voidcenterOn(float centerX, float centerY) Centers the object on the specified coordinates.booleancollidesWith(GameObject other) Checks if this GameObject collides with another GameObject, considering all hitboxes.booleancontains(com.badlogic.gdx.math.Vector2 point) Checks if a given point is inside this GameObject's bounding rectangle.com.badlogic.gdx.math.Vector2directionTo(GameObject other) Returns a normalized direction vector from this GameObject to another.floatdistanceTo(GameObject other) Calculates the Euclidean distance from this GameObject to another.voiddrawHitboxes(String color) Draws all hitboxes of this GameObject using the given color.com.badlogic.gdx.math.Vector2Returns the bottom-left corner of the GameObject as a Vector2.com.badlogic.gdx.math.Vector2Returns the bottom-right corner of the GameObject as a Vector2.com.badlogic.gdx.math.RectangleReturns the bounding rectangle of the GameObject.com.badlogic.gdx.math.Vector2Returns the center point of the GameObject.floatReturns the center x-coordinate of the GameObject.floatReturns the center y-coordinate of the GameObject.floatReturns the list of all hitboxes attached to this GameObject.com.badlogic.gdx.math.Vector2Returns the top-left corner of the GameObject as a Vector2.com.badlogic.gdx.math.Vector2Returns the top-right corner of the GameObject as a Vector2.floatgetWidth()floatgetX()floatgetY()voidmoveBy(float dx, float dy) Moves the object by the specified offsets.voidmoveTo(float newX, float newY) Moves the object to the specified coordinates.abstract voidrender(float delta) Renders the GameObject.voidsetHeight(float height) Sets the height of the object.voidsetWidth(float width) Sets the width of the object.voidsetX(float x) Sets the x-coordinate of the object.voidsetY(float y) Sets the y-coordinate of the object.
-
Field Details
-
x
protected float xThe x-coordinate of the object. -
y
protected float yThe y-coordinate of the object. -
width
protected float widthThe width of the object. -
height
protected float heightThe height of the object. -
hitboxes
The list of hitboxes attached to this object.
-
-
Constructor Details
-
GameObject
public GameObject(float x, float y, float width, float height) Creates a new game object with the specified position and size.- Parameters:
x- initial x-coordinatey- initial y-coordinatewidth- width of the objectheight- height of the object
-
-
Method Details
-
addHitbox
Adds a custom hitbox to this GameObject.- Parameters:
hitbox- the hitbox to add
-
addHitbox
public void addHitbox(float relOffsetX, float relOffsetY, float relWidth, float relHeight) Adds a rectangular hitbox defined in relative percentages (0–1).- Parameters:
relOffsetX- x-offset as percentage of widthrelOffsetY- y-offset as percentage of heightrelWidth- width as percentage of widthrelHeight- height as percentage of height
-
addCircleHitbox
public void addCircleHitbox(float relCenterX, float relCenterY, float relRadius) Adds a circular hitbox defined in relative percentages.- Parameters:
relCenterX- x-coordinate of center as percentage of widthrelCenterY- y-coordinate of center as percentage of heightrelRadius- radius as percentage of min(width, height)
-
addPolygonHitbox
public void addPolygonHitbox(float[] relVertices) Adds a polygon hitbox defined in relative percentages.- Parameters:
relVertices- vertices as percentages of width/height (x1,y1,x2,y2,...)
-
getHitboxes
Returns the list of all hitboxes attached to this GameObject.- Returns:
- a list of hitboxes
-
collidesWith
Checks if this GameObject collides with another GameObject, considering all hitboxes.- Parameters:
other- the other GameObject to check collision against- Returns:
- true if any hitboxes overlap, false otherwise
-
render
public abstract void render(float delta) Renders the GameObject.Subclasses must implement this method to define how the object is drawn to the screen.
- Parameters:
delta- time elapsed since the last frame
-
drawHitboxes
Draws all hitboxes of this GameObject using the given color.- Parameters:
color- a string representing the color (e.g., "red-500")
-
getX
public float getX()- Returns:
- the x-coordinate of the object
-
getY
public float getY()- Returns:
- the y-coordinate of the object
-
getWidth
public float getWidth()- Returns:
- the width of the object
-
getHeight
public float getHeight()- Returns:
- the height of the object
-
setX
public void setX(float x) Sets the x-coordinate of the object.- Parameters:
x- new x-coordinate
-
setY
public void setY(float y) Sets the y-coordinate of the object.- Parameters:
y- new y-coordinate
-
setWidth
public void setWidth(float width) Sets the width of the object.- Parameters:
width- new width
-
setHeight
public void setHeight(float height) Sets the height of the object.- Parameters:
height- new height
-
moveBy
public void moveBy(float dx, float dy) Moves the object by the specified offsets.- Parameters:
dx- offset in the x-directiondy- offset in the y-direction
-
moveTo
public void moveTo(float newX, float newY) Moves the object to the specified coordinates.- Parameters:
newX- new x-coordinatenewY- new y-coordinate
-
centerOn
public void centerOn(float centerX, float centerY) Centers the object on the specified coordinates.- Parameters:
centerX- x-coordinate of the centercenterY- y-coordinate of the center
-
getBounds
public com.badlogic.gdx.math.Rectangle getBounds()Returns the bounding rectangle of the GameObject.- Returns:
- rectangle representing the object's bounds
-
getCenterPoint
public com.badlogic.gdx.math.Vector2 getCenterPoint()Returns the center point of the GameObject.- Returns:
- center point as a Vector2
-
distanceTo
Calculates the Euclidean distance from this GameObject to another.- Parameters:
other- the other GameObject- Returns:
- the distance between the centers of the two objects
-
directionTo
Returns a normalized direction vector from this GameObject to another.- Parameters:
other- the target GameObject- Returns:
- a unit vector pointing from this object to the other
-
getCenterX
public float getCenterX()Returns the center x-coordinate of the GameObject.- Returns:
- x-coordinate of the center
-
getCenterY
public float getCenterY()Returns the center y-coordinate of the GameObject.- Returns:
- y-coordinate of the center
-
getBottomLeft
public com.badlogic.gdx.math.Vector2 getBottomLeft()Returns the bottom-left corner of the GameObject as a Vector2.- Returns:
- bottom-left corner position
-
getBottomRight
public com.badlogic.gdx.math.Vector2 getBottomRight()Returns the bottom-right corner of the GameObject as a Vector2.- Returns:
- bottom-right corner position
-
getTopLeft
public com.badlogic.gdx.math.Vector2 getTopLeft()Returns the top-left corner of the GameObject as a Vector2.- Returns:
- top-left corner position
-
getTopRight
public com.badlogic.gdx.math.Vector2 getTopRight()Returns the top-right corner of the GameObject as a Vector2.- Returns:
- top-right corner position
-
contains
public boolean contains(com.badlogic.gdx.math.Vector2 point) Checks if a given point is inside this GameObject's bounding rectangle.- Parameters:
point- the point to test- Returns:
- true if the point is inside, false otherwise
-