Code refactoring.

master
Vectozavr 2021-10-18 00:52:21 +07:00
parent e232435246
commit 8a6d31eded
33 changed files with 62 additions and 3532 deletions

View File

@ -4,8 +4,8 @@
#include "Bonus.h"
Bonus::Bonus(const std::string &bonusName, const std::string &filename, const std::string &materials, const Vec3D &scale) {
Bonus::Bonus(const std::string &bonusName, const std::string &filename, const Vec3D &scale) {
_name = bonusName;
loadObj(filename, materials, scale);
loadObj(filename, scale);
setCollider(false);
}

View File

@ -12,7 +12,7 @@ class Bonus final : public RigidBody {
private:
std::string _name;
public:
explicit Bonus(const std::string &bonusName, const std::string& filename, const std::string &materials = "", const Vec3D& scale = Vec3D{1, 1, 1});
explicit Bonus(const std::string &bonusName, const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
[[nodiscard]] std::string name() const { return _name; }
};

View File

@ -8,7 +8,7 @@
#include "engine/utils/Log.h"
Player::Player() {
loadObj(ShooterConsts::CUBE_OBJ, "", Vec3D{0.5, 1.9, 0.5});
loadObj(ShooterConsts::CUBE_OBJ, Vec3D{0.5, 1.9, 0.5});
setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0});
setCollision(true);
setVisible(false);

View File

@ -56,6 +56,7 @@ void Shooter::InitNetwork()
client->connect(clientIp, clientPort);
// TODO: encapsulate call backs inside Client
client->setSpawnPlayerCallBack([this](sf::Uint16 id){ spawnPlayer(id); });
client->setRemovePlayerCallBack([this](sf::Uint16 id){ removePlayer(id); });
client->setAddFireTraceCallBack([this](const Vec3D& from, const Vec3D& to){ addFireTrace(from, to); });
@ -70,11 +71,12 @@ void Shooter::start() {
mouse->setMouseCursorVisible(true);
world->loadMap(ShooterConsts::MAP_OBJ, "maps/materials.txt", "map", Vec3D{5, 5, 5});
world->loadMap(ShooterConsts::MAP_OBJ, Vec3D{5, 5, 5});
player = std::make_shared<Player>();
playerController = std::make_shared<PlayerController>(player, keyboard, mouse);
// TODO: encapsulate call backs inside Player
player->setAddTraceCallBack([this](const Vec3D& from, const Vec3D& to){ client->addTrace(from, to); addFireTrace(from, to); });
player->setDamagePlayerCallBack([this] (sf::Uint16 targetId, double damage) { client->damagePlayer(targetId, damage); });
player->setRayCastFunction([this](const Vec3D& from, const Vec3D& to) { return world->rayCast(from, to, "Enemy"); });
@ -208,18 +210,18 @@ void Shooter::spawnPlayer(sf::Uint16 id) {
newPlayer->setAcceleration(Vec3D{0, 0, 0});
// add head and other stuff:
world->loadBody(ObjectNameTag(name + "_head"), ShooterConsts::CUBE_OBJ, "", Vec3D{0.7, 0.7, 0.7});
world->loadBody(ObjectNameTag(name + "_head"), ShooterConsts::CUBE_OBJ, Vec3D{0.7, 0.7, 0.7});
world->body(ObjectNameTag(name + "_head"))->translate(Vec3D{0, 2, 0});
world->body(ObjectNameTag(name + "_head"))->setCollider(false);
newPlayer->attach(world->body(ObjectNameTag(name + "_head")), ObjectNameTag("head"));
world->loadBody(ObjectNameTag(name + "_eye1"), ShooterConsts::CUBE_OBJ, "", Vec3D{0.2, 0.2, 0.05});
world->loadBody(ObjectNameTag(name + "_eye1"), ShooterConsts::CUBE_OBJ, Vec3D{0.2, 0.2, 0.05});
world->body(ObjectNameTag(name + "_eye1"))->translate(Vec3D{0.3, 2.1, 0.7});
world->body(ObjectNameTag(name + "_eye1"))->setCollider(false);
world->body(ObjectNameTag(name + "_eye1"))->setColor({147, 159, 255});
world->body(ObjectNameTag(name + "_head"))->attach(world->body(ObjectNameTag(name + "_eye1")), ObjectNameTag("eye1"));
world->loadBody(ObjectNameTag(name + "_eye2"), ShooterConsts::CUBE_OBJ, "", Vec3D{0.2, 0.2, 0.05});
world->loadBody(ObjectNameTag(name + "_eye2"), ShooterConsts::CUBE_OBJ, Vec3D{0.2, 0.2, 0.05});
world->body(ObjectNameTag(name + "_eye2"))->translate(Vec3D{-0.3, 2.1, 0.7});
world->body(ObjectNameTag(name + "_eye2"))->setCollider(false);
world->body(ObjectNameTag(name + "_eye2"))->setColor({147, 159, 255});
@ -249,7 +251,7 @@ void Shooter::removeFireTrace(const ObjectNameTag& traceName) {
void Shooter::addBonus(const string &bonusName, const Vec3D &position) {
std::string name = bonusName.substr(6, bonusName.size()-3-5);
world->addBody(std::make_shared<Bonus>(bonusName, "obj/" + name + ".obj", "obj/" + name + "_mat.txt", Vec3D{3, 3, 3}), ObjectNameTag(bonusName));
world->addBody(std::make_shared<Bonus>(bonusName, "obj/" + name + ".obj", Vec3D{3, 3, 3}), ObjectNameTag(bonusName));
world->body(ObjectNameTag(bonusName))->translateToPoint(position);
Timeline::animate(AnimationListTag(bonusName + "_rotation"), new ARotate(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::linear));
}

View File

@ -36,7 +36,7 @@ namespace ShooterConsts {
const std::string SHOTGUN_RELOAD_SOUND = "sound/weapons/reload_shotgun.ogg";
const std::string CUBE_OBJ = "obj/cube.obj";
const std::string MAP_OBJ = "maps/map1.obj";
const std::string MAP_OBJ = "maps/map.obj";
const std::string MAIN_MENU_BACK = "textures/back.png";
const std::string MAIN_MENU_GUI = "textures/gui.png";

View File

@ -21,9 +21,9 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) {
return *this;
}
Mesh &Mesh::loadObj(const std::string& filename, const std::string &materials, const Vec3D& scale) {
Mesh &Mesh::loadObj(const std::string& filename, const Vec3D& scale) {
auto objects = Mesh::LoadObjects(filename, materials, scale);
auto objects = Mesh::LoadObjects(filename, scale);
for(auto& obj : objects) {
for (auto &tri : obj->triangles()) {
_tris.push_back(tri);
@ -32,8 +32,8 @@ Mesh &Mesh::loadObj(const std::string& filename, const std::string &materials, c
return *this;
}
Mesh::Mesh(const std::string& filename, const std::string &materials, const Vec3D& scale){
loadObj(filename, materials, scale);
Mesh::Mesh(const std::string& filename, const Vec3D& scale){
loadObj(filename, scale);
}
Mesh::Mesh(const vector<Triangle> &tries) : _tris(tries) {
@ -79,7 +79,7 @@ void Mesh::setColor(const sf::Color& c) {
setTriangles(newTriangles);
}
std::vector<std::shared_ptr<Mesh>> Mesh::LoadObjects(const string &filename, const string &materials, const Vec3D &scale) {
std::vector<std::shared_ptr<Mesh>> Mesh::LoadObjects(const string &filename, const Vec3D &scale) {
std::vector<std::shared_ptr<Mesh>> objects;
map<string, sf::Color> maters;
@ -90,32 +90,6 @@ std::vector<std::shared_ptr<Mesh>> Mesh::LoadObjects(const string &filename, con
return objects;
}
if(!materials.empty()) {
ifstream mat(materials);
if (!mat.is_open())
{
Log::log("Mesh::LoadObjects(): cannot load mat from " + materials);
return objects;
} else {
while (!mat.eof())
{
char line[128];
mat.getline(line, 128);
stringstream s;
s << line;
int color[4];
string matName;
s >> matName >> color[0] >> color[1] >> color[2] >> color[3];
maters.insert({matName, sf::Color(color[0],color[1],color[2], color[3])});
}
mat.close();
}
}
vector<Point4D> verts;
std::vector<Triangle> tris;
sf::Color currentColor = sf::Color(255, 245, 194, 255);
@ -154,6 +128,14 @@ std::vector<std::shared_ptr<Mesh>> Mesh::LoadObjects(const string &filename, con
s >> junk >> f[0] >> f[1] >> f[2];
tris.emplace_back(verts[f[0] - 1], verts[f[1] - 1], verts[f[2] - 1], currentColor);
}
if(line[0] == 'm')
{
int color[4];
string matName;
s >> junk >> matName >> color[0] >> color[1] >> color[2] >> color[3];
maters.insert({matName, sf::Color(color[0],color[1],color[2], color[3])});
}
}
if(!tris.empty()) {

View File

@ -23,9 +23,9 @@ public:
Mesh(const Mesh& mesh);
explicit Mesh(const std::vector<Triangle>& tries);
explicit Mesh(const std::string& filename, const std::string &materials = "", const Vec3D& scale = Vec3D{1, 1, 1});
explicit Mesh(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
Mesh& loadObj(const std::string& filename, const std::string &materials = "", const Vec3D& scale = Vec3D{1, 1, 1});
Mesh& loadObj(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
[[nodiscard]] std::vector<Triangle>const &triangles() const { return _tris; }
[[nodiscard]] std::vector<Triangle>& triangles() { return _tris; }
@ -52,7 +52,7 @@ public:
~Mesh() override;
Mesh static Obj(const std::string& filename);
std::vector<std::shared_ptr<Mesh>> static LoadObjects(const std::string& filename, const std::string &materials = "", const Vec3D& scale = Vec3D{1, 1, 1});
std::vector<std::shared_ptr<Mesh>> static LoadObjects(const std::string& filename, const Vec3D& scale = Vec3D{1, 1, 1});
Mesh static LineTo(const Vec3D& from, const Vec3D& to, double line_width = 0.1, const sf::Color& color = {150, 150, 150, 255});
};

View File

@ -13,8 +13,8 @@ void World::addBody(std::shared_ptr<RigidBody> body, const ObjectNameTag& tag) {
Log::log("World::addBody(): inserted body '" + tag.str() + "' with " + std::to_string(_objects[tag]->triangles().size()) + " tris.");
}
void World::loadBody(const ObjectNameTag& tag, const string &filename, const std::string &materials, const Vec3D& scale) {
_objects.emplace(tag, std::make_shared<RigidBody>(Mesh(filename, materials, scale)));
void World::loadBody(const ObjectNameTag& tag, const string &filename, const Vec3D& scale) {
_objects.emplace(tag, std::make_shared<RigidBody>(Mesh(filename, scale)));
Log::log("World::loadBody(): inserted body from " + filename + " with title '" + tag.str() + "' with " + std::to_string(_objects[tag]->triangles().size()) + " tris.");
}
@ -45,10 +45,10 @@ std::pair<Vec3D, ObjectNameTag> World::rayCast(const Vec3D& from, const Vec3D& t
return {*point, ObjectNameTag(bodyName)};
}
void World::loadMap(const std::string& filename, const std::string& materials, const std::string& name, const Vec3D& scale) {
auto objs = Mesh::LoadObjects(filename, materials, scale);
void World::loadMap(const std::string& filename, const Vec3D& scale) {
auto objs = Mesh::LoadObjects(filename, scale);
for(unsigned i = 0; i < objs.size(); i++) {
ObjectNameTag meshName = ObjectNameTag(name + "_" + to_string(i));
ObjectNameTag meshName = ObjectNameTag("map_" + to_string(i));
addBody(std::make_shared<RigidBody>(*objs[i]), meshName);
}
}

View File

@ -22,14 +22,14 @@ public:
void addBody(std::shared_ptr<RigidBody> mesh, const ObjectNameTag& tag);
std::shared_ptr<RigidBody> body(const ObjectNameTag& tag);
void removeBody(const ObjectNameTag& tag);
void loadBody(const ObjectNameTag& tag, const std::string &filename, const std::string &materials = "", const Vec3D& scale = Vec3D{1, 1, 1});
void loadBody(const ObjectNameTag& tag, const std::string &filename, const Vec3D& scale = Vec3D{1, 1, 1});
// rayCast returns pair of Point4D and std::string:
// 1) Point4D is point of collision
// 2) std::string - title of the object
std::pair<Vec3D, ObjectNameTag> rayCast(const Vec3D& from, const Vec3D& to, const std::string& tag = "");
void loadMap(const std::string& filename, const std::string& materials, const std::string& name = "map", const Vec3D & scale = Vec3D{1, 1, 1});
void loadMap(const std::string& filename, const Vec3D & scale = Vec3D{1, 1, 1});
};

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +0,0 @@
000 196 173 255 255
001 255 255 255 100
002 179 116 122 255
003 198 73 231 255
004 86 81 125 255
005 126 179 231 255
006 231 180 162 255
007 105 148 231 255
008 231 160 111 255
009 231 161 90 255
010 147 231 139 255
011 255 199 179 100
012 231 88 85 100
013 231 66 78 255
014 198 73 231 255
016 117 231 150 100
017 103 79 100 100
018 198 73 231 255
019 103 79 231 100
020 231 30 217 255
021 117 231 150 100
022 231 66 78 255
023 231 30 217 255
024 231 88 85 100
025 231 161 90 255
026 85 231 139 255
027 255 199 179 100
028 126 179 231 255
029 198 73 231 255
030 105 148 231 255
031 231 180 162 255
032 85 231 139 255
033 231 160 111 255
034 144 103 84 255
035 179 116 122 255
036 196 173 255 255
037 86 81 125 255
038 147 231 139 255

View File

@ -1,6 +1,4 @@
# Blender v2.91.0 OBJ File: 'ability.blend'
# www.blender.org
mtllib ability.mtl
m 000 51 19 198 150
o Cube
v 0.005449 0.446474 -0.361719
v 0.005449 0.446546 -0.372617

View File

@ -1 +0,0 @@
000 51 19 198 150

View File

@ -1,6 +1,6 @@
# Blender v2.91.0 OBJ File: 'ak47.blend'
# www.blender.org
mtllib ak47.mtl
m 001 59 39 34 255
m 002 104 105 110 255
m 003 64 64 64 255
o Cube
v 0.035068 -0.126740 0.120392
v 0.035068 0.049771 0.120392

View File

@ -1,3 +0,0 @@
001 59 39 34 255
002 104 105 110 255
003 64 64 64 255

View File

@ -1,5 +1,3 @@
# Blender v2.78 (sub 0) OBJ File: ''
# www.blender.org
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000

View File

@ -1,6 +1,6 @@
# Blender v2.91.0 OBJ File: 'ak47.blend'
# www.blender.org
mtllib ak47.mtl
m 001 59 39 34 255
m 002 152 111 22 255
m 003 191 154 46 255
o Cube
v 0.035068 -0.126740 0.120392
v 0.035068 0.049771 0.120392

View File

@ -1,3 +0,0 @@
001 59 39 34 255
002 152 111 22 255
003 191 154 46 255

View File

@ -1,6 +1,6 @@
# Blender v2.91.0 OBJ File: 'gun.blend'
# www.blender.org
mtllib gun.mtl
m 000 115 115 115 255
m 001 186 185 185 255
m 002 59 59 59 255
o Cube
v 0.023535 -0.036121 0.021633
v 0.022901 -0.107817 0.006247

View File

@ -1,3 +0,0 @@
000 115 115 115 255
001 186 185 185 255
002 59 59 59 255

View File

@ -1,6 +1,6 @@
# Blender v2.91.0 OBJ File: 'hill.blend'
# www.blender.org
mtllib hill.mtl
m 000 255 255 255 255
m 001 139 139 139 255
m 002 231 92 71 255
o Cube
v -0.180827 -0.334950 0.425389
v -0.180827 0.138945 0.425389

View File

@ -1,3 +0,0 @@
000 255 255 255 255
001 139 139 139 255
002 231 92 71 255

View File

@ -1,6 +1,5 @@
# Blender v2.91.0 OBJ File: 'rifle.blend'
# www.blender.org
mtllib rifle.mtl
m 000 122 122 122 255
m 001 65 65 65 255
o Cube
v -0.000009 0.082987 -0.980972
v 0.082987 0.000009 -0.980972

View File

@ -1,2 +0,0 @@
000 122 122 122 255
001 65 65 65 255

View File

@ -1,6 +1,7 @@
# Blender v2.91.0 OBJ File: 'shotgun.blend'
# www.blender.org
mtllib shotgun.mtl
m 000 169 169 169 255
m 001 166 102 74 255
m 002 125 125 125 255
m 003 77 77 77 255
o Cube
v 0.037044 0.012938 0.018749
v 0.037044 -0.100172 0.018749

View File

@ -1,4 +0,0 @@
000 169 169 169 255
001 166 102 74 255
002 125 125 125 255
003 77 77 77 255

View File

@ -8,7 +8,7 @@
using namespace std;
Ak47::Ak47(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::AK47_OBJ, "obj/ak47_mat.txt", Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
Ak47::Ak47(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::AK47_OBJ, Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
fireSound = ShooterConsts::AK47_FIRE_SOUND;
reloadSound = ShooterConsts::AK47_RELOAD_SOUND;

View File

@ -10,7 +10,7 @@
class Gold_Ak47 final : public Weapon {
public:
explicit Gold_Ak47(int ammo = 200, const std::string& weaponName = "gold_ak47") : Weapon(weaponName, ShooterConsts::GOLD_AK47_OBJ, "obj/gold_ak47_mat.txt", Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
explicit Gold_Ak47(int ammo = 200, const std::string& weaponName = "gold_ak47") : Weapon(weaponName, ShooterConsts::GOLD_AK47_OBJ, Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
fireSound = ShooterConsts::GOLD_AK47_FIRE_SOUND;
reloadSound = ShooterConsts::GOLD_AK47_RELOAD_SOUND;

View File

@ -8,7 +8,7 @@
using namespace std;
Gun::Gun(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::GUN_OBJ, "obj/gun_mat.txt", Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
Gun::Gun(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::GUN_OBJ, Vec3D{3, 3, 3}, Vec3D{-0.8, 1.3, 0.3}, Vec3D{0, Consts::PI, 0}) {
fireSound = ShooterConsts::GUN_FIRE_SOUND;
reloadSound = ShooterConsts::GUN_RELOAD_SOUND;

View File

@ -6,7 +6,7 @@
#include "Rifle.h"
#include "../ShooterConsts.h"
Rifle::Rifle(int ammo, const std::string &weaponName) : Weapon(weaponName, ShooterConsts::RIFLE_OBJ, "obj/rifle_mat.txt", Vec3D{3, 3, 3}, Vec3D{-1.2, 1, 0.3}, Vec3D{0, Consts::PI, 0}) {
Rifle::Rifle(int ammo, const std::string &weaponName) : Weapon(weaponName, ShooterConsts::RIFLE_OBJ, Vec3D{3, 3, 3}, Vec3D{-1.2, 1, 0.3}, Vec3D{0, Consts::PI, 0}) {
fireSound = ShooterConsts::RIFLE_FIRE_SOUND;
reloadSound = ShooterConsts::RIFLE_RELOAD_SOUND;

View File

@ -9,7 +9,7 @@
using namespace std;
Shotgun::Shotgun(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::SHOTGUN_OBJ, "obj/shotgun_mat.txt", Vec3D{3, 3, 3}, Vec3D{-0.95, 1.3, -0.6}, Vec3D{0, Consts::PI, 0}) {
Shotgun::Shotgun(int ammo, const std::string& weaponName) : Weapon(weaponName, ShooterConsts::SHOTGUN_OBJ, Vec3D{3, 3, 3}, Vec3D{-0.95, 1.3, -0.6}, Vec3D{0, Consts::PI, 0}) {
fireSound = ShooterConsts::SHOTGUN_FIRE_SOUND;
reloadSound = ShooterConsts::SHOTGUN_RELOAD_SOUND;

View File

@ -12,10 +12,10 @@
using namespace std;
Weapon::Weapon(const std::string& weaponName, const std::string& objFileName, const std::string& matFileName, const Vec3D& scale, const Vec3D& t, const Vec3D& r) {
Weapon::Weapon(const std::string& weaponName, const std::string& objFileName, const Vec3D& scale, const Vec3D& t, const Vec3D& r) {
_name = weaponName;
loadObj(objFileName, matFileName, scale);
loadObj(objFileName, scale);
setCollider(false);
rotate(r);
translate(t);

View File

@ -44,7 +44,7 @@ protected:
virtual std::map<ObjectNameTag, double> processFire(std::function<std::pair<Vec3D, ObjectNameTag>(const Vec3D&, const Vec3D&)> rayCastFunction);
public:
Weapon(const std::string& weaponName, const std::string& objFileName, const std::string& matFileName, const Vec3D& scale, const Vec3D& translate, const Vec3D& rotate);
Weapon(const std::string& weaponName, const std::string& objFileName, const Vec3D& scale, const Vec3D& translate, const Vec3D& rotate);
std::map<ObjectNameTag, double> fire(std::function<std::pair<Vec3D, ObjectNameTag>(const Vec3D&, const Vec3D&)> rayCastFunction);
void reload();