summary refs log tree commit diff stats
path: root/src/behaviour_system.cpp
blob: 2d462057d49d85ce4e417c018599b126eac81205 (plain) (blame)
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
#include "behaviour_system.h"
#include <random>
#include "game.h"
#include "character_system.h"
#include "direction.h"

void BehaviourSystem::tick(double dt) {
  if (game_.isGameplayPaused()) return;

  timer_.accumulate(dt);
  while (timer_.step()) {
    for (int spriteId : game_.getSprites()) {
      Sprite& sprite = game_.getSprite(spriteId);
      if (sprite.wander && !sprite.paused) {
        // 75% chance of changing what's happening
        if (std::bernoulli_distribution(0.75)(game_.getRng())) {
          // 50% chance of choosing a direction or stopping
          if (std::bernoulli_distribution(0.5)(game_.getRng())) {
            Direction dir;
            switch (std::uniform_int_distribution(0,3)(game_.getRng())) {
              case 0: dir = Direction::left; break;
              case 1: dir = Direction::up; break;
              case 2: dir = Direction::right; break;
              default: dir = Direction::down; break;
            }
            game_.getSystem<CharacterSystem>().moveInDirection(spriteId, dir);
          } else {
            game_.getSystem<CharacterSystem>().stopDirecting(spriteId);
          }
        }
      }
    }
  }
}