void Player::canGoY(float dt, const Physics &phys) {
  pos.y += vel.y * dt;
  for (float changeY = phys.testY(*this); changeY!=0; changeY = phys.testY(*this)) {
    pos.y += changeY; 
    vel.y = 0;
    if (  // if there was a collision with the ground
      ( (changeY < 0 && phys.gravAngle==0*90) || (changeY>0 && phys.gravAngle==2*90) ) 
      && abs(changeY)<=abs(phys.testX(*this))
    ) inAir = false;
  }
  if (phys.testBoundsY(*this) != 0) dead = true;  // player fell into a pit.
}
Exemple #2
0
void Enemy::canGoY(float dt, const Physics &phys) {
  pos.y += vel.y * dt;
  for (float changeY = phys.testY(*this); changeY!=0; changeY = phys.testY(*this)) {
    pos.y += phys.testY(*this); 
    if (phys.gravAngle%180 == 0) vel.setY(0, phys.gravAngle);
    else hitWall = true;
  }
  if (phys.testBoundsY(*this) != 0) {  // when fallen into the pit
    anger(phys);
    dead = true;  // indicate death to reset
  }
}
// === private functions: ===
void Player::canGoX(float dt, const Physics &phys) {
  pos.x += vel.x * dt;
  for (float changeX = phys.testX(*this); changeX!=0; changeX = phys.testX(*this)) {  // as long as there are collisions, keep checking
    pos.x += changeX;
    vel.x = 0;
    if (  // if there was a collision with the ground
      ( (changeX < 0 && phys.gravAngle==1*90) || (changeX>0 && phys.gravAngle==3*90) ) 
      && abs(changeX)<=abs(phys.testY(*this))
    ) inAir = false;
  }
  if (phys.testBoundsX(*this) != 0) dead = true; // player fell into a pit.
}