Pacman Torben Petré
Loading...
Searching...
No Matches
logic::GhostModel Class Referenceabstract

#include <GhostModel.h>

Inheritance diagram for logic::GhostModel:
Collaboration diagram for logic::GhostModel:

Public Member Functions

 GhostModel (float normalizedX, float normalizedY, float mapWidth, float mapHeight, double cooldown)
 Base class for Ghosts. Handles Ghost state and how that influences ghost behaviour.
 ~GhostModel () override=default
GhostState getState () const
 Returns the current state of the Ghost.
bool isFrightened () const
 Returns whether the Ghost is currently frightened.
int getGridSpawnX () const
 Returns the Grid mapped X coordinate of where the Ghost spawned.
int getGridSpawnY () const
 Returns the Grid mapped Y coordinate of where the Ghost spawned.
void setFrightened (bool frightened, const World &world)
 Allows the World to set toggle whether the Ghost is Frigthened or not.
void pacmanCollides (World &world)
 Tells the Ghost it collided with Pacman and should handle this situation.
void move (const World &world, float dt) override
 This function is to be called in the World update loop. The Ghost will act based on it's current State and possibly move.
void respawn () override
 Tell the Ghost to respawn. It will go back to its spawnpoint and start in a GhostState::WAITING state again.
Public Member Functions inherited from logic::MovingEntityModel
 MovingEntityModel (float normalizedX, float normalizedY, float mapWidth, float mapHeight, float speed)
 Subclass of EntityModel to be used for any moving entity.
Moves getDirection () const
 Get the direction the entity is currently moving in.
int getGridX () const
int getGridY () const
Public Member Functions inherited from logic::EntityModel
 EntityModel (float normalizedX, float normalizedY)
 Base class for entities.
 ~EntityModel () override=default
float getX () const
float getY () const
Public Member Functions inherited from logic::Subject
virtual ~Subject ()=default
void attach (const std::shared_ptr< Observer > &observer)
 Attaches an observer to this Subject.
void notify (Events event)
 Call the Observer update function with the provided event.

Protected Member Functions

virtual Moves decideNextMove (const World &world)=0
 This function must be overriden by any inheriting Ghost classes. It must return the best Move according to its attacking personality.
Protected Member Functions inherited from logic::MovingEntityModel
void normalizeTarget ()
 Normalizes the grid coordinates and stores these values in as target coordinates. Moving entities strive towards normalized grid coordinates.

Additional Inherited Members

Protected Attributes inherited from logic::MovingEntityModel
float mapWidth
float mapHeight
float spawnX
float spawnY
int gridY
int gridX
float targetX
float targetY
Moves direction = Moves::RIGHT
float speed
Protected Attributes inherited from logic::EntityModel
float x
float y
Protected Attributes inherited from logic::Subject
std::list< std::weak_ptr< Observer > > observers

Constructor & Destructor Documentation

◆ GhostModel()

GhostModel::GhostModel ( float normalizedX,
float normalizedY,
float mapWidth,
float mapHeight,
double cooldown )
explicit

Base class for Ghosts. Handles Ghost state and how that influences ghost behaviour.

Uses GhostNavigator helper functions for Pathfinding and Path related decision-making.

Parameters
normalizedXNormalized x spawn coordinate
normalizedYNormalized y spawn coordinate
mapWidthMap width (in tiles)
mapHeightMap height (in tiles)
cooldownSpawn cooldown before leaving enclosure

◆ ~GhostModel()

logic::GhostModel::~GhostModel ( )
overridedefault

Member Function Documentation

◆ decideNextMove()

virtual Moves logic::GhostModel::decideNextMove ( const World & world)
protectedpure virtual

This function must be overriden by any inheriting Ghost classes. It must return the best Move according to its attacking personality.

Parameters
worldWorld
Returns
The best Move for this Ghost

◆ getGridSpawnX()

int GhostModel::getGridSpawnX ( ) const
nodiscard

Returns the Grid mapped X coordinate of where the Ghost spawned.

Returns
Grid X coordinate of Ghost spawn

◆ getGridSpawnY()

int GhostModel::getGridSpawnY ( ) const
nodiscard

Returns the Grid mapped Y coordinate of where the Ghost spawned.

Returns
Grid Y coordinate of Ghost spawn

◆ getState()

GhostState GhostModel::getState ( ) const
nodiscard

Returns the current state of the Ghost.

Returns
GhostState

◆ isFrightened()

bool GhostModel::isFrightened ( ) const
nodiscard

Returns whether the Ghost is currently frightened.

Returns
Frightened

◆ move()

void GhostModel::move ( const World & world,
float dt )
overridevirtual

This function is to be called in the World update loop. The Ghost will act based on it's current State and possibly move.

Whether the Ghost actually moves or not depends on its state. A GhostState::WAITING will not move until its cooldown runs out. Otherwise, it will move based on it's behaviour and whether it is frightened.

Parameters
worldWorld
dtDeltatime since last call

Implements logic::MovingEntityModel.

◆ pacmanCollides()

void GhostModel::pacmanCollides ( World & world)

Tells the Ghost it collided with Pacman and should handle this situation.

Depending on whether the Ghost is frightened or not it will kill Pacman, or get killed.

Parameters
worldWorld

◆ respawn()

void GhostModel::respawn ( )
overridevirtual

Tell the Ghost to respawn. It will go back to its spawnpoint and start in a GhostState::WAITING state again.

If the Ghost must respawn because of getting eaten while being frightened, it may instantly switch to a GhostState::EXITING without waiting for its cooldown to pass. If Pacman dies and the World resets, the Ghost will have to watch its cooldown again.

Implements logic::MovingEntityModel.

◆ setFrightened()

void GhostModel::setFrightened ( bool frightened,
const World & world )

Allows the World to set toggle whether the Ghost is Frigthened or not.

Warning
Any change to the frightened state is ignored while a Ghost is dead.

Changes the Ghost's internal state and notifies Observers. Updates the Ghost speed to match its frightenedness. Updates the Ghost's internal pathing to either run away or re-target Pacman.

Parameters
frightenedTrue or False toggle
worldWorld

The documentation for this class was generated from the following files: