Class EnemyModel
- Direct Known Subclasses:
MaitaModel,SkelMonstaModel,ZenChanModel
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 Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanprotected intprotected Constants.EnemyConstants.EnemyTypeprotected floatprotected floatprotected booleanprotected booleanprotected booleanprotected booleanprotected intprotected intprotected Constants.Directionprotected booleanprotected floatprotected Constants.Directionprotected intprotected Constants.Directionprotected floatprotected floatprotected floatFields inherited from class model.entities.EntityModel
height, hitbox, immune, levelManagerModel, timer, width, x, y -
Constructor Summary
ConstructorsConstructorDescriptionEnemyModel(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. -
Method Summary
Modifier and TypeMethodDescriptionvoidbubbleCapture(Constants.Direction direction) Captures the enemy in a bubble.protected voidcalculatePlayersPos(PlayerModel playerModel) Calculates the player's position in tile coordinates.protected booleancanFall()Checks if the enemy can fall.protected voidChanges the walking direction of the enemy to the opposite direction of the current direction.intflipW()Returns the width multiplier for flipping the enemy's sprite horizontally.intflipX()Returns the x-coordinate offset for flipping the enemy's sprite horizontally.intReturns the current state of the enemy.abstract Constants.EnemyConstants.EnemyTypeReturns the type of the enemy, (seeConstants.EnemyConstants.EnemyType).booleanReturns whether the enemy has reached its spawn point.voidinstantKill(PlayerModel playerModel) Instantly kills the enemy and creates an enemy bubble.booleanisActive()Returns the active status of the enemy.booleanisAlive()Returns the alive status of the enemy.protected booleanisPlayerInViewingRange(PlayerModel playerModel) Checks if the player is within the enemy's viewing range.protected Constants.DirectionisPlayerLeftOrRight(PlayerModel playerModel) Determines if the player is to the left or right of the enemy.protected Constants.DirectionisPlayerUpOrDown(PlayerModel playerModel) Determines if the player is above or below the enemy.voidResets the enemy to its initial state.voidsetActive(boolean active) Sets the active status of the enemy.voidsetAlive(boolean alive) Sets the alive status of the enemy.voidsetEnemyState(int state) Sets the state of the enemy.abstract voidupdate(PlayerModel playerModel) Updates the state of the enemy based on the player's position.protected voidUpdates the spawning state of the enemy.protected voidUpdates the state variables of the enemy based on its current state.protected voidUpdates the walking direction of the enemy based on the player's position.Methods inherited from class model.entities.EntityModel
conpenetrationSafeUpdateXPos, deactivate, getHitbox, getTileX, getTileY, initHitbox, initLevelManager, isImmune, updateXPos
-
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
-
previousWalkingDir
-
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 positiony- the y-coordinate of the enemy's initial positionwidth- the width of the enemyheight- the height of the enemyenemyType- the type of the enemystartWalkingDir- the initial walking direction of the enemy
-
-
Method Details
-
update
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
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
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
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
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
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
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:
setActivein classEntityModel- 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:
isActivein classEntityModel- 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
Returns the type of the enemy, (seeConstants.EnemyConstants.EnemyType).- Returns:
- the type of the enemy
-