Skip to content

Commit

Permalink
use the Motion2D class for pushing and dragging (by a conveyor) too
Browse files Browse the repository at this point in the history
now the #64 “enumeration of directed activities” model is almost* replaced

* “pushed up” is still there
  • Loading branch information
dougmencken committed Oct 28, 2024
1 parent 43c8777 commit b202272
Show file tree
Hide file tree
Showing 34 changed files with 372 additions and 607 deletions.
6 changes: 3 additions & 3 deletions source/Item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ class Item : public Mediated, public Shady
void changeFrame ( unsigned int newFrame ) ;

/**
* Add value to coordinate X
* Add value to the X coordinate
* @return true on change or false when there’s collision
*/
virtual bool addToX ( int value ) { return addToPosition( value, 0, 0 ) ; }

/**
* Add value to coordinate Y
* Add value to the Y coordinate
* @return true on change or false when there’s collision
*/
virtual bool addToY ( int value ) { return addToPosition( 0, value, 0 ) ; }

/**
* Add value to coordinate Z
* Add value to the Z coordinate
* @return true on change or false when there’s collision
*/
virtual bool addToZ ( int value ) { return addToPosition( 0, 0, value ) ; }
Expand Down
36 changes: 30 additions & 6 deletions source/Motion2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ class Motion2D
int getMotionAlongX () const { return this->alongX ; }
int getMotionAlongY () const { return this->alongY ; }

bool equals ( const Motion2D & that )
{
return this->alongX == that.alongX && this->alongY == that.alongY ;
}
bool operator == ( const Motion2D & that ) { return this->equals( that ) ; }
bool operator != ( const Motion2D & that ) { return ! this->equals( that ) ; }

Motion2D& add ( const Motion2D & that )
{
this->alongX += that.alongX ;
this->alongY += that.alongY ;
return *this ;
}

Motion2D& multiplyBy ( double multiplier )
{
this->alongX *= multiplier ;
this->alongY *= multiplier ;
return *this ;
}

Motion2D& reverse ()
{
this->alongX = - this->alongX ;
this->alongY = - this->alongY ;
return *this ;
}

bool isRest () const
{
return getMotionAlongX() == 0 && getMotionAlongY() == 0 ;
Expand All @@ -70,12 +98,8 @@ class Motion2D
bool isMovingSoutheast () const { return isMovingSouth() && isMovingEast() ; }
bool isMovingSouthwest () const { return isMovingSouth() && isMovingWest() ; }

Motion2D& reverse ()
{
this->alongX = - this->alongX ;
this->alongY = - this->alongY ;
return *this ;
}
void resetX () { this->alongX = 0 ; }
void resetY () { this->alongY = 0 ; }

} ;

Expand Down
23 changes: 23 additions & 0 deletions source/Motion3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ class Motion3D : public Motion2D

int getMotionAlongZ () const { return this->alongZ ; }

bool equals ( const Motion3D & that )
{
return Motion2D::equals( that ) && this->alongZ == that.alongZ ;
}
bool operator == ( const Motion3D & that ) { return this->equals( that ) ; }
bool operator != ( const Motion3D & that ) { return ! this->equals( that ) ; }

Motion3D& add ( const Motion3D & that )
{
Motion2D::add( that );
this->alongZ += that.alongZ ;
return *this ;
}

Motion3D& multiplyBy ( double multiplier )
{
Motion2D::multiplyBy( multiplier );
this->alongZ *= multiplier ;
return *this ;
}

Motion2D to2D () const
{
return Motion2D( getMotionAlongX(), getMotionAlongY() );
Expand Down Expand Up @@ -86,6 +107,8 @@ class Motion3D : public Motion2D
return isMovingWest() && getMotionAlongX() == 0 && getMotionAlongZ() == 0 ;
}

void resetZ () { this->alongZ = 0 ; }

} ;

