//A stupid Walk algorithm. Change Direction When The Objects X-velocity is == 0 void EnemyObject::stupidWalk(const float delta) { if (getVelocityX() == 0) { if (getPrevDir() == GO_LEFT) { setVelocityX(speed); setPrevDir(GO_RIGHT); objectSprite->setScaleX(1 * abs(objectSprite->getScale()));; } else { setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite->setScaleX(-1 * abs(objectSprite->getScale()));; } } }
void EnemyObject::walkInZone(const float delta){ auto const rect = getHitbox(); const float minX = rect->getMidX(); const float maxX = rect->getMaxX(); if(maxX >softXMax){ setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite->setScaleX(-1 * abs(objectSprite->getScale())); }else if(minX < softXMin){ setVelocityX(speed); setPrevDir(GO_RIGHT); objectSprite->setScaleX(1 * abs(objectSprite->getScale())); } }
/*** Add this function to the objects update function to make it turn at edges. This Algorithm was not designed For slopes. If the objects path includes slopes use a predefined zone instead***/ void EnemyObject::turnAtEdge(const float delta) { const float lookAhead = 0.5f*getVelocityX() / delta; auto const hitbox = getHitbox(); if (!isBlocked((uint32_t)((hitbox->getMidX() + lookAhead) / 16.0), (uint32_t)(hitbox->getMinY() - 1) / 16.0)) { if (getPrevDir() == GO_LEFT) { setVelocityX(speed); setPrevDir(GO_RIGHT); objectSprite->setScaleX(1); } else { setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite->setScaleX(-1); } } }
bool ZombieObject::init() { ////////////////////////////// // 1. super init first if (!EnemyObject::init()) { return false; } setScale(2); this->schedule(schedule_selector(ZombieObject::AIUpdate), 0.8); addGravityToObject(true); setElastic(0.f); plingSFX = SoundFx::create(); plingSFX->loadEffect("small_explosion.aif", 0, 1, false); addChild(plingSFX); dmg = 1; // Start Speed setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite = cocos2d::Sprite::create(); objectSprite->setPosition(12, 0);// Aling sprite in Hitbox objectSprite->setAnchorPoint(Point(0.5, 0)); addChild(objectSprite); objectSprite->setScaleX(-1); return true; }
bool UfoObject::init() { ////////////////////////////// // 1. super init first if (!GameObject::init()) { return false; } this->schedule(schedule_selector(UfoObject::AIUpdate),1.2); addGravityToObject(false); setElastic(1.f); // SETUP ANIMATIONS objectSprite = cocos2d::Sprite::create(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("patrol_ufo.plist"); addAnimation("patrol_ufo ", "ufo_idle", 1, 2, 0.2f); setAnimation("ufo_idle"); objectSprite->setPosition(32, 32);// Aling sprite in Hitbox objectSprite->setScale(2); addChild(objectSprite); // Start Speed const float xVel = -50; setVelocityX(xVel); plingSFX = SoundFx::create(); plingSFX->loadEffect("small_explosion.aif", 0, 1, false); addChild(plingSFX); return true; }
/* * This is the plane's constructor */ Plane::Plane() : collisionBoxes_(7) { //Randomly select color of the plane from the start srand(time(0)); color_ = (Color)(rand() % 4); //Initialize the motion of the plane motion_ = MOTION_Falling; //Initialize the frame counter frameCounter_ = 0; //Initialize the offset x and offset y of the plane //The offset x of the plane is constant setOffsetX(INITIAL_OFFSET_X); setOffsetY(INITIAL_OFFSET_Y); //Set the width and height of the plane //Width and height are constant setSourceWidth(SPRITE_WIDTH); setSourceHeight(SPRITE_HEIGHT); //Velocity of plane on the x axis is constant and does not change setVelocityX(0.0f); setVelocityY(0.0f); elapseTime_ = 0; puffCloud_.setOffsetY(getOffsetY() + 10.0f); //Initialize the collision boxes width and height for (int i = 0; i < collisionBoxes_.size(); i++) { collisionBoxes_[i].w = COLLISION_BOX_W[i]; collisionBoxes_[i].h = COLLISION_BOX_H[i]; } }
/* * The ground class constructor */ Ground::Ground() : collisionBoxes_(34), collisionBoxes_Loop_(34) { //Randomly initialize the type of the ground srand(time(0)); type_ = (Type)(rand() % 4); //Set the initial location of the ground setOffsetX(INITIAL_OFFSET_X); setOffsetY(INITIAL_OFFSET_Y); //Initialize the ground's velocity setVelocityX(GROUND_VELOCITY_X); setVelocityY(GROUND_VELOCITY_Y); //Initialize the width and height of the collision boxes for (int i = 0; i < collisionBoxes_.size(); i++) { collisionBoxes_[i].w = COLLISION_BOX_W[i]; collisionBoxes_[i].h = COLLISION_BOX_H[i]; } for (int i = 0; i < collisionBoxes_Loop_.size(); i++) { collisionBoxes_Loop_[i].w = COLLISION_BOX_W[i]; collisionBoxes_Loop_[i].h = COLLISION_BOX_H[i]; } }
void VelocityComponent::setVelocity(float x, float y, bool turnFaceDir) { /*if ((x != 0.f || y != 0.f) && mOwnerEntity->comp<CategoryComponent>()->getCategory() == 8) int i = 0;*/ setVelocityX(x, turnFaceDir); setVelocityY(y, turnFaceDir); }
void Mogh::collision(gen::Sprite* other, gen::World* w){ if(Troll* ch = dynamic_cast<Troll*>(other)){ damaged = true; setCurrentAnimationVector("DAMAGED"); setHealth(getHealth() - 10); setTimer( SDL_GetTicks() + 2000 ); setCollidable(false); setVelocityX(getVelocityX()*2); setVelocityY(getVelocityY()*2); } else if(gen::Object* o = dynamic_cast<gen::Object*>(other)){ std::string direction = getKeyOfCurrentAnimationVector(); if(direction == "LEFT"){ setMovementX(getVelocityX()); moveX(w); } else if(direction == "RIGHT"){ setMovementX(-getVelocityX()); moveX(w); } else if(direction == "BACK"){ setMovementY(getVelocityY()); moveY(w); } else if(direction == "FRONT"){ setMovementY(-getVelocityY()); moveY(w); } } else if(SeaStar* s = dynamic_cast<SeaStar*>(other)){ damaged = true; setCurrentAnimationVector("DAMAGED"); setHealth(getHealth() - s->getDamage()); setTimer( SDL_GetTicks() + 2000 ); setCollidable(false); setVelocityX(getVelocityX()*2); setVelocityY(getVelocityY()*2); } }
void UfoObject::deadState(){ HP = 0; plingSFX->play(0.3f); addGravityToObject(true); this->bWallCollisions = false; setVelocityY(70); this->unschedule(schedule_selector(UfoObject::AIUpdate)); this->unschedule(schedule_selector(UfoObject::update)); removeWhenBelowZero(); setVelocityX(getVelocityX()*0.1); setVelocityY(getVelocityY()*0.1); objectSprite->setRotation(25); dropCoin(3); }
void Mob::checkCollision(std::vector<std::vector<int>> colMap) { collision = bounds; // Checking x-axis if (velocity.x < 0) startX = endX = (int) floor(bounds.left + (velocity.x)); else startX = endX = (int) floor(bounds.left + bounds.width + (velocity.x)); startY = (int) (bounds.top); endY = (int) (bounds.top + bounds.height); collidableTiles(colMap, startX, endX, startY, endY); collision.left += velocity.x; for (unsigned int i = 0; i < collidable.size(); i++) { if (contains(collision, collidable[i])) { setVelocityX(0); break; } } collision.left = getPosition().x; // Checking y-axis startX = (int) bounds.left; endX = (int) (bounds.left + bounds.width); if (velocity.y < 0) startY = endY = (int) floor(bounds.top + (velocity.y)); else startY = endY = (int) floor(bounds.top + bounds.height + (velocity.y)); collidableTiles(colMap, startX, endX, startY, endY); collision.top += velocity.y; for (unsigned int i = 0; i < collidable.size(); i++) { if (collision.top < collidable[i].top + collidable[i].height || collision.top + collision.height > collidable[i].top || collision.left < collidable[i].left + collidable[i].width || collision.left + collision.width < collidable[i].left) { setVelocityY(0); break; } } collision.top = getPosition().y; velocity.x *= FRICTION; velocity.y *= FRICTION; if (velocity.x > MAX_VEL) velocity.x = MAX_VEL; if (velocity.x < -MAX_VEL) velocity.x = -MAX_VEL; if (velocity.y > MAX_VEL) velocity.y = MAX_VEL; if (velocity.y < -MAX_VEL) velocity.y = -MAX_VEL; move(velocity); bounds.left = getPosition().x; bounds.top = getPosition().y; }
void UfoObject::AIUpdate(const float delta) { if((std::abs(target->getPositionX() - getPositionX() )< xAttackDistance)){ const float yDist = getPositionY() - target->getPositionY(); if(yDist<yAttackDistance && yDist > -100 && HP>0){ auto babyTurf = BabyTurfelObject::create(); babyTurf->setupHitbox(0.1f, 1.0f, 16, 16, 16, 16, false); babyTurf->setObjectPositionX(getPositionX() + objectSprite->getTexture()->getPixelsWide()/2); babyTurf->setObjectPositionY(getPositionY() + objectSprite->getTexture()->getPixelsWide()); babyTurf->getPhysicsBody()->setCategoryBitmask((int)PhysicsCategory::Enemy); babyTurf->getPhysicsBody()->setCollisionBitmask((int)PhysicsCategory::None); babyTurf->getPhysicsBody()->setContactTestBitmask((int)PhysicsCategory::Player|(int)PhysicsCategory::Hazard); const float xPow = (float)100.0f/cocos2d::random(1000, 2000) + (target->getPositionX() - getPositionX())/xAttackDistance; babyTurf->setVelocityX(xPow * throwSpeedX); babyTurf->setVelocityY(throwSpeedY); addToGameObjects.pushBack(babyTurf); } } }
void Mogh::tick(gen::GameEngine* ge){ if(getHealth() < 1){ SDL_Flip(gen::global.screen); SDL_Delay(4000); ge->setGameOver(true); return; } if(damaged){ if(SDL_GetTicks() > getTimer()){ setCollidable(true); setVelocityX(getVelocityX()/2); setVelocityY(getVelocityY()/2); damaged = false; } } Hero::tick(ge); if(hpText == 0){ hpText = gen::Text::createText("c:\\WINDOWS\\Fonts\\arial.ttf", 30, "0", 0, 0, 0); hpText->setPositions(10, 10); } std::stringstream s; s << "HP: "; s << getHealth(); hpText->setMessage(s.str()); if(amountKilled == 0){ amountKilled = gen::Text::createText("c:\\WINDOWS\\Fonts\\arial.ttf", 30, "0", 0, 0, 0); amountKilled->setPositions(200, 10); } std::stringstream s2; s2 << "Killscore: "; s2 << Character::getAmountRemoved(); amountKilled->setMessage(s2.str()); hpText->tick(ge); amountKilled->tick(ge); }
void mog::Pawn::moveRightReleased() { setVelocityX(getVelocity().getX() - speed.getValue()); }
void mog::Pawn::moveRightPressed() { setVelocityX(getVelocity().getX() + speed.getValue()); }
void mog::Pawn::moveLeftReleased() { setVelocityX(getVelocity().getX() + speed.getValue()); }
void mog::Pawn::moveLeftPressed() { setVelocityX(getVelocity().getX() - speed.getValue()); }
void LevelMovableTile::checkCollisions(const sf::Vector2f& nextPosition) { sf::Vector2f oldPosition = getPosition(); const sf::FloatRect& bb = *getBoundingBox(); sf::FloatRect nextBoundingBoxX(nextPosition.x, bb.top, bb.width, bb.height); sf::FloatRect nextBoundingBoxY(bb.left, nextPosition.y, bb.width, bb.height); WorldCollisionQueryRecord rec; rec.excludedGameObject = this; rec.ignoreMobs = false; bool isMovingDown = nextPosition.y > bb.top; bool isMovingRight = nextPosition.x > bb.left; // should we use strategy 2: try y direction first, then x direction? bool tryYfirst = false; rec.boundingBox = nextBoundingBoxX; rec.collisionDirection = isMovingRight ? CollisionDirection::Right : CollisionDirection::Left; if (m_level->collides(rec)) { if (std::abs(nextPosition.x - rec.safeLeft) > std::abs(getVelocity().x) + 10.f) { tryYfirst = true; } } if (!tryYfirst) { // check for collision on x axis rec.boundingBox = nextBoundingBoxX; rec.collisionDirection = isMovingRight ? CollisionDirection::Right : CollisionDirection::Left; bool collidesX = m_level->collides(rec); if (collidesX) { setAccelerationX(0.f); setVelocityX(0.f); setPositionX(rec.safeLeft); nextBoundingBoxY.left = rec.safeLeft; } else { nextBoundingBoxY.left = nextPosition.x; } // check for collision on y axis rec.boundingBox = nextBoundingBoxY; rec.collisionDirection = isMovingDown ? CollisionDirection::Down : CollisionDirection::Up; rec.movingParent = nullptr; bool collidesY = m_level->collides(rec); setMovingParent(rec.movingParent); if (collidesY) { setAccelerationY(0.f); setVelocityY(0.f); setPositionY(rec.safeTop); } } else { // check for collision on y axis rec.boundingBox = nextBoundingBoxY; rec.collisionDirection = isMovingDown ? CollisionDirection::Down : CollisionDirection::Up; bool collidesY = m_level->collides(rec); if (collidesY) { setMovingParent(rec.movingParent); setAccelerationY(0.f); setVelocityY(0.f); setPositionY(rec.safeTop); nextBoundingBoxX.top = rec.safeTop; } else { nextBoundingBoxX.top = nextPosition.y; } // check for collision on x axis rec.boundingBox = nextBoundingBoxX; rec.collisionDirection = isMovingRight ? CollisionDirection::Right : CollisionDirection::Left; bool collidesX = m_level->collides(rec); if (collidesX) { setAccelerationX(0.f); setVelocityX(0.f); setPositionX(rec.safeLeft); } } // check for wrong parent if (MovingTile* mt = getMovingParent()) { if (mt->getBoundingBox()->top + Epsilon < getBoundingBox()->top + getBoundingBox()->width) { setMovingParent(nullptr); } } if (dist(oldPosition, getPosition()) > TILE_SIZE_F / 2.f + norm(getVelocity())) { setPosition(oldPosition); setMovingParent(nullptr); setVelocity(sf::Vector2f(0.f, 0.f)); setAcceleration(sf::Vector2f(0.f, 0.f)); setState(GameObjectState::Crumbling); m_isCollidable = false; } }