void Astroid::update(sf::Time * delta_time) { Entity::update(); setXVelocity(cos(getAngle() * kDegreesToRadians) * delta_time->asSeconds()); setYVelocity(cos(getAngle() * kDegreesToRadians) * delta_time->asSeconds()); move(getXVelocity(), getYVelocity()); }
int ExampleObject::eventHandler(Event *p_event) { auto& gameManager = GameManager::getInstance(); auto& logManager = LogManager::getInstance(); auto& worldManager = WorldManager::getInstance(); if (p_event->getType() == DF_STEP_EVENT) { logManager.writeLog("ExampleObject: Step Event processed.\n"); if (getPosition().getX() == 30) { //gameManager.setGameOver(); logManager.writeLog("ExampleObject: X position reached 30. Requested to end game.\n"); } } else if (p_event->getType() == DF_OUT_EVENT) { logManager.writeLog("ExampleObject: Out Event processed.\n"); setPosition(Position(40, getPosition().getY())); } else if (p_event->getType() == DF_KEYBOARD_EVENT) { logManager.writeLog("ExampleObject: Keyboard Event processed.\n"); EventKeyboard *p_keyboard_event = static_cast <EventKeyboard *> (p_event); switch (p_keyboard_event->getKey()) { case KEY_RIGHT: // up arrow setXVelocity(abs(getXVelocity())); break; case KEY_LEFT: // down arrow setXVelocity(-abs(getXVelocity())); break; }; return 1; } return 0; }
void Platform::step(EventStep *p_s) { // move left and right if (move_countdown < 0) { setXVelocity(getXVelocity() * -1); setYVelocity(getYVelocity() * -1); move_countdown = 50; } move_countdown--; }
void Platform::hit(EventCollision *p_c) { if (((p_c->getObject1()->getType()) == "Hero") || ((p_c->getObject2()->getType()) == "Hero")) { auto heroPtr = (Object*)nullptr; auto platformPtr = (Object*)nullptr; if ((p_c->getObject1()->getType()) == "Hero") { heroPtr = p_c->getObject1(); platformPtr = p_c->getObject2(); } else { heroPtr = p_c->getObject2(); platformPtr = p_c->getObject1(); } WorldManager &world_manager = WorldManager::getInstance(); auto heroPos = heroPtr->getPosition(); auto platformPos = platformPtr->getPosition(); int xOffset = 0; int yOffset = 0; if (heroPtr->getPosition().getY() <= (this->getPosition().getY() + this->getHeight())) { if (getXVelocity() > 0) { xOffset = this->lastXDisplacement; } else if (getXVelocity() < 0) { xOffset = this->lastXDisplacement; } } if (getYVelocity() > 0) { //only adjust hero position when platform goes up //yOffset = 1; } else if (getYVelocity() < 0) { yOffset = this->lastYDisplacement; } float xCD; float yCD; bool xChanged; if (xOffset || yOffset) { heroPos = Position(heroPos.getX(), heroPos.getY()); platformPos = Position(platformPos.getX(), platformPos.getY()); xCD = platformPtr->getXVelocityCountdown(); xChanged = xCD > prevXCD; yCD = platformPtr->getYVelocityCountdown(); heroPos.setX(heroPos.getX() + xOffset); platformPos.setX(platformPos.getX()); if (!xChanged) { heroPos.setX(heroPos.getX() - xOffset); //platformPos.setX(platformPos.getX() - xOffset); } prevXCD = xCD; if (yCD <= 0) { heroPos.setY(heroPos.getY() + yOffset); platformPos.setY(platformPos.getY() + yOffset); } } if (yOffset > 0) { world_manager.moveObject(platformPtr, platformPos); world_manager.moveObject(heroPtr, heroPos); } else if (yOffset < 0) { ObjectList list = world_manager.isCollision(heroPtr, heroPos); if (!list.isEmpty()) { world_manager.markForDelete(heroPtr); } else { world_manager.moveObject(heroPtr, heroPos); world_manager.moveObject(platformPtr, platformPos); } } if (xOffset) { ObjectList list = world_manager.isCollision(heroPtr, heroPos); if (list.isEmpty()) { world_manager.moveObject(heroPtr, heroPos); world_manager.moveObject(platformPtr, platformPos); } else { world_manager.moveObject(platformPtr, platformPos); list = world_manager.isCollision(heroPtr, heroPos); if (list.isEmpty()) { world_manager.moveObject(heroPtr, heroPos); } else { //world_manager.markForDelete(heroPtr); } } } } else { // monster, trap, etc. will stop the platform completely //setXVelocityCountdown(getXVelocityCountdown() + fabs(getXVelocity())); //setYVelocityCountdown(getYVelocityCountdown() + fabs(getYVelocity())); } }