SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Steer.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/ITask.h"
7 #include "common/String.h"
8 #include "common/Types.h"
9 #include "common/Math.h"
10 #include "movement/Steering.h"
12 #include "common/Random.h"
13 
14 namespace ai {
15 
16 class Steer: public ITask {
17 protected:
19 public:
20  Steer(const std::string& name, const std::string& parameters, const ConditionPtr& condition, const movement::WeightedSteering &w) :
21  ITask(name, parameters, condition), _w(w) {
22  _type = "Steer";
23  }
24  class Factory: public ISteerNodeFactory {
25  public:
26  TreeNodePtr create (const SteerNodeFactoryContext *ctx) const override {
27  movement::WeightedSteerings weightedSteerings;
28 
29  if (ctx->parameters.empty()) {
30  for (const SteeringPtr& s : ctx->steerings) {
31  weightedSteerings.push_back(movement::WeightedData(s, 1.0f));
32  }
33  } else {
34  std::vector<std::string> tokens;
35  Str::splitString(ctx->parameters, tokens, ",");
36  ai_assert(tokens.size() == ctx->steerings.size(), "weights doesn't match steerings methods count");
37  const int tokenAmount = static_cast<int>(tokens.size());
38  for (int i = 0; i < tokenAmount; ++i) {
39  weightedSteerings.push_back(movement::WeightedData(ctx->steerings[i], Str::strToFloat(tokens[i])));
40  }
41  }
42  const movement::WeightedSteering w(weightedSteerings);
43  return std::make_shared<Steer>(ctx->name, ctx->parameters, ctx->condition, w);
44  }
45  };
46  static const Factory& getFactory() {
47  static Factory FACTORY;
48  return FACTORY;
49  }
50 
51  TreeNodeStatus doAction(const AIPtr& entity, int64_t deltaMillis) override {
52  const ICharacterPtr& chr = entity->getCharacter();
53  const MoveVector& mv = _w.execute(entity, chr->getSpeed());
54  if (isInfinite(mv.getVector())) {
55  return FAILED;
56  }
57 
58  const float deltaSeconds = static_cast<float>(deltaMillis) / 1000.0f;
59  chr->setPosition(chr->getPosition() + (mv.getVector() * deltaSeconds));
60  chr->setOrientation(fmodf(chr->getOrientation() + mv.getRotation() * deltaSeconds, glm::two_pi<float>()));
61  return FINISHED;
62  }
63 };
64 
65 }
A node for your real actions in the behaviour tree.
Definition: ITask.h:33
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
Definition: Steer.h:24
Definition: MoveVector.h:10
TreeNodeStatus doAction(const AIPtr &entity, int64_t deltaMillis) override
Definition: Steer.h:51
Defines some basic movement algorithms like Wandering, Seeking and Fleeing.
Definition: AIFactories.h:24
Definition: Steer.h:16
This class allows you to weight several steering methods and get a blended MoveVector out of it...
Definition: WeightedSteering.h:29
Definition: AIFactories.h:82
Steering and weight as input for WeightedSteering.
Definition: WeightedSteering.h:14