Class EnemyModel

java.lang.Object
model.entities.EntityModel
model.entities.EnemyModel
Direct Known Subclasses:
MaitaModel, SkelMonstaModel, ZenChanModel

public abstract class EnemyModel extends EntityModel
Represents an enemy in the game, including its state, movement, and interactions with the player.

This abstract class provides the basic properties and methods for enemy entities, such as movement variables, state management, and interactions with the player. Specific enemy types should extend this class and implement the abstract methods.

  • Field Details

    • active

      protected boolean active
    • alive

      protected boolean alive
    • enemyState

      protected int enemyState
    • enemyType

    • xSpeed

      protected float xSpeed
    • ySpeed

      protected float ySpeed
    • fallSpeed

      protected float fallSpeed
    • flySpeed

      protected float flySpeed
    • walkSpeed

      protected float walkSpeed
    • walkingDir

      protected Constants.Direction walkingDir
    • previousWalkingDir

      protected Constants.Direction previousWalkingDir
    • startWalkingDir

      protected Constants.Direction startWalkingDir
    • goUp

      protected boolean goUp
    • goDown

      protected boolean goDown
    • isFalling

      protected boolean isFalling
    • isJumping

      protected boolean isJumping
    • spawnY

      protected float spawnY
    • reachedSpawn

      protected boolean reachedSpawn
    • playerTileX

      protected int playerTileX
    • playerTileY

      protected int playerTileY
    • updatePlayerPosMaxInterval

      protected int updatePlayerPosMaxInterval
  • Constructor Details

    • EnemyModel

      public EnemyModel(float x, float y, int width, int height, Constants.EnemyConstants.EnemyType enemyType, Constants.Direction startWalkingDir)
      Constructs an EnemyModel with the specified position, dimensions, enemy type, and initial walking direction.
      Parameters:
      x - the x-coordinate of the enemy's initial position
      y - the y-coordinate of the enemy's initial position
      width - the width of the enemy
      height - the height of the enemy
      enemyType - the type of the enemy
      startWalkingDir - the initial walking direction of the enemy
  • Method Details

    • update

      public abstract void update(PlayerModel playerModel)
      Updates the state of the enemy based on the player's position.

      This method is abstract and should be implemented by subclasses to define how the enemy's state and behavior are updated in response to the player's actions and position.

      Parameters:
      playerModel - the player model used to update the enemy's state
    • updateSpawning

      protected void updateSpawning()
      Updates the spawning state of the enemy.

      This method checks if the enemy has reached its spawn point. If the enemy has not reached the spawn point, it moves the enemy towards the spawn point and sets it to be immune. Once the enemy reaches the spawn point, it sets the enemy's position above the floor, marks it as having reached the spawn point, and removes its immunity.

    • updateStateVariables

      protected void updateStateVariables()
      Updates the state variables of the enemy based on its current state.

      This method adjusts the enemy's movement speeds and the interval for updating the player's position based on the enemy's current state. If the enemy is in the dead state, it sets the enemy to inactive.

    • isPlayerInViewingRange

      protected boolean isPlayerInViewingRange(PlayerModel playerModel)
      Checks if the player is within the enemy's viewing range.

      This method calculates the horizontal distance between the enemy and the player and checks if it is within the defined viewing range.

      Parameters:
      playerModel - the player model used to determine the player's position
      Returns:
      true if the player is within the viewing range, false otherwise
    • calculatePlayersPos

      protected void calculatePlayersPos(PlayerModel playerModel)
      Calculates the player's position in tile coordinates.

      This method converts the player's position from pixel coordinates to tile coordinates and updates the player's tile position variables.

      Parameters:
      playerModel - the player model used to determine the player's position
    • changeWalkingDir

      protected void changeWalkingDir()
      Changes the walking direction of the enemy to the opposite direction of the current direction.
    • flipX

      public int flipX()
      Returns the x-coordinate offset for flipping the enemy's sprite horizontally.
    • flipW

      public int flipW()
      Returns the width multiplier for flipping the enemy's sprite horizontally.
    • updateWalkingDir

      protected void updateWalkingDir()
      Updates the walking direction of the enemy based on the player's position.

      This method compares the player's tile position with the enemy's tile position and sets the walking direction of the enemy to left or right accordingly.

    • isPlayerLeftOrRight

      protected Constants.Direction isPlayerLeftOrRight(PlayerModel playerModel)
      Determines if the player is to the left or right of the enemy.

      This method compares the player's tile x-coordinate with the enemy's tile x-coordinate and returns the direction indicating whether the player is to the left or right of the enemy. If the player is on the same tile as the enemy, it returns NONE.

      Parameters:
      playerModel - the player model used to determine the player's position
      Returns:
      the direction indicating if the player is to the left or right of the enemy, or NONE if they are on the same tile
    • isPlayerUpOrDown

      protected Constants.Direction isPlayerUpOrDown(PlayerModel playerModel)
      Determines if the player is above or below the enemy.

      This method compares the player's tile y-coordinate with the enemy's tile y-coordinate and returns the direction indicating whether the player is above or below the enemy. If the player is on the same tile as the enemy, it returns NONE.

      Parameters:
      playerModel - the player model used to determine the player's position
      Returns:
      the direction indicating if the player is above or below the enemy, or NONE if they are on the same tile
    • canFall

      protected boolean canFall()
      Checks if the enemy can fall.

      This method determines if the enemy can fall by checking if the tile below the enemy is within the level boundaries.

      Returns:
      true if the enemy can fall, false otherwise
    • resetEnemy

      public void resetEnemy()
      Resets the enemy to its initial state.

      This method resets the enemy's position to its initial coordinates, sets the enemy to be active, changes its state to NORMAL_STATE, and sets its walking direction to the initial direction.

    • bubbleCapture

      public void bubbleCapture(Constants.Direction direction)
      Captures the enemy in a bubble.

      This method creates a new `EnemyBubbleModel` instance with the enemy's current position and the specified direction, adds it to the `PlayerBubblesManagerModel`, and sets the enemy's state to `BOBBLE_STATE` and inactive.

      Parameters:
      direction - the direction in which the bubble will move
    • instantKill

      public void instantKill(PlayerModel playerModel)
      Instantly kills the enemy and creates an enemy bubble.

      This method sets the enemy's state to DEAD_STATE, marks it as inactive, and creates an enemy bubble using the PlayerBubblesManagerModel with the current enemy and player model.

      Since the enemy is in the dead state, the bubble will be updated by EnemyBubbleModel.updateDeadAction() method and will not behave as a normal "floating" bubble.

      Parameters:
      playerModel - the player model used to create the enemy bubble
    • setAlive

      public void setAlive(boolean alive)
      Sets the alive status of the enemy.

      This method updates the alive status of the enemy to the specified value.

      Parameters:
      alive - the new alive status to set
    • setActive

      public void setActive(boolean active)
      Sets the active status of the enemy.

      This method updates the active status of the enemy to the specified value.

      Overrides:
      setActive in class EntityModel
      Parameters:
      active - the new active status to set
    • setEnemyState

      public void setEnemyState(int state)
      Sets the state of the enemy.
    • isActive

      public boolean isActive()
      Returns the active status of the enemy.
      Overrides:
      isActive in class EntityModel
      Returns:
      true if the enemy is active, false otherwise
    • isAlive

      public boolean isAlive()
      Returns the alive status of the enemy.
      Returns:
      true if the enemy is alive, false otherwise
    • getEnemyState

      public int getEnemyState()
      Returns the current state of the enemy.
      Returns:
      the current state of the enemy
    • getReachedSpawn

      public boolean getReachedSpawn()
      Returns whether the enemy has reached its spawn point.
      Returns:
      true if the enemy has reached its spawn point, false otherwise
    • getEnemyType

      public abstract Constants.EnemyConstants.EnemyType getEnemyType()
      Returns the type of the enemy, (see Constants.EnemyConstants.EnemyType).
      Returns:
      the type of the enemy