Class Laser

java.lang.Object
core.GameObject
entities.bullets.Laser

public class Laser extends GameObject
Represents a laser beam in the game. The laser has a direction, length, damage, and limited lifetime. It is rendered as a thick line and has a rectangular hitbox for collision detection.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether the laser is still active/alive.
    private int
    Damage dealt by the laser.
    private double
    Direction vector of the laser (normalized).
    private double
    Direction vector of the laser (normalized).
    private double
    Length of the laser in pixels.
    private double
    Time remaining before the laser disappears (in seconds).

    Fields inherited from class core.GameObject

    x, y
  • Constructor Summary

    Constructors
    Constructor
    Description
    Laser(double x, double y, double dirX, double dirY, int damage)
    Constructs a new Laser.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the damage/power of the laser.
    javafx.geometry.Rectangle2D
    Returns the rectangular hitbox of the laser for collision detection.
    boolean
    Checks if the laser is still active.
    void
    render(javafx.scene.canvas.GraphicsContext gc)
    Renders the laser as a red line on the screen.
    void
    update(double dt)
    Updates the laser's lifetime.

    Methods inherited from class core.GameObject

    getX, getY, setX, setY

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • dirX

      private double dirX
      Direction vector of the laser (normalized).
    • dirY

      private double dirY
      Direction vector of the laser (normalized).
    • length

      private double length
      Length of the laser in pixels.
    • damage

      private int damage
      Damage dealt by the laser.
    • alive

      private boolean alive
      Whether the laser is still active/alive.
    • lifetime

      private double lifetime
      Time remaining before the laser disappears (in seconds).
  • Constructor Details

    • Laser

      public Laser(double x, double y, double dirX, double dirY, int damage)
      Constructs a new Laser.
      Parameters:
      x - Starting X position of the laser.
      y - Starting Y position of the laser.
      dirX - X component of the laser's direction vector.
      dirY - Y component of the laser's direction vector.
      damage - Damage dealt by the laser.
  • Method Details

    • update

      public void update(double dt)
      Updates the laser's lifetime. Marks it as inactive when lifetime expires.
      Specified by:
      update in class GameObject
      Parameters:
      dt - Time elapsed since last update (seconds).
    • isAlive

      public boolean isAlive()
      Checks if the laser is still active.
      Returns:
      true if the laser is alive/active, false otherwise.
    • render

      public void render(javafx.scene.canvas.GraphicsContext gc)
      Renders the laser as a red line on the screen.
      Specified by:
      render in class GameObject
      Parameters:
      gc - GraphicsContext used for drawing.
    • getHitbox

      public javafx.geometry.Rectangle2D getHitbox()
      Returns the rectangular hitbox of the laser for collision detection.
      Returns:
      Rectangle2D representing the laser's hitbox.
    • getDamage

      public int getDamage()
      Returns the damage/power of the laser.
      Returns:
      Damage value.