void CEntity::OnMove(float MoveX, float MoveY) { if (MoveX == 0 && MoveY == 0) return; double NewX = 0; double NewY = 0; MoveX *= CFPS::FPSControl.GetSpeedFactor(); MoveY *= CFPS::FPSControl.GetSpeedFactor(); if (MoveX != 0) { if (MoveX >= 0) NewX = CFPS::FPSControl.GetSpeedFactor(); else NewX = -CFPS::FPSControl.GetSpeedFactor(); } if (MoveY != 0) { if (MoveY >= 0) NewY = CFPS::FPSControl.GetSpeedFactor(); else NewY = -CFPS::FPSControl.GetSpeedFactor(); } while (true) { if (Flags & ENTITY_FLAG_GHOST) { PosValid((int)(X + NewX), (int)(Y + NewY)); //We don’t care about collisions, but we need to send events to other entities X += NewX; Y += NewY; } else { if (PosValid((int)(X + NewX), (int)(Y))) { X += NewX; } else { SpeedX = 0; } if (PosValid((int)(X), (int)(Y + NewY))) { Y += NewY; } else { SpeedY = 0; } } MoveX += -NewX; MoveY += -NewY; if (NewX > 0 && MoveX <= 0) NewX = 0; if (NewX < 0 && MoveX >= 0) NewX = 0; if (NewY > 0 && MoveY <= 0) NewY = 0; if (NewY < 0 && MoveY >= 0) NewY = 0; if (MoveX == 0) NewX = 0; if (MoveY == 0) NewY = 0; if (MoveX == 0 && MoveY == 0) break; if (NewX == 0 && NewY == 0) break; } }
void cMushroom :: Update( void ) { if( !visible ) { return; } if( CollideBoundingBox( &rect, &pPlayer->rect ) ) { visible = 0; pPlayer->Get_Item( type ); if( type == TYPE_MUSHROOM_DEFAULT ) { pointsdisplay->AddPoints( 500, (int)posx + image->w/2, (int)posy + 3 ); } else if( type == TYPE_MUSHROOM_LIVE_1 ) { pointsdisplay->AddPoints( 1000, (int)posx + image->w/2,(int)posy + 3 ); } } onGround = !PosValid( (int)posx, (int)posy + 1 ); if( !onGround && vely < 50 ) { if( vely > 0 ) { AddVel( 0, 0.5 + vely*0.05 ); } else { AddVel( 0, 2.5 ); } } else { vely = 0; } if( CollideMove() != DIR_NOTHING ) { SDL_Rect r2 = Get_Collision_rect(); if( ( iCollisionType == 1 || iCollisionType == 2 ) && ( col_dir == DIR_LEFT || col_dir == DIR_RIGHT ) ) { velx *= -1; Move( velx, vely ); } else if( posx == 0 && col_dir == DIR_LEFT ) { velx *= -1; Move( velx, vely ); } } Draw( screen ); }
void CEnemy::OnLoop() { // move in one direction as long as possible if(_walkDir == WALKDIR_LEFT) { if(!PosValid(x-width, y-height) || !PosValid(x-width, y) || PosValid(x-width, y+height)) _walkDir = WALKDIR_RIGHT; } else if(_walkDir == WALKDIR_RIGHT) { if(!PosValid(x+width, y-height) || !PosValid(x+width, y) || PosValid(x+width, y+height)) _walkDir = WALKDIR_LEFT; } if(_walkDir == WALKDIR_RIGHT) { moveLeft = false; moveRight = true; } else if(_walkDir == WALKDIR_LEFT) { moveRight = false; moveLeft = true; } CEntity::OnLoop(); }