OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
bullet.cpp
Go to the documentation of this file.
1#include "bullet.h"
2
3#include <Tempest/Log>
4
5#include "graphics/visualfx.h"
7#include "world/objects/npc.h"
8#include "world.h"
9
10using namespace Tempest;
11
12Bullet::Bullet(World& owner, const Item& itm, const Vec3& pos)
13 :wrld(&owner) {
14 obj = wrld->physic()->bulletObj(this);
15 if(itm.isSpellOrRune()) {
16 obj->setSpellId(itm.spellId());
17 }
18
19 if(itm.isSpellOrRune()) {
20 material = ItemMaterial::MAT_COUNT;
21 int32_t id = itm.spellId();
22 const VisualFx* vfx = owner.script().spellVfx(id);
23 if(vfx!=nullptr) {
24 auto e = Effect(*vfx,owner,pos,SpellFxKey::Cast);
25 setView(std::move(e));
26 }
27 } else {
28 material = uint8_t(itm.handle().material);
29 setView(owner.addView(itm.handle()));
30 }
31
32 setPosition(pos);
33 }
34
36 vfx.setBullet(nullptr,*wrld);
37 wrld->physic()->deleteObj(obj);
38 }
39
40void Bullet::setPosition(const Tempest::Vec3& p) {
41 obj->setPosition(p);
42 updateMatrix();
43 }
44
45void Bullet::setPosition(float x, float y, float z) {
46 obj->setPosition(Vec3(x,y,z));
47 updateMatrix();
48 }
49
50void Bullet::setDirection(const Tempest::Vec3& dir) {
51 obj->setDirection(dir);
52 updateMatrix();
53 }
54
56 obj->setTargetRange(t);
57 }
58
60 view = std::move(m);
61 updateMatrix();
62 }
63
65 vfx = std::move(p);
66 vfx.setActive(true);
67 vfx.setLooped(true);
68 vfx.setBullet(this,*wrld);
69 vfx.setPhysicsDisable();
70 updateMatrix();
71 }
72
73bool Bullet::isSpell() const {
74 return obj->isSpell();
75 }
76
77int32_t Bullet::spellId() const {
78 return obj->spellId();
79 }
80
82 ow = n;
83 vfx.setOrigin(n);
84 }
85
87 return ow;
88 }
89
90void Bullet::setTarget(const Npc* n) {
91 vfx.setTarget(n);
92 }
93
95 if(material>=ItemMaterial::MAT_COUNT)
97 return ItemMaterial(material);
98 }
99
100bool Bullet::isFinished() const {
102 return false;
103 return true;
104 }
105
106float Bullet::pathLength() const {
107 return obj->pathLength();
108 }
109
111 flg = Flg(flg|Stopped);
112 updateMatrix();
113 }
114
116 updateMatrix();
117 }
118
119void Bullet::onCollide(zenkit::MaterialGroup matId) {
120 if(isFinished())
121 return;
122 if(matId != zenkit::MaterialGroup::NONE) {
123 if(material < ItemMaterial::MAT_COUNT) {
124 auto s = wrld->addLandHitEffect(ItemMaterial(material),matId,obj->matrix());
125 s.play();
126 }
127 }
128 Effect::onCollide(*wrld,vfx.handle(),obj->position(),nullptr,ow,spellId());
129 vfx.setLooped(false);
130 vfx.setPhysicsDisable();
131 wrld->runEffect(std::move(vfx));
132 }
133
135 if(&npc==origin() || isFinished())
136 return false;
137
138 if(ow!=nullptr) {
139 // no damage between ally npc's, only emit pfx effect
140 const bool friendlyFire = wrld->script().isFriendlyFire(*ow, npc);
141 if(!friendlyFire) {
142 if(isSpell())
143 npc.takeDamage(*ow,this,vfx.handle(),spellId()); else
144 npc.takeDamage(*ow,this);
145 }
146 }
147 vfx.setKey(*wrld,SpellFxKey::Collide);
148 vfx.setLooped(false);
149 vfx.setPhysicsDisable();
150 wrld->runEffect(std::move(vfx));
151
152 return true;
153 }
154
155void Bullet::updateMatrix() {
156 if(obj==nullptr)
157 return;
158 auto mat = obj->matrix();
159 view.setObjMatrix(mat);
160 // HACK: lighting bolt spell
161 mat.rotateOY(90);
162 vfx.setObjMatrix(mat);
163 // vfx .setTarget(obj->position()+obj->direction());
164 }
Flg flags() const
Definition bullet.h:45
void setTargetRange(float tgRange)
Definition bullet.cpp:55
ItemMaterial itemMaterial() const
Definition bullet.cpp:94
void onStop() override
Definition bullet.cpp:110
void setTarget(const Npc *n)
Definition bullet.cpp:90
bool isFinished() const
Definition bullet.cpp:100
Bullet()=default
int32_t spellId() const
Definition bullet.cpp:77
@ Stopped
Definition bullet.h:25
void onCollide(zenkit::MaterialGroup matId) override
Definition bullet.cpp:119
float pathLength() const
Definition bullet.cpp:106
Npc * origin() const
Definition bullet.cpp:86
bool isSpell() const
Definition bullet.cpp:73
void onMove() override
Definition bullet.cpp:115
void setOrigin(Npc *n)
Definition bullet.cpp:81
void setPosition(const Tempest::Vec3 &p)
Definition bullet.cpp:40
~Bullet() override
Definition bullet.cpp:35
void setDirection(const Tempest::Vec3 &dir)
Definition bullet.cpp:50
void setView(MeshObjects::Mesh &&m)
Definition bullet.cpp:59
void deleteObj(BulletBody *obj)
static void onCollide(World &owner, const VisualFx *root, const Tempest::Vec3 &pos, Npc *npc, Npc *other, int32_t splId)
Definition effect.cpp:301
bool isFriendlyFire(const Npc &src, const Npc &dst) const
const VisualFx * spellVfx(int32_t splId)
Definition item.h:14
int32_t spellId() const
Definition item.cpp:258
bool isSpellOrRune() const
Definition item.cpp:224
const zenkit::IItem & handle() const
Definition item.h:83
void setObjMatrix(const Tempest::Matrix4x4 &mt)
Definition npc.h:25
void takeDamage(Npc &other, const Bullet *b)
Definition npc.cpp:1868
void play()
Definition sound.cpp:126
Definition world.h:31
void runEffect(Effect &&e)
Definition world.cpp:234
DynamicWorld * physic() const
Definition world.h:81
Sound addLandHitEffect(ItemMaterial src, zenkit::MaterialGroup reciver, const Tempest::Matrix4x4 &pos)
Definition world.cpp:752
MeshObjects::Mesh addView(std::string_view visual) const
Definition world.cpp:251
GameScript & script() const
Definition world.cpp:1019
ItemMaterial
Definition constants.h:242
@ MAT_WOOD
Definition constants.h:243
@ MAT_COUNT
Definition constants.h:249
void setPosition(const Tempest::Vec3 &pos)
void setTargetRange(float tgRange)
Tempest::Matrix4x4 matrix() const
Tempest::Vec3 position() const
void setDirection(const Tempest::Vec3 &dir)