完善movement
This commit is contained in:
parent
2423977721
commit
13a82ab13d
@ -18,6 +18,7 @@ include_directories(
|
|||||||
/usr/include/mysql
|
/usr/include/mysql
|
||||||
/usr/include/jsoncpp
|
/usr/include/jsoncpp
|
||||||
/usr/include/hiredis
|
/usr/include/hiredis
|
||||||
|
/usr/include/eigen3
|
||||||
../../third_party
|
../../third_party
|
||||||
.
|
.
|
||||||
)
|
)
|
||||||
|
@ -1,16 +1,28 @@
|
|||||||
#include "precompile.h"
|
#include "precompile.h"
|
||||||
|
|
||||||
#include "movement.h"
|
#include "movement.h"
|
||||||
|
#include "entity.h"
|
||||||
|
|
||||||
void MovementComponent::Update(int delta_time)
|
void MovementComponent::Update(int delta_time)
|
||||||
{
|
{
|
||||||
if (path_index_ < paths_.size()) {
|
if (path_index_ < paths_.size()) {
|
||||||
|
MovePathPoint& target_point = paths_[path_index_];
|
||||||
|
owner->pos = owner->pos + (target_point.pos - owner->pos) * delta_time * move_speed_;
|
||||||
|
if (owner->pos == target_point.pos) {
|
||||||
|
++path_index_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MovementComponent::GetMovePosition(int delta_time, Vector2D& out_pos)
|
bool MovementComponent::GetMovePosition(int delta_time, Vector2D& out_pos)
|
||||||
{
|
{
|
||||||
return false;
|
if (path_index_ < paths_.size()) {
|
||||||
|
MovePathPoint& target_point = paths_[path_index_];
|
||||||
|
out_pos = owner->pos + (target_point.pos - owner->pos) * delta_time * move_speed_;
|
||||||
|
} else {
|
||||||
|
out_pos = owner->pos;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MovementComponent::AddPathPoint(Vector2D& pos, float distance, float speed)
|
void MovementComponent::AddPathPoint(Vector2D& pos, float distance, float speed)
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#include "precompile.h"
|
#include "precompile.h"
|
||||||
|
|
||||||
|
#include <Eigen/Core>
|
||||||
|
#include <Eigen/Dense>
|
||||||
|
|
||||||
#include "cs_proto.pb.h"
|
#include "cs_proto.pb.h"
|
||||||
|
|
||||||
void Vector2D::ToPB(cs::MFVector2D* pb_obj)
|
void Vector2D::ToPB(cs::MFVector2D* pb_obj)
|
||||||
@ -7,3 +10,34 @@ void Vector2D::ToPB(cs::MFVector2D* pb_obj)
|
|||||||
pb_obj->set_x(x);
|
pb_obj->set_x(x);
|
||||||
pb_obj->set_y(y);
|
pb_obj->set_y(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Vector2D::Normalize()
|
||||||
|
{
|
||||||
|
Eigen::Vector2f v(x, y);
|
||||||
|
v.normalize();
|
||||||
|
x = v[0];
|
||||||
|
y = v[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Vector2D::operator == (const Vector2D& b)
|
||||||
|
{
|
||||||
|
return std::abs(x - b.x) < 0.01f && std::abs(y - b.y) < 0.01f;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2D Vector2D::operator + (const Vector2D& b)
|
||||||
|
{
|
||||||
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) + Eigen::Vector2f(b.x, b.y);
|
||||||
|
return Vector2D(v[0], v[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2D Vector2D::operator - (const Vector2D& b)
|
||||||
|
{
|
||||||
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) - Eigen::Vector2f(b.x, b.y);
|
||||||
|
return Vector2D(v[0], v[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2D Vector2D::operator * (float scale)
|
||||||
|
{
|
||||||
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) * scale;
|
||||||
|
return Vector2D(v[0], v[1]);
|
||||||
|
}
|
||||||
|
@ -16,8 +16,16 @@ namespace cs
|
|||||||
|
|
||||||
struct Vector2D
|
struct Vector2D
|
||||||
{
|
{
|
||||||
float x = 0.0;
|
float x = 0.0f;
|
||||||
float y = 0.0;
|
float y = 0.0f;
|
||||||
|
|
||||||
|
Vector2D(float _x = 0.0f, float _y = 0.0f):x(_x), y(_y) {};
|
||||||
|
|
||||||
void ToPB(cs::MFVector2D* pb_obj);
|
void ToPB(cs::MFVector2D* pb_obj);
|
||||||
|
void Normalize();
|
||||||
|
|
||||||
|
bool operator == (const Vector2D& b);
|
||||||
|
Vector2D operator + (const Vector2D& b);
|
||||||
|
Vector2D operator - (const Vector2D& b);
|
||||||
|
Vector2D operator * (float scale);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user