void LifeForm::handle_event(const SDL_Event &event) { auto world = m_world.lock(); if (!world) { return; } if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.button.button == SDL_BUTTON_LEFT) { const WorldRect viewport(world->get_viewport()); WorldPosition pos = get_pos(); WorldRect rect((int32_t)round(pos.x) - width/2, (int32_t)round(pos.y) - height/2, width, height); if (rect.contains(WorldPoint(event.button.x + viewport.x, event.button.y + viewport.y))) { set_focused(true); } else { set_focused(false); } } else if (focused() && event.button.button == SDL_BUTTON_RIGHT) { // Cancel m_lifeform's commands while (!m_commands.empty()) { m_commands.pop(); } const WorldRect viewport(world->get_viewport()); for (auto pt : world->get_path(get_pos(), WorldPosition(event.button.x + viewport.x, event.button.y + viewport.y))) { m_commands.emplace(new MoveCommand(this, WorldPosition(pt.x, pt.y))); } } } }
// Assumes that the extents for the current tree have been set and the items belonging // to this level are in m_objects. void PopulateSubTrees() { // Create children trees. WorldPoint mid = m_p0 + (m_p1-m_p0)/2.0; Octree<T>* boxes[8]; boxes[0] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(m_p0.x()-.001, m_p0.y()-.001, m_p0.z()-.001), WorldPoint(mid.x()+.001, mid.y()+.001, mid.z()+.001)); boxes[1] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(mid.x()-.001, m_p0.y()-.001, m_p0.z()-.001), WorldPoint(m_p1.x()+.001, mid.y()+.001, mid.z()+.001)); boxes[2] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(m_p0.x()-.001, mid.y()-.001, m_p0.z()-.001), WorldPoint(mid.x()+.001, m_p1.y()+.001, mid.z()+.001)); boxes[3] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(mid.x()-.001, mid.y()-.001, m_p0.z()-.001), WorldPoint(m_p1.x()+.001, m_p1.y()+.001, mid.z()+.001)); boxes[4] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(m_p0.x()-.001, m_p0.y()-.001, mid.z()-.001), WorldPoint(mid.x()+.001, mid.y()+.001, m_p1.z()+.001)); boxes[5] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(mid.x()-.001, m_p0.y()-.001, mid.z()-.001), WorldPoint(m_p1.x()+.001, mid.y()+.001, m_p1.z()+.001)); boxes[6] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(m_p0.x()-.001, mid.y()-.001, mid.z()-.001), WorldPoint(mid.x()+.001, m_p1.y()+.001, m_p1.z()+.001)); boxes[7] = new Octree<T>(m_objects.begin(), m_objects.end(), m_minObjects, WorldPoint(mid.x()-.001, mid.y()-.001, mid.z()-.001), WorldPoint(m_p1.x()+.001, m_p1.y()+.001, m_p1.z()+.001)); for(unsigned int i = 0; i < 8; ++i) { if( boxes[i]->GetNumObjects() > 0 ) { m_children.push_back(boxes[i]); } else { delete boxes[i]; } } }