/*** This should be called when Objects colided. If 1/2 of the object height is > the other objects minY it will hurt the other object with damage dmg.***/ void EnemyObject::simpleWalkerHurt(GameObject* pPlayer, const uint32_t otherType) { auto player = pPlayer->getHitbox(); auto rThis = getHitbox(); if (rThis->getMidY() > player->getMinY()) { pPlayer->hurt(dmg, Vec2(getVelocityX(), fSimpleHurtForce)); } }
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); } } }
// Check for collision, correct position if necessary // Colliding will stop the player and push them back. void Player::collideWithTile(World *ptr, int id){ if(ptr->world.find(id) == ptr->world.end()) return; //No such field on map Entity *entity = nullptr; if(ptr->world[id].find(LayerType::LAYER_STONES) != ptr->world[id].end()) entity = ptr->world[id][LayerType::LAYER_STONES]; else if(ptr->world[id].find(LayerType::LAYER_BLOCKS) != ptr->world[id].end()) entity = ptr->world[id][LayerType::LAYER_BLOCKS]; else return; //Field has no blocks and stones Hitbox self = getHitbox(); Hitbox block = entity->getHitbox(); if(Hitbox::collide(self, block)) { sf::Vector2f offset(0, 0); // calculate where to push the player if(goDown) offset.y = block.getMinY() - self.getMaxY() - 1; // Negative value -- push up and a little more if(goUp) offset.y = block.getMaxY() - self.getMinY() + 1; // Positive value -- push down if(goRight) offset.x = block.getMinX() - self.getMaxX() - 1; // push left if(goLeft) offset.x = block.getMaxX() - self.getMinX() + 1; // push right // Correct position and update player position += offset; update(0); // instant correction, no velocity // TODO: Going towards a wall causes player's sprite to derp out -- find out why // Lock further movement if(goDown) goDown = lockChangeDirection = false; if(goUp) goUp = lockChangeDirection = false; if(goRight) goRight = lockChangeDirection = false; if(goLeft) goLeft = lockChangeDirection = false; } }
void Bracket::draw(float dt) { Window::instance()->getRW()->Draw(sprite); Window::instance()->drawHitbox(getHitbox(), sf::Color::Blue); }
void Explosion::draw(float dt) { if(animation == nullptr) return; animation->process(dt); Window::instance()->drawHitbox(getHitbox(),sf::Color::Green); }
void Player::draw(float dt) { animation->process(dt); animation->draw(); Window::instance()->drawHitbox(getHitbox(), hitboxColor); }