float distance (const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE) const { Vector v1 = get_anchor_pos(*this, ap); Vector v2 = get_anchor_pos(other, ap); return ((v1 - v2).norm ()); }
void Owl::active_update (float elapsed_time) { BadGuy::active_update (elapsed_time); if(frozen) return; if (carried_object != NULL) { if (!is_above_player ()) { Vector obj_pos = get_anchor_pos (bbox, ANCHOR_BOTTOM); obj_pos.x -= 16.0; /* FIXME: Actually do use the half width of the carried object here. */ obj_pos.y += 3.0; /* Move a little away from the hitbox (the body). Looks nicer. */ //To drop enemie before leave the screen if (obj_pos.x<=16 || obj_pos.x+16>=Sector::current()->get_width()){ carried_object->ungrab (*this, dir); carried_object = NULL; } else carried_object->grab (*this, obj_pos, dir); } else { /* if (is_above_player) */ carried_object->ungrab (*this, dir); carried_object = NULL; } } }
void TextObject::draw(DrawingContext& context) { context.push_transform(); context.set_translation(Vector(0, 0)); if(fading > 0) { context.set_alpha((fadetime-fading) / fadetime); } else if(fading < 0) { context.set_alpha(-fading / fadetime); } else if(!visible) { context.pop_transform(); return; } float width = 500; float height = 70; Vector spos = pos + get_anchor_pos(Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), width, height, anchor); context.draw_filled_rect(spos, Vector(width, height), Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50); if (centered) { context.draw_center_text(font, text, spos, LAYER_GUI-40, TextObject::default_color); } else { context.draw_text(font, text, spos + Vector(10, 10), ALIGN_LEFT, LAYER_GUI-40, TextObject::default_color); } context.pop_transform(); }
SpriteParticle::SpriteParticle(std::string sprite_name, std::string action, Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration, int drawing_layer) : sprite(), position(position), velocity(velocity), acceleration(acceleration), drawing_layer(drawing_layer), light(0.0f,0.0f,0.0f), lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")), glow(false) { sprite = sprite_manager->create(sprite_name); if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name); sprite->set_action(action, 1); sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor); if(sprite_name=="images/objects/particles/sparkle.sprite") glow = true; if(action=="dark") { lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE)); lightsprite->set_color(Color(0.1f, 0.1f, 0.1f)); } }
void MovingSprite::set_action(const std::string& action, int loops, AnchorPoint anchorPoint) { Rectf old_bbox = bbox; sprite->set_action(action, loops); float w = sprite->get_current_hitbox_width(); float h = sprite->get_current_hitbox_height(); set_size(w, h); set_pos(get_anchor_pos(old_bbox, w, h, anchorPoint)); }
void SkyDive::explode (void) { if (!is_valid()) return; auto explosion = std::make_shared<Explosion>(get_anchor_pos (bbox, ANCHOR_BOTTOM)); explosion->hurts(true); explosion->pushes(false); Sector::current()->add_object(explosion); remove_me (); } /* void explode */
SpriteParticle::SpriteParticle(std::string sprite_name, std::string action, Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration, int drawing_layer) : sprite(), position(position), velocity(velocity), acceleration(acceleration), drawing_layer(drawing_layer) { sprite = sprite_manager->create(sprite_name); if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name); sprite->set_action(action, 1); sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor); }
void FloatingImage::draw(DrawingContext& context) { context.push_transform(); context.set_translation(Vector(0, 0)); if(fading > 0) { context.set_alpha((fadetime-fading) / fadetime); } else if(fading < 0) { context.set_alpha(-fading / fadetime); } else if(!visible) { context.pop_transform(); return; } Vector spos = pos + get_anchor_pos(Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), sprite->get_width(), sprite->get_height(), anchor); sprite->draw(context, spos, layer); context.pop_transform(); }
float distance (const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE) const { Vector v = get_anchor_pos (*this, ap); return ((v - other).norm ()); }
void Dispenser::launch_badguy() { if (frozen) { return; } //FIXME: Does is_offscreen() work right here? if (!is_offscreen()) { Direction launchdir = dir; if( !autotarget && start_dir == AUTO ){ Player* player = this->get_nearest_player(); if( player ){ launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT; } } if (badguys.size() > 1) { if (random) { next_badguy = gameRandom.rand(badguys.size()); } else { next_badguy++; if (next_badguy >= badguys.size()) next_badguy = 0; } } std::string badguy = badguys[next_badguy]; if(badguy == "random") { log_warning << "random is outdated; use a list of badguys to select from." << std::endl; return; } if(badguy == "goldbomb") { log_warning << "goldbomb is not allowed to be dispensed" << std::endl; return; } try { GameObjectPtr game_object; Vector spawnpoint; Rectf object_bbox; /* Need to allocate the badguy first to figure out its bounding box. */ game_object = ObjectFactory::instance().create(badguy, get_pos(), launchdir); if (game_object == NULL) throw std::runtime_error("Creating " + badguy + " object failed."); BadGuy& bad_guy = dynamic_cast<BadGuy&>(*game_object); object_bbox = bad_guy.get_bbox(); switch (type) { case DT_DROPPER: spawnpoint = get_anchor_pos (bbox, ANCHOR_BOTTOM); spawnpoint.x -= 0.5 * object_bbox.get_width(); break; case DT_ROCKETLAUNCHER: case DT_CANNON: spawnpoint = get_pos(); /* top-left corner of the cannon */ if (launchdir == LEFT) spawnpoint.x -= object_bbox.get_width() + 1; else spawnpoint.x += bbox.get_width() + 1; break; default: break; } /* Now we set the real spawn position */ bad_guy.set_pos(spawnpoint); /* We don't want to count dispensed badguys in level stats */ bad_guy.countMe = false; Sector::current()->add_object(game_object); } catch(const std::exception& e) { log_warning << "Error dispensing badguy: " << e.what() << std::endl; return; } } }
Player* get_nearest_player (const Rectf& pos) const { return (get_nearest_player (get_anchor_pos (pos, ANCHOR_MIDDLE))); }
void Dispenser::launch_badguy() { if (m_badguys.empty()) return; if (m_frozen) return; if (m_limit_dispensed_badguys && m_current_badguys >= m_max_concurrent_badguys) return; //FIXME: Does is_offscreen() work right here? if (!is_offscreen() && !Editor::is_active()) { Direction launchdir = m_dir; if ( !m_autotarget && m_start_dir == Direction::AUTO ){ Player* player = get_nearest_player(); if ( player ){ launchdir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT; } } if (m_badguys.size() > 1) { if (m_random) { m_next_badguy = static_cast<unsigned int>(gameRandom.rand(static_cast<int>(m_badguys.size()))); } else { m_next_badguy++; if (m_next_badguy >= m_badguys.size()) m_next_badguy = 0; } } std::string badguy = m_badguys[m_next_badguy]; if (badguy == "random") { log_warning << "random is outdated; use a list of badguys to select from." << std::endl; return; } if (badguy == "goldbomb") { log_warning << "goldbomb is not allowed to be dispensed" << std::endl; return; } try { /* Need to allocate the badguy first to figure out its bounding box. */ auto game_object = GameObjectFactory::instance().create(badguy, get_pos(), launchdir); if (game_object == nullptr) throw std::runtime_error("Creating " + badguy + " object failed."); auto& bad_guy = dynamic_cast<BadGuy&>(*game_object); Rectf object_bbox = bad_guy.get_bbox(); Vector spawnpoint; switch (m_type) { case DispenserType::DROPPER: spawnpoint = get_anchor_pos (m_col.m_bbox, ANCHOR_BOTTOM); spawnpoint.x -= 0.5f * object_bbox.get_width(); break; case DispenserType::ROCKETLAUNCHER: case DispenserType::CANNON: spawnpoint = get_pos(); /* top-left corner of the cannon */ if (launchdir == Direction::LEFT) spawnpoint.x -= object_bbox.get_width() + 1; else spawnpoint.x += m_col.m_bbox.get_width() + 1; break; case DispenserType::POINT: spawnpoint = m_col.m_bbox.p1(); break; default: break; } /* Now we set the real spawn position */ bad_guy.set_pos(spawnpoint); /* We don't want to count dispensed badguys in level stats */ bad_guy.m_countMe = false; /* Set reference to dispenser in badguy itself */ if (m_limit_dispensed_badguys) { bad_guy.set_parent_dispenser(this); m_current_badguys++; } Sector::get().add_object(std::move(game_object)); } catch(const std::exception& e) { log_warning << "Error dispensing badguy: " << e.what() << std::endl; return; } } }