nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
reactions.cc
Go to the documentation of this file.
1#include "client/reactions.h"
2
3#include <algorithm>
4#include <cmath>
5#include <map>
6#include <vector>
7
8namespace nanommo {
9
10void Reactions::Hit(uint32_t monster, uint32_t player) {
11 blows_[monster] = Blow{.player = player};
12}
13
14void Reactions::Update(float seconds) {
15 for (auto& [id, blow] : blows_) {
16 blow.age += seconds;
17 }
18 // By then a monster's done rocking, and come down from any throw.
19 std::erase_if(blows_,
20 [](const auto& entry) { return entry.second.age > kHeld; });
21}
22
23void Reactions::Apply(std::vector<Figure>& figures) {
24 std::map<uint32_t, const Figure*> players;
25 for (const Figure& figure : figures) {
26 if (!figure.monster && !figure.npc) {
27 players[figure.id] = &figure;
28 }
29 }
30 for (Figure& figure : figures) {
31 if (!figure.monster) {
32 continue;
33 }
34 const auto found = blows_.find(figure.id);
35 if (found != blows_.end()) {
36 Blow& blow = found->second;
37 const auto player = players.find(blow.player);
38 if (player != players.end()) {
39 const Vector3 from = player->second->position;
40 blow.away =
41 std::atan2(figure.position.x - from.x, figure.position.z - from.z);
42 blow.aimed = true;
43 }
44 // Fast back, as the blow lands, then slowly upright again.
45 const float t = blow.age;
46 const float rock =
47 t < kRockIn ? t / kRockIn : std::exp(-(t - kRockIn) / kRockOut);
48 figure.flash = std::max(1.0f - t / kFlashTime, 0.0f);
49 figure.lean = blow.aimed ? kRock * rock : 0.0f;
50 figure.lean_yaw = blow.away;
51 }
52 // Up in the air, it tips over further the higher it's thrown, the way the
53 // last blow sent it, and comes down upright.
54 if (figure.lift > 0.0f) {
55 figure.lean += kTumble * figure.lift;
56 if (found != blows_.end()) {
57 figure.lean_yaw = found->second.away;
58 }
59 }
60 }
61}
62
63} // namespace nanommo
static constexpr float kRockOut
Definition reactions.h:22
void Apply(std::vector< Figure > &figures)
Lights up, rocks and tips the monsters, as their blows have them now.
Definition reactions.cc:23
static constexpr float kRockIn
Definition reactions.h:21
static constexpr float kFlashTime
How long a flash takes to fade, in seconds.
Definition reactions.h:17
void Hit(uint32_t monster, uint32_t player)
The player hit the monster, both by their ids.
Definition reactions.cc:10
static constexpr float kHeld
Definition reactions.h:27
void Update(float seconds)
Moves each blow's reaction on.
Definition reactions.cc:14
static constexpr float kRock
Definition reactions.h:20
static constexpr float kTumble
How far over a monster in the air tips, in radians, for each metre up.
Definition reactions.h:24
Definition chat.cc:9