|
|
@@ -6,6 +6,7 @@
|
|
|
#include "anki/scene/PlayerNode.h"
|
|
|
#include "anki/scene/SceneGraph.h"
|
|
|
#include "anki/scene/MoveComponent.h"
|
|
|
+#include "anki/scene/PlayerControllerComponent.h"
|
|
|
#include "anki/physics/PhysicsPlayerController.h"
|
|
|
#include "anki/physics/PhysicsWorld.h"
|
|
|
#include "anki/input/Input.h"
|
|
|
@@ -26,33 +27,59 @@ public:
|
|
|
|
|
|
Error update(SceneNode& node, F32, F32, Bool& updated) override
|
|
|
{
|
|
|
- PlayerNode& pnode = static_cast<PlayerNode&>(node);
|
|
|
+ updated = false;
|
|
|
+
|
|
|
+ PlayerControllerComponent& playerc =
|
|
|
+ node.getComponent<PlayerControllerComponent>();
|
|
|
|
|
|
// Process physics move
|
|
|
- const Transform& trf = pnode.m_player->getTransform(updated);
|
|
|
- if(updated)
|
|
|
+ if(playerc.getTimestamp() == getGlobTimestamp())
|
|
|
{
|
|
|
MoveComponent& move = node.getComponent<MoveComponent>();
|
|
|
- move.setLocalTransform(trf);
|
|
|
+ move.setLocalTransform(playerc.getTransform());
|
|
|
}
|
|
|
|
|
|
+ return ErrorCode::NONE;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+//==============================================================================
|
|
|
+// PlayerNodeFeedbackComponent2 =
|
|
|
+//==============================================================================
|
|
|
+
|
|
|
+/// Feedback component.
|
|
|
+class PlayerNodeFeedbackComponent2 final: public SceneComponent
|
|
|
+{
|
|
|
+public:
|
|
|
+ PlayerNodeFeedbackComponent2(SceneNode* node)
|
|
|
+ : SceneComponent(SceneComponent::Type::NONE, node)
|
|
|
+ {}
|
|
|
+
|
|
|
+ Error update(SceneNode& node, F32, F32, Bool& updated) override
|
|
|
+ {
|
|
|
+ updated = false;
|
|
|
+
|
|
|
+ PlayerControllerComponent& playerc =
|
|
|
+ node.getComponent<PlayerControllerComponent>();
|
|
|
+ MoveComponent& move = node.getComponent<MoveComponent>();
|
|
|
+
|
|
|
// Process input
|
|
|
const Input& in = node.getSceneGraph().getInput();
|
|
|
Vec4 moveVec(0.0);
|
|
|
|
|
|
if(in.getKey(KeyCode::W))
|
|
|
{
|
|
|
- moveVec.z() += -1.0;
|
|
|
+ moveVec.z() += 1.0;
|
|
|
}
|
|
|
|
|
|
if(in.getKey(KeyCode::A))
|
|
|
{
|
|
|
- moveVec.x() += -1.0;
|
|
|
+ moveVec.x() -= 1.0;
|
|
|
}
|
|
|
|
|
|
if(in.getKey(KeyCode::S))
|
|
|
{
|
|
|
- moveVec.z() += 1.0;
|
|
|
+ moveVec.z() -= 1.0;
|
|
|
}
|
|
|
|
|
|
if(in.getKey(KeyCode::D))
|
|
|
@@ -60,16 +87,32 @@ public:
|
|
|
moveVec.x() += 1.0;
|
|
|
}
|
|
|
|
|
|
- //if(moveVec != Vec4(0.0))
|
|
|
+ const Transform& trf = playerc.getTransform();
|
|
|
+ Vec4 dir = trf.getRotation().getColumn(2).xyz0();
|
|
|
+
|
|
|
+ if(in.getKey(KeyCode::LEFT))
|
|
|
{
|
|
|
- const F32 speed = 2.0;
|
|
|
- pnode.m_player->setVelocity(
|
|
|
- moveVec.x() * speed,
|
|
|
- moveVec.z() * speed,
|
|
|
- 0.0,
|
|
|
- Vec4(0.0, 0.0, -1.0, 0.0));
|
|
|
+ Mat3x4 rot = trf.getRotation();
|
|
|
+ rot.rotateYAxis(toRad(10.0));
|
|
|
+
|
|
|
+ dir = rot.getColumn(2).xyz0();
|
|
|
}
|
|
|
|
|
|
+ if(in.getKey(KeyCode::RIGHT))
|
|
|
+ {
|
|
|
+ Mat3x4 rot = trf.getRotation();
|
|
|
+ rot.rotateYAxis(toRad(-10.0));
|
|
|
+
|
|
|
+ dir = rot.getColumn(2).xyz0();
|
|
|
+ }
|
|
|
+
|
|
|
+ const F32 speed = 2.0;
|
|
|
+ playerc.setVelocity(
|
|
|
+ moveVec.z() * speed,
|
|
|
+ moveVec.x() * speed,
|
|
|
+ 0.0,
|
|
|
+ dir);
|
|
|
+
|
|
|
return ErrorCode::NONE;
|
|
|
}
|
|
|
};
|
|
|
@@ -107,6 +150,20 @@ Error PlayerNode::create(const CString& name)
|
|
|
|
|
|
SceneComponent* comp;
|
|
|
|
|
|
+ // Player controller component
|
|
|
+ comp = getSceneAllocator().newInstance<PlayerControllerComponent>(
|
|
|
+ this, m_player);
|
|
|
+ if(comp == nullptr)
|
|
|
+ {
|
|
|
+ return ErrorCode::OUT_OF_MEMORY;
|
|
|
+ }
|
|
|
+
|
|
|
+ err = addComponent(comp, true);
|
|
|
+ if(err)
|
|
|
+ {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+
|
|
|
// Feedback component
|
|
|
comp = getSceneAllocator().newInstance<PlayerNodeFeedbackComponent>(this);
|
|
|
if(comp == nullptr)
|
|
|
@@ -133,6 +190,19 @@ Error PlayerNode::create(const CString& name)
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
+ // Feedback component #2
|
|
|
+ comp = getSceneAllocator().newInstance<PlayerNodeFeedbackComponent2>(this);
|
|
|
+ if(comp == nullptr)
|
|
|
+ {
|
|
|
+ return ErrorCode::OUT_OF_MEMORY;
|
|
|
+ }
|
|
|
+
|
|
|
+ err = addComponent(comp, true);
|
|
|
+ if(err)
|
|
|
+ {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+
|
|
|
return err;
|
|
|
}
|
|
|
|