OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
collisionzone.cpp
Go to the documentation of this file.
1#include "collisionzone.h"
2
3#include "world/objects/npc.h"
4#include "worldobjects.h"
5#include "world.h"
6
7#include "game/serialize.h"
9
12
13CollisionZone::CollisionZone(World& w, const Tempest::Vec3& pos, const ParticleFx& pfx)
14 :owner(&w), time0(w.tickCount()), type(T_Capsule), pos(pos), size(0,0,0), pfx(&pfx) {
15 owner->enableCollizionZone(*this);
16 }
17
18CollisionZone::CollisionZone(World& w, const Tempest::Vec3& pos, const Tempest::Vec3& size)
19 :owner(&w), time0(w.tickCount()), type(T_BBox), pos(pos), size(size) {
20 owner->enableCollizionZone(*this);
21 }
22
24 : owner(other.owner), cb(std::move(other.cb)), time0(other.time0), type(other.type), pos(other.pos), size(other.size),
25 pfx(other.pfx), intersect(std::move(other.intersect)) {
26 other.owner = nullptr;
27 if(owner!=nullptr) {
28 owner->enableCollizionZone (*this);
29 owner->disableCollizionZone(other);
30 }
31 }
32
34 if(other.owner!=nullptr)
35 other.owner->disableCollizionZone(other);
36 if(owner!=nullptr)
37 owner->disableCollizionZone(*this);
38
39 std::swap(owner, other.owner);
40 std::swap(cb, other.cb);
41 std::swap(time0, other.time0);
42 std::swap(type, other.type);
43 std::swap(pos, other.pos);
44 std::swap(size, other.size);
45 std::swap(pfx, other.pfx);
46 std::swap(intersect, other.intersect);
47
48 if(other.owner!=nullptr)
49 other.owner->enableCollizionZone(other);
50 if(owner!=nullptr)
51 owner->enableCollizionZone(*this);
52 return *this;
53 }
54
56 if(owner!=nullptr)
57 owner->disableCollizionZone(*this);
58 }
59
60void CollisionZone::save(Serialize& fout) const {
61 fout.write(uint32_t(intersect.size()));
62 for(auto i:intersect)
63 fout.write(i);
64 }
65
67 uint32_t size=0;
68 fin.read(size);
69 intersect.resize(size);
70 for(auto& i:intersect)
71 fin.read(i);
72 for(size_t i=0;i<intersect.size();)
73 if(intersect[i]==nullptr) {
74 intersect[i] = intersect.back();
75 intersect.pop_back();
76 } else {
77 ++i;
78 }
79 }
80
81bool CollisionZone::checkPos(const Tempest::Vec3& p) const {
82 auto dp = p - pos;
83 if(type==T_BBox) {
84 if(std::fabs(dp.x)<size.x &&
85 std::fabs(dp.y)<size.y &&
86 std::fabs(dp.z)<size.z)
87 return true;
88 }
89 else if(type==T_Capsule) {
90 if(dp.x*dp.x+dp.z*dp.z<size.x*size.x &&
91 std::fabs(dp.y)<std::fabs(size.y))
92 return true;
93 }
94 return false;
95 }
96
98 for(auto i:intersect)
99 if(i==&npc)
100 return;
101 intersect.push_back(&npc);
102
103 if(cb)
104 cb(npc);
105 }
106
107void CollisionZone::tick(uint64_t /*dt*/) {
108 for(size_t i=0;i<intersect.size();) {
109 Npc& npc = *intersect[i];
110 auto pos = npc.position();
111 if(!checkPos(pos+Tempest::Vec3(0,npc.translateY(),0))) {
112 intersect[i] = intersect.back();
113 intersect.pop_back();
114 } else {
115 ++i;
116 }
117 }
118 if(pfx!=nullptr) {
119 auto dim = pfx->shpDim*pfx->shpScale(owner->tickCount()-time0);
120 size = dim;
121 }
122 /*
123 if(intersect.size()==0) {
124 disableTicks();
125 }*/
126 }
127
128void CollisionZone::setCallback(std::function<void(Npc&)> f) {
129 cb = f;
130 }
131
132void CollisionZone::setPosition(const Tempest::Vec3& p) {
133 pos = p;
134 }
void tick(uint64_t dt)
void setCallback(std::function< void(Npc &npc)> f)
void setPosition(const Tempest::Vec3 &p)
void load(Serialize &fin)
bool checkPos(const Tempest::Vec3 &pos) const
void save(Serialize &fout) const
CollisionZone & operator=(CollisionZone &&other)
void onIntersect(Npc &npc)
Definition npc.h:25
float translateY() const
Definition npc.cpp:680
auto position() const -> Tempest::Vec3
Definition npc.cpp:628
Tempest::Vec3 shpDim
Definition particlefx.h:72
float shpScale(uint64_t time) const
void write(const Arg &... a)
Definition serialize.h:76
void read(Arg &... a)
Definition serialize.h:81
Definition world.h:31
void disableCollizionZone(CollisionZone &z)
Definition world.cpp:550
void enableCollizionZone(CollisionZone &z)
Definition world.cpp:546
uint64_t tickCount() const
Definition world.cpp:387