df::Position df::Position::getNextAdjacentPosition(df::Position to){ int delta_x = abs(to.getX() - this->getX()); int delta_y = abs(to.getY() - this->getY()); if (delta_x > delta_y){ if (to.getX() > this->getX()) return df::Position(this->getX() + 1, this->getY()); else return df::Position(this->getX() - 1, this->getY()); } else if(delta_x < delta_y){ if (to.getY() > this->getY()) return df::Position(this->getX(), this->getY() + 1); else return df::Position(this->getX(), this->getY() - 1); } else if (delta_x != 0){ if (to.getX() > this->getX()) return df::Position(this->getX() + 1, this->getY()); else return df::Position(this->getX() - 1, this->getY()); } else{ return (*this); } }
Bullet::Bullet(df::Position hero_pos) { // Set object properties. setType("Bullet"); setXVelocity(1); // move 1 space right every frame // Set starting location, based on hero's position passed in. df::Position pos(hero_pos.getX()+3, hero_pos.getY()); setPosition(pos); }
// Fire bullet towards target. void Hero::fire(df::Position target) { // See if time to fire. if (fire_countdown > 0) return; fire_countdown = fire_slowdown; // Fire Bullet towards target. df::Bullet *p = new df::Bullet(getPosition()); p->setYVelocity((float) (target.getY() - getPosition().getY()) / (float) (target.getX() - getPosition().getX())); }
int Weapon::setWeaponPosition(df::Position pos, Direction direction) { df::WorldManager &wm = df::WorldManager::getInstance(); if (direction == NORTH) { df::Position new_pos = df::Position(pos.getX() + 1, pos.getY() - 2); setSpriteIndex(0); wm.moveObject(this, new_pos); return 0; } if (direction == EAST) { df::Position new_pos = df::Position(pos.getX() + 3, pos.getY()); setSpriteIndex(1); wm.moveObject(this, new_pos); return 0; } if (direction == SOUTH) { df::Position new_pos = df::Position(pos.getX() + 1, pos.getY() + 3); setSpriteIndex(2); wm.moveObject(this, new_pos); return 0; } if (direction == WEST) { df::Position new_pos = df::Position(pos.getX() - 2, pos.getY()); setSpriteIndex(3); wm.moveObject(this, new_pos); return 0; } return -1; }
// Fire bullet towards target. void Hero::fire(df::Position target) { // See if time to fire. if (fire_countdown > 0) return; fire_countdown = fire_slowdown; // Fire Bullet towards target. df::Position *pos = new df::Position(getPosition().getX(), getPosition().getY()); Bullet *p = new Bullet(*pos); p->setYVelocity((float) (target.getY() - getPosition().getY()) / (float) (target.getX() - getPosition().getX())); // Play "fire" sound. df::Sound *p_sound = df::ResourceManager::getInstance().getSound("fire"); p_sound->play(); }
// get manhattan distance of positions int df::Position::getManhattanDistance(df::Position pos){ return abs(pos.getX() - getX()) + abs(pos.getY() - getY()); }
// return if positions are equal to each other bool df::Position::equalTo(df::Position pos){ if (this->getX() == pos.getX() && this->getY() == pos.getY()){ return true; } return false; }