-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhiteBall.cpp
56 lines (44 loc) · 1.67 KB
/
WhiteBall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <Urho3D/Core/Context.h>
#include <Urho3D/Scene/LogicComponent.h>
#include <Urho3D/Scene/Node.h>
#include <Urho3D/Physics/RigidBody.h>
#include <Urho3D/Physics/PhysicsEvents.h>
#include <Urho3D/IO/Log.h>
#include "WhiteBall.h"
WhiteBall::WhiteBall(Context *context) : Ball(context) {
// set initial camera position
controls_.pitch_ = WHITE_BALL_INITIAL_PITH;
controls_.yaw_ = WHITE_BALL_INITIAL_YAW;
// Only the physics update event is needed: unsubscribe from the rest for optimization
SetUpdateEventMask(USE_FIXEDUPDATE);
}
void WhiteBall::RegisterObject(Context *context) {
context->RegisterFactory<WhiteBall>();
}
void WhiteBall::Init(WeakPtr<Node> cameraNode) {
Ball::Init("Materials/White.xml");
cameraNode_ = cameraNode;
}
void WhiteBall::FixedUpdate(float timeStep) {
if ((controls_.buttons_ & CTRL_PUSH)) {
if (pushButtonHoldingTime_ < MAX_PUSH_BUTTON_HOLD_TIME) {
pushButtonHoldingTime_ += timeStep;
}
} else {
if (!Urho3D::Equals(pushButtonHoldingTime_, 0.f)) {
URHO3D_LOGINFO("Push the ball!" + (String) pushButtonHoldingTime_);
Vector3 moveDirection = (node_->GetPosition() - cameraNode_->GetPosition()).Normalized();
// project the moveDirection vector to x z -> set y to 0
moveDirection.y_ = 0.f;
body_->ApplyForce(moveDirection * PUSH_FORCE_PER_SECOND * pushButtonHoldingTime_);
}
// reset holding time value
pushButtonHoldingTime_ = 0.f;
}
}
void WhiteBall::HandleCollisionWithPocket(VariantMap &eventData) {
body_->ResetForces();
SendEvent(E_WHITEBALLINPOCKET);
node_->Remove();
Remove();
}