Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "speed" property to path nodes. #1424

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 50 additions & 10 deletions src/editor/node_marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,11 @@ NodeMarker::update_iterator()
Vector
NodeMarker::get_point_vector() const
{
std::vector<Path::Node>::iterator next_node = m_node + 1;
if (next_node == m_path->m_nodes.end()) {
if (m_path->m_mode == WalkMode::CIRCULAR) {
//loop to the first node
return m_path->m_nodes.begin()->position - m_node->position;
} else {
return Vector(0,0);
}
std::vector<Path::Node>::const_iterator next = next_node();
if (next == m_path->m_nodes.end()) {
return Vector(0,0);
} else {
//point to the next node
return next_node->position - m_node->position;
return next->position - m_node->position;
}
}

Expand All @@ -64,6 +58,7 @@ NodeMarker::move_to(const Vector& pos)
{
MovingObject::move_to(pos);
m_node->position = m_col.m_bbox.get_middle();
update_node_times();
}

void
Expand All @@ -73,6 +68,9 @@ NodeMarker::editor_delete()
{
return;
}
std::vector<Path::Node>::iterator prev = prev_node();
std::vector<Path::Node>::const_iterator next = next_node();
update_node_time(prev, next);
m_path->m_nodes.erase(m_node);
Editor::current()->update_node_iterators();
}
Expand All @@ -82,13 +80,55 @@ NodeMarker::get_settings()
{
ObjectSettings result(_("Path Node"));
result.add_float(_("Time"), &(m_node->time));
result.add_float(_("Speed"), &(m_node->speed));
return result;
}

void
NodeMarker::editor_update()
{
set_pos(m_node->position - Vector(8, 8));
update_node_time(m_node, next_node());
}

std::vector<Path::Node>::iterator NodeMarker::prev_node() {
Copy link
Member

@Zwatotem Zwatotem Jun 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me type name std::vector<Path::Node>::iterator seems a bit long and hard to read. It could be my lack of experience in bigger projects. Also I'm not sure, what is our policy regarding typedef.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a couple typedef's lying around the code, so we definitely use them.

std::vector<Path::Node>::iterator node = m_node;
if (node == m_path->m_nodes.begin()) {
if (m_path->m_mode == WalkMode::CIRCULAR) {
node = m_path->m_nodes.end();
} else {
return m_path->m_nodes.end();
}
}
--node;
return node;
}

std::vector<Path::Node>::const_iterator NodeMarker::next_node() const {
std::vector<Path::Node>::const_iterator node = m_node + 1;
if (node == m_path->m_nodes.end()) {
if (m_path->m_mode == WalkMode::CIRCULAR) {
node = m_path->m_nodes.begin();
}
}
return node;
}

void NodeMarker::update_node_times() {
update_node_time(prev_node(), m_node);
update_node_time(m_node, next_node());
}

void NodeMarker::update_node_time(std::vector<Path::Node>::iterator current, std::vector<Path::Node>::const_iterator next) {
if (current == m_path->m_nodes.end() || next == m_path->m_nodes.end()) {
return; // Nothing to do.
}
if (current->speed > 0) {
float delta = (next->position - current->position).norm();
if (delta > 0) {
current->time = delta / current->speed;
}
}
}

/* EOF */
4 changes: 4 additions & 0 deletions src/editor/node_marker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ class NodeMarker : public MarkerObject
virtual void editor_update() override;

void update_iterator();
void update_node_times();

private:
Path* m_path;
std::vector<Path::Node>::iterator prev_node();
std::vector<Path::Node>::const_iterator next_node() const;
void update_node_time(std::vector<Path::Node>::iterator current, std::vector<Path::Node>::const_iterator next);

public:
std::vector<Path::Node>::iterator m_node;
Expand Down
3 changes: 2 additions & 1 deletion src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,10 @@ EditorOverlayWidget::add_path_node()
new_node.position = m_sector_pos;
new_node.time = 1;
m_edited_path->m_nodes.insert(m_last_node_marker->m_node + 1, new_node);
Sector::get().add<NodeMarker>(m_edited_path, m_edited_path->m_nodes.end() - 1, m_edited_path->m_nodes.size() - 1);
auto& new_marker = Sector::get().add<NodeMarker>(m_edited_path, m_edited_path->m_nodes.end() - 1, m_edited_path->m_nodes.size() - 1);
//last_node_marker = dynamic_cast<NodeMarker*>(marker.get());
update_node_iterators();
new_marker.update_node_times();
m_editor.get_sector()->flush_game_objects();
grab_object();
}
Expand Down
6 changes: 6 additions & 0 deletions src/object/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Path::Path(const Vector& pos) :
Node first_node;
first_node.position = pos;
first_node.time = 1;
first_node.speed = 0;
m_nodes.push_back(first_node);
}

Expand All @@ -88,10 +89,12 @@ Path::read(const ReaderMapping& reader)
// each new node will inherit all values from the last one
Node node;
node.time = 1;
node.speed = 0;
if ( (!node_mapping.get("x", node.position.x) ||
!node_mapping.get("y", node.position.y)))
throw std::runtime_error("Path node without x and y coordinate specified");
node_mapping.get("time", node.time);
node_mapping.get("speed", node.speed);

if (node.time <= 0)
throw std::runtime_error("Path node with non-positive time");
Expand Down Expand Up @@ -123,6 +126,9 @@ Path::save(Writer& writer)
if (nod.time != 1.0f) {
writer.write("time", nod.time);
}
if (nod.speed != 0.0f) {
writer.write("speed", nod.speed);
}
writer.end_list("node");
}

Expand Down
4 changes: 3 additions & 1 deletion src/object/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ class Path final
public:
Vector position; /**< the position of this node */
float time; /**< time (in seconds) to get from this node to next node */
float speed; /**< speed (in px/seconds); editor use only */

Node() :
position(),
time()
time(),
speed()
{}
};

Expand Down