3 #include <BLIB/Util/Random.hpp>
15 , transitionTime(0.f) {
21 if (stateTime >= transitionTime && state != Paused) { switchState(); }
25 const auto changeDirection = [
this, &position]() {
26 if (bl::util::Random::chance(1, 2)) {
28 const float xf = dx + radius;
29 const float p = 1.f - xf /
static_cast<float>(2 * radius);
30 if (bl::util::Random::get<float>(0.f, 1.f) < p) {
31 data.walking.dir = bl::tmap::Direction::Right;
33 else { data.walking.dir = bl::tmap::Direction::Left; }
37 const float yf = dy + radius;
38 const float p = 1.f - yf /
static_cast<float>(2 * radius);
39 if (bl::util::Random::get<float>(0.f, 1.f) < p) {
40 data.walking.dir = bl::tmap::Direction::Down;
42 else { data.walking.dir = bl::tmap::Direction::Up; }
46 transitionTime -= stateTime;
51 data.walking.steps += 1;
52 if (data.walking.steps >= data.walking.total) changeDirection();
57 data.standing.update(position, controller);
60 data.spinning.update(position, controller, dt);
63 if (stateTime >= bl::util::Random::get<float>(1.f, 5.f)) {
64 data.walking.total = bl::util::Random::get<unsigned short int>(1, 3);
65 if (position.direction != data.walking.dir)
75 void WanderBehavior::switchState() {
78 data.walking.~WalkData();
81 data.standing.~StandingBehavior();
84 data.spinning.~SpinBehavior();
90 state =
static_cast<State
>(bl::util::Random::get<int>(0, NUM_STATES));
95 new (&data.walking) WalkData();
97 static_cast<bl::tmap::Direction
>(bl::util::Random::get<unsigned int>(0, 3));
98 transitionTime = bl::util::Random::get<float>(5.f, 13.f);
101 new (&data.standing) StandingBehavior(
102 static_cast<bl::tmap::Direction
>(bl::util::Random::get<unsigned int>(0, 3)));
103 transitionTime = bl::util::Random::get<float>(1.f, 3.f);
107 bl::util::Random::get<unsigned int>(0, 2)));
108 transitionTime = bl::util::Random::get<float>(1.5f, 3.f);
115 WanderBehavior::WalkData::WalkData()
116 : dir(
bl::tmap::Direction::Down)
120 WanderBehavior::Data::Data()
Core classes and functionality for both the editor and game.
EntityControl fromDirection(bl::tmap::Direction direction)
Helper method to convert a move direction to a control.
Adding this component to an entity allows it to be controlled.
bool processControl(input::EntityControl command, bool sprint=false, bool overrideLock=false)
Processes the given command and manipulates the entity accordingly.
WanderBehavior(unsigned int radius, const sf::Vector2i &origin)
Construct a new Wander Behavior component.
void update(bl::tmap::Position &position, Controllable &controller, float dt)
Updates the entity and wanders around.
Direction
The direction to spin.
static int PixelsPerTile()