diff --git a/Player.cpp b/Player.cpp index 1546e1c..8eaa4ff 100644 --- a/Player.cpp +++ b/Player.cpp @@ -11,16 +11,12 @@ #include "engine/animation/Timeline.h" #include "engine/animation/ARotateLeft.h" -Player::Player(ObjectNameTag name) : RigidBody(std::move(name), ShooterConsts::CUBE_OBJ,Vec3D{0.5, 1.9, 0.5}) { +Player::Player(ObjectNameTag name, const std::string &filename, const Vec3D &scale) : RigidBody(std::move(name), filename,scale) { setAcceleration(Vec3D{0, -ShooterConsts::GRAVITY, 0}); setCollision(true); setVisible(false); setSimpleHitBox(true); - Vec3D randColor = Vec3D::Random(); - setColor({static_cast(randColor.x() * 255), static_cast(randColor.y() * 255), - static_cast(randColor.z() * 255)}); - setCollisionCallBack( [this](const ObjectNameTag &tag, std::shared_ptr obj) { collisionWithObject(tag, obj); }); } diff --git a/Player.h b/Player.h index 00823ed..d0375ca 100644 --- a/Player.h +++ b/Player.h @@ -43,7 +43,7 @@ private: void collisionWithObject(const ObjectNameTag &tag, std::shared_ptr obj); public: - explicit Player(ObjectNameTag name); + explicit Player(ObjectNameTag name, const std::string &filename = ShooterConsts::CUBE_OBJ, const Vec3D &scale = Vec3D{1, 1, 1}); void setHealth(double h) { _health = h; } diff --git a/Shooter.cpp b/Shooter.cpp index c95514f..84348b8 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -82,8 +82,6 @@ void Shooter::start() { world->loadMap(ShooterConsts::MAP_OBJ, Vec3D{5, 5, 5}); - player->scale(Vec3D(3, 1, 3)); - // TODO: encapsulate call backs inside Player player->setAddTraceCallBack([this](const Vec3D &from, const Vec3D &to) { client->addTrace(from, to); @@ -256,31 +254,39 @@ void Shooter::play() { void Shooter::spawnPlayer(sf::Uint16 id) { std::string name = "Enemy_" + std::to_string(id); - std::shared_ptr newPlayer = std::make_shared(ObjectNameTag(name)); - newPlayer->setCollision(false); + std::shared_ptr newPlayer = std::make_shared(ObjectNameTag(name), ShooterConsts::BODY_OBJ, + Vec3D{0.4, 0.4, 0.4}); client->addPlayer(id, newPlayer); world->addBody(newPlayer); newPlayer->setVisible(true); + newPlayer->setCollision(false); 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->body(ObjectNameTag(name + "_head"))->translate(Vec3D{0, 2, 0}); - world->body(ObjectNameTag(name + "_head"))->setCollider(false); + world->loadBody(ObjectNameTag(name + "_head"), ShooterConsts::HEAD_OBJ, Vec3D{0.4, 0.4, 0.4}); + world->body(ObjectNameTag(name + "_head"))->translate(Vec3D{0, 2.2, 0}); newPlayer->attach(world->body(ObjectNameTag(name + "_head"))); - 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"))); + world->loadBody(ObjectNameTag(name + "_foot_1"), ShooterConsts::FOOT_OBJ, Vec3D{0.4, 0.4, 0.4}); + world->body(ObjectNameTag(name + "_foot_1"))->translate(Vec3D{-0.25, 0, 0}); + newPlayer->attach(world->body(ObjectNameTag(name + "_foot_1"))); - 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}); - world->body(ObjectNameTag(name + "_head"))->attach(world->body(ObjectNameTag(name + "_eye2"))); + world->loadBody(ObjectNameTag(name + "_foot_2"), ShooterConsts::FOOT_OBJ, Vec3D{0.4, 0.4, 0.4}); + world->body(ObjectNameTag(name + "_foot_2"))->translate(Vec3D{0.25, 0, 0}); + newPlayer->attach(world->body(ObjectNameTag(name + "_foot_2"))); + + Vec3D randColor1 = Vec3D::Random(); + Vec3D randColor2 = Vec3D::Random(); + + newPlayer->setColor({static_cast(randColor1.x() * 255), static_cast(randColor1.y() * 255), + static_cast(randColor1.z() * 255)}); + world->body(ObjectNameTag(name + "_foot_1"))->setColor( + {static_cast(randColor2.x() * 255), static_cast(randColor2.y() * 255), + static_cast(randColor2.z() * 255)}); + world->body(ObjectNameTag(name + "_foot_2"))->setColor( + {static_cast(randColor2.x() * 255), static_cast(randColor2.y() * 255), + static_cast(randColor2.z() * 255)}); changeEnemyWeapon("gun", id); } @@ -289,9 +295,10 @@ void Shooter::removePlayer(sf::Uint16 id) { std::string name = "Enemy_" + std::to_string(id); world->removeBody(ObjectNameTag(name)); world->removeBody(ObjectNameTag(name + "_head")); - world->removeBody(ObjectNameTag(name + "_eye1")); - world->removeBody(ObjectNameTag(name + "_eye2")); - world->removeBody(ObjectNameTag("Enemy_" + std::to_string(id) + "_weapon")); + world->removeBody(ObjectNameTag(name + "_weapon")); + world->removeBody(ObjectNameTag(name + "_foot_1")); + world->removeBody(ObjectNameTag(name + "_foot_2")); + } void Shooter::addFireTrace(const Vec3D &from, const Vec3D &to) { @@ -351,10 +358,11 @@ void Shooter::changeEnemyWeapon(const std::string &weaponName, sf::Uint16 enemyI world->body(weaponTag)->setCollider(false); world->body(weaponTag)->scale(Vec3D(3, 3, 3)); - world->body(weaponTag)->translateToPoint(head->position() - enemy->left() * 2 - enemy->up() * 0.5); + world->body(weaponTag)->translateToPoint( + head->position() - enemy->left() * 2.5 - enemy->up() * 2.5 + enemy->lookAt()); - world->body(weaponTag)->rotate(Vec3D(0, Consts::PI + enemy->angle().y(), 0)); - world->body(weaponTag)->rotateLeft(-head->angleLeftUpLookAt().x()); + world->body(weaponTag)->rotate(Vec3D(0, enemy->angle().y(), 0)); + world->body(weaponTag)->rotateLeft(head->angleLeftUpLookAt().x()); enemy->attach(world->body(weaponTag)); } diff --git a/Shooter.h b/Shooter.h index 1c4f829..0392231 100644 --- a/Shooter.h +++ b/Shooter.h @@ -17,7 +17,9 @@ class Shooter final : public Engine { private: - std::shared_ptr player = std::make_shared(ObjectNameTag("Player")); + std::shared_ptr player = std::make_shared(ObjectNameTag("Player"), + ShooterConsts::CUBE_OBJ, + Vec3D{1.5, 1.9, 1.5}); std::shared_ptr playerController = std::make_shared(player, keyboard, mouse); Window mainMenu; diff --git a/ShooterClient.cpp b/ShooterClient.cpp index a0bc027..6340a24 100644 --- a/ShooterClient.cpp +++ b/ShooterClient.cpp @@ -14,6 +14,7 @@ #include "engine/animation/ATranslateToPoint.h" #include "engine/animation/AWait.h" #include "engine/animation/AFunction.h" +#include "engine/animation/ARotateLeft.h" void ShooterClient::updatePacket() { sf::Packet packet; @@ -50,7 +51,11 @@ void ShooterClient::processUpdate(sf::Packet &packet) { if (_players.count(targetId)) { std::string name = "Enemy_" + std::to_string(targetId); - _players[targetId]->translateToPoint(Vec3D{buf[0], buf[1], buf[2]}); + Vec3D newPosition = Vec3D{buf[0], buf[1], buf[2]}; + + bool isAnimate = (_players[targetId]->position() - newPosition).sqrAbs() > 0.2; + + _players[targetId]->translateToPoint(newPosition); _players[targetId]->setHealth(buf[3]); _players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0}); @@ -59,11 +64,30 @@ void ShooterClient::processUpdate(sf::Packet &packet) { auto head = _players[targetId]->attached(ObjectNameTag(name + "_head")); auto weapon = _players[targetId]->attached(ObjectNameTag("Enemy_" + std::to_string(targetId) + "_weapon")); + auto foot1 = _players[targetId]->attached(ObjectNameTag(name + "_foot_1")); + auto foot2 = _players[targetId]->attached(ObjectNameTag(name + "_foot_2")); + if (head != nullptr) { head->rotateLeft(buf[5] - _players[targetId]->headAngle()); } if (weapon != nullptr) { - weapon->rotateLeft(-(buf[5] - _players[targetId]->headAngle())); + weapon->rotateLeft(buf[5] - _players[targetId]->headAngle()); + } + + if(isAnimate) { + if(foot1 != nullptr && foot2 != nullptr && !Timeline::isInAnimList(AnimationListTag(name + "_foot1_rotation"))) { + Timeline::animate(AnimationListTag(name + "_foot1_rotation"), std::make_shared(foot1, 0.4, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(name + "_foot1_rotation"), std::make_shared(0)); + Timeline::animate(AnimationListTag(name + "_foot1_rotation"), std::make_shared(foot1, -0.8, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(name + "_foot1_rotation"), std::make_shared(0)); + Timeline::animate(AnimationListTag(name + "_foot1_rotation"), std::make_shared(foot1, 0.4, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + + Timeline::animate(AnimationListTag(name + "_foot2_rotation"), std::make_shared(foot2, -0.4, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(name + "_foot2_rotation"), std::make_shared(0)); + Timeline::animate(AnimationListTag(name + "_foot2_rotation"), std::make_shared(foot2, 0.8, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(name + "_foot2_rotation"), std::make_shared(0)); + Timeline::animate(AnimationListTag(name + "_foot2_rotation"), std::make_shared(foot2, -0.4, 0.2, Animation::LoopOut::None, Animation::InterpolationType::Linear)); + } } _players[targetId]->setHeadAngle(buf[5]); diff --git a/ShooterConsts.h b/ShooterConsts.h index ff18227..d796606 100644 --- a/ShooterConsts.h +++ b/ShooterConsts.h @@ -42,6 +42,11 @@ namespace ShooterConsts { const std::string MAIN_MENU_BACK = "textures/back.png"; const std::string MAIN_MENU_GUI = "textures/gui.png"; + const std::string HEAD_OBJ = "obj/man/head.obj"; + const std::string BODY_OBJ = "obj/man/body.obj"; + const std::string FOOT_OBJ = "obj/man/foot.obj"; + const std::string MAN_OBJ = "obj/man/man.obj"; + const std::string CLICK_SOUND = "sound/click.ogg"; const std::string BACK_NOISE = "sound/backNoise.ogg"; const std::string CHANGE_WEAPON_SOUND = "sound/weapons/change_weapon.ogg"; diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index e191c52..dc0826f 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -22,7 +22,7 @@ Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { } void Mesh::loadObj(const std::string &filename, const Vec3D &scale) { - + _tris.clear(); auto objects = ResourceManager::loadObjects(filename); for (auto &obj : objects) { for (auto &tri : obj->triangles()) { diff --git a/obj/man/body.obj b/obj/man/body.obj new file mode 100644 index 0000000..c9926d7 --- /dev/null +++ b/obj/man/body.obj @@ -0,0 +1,39 @@ +# Blender v2.91.0 OBJ File: 'body.blend' +# www.blender.org +mtllib body.mtl +o Cube +v 1.470880 4.046328 0.990075 +v 1.227793 -0.007857 0.756304 +v -1.471015 4.046328 0.989874 +v -1.227895 -0.007857 0.756137 +v 1.471015 4.046328 -0.989874 +v 1.227895 -0.007857 -0.756137 +v -1.470880 4.046328 -0.990075 +v -1.227793 -0.007857 -0.756304 +v -0.362043 4.746272 0.386594 +v -0.361991 4.746272 -0.386643 +v 0.361991 4.746272 0.386643 +v 0.362043 4.746272 -0.386594 +g Cube_Cube_Material +usemtl Material +s off +f 1 9 3 +f 3 8 4 +f 7 6 8 +f 2 8 6 +f 1 4 2 +f 5 2 6 +f 12 9 11 +f 5 11 1 +f 3 10 7 +f 7 12 5 +f 1 11 9 +f 3 7 8 +f 7 5 6 +f 2 4 8 +f 1 3 4 +f 5 1 2 +f 12 10 9 +f 5 12 11 +f 3 9 10 +f 7 10 12 diff --git a/obj/man/foot.obj b/obj/man/foot.obj new file mode 100644 index 0000000..c232953 --- /dev/null +++ b/obj/man/foot.obj @@ -0,0 +1,27 @@ +# Blender v2.91.0 OBJ File: 'foot.blend' +# www.blender.org +mtllib foot.mtl +o Cube +v 0.483757 0.004051 -0.472253 +v 0.320470 -5.020987 -0.308967 +v 0.483757 0.004051 0.495260 +v 0.320470 -5.020987 0.331974 +v -0.483757 0.004051 -0.472253 +v -0.320470 -5.020987 -0.308967 +v -0.483757 0.004051 0.495260 +v -0.320470 -5.020987 0.331974 +g Cube_Cube_Material +usemtl Material +s off +f 5 3 1 +f 3 8 4 +f 7 6 8 +f 2 8 6 +f 1 4 2 +f 5 2 6 +f 5 7 3 +f 3 7 8 +f 7 5 6 +f 2 4 8 +f 1 3 4 +f 5 1 2 diff --git a/obj/man/head.obj b/obj/man/head.obj new file mode 100644 index 0000000..2c3ffd1 --- /dev/null +++ b/obj/man/head.obj @@ -0,0 +1,221 @@ +m 000 255 213 196 255 +m 001 43 64 59 255 +m 002 54 75 161 255 +m 003 166 101 103 255 +m 004 205 157 154 255 +o Cube +v 0.907299 1.198761 0.858237 +v 1.010992 0.159109 0.961946 +v -0.885299 1.198761 0.858105 +v -0.989008 0.159109 0.961798 +v 0.907431 1.198761 -0.934361 +v 1.011140 0.159109 -1.038054 +v -0.885167 1.198761 -0.934493 +v -0.988860 0.159109 -1.038202 +v 0.562583 -0.761975 -0.808011 +v -0.540337 -0.761975 -0.808092 +v 0.562469 -0.761975 0.731837 +v -0.540450 -0.761975 0.731755 +g Cube_Cube_Material.000 +usemtl Material.000 +s off +f 5 3 1 +f 3 8 4 +f 7 6 8 +f 2 9 6 +f 1 4 2 +f 5 2 6 +f 11 10 9 +f 8 12 4 +f 6 10 8 +f 4 11 2 +f 5 7 3 +f 3 7 8 +f 7 5 6 +f 2 11 9 +f 1 3 4 +f 5 1 2 +f 11 12 10 +f 8 10 12 +f 6 9 10 +f 4 12 11 +o Cube.001 +v 0.723306 1.864736 0.674217 +v 1.169709 1.456717 1.120687 +v -0.701279 1.864736 0.674112 +v -1.147748 1.456717 1.120516 +v 0.723411 1.864736 -0.750368 +v 1.169881 1.456717 -1.196771 +v -0.701174 1.864736 -0.750473 +v -1.147577 1.456717 -1.196942 +v -0.140901 2.162206 0.113817 +v -0.140879 2.162206 -0.190095 +v 0.163011 2.162206 0.113839 +v 0.163033 2.162206 -0.190073 +v 1.232467 1.103483 -1.259348 +v -1.210154 1.103483 -1.259528 +v 1.232286 1.103483 1.183273 +v -1.210334 1.103483 1.183092 +v -0.809460 1.235197 1.338089 +v 0.831389 1.235197 1.338210 +v -0.853770 1.108245 1.335328 +v 0.875699 1.108245 1.335456 +v -0.518071 1.194810 2.158740 +v 0.539879 1.194810 2.158818 +v -0.546640 1.112957 2.156960 +v 0.568448 1.112957 2.157042 +g Cube.001_Cube.001_Material.001 +usemtl Material.001 +s off +f 15 22 19 +f 15 20 16 +f 19 18 20 +f 20 28 16 +f 13 16 14 +f 17 14 18 +f 24 21 23 +f 19 24 17 +f 13 21 15 +f 17 23 13 +f 27 26 25 +f 18 26 20 +f 28 29 16 +f 14 25 18 +f 29 35 33 +f 27 31 28 +f 16 30 14 +f 27 30 32 +f 33 36 34 +f 29 34 30 +f 30 36 32 +f 32 35 31 +f 15 21 22 +f 15 19 20 +f 19 17 18 +f 20 26 28 +f 13 15 16 +f 17 13 14 +f 24 22 21 +f 19 22 24 +f 13 23 21 +f 17 24 23 +f 27 28 26 +f 18 25 26 +f 28 31 29 +f 14 27 25 +f 29 31 35 +f 27 32 31 +f 16 29 30 +f 27 14 30 +f 33 35 36 +f 29 33 34 +f 30 34 36 +f 32 36 35 +o Cylinder +v -0.170726 0.848493 0.964507 +v -0.170718 0.835333 0.848476 +v -0.476666 1.071607 0.939183 +v -0.476659 1.058447 0.823152 +v -0.784764 0.851438 0.964134 +v -0.784757 0.838279 0.848103 +v -0.669238 0.492252 1.004878 +v -0.669231 0.479092 0.888847 +v -0.289742 0.490431 1.005108 +v -0.289734 0.477272 0.889077 +g Cylinder_Cylinder_Material.002 +usemtl Material.002 +s off +f 38 39 37 +f 40 41 39 +f 42 43 41 +f 46 44 42 +f 44 45 43 +f 46 37 45 +f 39 43 45 +f 38 40 39 +f 40 42 41 +f 42 44 43 +f 42 40 38 +f 38 46 42 +f 44 46 45 +f 46 38 37 +f 45 37 39 +f 39 41 43 +o Cylinder.001 +v 0.782291 0.848501 0.964577 +v 0.782299 0.835341 0.848545 +v 0.476351 1.071615 0.939253 +v 0.476358 1.058455 0.823222 +v 0.168253 0.851446 0.964204 +v 0.168260 0.838287 0.848172 +v 0.283779 0.492260 1.004948 +v 0.283786 0.479100 0.888916 +v 0.663275 0.490439 1.005178 +v 0.663283 0.477280 0.889147 +g Cylinder.001_Cylinder.001_Material.002 +usemtl Material.002 +s off +f 48 49 47 +f 50 51 49 +f 52 53 51 +f 56 54 52 +f 54 55 53 +f 56 47 55 +f 49 53 55 +f 48 50 49 +f 50 52 51 +f 52 54 53 +f 52 50 48 +f 48 56 52 +f 54 56 55 +f 56 48 47 +f 55 47 49 +f 49 51 53 +o Cone +v 0.315795 0.144472 0.925125 +v -0.293975 0.144719 0.925103 +v 0.011124 0.672672 0.925079 +v 0.010984 0.160587 1.252207 +g Cone_Cone_Material.004 +usemtl Material.004 +s off +f 57 60 58 +f 57 58 59 +f 58 60 59 +f 59 60 57 +o Cube.002 +v 0.450740 -0.323971 0.826628 +v 0.456975 -0.243129 0.850992 +v -0.429200 -0.261736 0.845319 +v -0.422965 -0.180894 0.869683 +v 0.450734 -0.348401 0.907690 +v 0.456969 -0.267559 0.932054 +v -0.429206 -0.286166 0.926381 +v -0.422971 -0.205324 0.950745 +v -0.615384 -0.012218 0.915792 +v -0.637537 -0.027855 0.911078 +v -0.637538 -0.035796 0.937429 +v -0.615386 -0.020160 0.942143 +g Cube.002_Cube.002_Material.003 +usemtl Material.003 +s off +f 62 61 63 +f 64 63 70 +f 68 67 65 +f 66 65 61 +f 67 63 61 +f 64 68 66 +f 69 70 71 +f 68 72 71 +f 63 67 71 +f 64 69 72 +f 62 63 64 +f 64 70 69 +f 68 65 66 +f 66 61 62 +f 67 61 65 +f 64 66 62 +f 69 71 72 +f 68 71 67 +f 63 71 70 +f 64 72 68