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; }
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); } }
// Draw single sprite frame at screen location (x,y) with color. // If centered true then center frame at (x,y). // Return 0 if ok, else -1. int df::GraphicsManager::drawFrame(df::Position world_pos, df::Frame frame, bool centered, Color color) const{ // Error check empty string. if (frame.getString().compare("") == 0){ return -1; } int x_offset, y_offset; // Centered? Then offset (x,y) by 1/2 (width,height). if (centered){ x_offset = frame.getWidth() / 2; y_offset = frame.getHeight() / 2; } else{ x_offset = 0; y_offset = 0; } // Frame data stored in string. std::string str = frame.getString(); // Draw row by row, character by character. for (int y = 0; y < frame.getHeight(); y++){ for (int x = 0; x < frame.getWidth(); x++){ Position temp_pos(world_pos.getX() - x_offset + x, world_pos.getY() - y_offset + y); drawCh(temp_pos, str[y * frame.getWidth() + x], color); }// x }// y }
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())); }
// 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; }