-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter_controller.cpp
More file actions
70 lines (59 loc) · 2.04 KB
/
Copy pathcharacter_controller.cpp
File metadata and controls
70 lines (59 loc) · 2.04 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "character_controller.h"
#include "iris/core/vector3.h"
#include "iris/physics/basic_character_controller.h"
#include "iris/physics/physics_system.h"
namespace trinket
{
CharacterController::CharacterController(
iris::PhysicsSystem *ps,
float speed,
float width,
float height,
float float_height)
: iris::BasicCharacterController(ps, speed, width, height, float_height)
, is_being_shunted_(false)
, shunt_end_(std::chrono::system_clock::now())
, saved_movement_direction_()
, saved_speed_(0.0)
{
}
void CharacterController::set_movement_direction(const iris::Vector3 &direction)
{
// update if not being shunted
if (!is_being_shunted_)
{
iris::BasicCharacterController::set_movement_direction(direction);
}
else
{
saved_movement_direction_ = direction;
}
}
void CharacterController::update(iris::PhysicsSystem *ps, std::chrono::milliseconds delta)
{
iris::BasicCharacterController::update(ps, delta);
if (is_being_shunted_ && (std::chrono::system_clock::now() > shunt_end_))
{
movement_direction_ = saved_movement_direction_;
speed_ = saved_speed_;
is_being_shunted_ = false;
}
}
void CharacterController::shunt(const iris::Vector3 direction, float distance, std::chrono::milliseconds time)
{
if (!is_being_shunted_)
{
saved_movement_direction_ = movement_direction_;
saved_speed_ = speed_;
movement_direction_ = direction;
speed_ = distance / (static_cast<float>(time.count()) / 1000.0f);
shunt_end_ = std::chrono::system_clock::now() + time;
is_being_shunted_ = true;
}
}
}