#endif
8 changes: 2 additions & 6 deletions source/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,11 @@ std::string SoundManager::activityToNameOfSound ( const activities::Activity & a
case activities::Activity::TakingItem : case activities::Activity::TakeAndJump : return "take" ;
case activities::Activity::DroppingItem : case activities::Activity::DropAndJump : return "drop" ;

case activities::Activity::PushedNorth : case activities::Activity::PushedSouth :
case activities::Activity::PushedEast : case activities::Activity::PushedWest :
case activities::Activity::PushedNortheast : case activities::Activity::PushedSoutheast :
case activities::Activity::PushedSouthwest : case activities::Activity::PushedNorthwest :
case activities::Activity::Pushed :
case activities::Activity::PushedUp : /* case activities::Activity::PushedDown : */
return "push" ;

case activities::Activity::DraggedNorth : case activities::Activity::DraggedSouth :
case activities::Activity::DraggedEast : case activities::Activity::DraggedWest : return "dragged" ;
case activities::Activity::Dragged : return "dragged" ;

case activities::Activity::CancelDragging: return "move" ;

Expand Down
33 changes: 12 additions & 21 deletions source/activities/Activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class Activity

public:

Activity( unsigned int business ) : activity( business ) { }
Activity( unsigned int business ) : activity( business ) {}

Activity( const Activity & copyMe ) : activity( copyMe.activity ) {}

operator unsigned int() const { return this->activity ; }

Expand All @@ -29,39 +31,28 @@ class Activity

public: /* constants */

static const unsigned int Waiting = 0 ;
static const unsigned int Blinking = 8 ;
static const unsigned int Waiting = 0 ;
static const unsigned int Blinking = 8 ;

static const unsigned int Moving = 32 ;
static const unsigned int Moving = 32 ;

static const unsigned int Automoving = 48 ;
static const unsigned int Automoving = 48 ;

static const unsigned int Jumping = 64 ;
static const unsigned int Falling = 72 ;
static const unsigned int Gliding = 80 ;
static const unsigned int Jumping = 64 ;
static const unsigned int Falling = 72 ;
static const unsigned int Gliding = 80 ;

static const unsigned int TakingItem = 100 ;
static const unsigned int DroppingItem = 105 ;
static const unsigned int TakeAndJump = 110 ;
static const unsigned int DropAndJump = 115 ;

static const unsigned int PushedNorth = 124 ;
static const unsigned int PushedSouth = 125 ;
static const unsigned int PushedEast = 126 ;
static const unsigned int PushedWest = 127 ;

static const unsigned int PushedNortheast = 132 ;
static const unsigned int PushedSoutheast = 133 ;
static const unsigned int PushedSouthwest = 134 ;
static const unsigned int PushedNorthwest = 135 ;
static const unsigned int Pushed = 140 ;

static const unsigned int PushedUp = 150 ;
/* static const unsigned int PushedDown = 160 ; */

static const unsigned int DraggedNorth = 181 ;
static const unsigned int DraggedSouth = 182 ;
static const unsigned int DraggedEast = 183 ;
static const unsigned int DraggedWest = 184 ;
static const unsigned int Dragged = 180 ;

static const unsigned int CancelDragging = 190 ;

Expand Down
15 changes: 2 additions & 13 deletions source/activities/ActivityToString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,11 @@ class ActivityToString
case Activity::TakeAndJump : return "take & jump" ;
case Activity::DropAndJump : return "drop & jump" ;

case Activity::PushedNorth : return "pushed north" ;
case Activity::PushedSouth : return "pushed south" ;
case Activity::PushedEast : return "pushed east" ;
case Activity::PushedWest : return "pushed west" ;

case Activity::PushedNortheast : return "pushed northeast" ;
case Activity::PushedSoutheast : return "pushed southeast" ;
case Activity::PushedSouthwest : return "pushed southwest" ;
case Activity::PushedNorthwest : return "pushed northwest" ;
case Activity::Pushed : return "pushed" ;

case Activity::PushedUp : return "pushed up" ;

case Activity::DraggedNorth : return "dragged north" ;
case Activity::DraggedSouth : return "dragged south" ;
case Activity::DraggedEast : return "dragged east" ;
case Activity::DraggedWest : return "dragged west" ;
case Activity::Dragged : return "dragged" ;

case Activity::CancelDragging : return "cancel dragging" ;

Expand Down
82 changes: 15 additions & 67 deletions source/activities/Displacing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,86 +24,34 @@ Displacing& Displacing::getInstance()

bool Displacing::displace( behaviors::Behavior & behavior, bool canFall )
{
bool displaced = false ;

Item & item = behavior.getItem ();

const Activity & currentActivity = behavior.getCurrentActivity ();
Activity activityToPropagate = currentActivity ;

switch ( currentActivity )
{
case activities::Activity::DraggedNorth:
activityToPropagate = activities::Activity::PushedNorth;
// fallthru
case activities::Activity::PushedNorth:
displaced = item.addToX( -1 );
break;

case activities::Activity::DraggedSouth:
activityToPropagate = activities::Activity::PushedSouth;
// fallthru
case activities::Activity::PushedSouth:
displaced = item.addToX( 1 );
break;

case activities::Activity::DraggedEast:
activityToPropagate = activities::Activity::PushedEast;
// fallthru
case activities::Activity::PushedEast:
displaced = item.addToY( -1 );
break;

case activities::Activity::DraggedWest:
activityToPropagate = activities::Activity::PushedWest;
// fallthru
case activities::Activity::PushedWest:
displaced = item.addToY( 1 );
break;

case activities::Activity::PushedNortheast:
displaced = item.addToPosition( -1, -1, 0 );
break;

case activities::Activity::PushedNorthwest:
displaced = item.addToPosition( -1, 1, 0 );
break;

case activities::Activity::PushedSoutheast:
displaced = item.addToPosition( 1, -1, 0 );
break;
bool displaced = false ;

case activities::Activity::PushedSouthwest:
displaced = item.addToPosition( 1, 1, 0 );
break;
const Activity & currentActivity = behavior.getCurrentActivity ();
Motion2D vector = behavior.get2DVelocityVector() ;

case activities::Activity::PushedUp:
displaced = item.addToZ( 1 );
break;

default:
;
}
if ( currentActivity == activities::Activity::Pushed || currentActivity == activities::Activity::Dragged )
displaced = item.addToPosition( vector.getMotionAlongX(), vector.getMotionAlongY(), 0 );
else if ( currentActivity == activities::Activity::PushedUp )
displaced = item.addToZ( 1 );

if ( item.whichItemClass() == "free item" || item.whichItemClass() == "avatar item" )
{
Activity activityToPropagate = ( currentActivity == activities::Activity::Dragged )
? Activity( activities::Activity::Pushed )
: currentActivity ;
// when there’s a collision
if ( ! displaced )
{
// move involved items
PropagateActivity::toAdjacentItems( item, activityToPropagate );
}
else {
PropagateActivity::toAdjacentItems( item, activityToPropagate, vector );
else
// maybe there’s something on top
PropagateActivity::toItemsAbove( item, activityToPropagate );
}
PropagateActivity::toItemsAbove( item, activityToPropagate, vector );
}

// when item can fall
if ( canFall )
{
if ( Falling::getInstance().fall( behavior ) )
{
if ( canFall ) {
if ( Falling::getInstance().fall( behavior ) ) {
behavior.setCurrentActivity( activities::Activity::Falling );
displaced = true ;
}
Expand Down
36 changes: 11 additions & 25 deletions source/activities/Jumping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ Jumping & Jumping::getInstance()

bool Jumping::jump( behaviors::Behavior & behavior, unsigned int jumpPhase, const std::vector< std::pair< int /* xy */, int /* z */ > > & jumpVector )
{
bool jumped = false ;

Activity displaceActivity = activities::Activity::Waiting ;

AvatarItem & character = dynamic_cast< AvatarItem & >( behavior.getItem() );
Mediator* mediator = character.getMediator() ;

bool jumped = false ;

int deltaXY = jumpVector[ jumpPhase ].first ;
int deltaZ = jumpVector[ jumpPhase ].second ;

Expand Down Expand Up @@ -79,42 +77,30 @@ bool Jumping::jump( behaviors::Behavior & behavior, unsigned int jumpPhase, cons
}
}

const std::string & heading = character.getHeading () ;
const std::string & heading = character.getHeading() ;

if ( heading == "north" )
{
jumped = character.addToX( - deltaXY );
displaceActivity = activities::Activity::PushedNorth ;
}
else if ( heading == "south" )
{
else
if ( heading == "south" )
jumped = character.addToX( deltaXY );
displaceActivity = activities::Activity::PushedSouth ;
}
else if ( heading == "east" )
{
else
if ( heading == "east" )
jumped = character.addToY( - deltaXY );
displaceActivity = activities::Activity::PushedEast ;
}
else if ( heading == "west" )
{
else
if ( heading == "west" )
jumped = character.addToY( deltaXY );
displaceActivity = activities::Activity::PushedWest ;
}

// displace adjacent items when there’s a horizontal collision
if ( ! jumped || ( jumped && jumpPhase > 4 ) )
{
// is it okay to move items above
// it is okay after the fourth phase of jump so the character can get rid of the item above
PropagateActivity::toAdjacentItems( character, displaceActivity );
// displacing the items above is okay after the fourth phase of jump so the character can get rid of the above item
PropagateActivity::toAdjacentItems( character, activities::Activity::Pushed, behavior.get2DVelocityVector() );
}

// end jump when it’s the last phase
if ( ( jumpPhase + 1 ) >= jumpVector.size() )
{
behavior.setCurrentActivity( activities::Activity::Falling );
}

return jumped ;
}
Expand Down
Loading

0 comments on commit b202272

Please sign in to comment.