Esempio n. 1
0
Direction CEnemy::chooseDirection(int targetx, int targety)
{
    int currentTileX = getXposition();
    int currentTileY = getYposition();
    
    // gathering distances, adding bonus distance for invert directions so they can't turn back, adding bonus if we can't move there aswell
    int upWay = distance(targetx, targety, currentTileX, currentTileY - 1) + (_direction == DirectionDown ? 1000 : 0) + (ableToMove(0, -1) ? 0 : 1000);
    int leftWay = distance(targetx, targety, currentTileX - 1, currentTileY) + (_direction == DirectionRight ? 1000 : 0) + (ableToMove(-1, 0) ? 0 : 1000);
    int downWay = distance(targetx, targety, currentTileX, currentTileY + 1) + (_direction == DirectionUp ? 1000 : 0) + (ableToMove(0, 1) ? 0 : 1000);
    int rightWay = distance(targetx, targety, currentTileX + 1, currentTileY) + (_direction == DirectionLeft ? 1000 : 0) + (ableToMove(1, 0) ? 0 : 1000);
    
    // finding best distance, ie shortest one, ie without added bonus distances aswell
    int bestDistance = min(upWay, min(leftWay, min(downWay, rightWay)));
    
    if (upWay == bestDistance) {
        return DirectionUp;
    }
    else if (leftWay == bestDistance) {
        return DirectionLeft;
    }
    else if (downWay == bestDistance) {
        return DirectionDown;
    }
    
    return DirectionRight;
}
Esempio n. 2
0
		void Ship2::update(unsigned long elapsed_millis) {
			Ship::update(elapsed_millis);
			if (_ship1->isColliding(_physics->getPosition(),_shieldrad)) {
				if(_ship1->isInvincible() && isInvincible()) {
					cg::Vector2d collisionPoint = cg::Vector2d( (getXposition() + _ship1->getXposition())/2 , (getYposition() + _ship1->getYposition())/2);
					cg::Vector2d ship2Velocity = getVelocity();
					shock(collisionPoint, 0.5 * _ship1->getMass()*length(_ship1->getVelocity()));
					_ship1->shock(collisionPoint, 0.5 * _physics->getMass()*length(ship2Velocity));
					return;
				}
				explode();
				return;
			}
		}
Esempio n. 3
0
void CEnemy::Update()
{
    CEntity::Update(); // animation and movement stuff
    
    // handling new movement in different states
    if (!_moving && _state == EnemyStateScatter)
    {
        Move(chooseDirection(_scatterX, _scatterY)); // try to move to our scatter corner
    }
    else if (!_moving && _state == EnemyStateChase)
    {
        Move(chooseDirection(_chaseX, _chaseY)); // try to move to our chase position (different for every ghost)
    }
    else if (!_moving && _state == EnemyStateFrighten)
    {
        while (true) { // just move in random direction
            int direction = rand() % 4;
            if (!isReverse((Direction)direction)) {
                Move((Direction)direction);
                if (_moving) {
                    break;
                }
            }
        }
    }
    else if (!_moving && _state == EnemyStateEyes)
    {
        if (getXposition() == _startX && getYposition() == _startY) {
            Reset(); // reset our states when home reached
            
            _state = EnemyStateChase;
            Update(); // start chase over
        }
        else {
            Move(chooseDirection(_startX, _startY)); // go home
        }
    }
}