From b1c8c3f59ed6ae0e0ac7a9be826d9dea0dde247d Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 25 Nov 2016 13:08:46 +0800 Subject: [PATCH] First refactoring of weapon dropping inside Weapon class Next step are adding a similar throw() method in Weapon, and then add drop and throw methods directly in Person. --- Source/GameTick.cpp | 13 ++--- Source/Person.cpp | 115 +++++++++----------------------------------- Source/Weapons.cpp | 14 ++++++ Source/Weapons.h | 2 + 4 files changed, 43 insertions(+), 101 deletions(-) diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 14bcd30..4235adc 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -6576,15 +6576,10 @@ void Game::Tick() if (Person::players[i]->weaponactive != -1 && Person::players[i]->aitype == playercontrolled) { if (Person::players[i]->isCrouch() || Person::players[i]->animTarget == sneakanim) { Person::players[i]->throwtogglekeydown = 1; - weapons[Person::players[i]->weaponids[0]].owner = -1; - weapons[Person::players[i]->weaponids[0]].velocity = Person::players[i]->velocity * .2; - if (weapons[Person::players[i]->weaponids[0]].velocity.x == 0) - weapons[Person::players[i]->weaponids[0]].velocity.x = .1; - weapons[Person::players[i]->weaponids[0]].tipvelocity = weapons[Person::players[i]->weaponids[0]].velocity; - weapons[Person::players[i]->weaponids[0]].missed = 1; - weapons[Person::players[i]->weaponids[0]].freetime = 0; - weapons[Person::players[i]->weaponids[0]].firstfree = 1; - weapons[Person::players[i]->weaponids[0]].physics = 1; + XYZ tempVelocity = Person::players[i]->velocity * .2; + if (tempVelocity.x == 0) + tempVelocity.x = .1; + weapons[Person::players[i]->weaponids[0]].drop(tempVelocity, tempVelocity, false); Person::players[i]->num_weapons--; if (Person::players[i]->num_weapons) { Person::players[i]->weaponids[0] = Person::players[i]->weaponids[Person::players[i]->num_weapons]; diff --git a/Source/Person.cpp b/Source/Person.cpp index 0580434..81267bc 100644 --- a/Source/Person.cpp +++ b/Source/Person.cpp @@ -893,15 +893,10 @@ void Person::Reverse() if (animTarget == staffhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) { if (victim->weaponactive != -1) { victim->throwtogglekeydown = 1; - weapons[victim->weaponids[0]].owner = -1; - weapons[victim->weaponids[0]].velocity = victim->velocity * .2; - if (weapons[victim->weaponids[0]].velocity.x == 0) - weapons[victim->weaponids[0]].velocity.x = .1; - weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity; - weapons[victim->weaponids[0]].missed = 1; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + XYZ tempVelocity = victim->velocity * .2; + if (tempVelocity.x == 0) + tempVelocity.x = .1; + weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[victim->num_weapons]; @@ -923,15 +918,10 @@ void Person::Reverse() if (animTarget == staffspinhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 2 == 0)) { if (victim->weaponactive != -1) { victim->throwtogglekeydown = 1; - weapons[victim->weaponids[0]].owner = -1; - weapons[victim->weaponids[0]].velocity = victim->velocity * .2; - if (weapons[victim->weaponids[0]].velocity.x == 0) - weapons[victim->weaponids[0]].velocity.x = .1; - weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity; - weapons[victim->weaponids[0]].missed = 1; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + XYZ tempVelocity = victim->velocity * .2; + if (tempVelocity.x == 0) + tempVelocity.x = .1; + weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[victim->num_weapons]; @@ -952,15 +942,10 @@ void Person::Reverse() if (animTarget == swordslashanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) { if (victim->weaponactive != -1) { victim->throwtogglekeydown = 1; - weapons[victim->weaponids[0]].owner = -1; - weapons[victim->weaponids[0]].velocity = victim->velocity * .2; - if (weapons[victim->weaponids[0]].velocity.x == 0) - weapons[victim->weaponids[0]].velocity.x = .1; - weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity; - weapons[victim->weaponids[0]].missed = 1; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + XYZ tempVelocity = victim->velocity * .2; + if (tempVelocity.x == 0) + tempVelocity.x = .1; + weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[victim->num_weapons]; @@ -981,15 +966,10 @@ void Person::Reverse() if (animTarget == knifeslashstartanim && distsq(&victim->coords, &coords) < 2 && (victim->id == 0 || Random() % 4 == 0)) { if (victim->weaponactive != -1) { victim->throwtogglekeydown = 1; - weapons[victim->weaponids[0]].owner = -1; - weapons[victim->weaponids[0]].velocity = victim->velocity * .2; - if (weapons[victim->weaponids[0]].velocity.x == 0) - weapons[victim->weaponids[0]].velocity.x = .1; - weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity; - weapons[victim->weaponids[0]].missed = 1; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + XYZ tempVelocity = victim->velocity * .2; + if (tempVelocity.x == 0) + tempVelocity.x = .1; + weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[victim->num_weapons]; @@ -1064,16 +1044,9 @@ void Person::Reverse() victim->animTarget = staggerbackhighanim; victim->targetyaw = targetyaw + 180; victim->target = 0; - weapons[victim->weaponids[0]].owner = -1; aim = DoRotation(facing, 0, 90, 0) * 21; aim.y += 7; - weapons[victim->weaponids[0]].velocity = aim * -.2; - weapons[victim->weaponids[0]].tipvelocity = aim; - weapons[victim->weaponids[0]].missed = 1; - weapons[victim->weaponids[0]].hitsomething = 0; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + weapons[victim->weaponids[0]].drop(aim * -.2, aim); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[num_weapons]; @@ -1107,16 +1080,9 @@ void Person::Reverse() animTarget = staggerbackhighanim; targetyaw = targetyaw + 180; target = 0; - weapons[weaponids[0]].owner = -1; aim = DoRotation(facing, 0, 90, 0) * 21; aim.y += 7; - weapons[weaponids[0]].velocity = aim * -.2; - weapons[weaponids[0]].tipvelocity = aim; - weapons[weaponids[0]].hitsomething = 0; - weapons[weaponids[0]].missed = 1; - weapons[weaponids[0]].freetime = 0; - weapons[weaponids[0]].firstfree = 1; - weapons[weaponids[0]].physics = 1; + weapons[victim->weaponids[0]].drop(aim * -.2, aim); num_weapons--; if (num_weapons) { weaponids[0] = weaponids[num_weapons]; @@ -1528,15 +1494,8 @@ void Person::RagDoll(bool checkcollision) // drop weapon if (Random() % 2 == 0) { if (weaponactive != -1 && animTarget != rabbitkickanim && num_weapons > 0) { - weapons[weaponids[0]].owner = -1; - weapons[weaponids[0]].hitsomething = 0; - weapons[weaponids[0]].velocity = jointVel(righthand) * scale * -.3; + weapons[weaponids[0]].drop(jointVel(righthand) * scale * -.3, jointVel(righthand) * scale); weapons[weaponids[0]].velocity.x += .01; - weapons[weaponids[0]].tipvelocity = jointVel(righthand) * scale; - weapons[weaponids[0]].missed = 1; - weapons[weaponids[0]].freetime = 0; - weapons[weaponids[0]].firstfree = 1; - weapons[weaponids[0]].physics = 1; num_weapons--; if (num_weapons) { weaponids[0] = weaponids[num_weapons]; @@ -2810,16 +2769,9 @@ void Person::DoAnimations() victim->animTarget = staggerbackhighanim; victim->targetyaw = targetyaw + 180; victim->target = 0; - weapons[victim->weaponids[0]].owner = -1; aim = DoRotation(facing, 0, 90, 0) * 21; aim.y += 7; - weapons[victim->weaponids[0]].velocity = aim * -.2; - weapons[victim->weaponids[0]].tipvelocity = aim; - weapons[victim->weaponids[0]].missed = 1; - weapons[weaponids[0]].hitsomething = 0; - weapons[victim->weaponids[0]].freetime = 0; - weapons[victim->weaponids[0]].firstfree = 1; - weapons[victim->weaponids[0]].physics = 1; + weapons[victim->weaponids[0]].drop(aim * -.2, aim); victim->num_weapons--; if (victim->num_weapons) { victim->weaponids[0] = victim->weaponids[num_weapons]; @@ -4268,15 +4220,8 @@ void Person::DoStuff() deathbleeding = 0; if (bloodloss > damagetolerance && animation[animTarget].attack == neutral) { if (weaponactive != -1) { - weapons[weaponids[0]].owner = -1; - weapons[weaponids[0]].velocity = velocity * scale * -.3; + weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale); weapons[weaponids[0]].velocity.x += .01; - weapons[weaponids[0]].tipvelocity = velocity * scale; - weapons[weaponids[0]].missed = 1; - weapons[weaponids[0]].hitsomething = 0; - weapons[weaponids[0]].freetime = 0; - weapons[weaponids[0]].firstfree = 1; - weapons[weaponids[0]].physics = 1; num_weapons--; if (num_weapons) { weaponids[0] = weaponids[num_weapons]; @@ -4694,15 +4639,8 @@ void Person::DoStuff() RagDoll(0); if (weaponactive != -1) { - weapons[weaponids[0]].owner = -1; - weapons[weaponids[0]].velocity = velocity * scale * -.3; + weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale); weapons[weaponids[0]].velocity.x += .01; - weapons[weaponids[0]].tipvelocity = velocity * scale; - weapons[weaponids[0]].missed = 1; - weapons[weaponids[0]].hitsomething = 0; - weapons[weaponids[0]].freetime = 0; - weapons[weaponids[0]].firstfree = 1; - weapons[weaponids[0]].physics = 1; num_weapons--; if (num_weapons) { weaponids[0] = weaponids[num_weapons]; @@ -4757,15 +4695,8 @@ void Person::DoStuff() DoBlood(1, 255); if (weaponactive != -1) { - weapons[weaponids[0]].owner = -1; - weapons[weaponids[0]].velocity = velocity * scale * -.3; + weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale); weapons[weaponids[0]].velocity.x += .01; - weapons[weaponids[0]].tipvelocity = velocity * scale; - weapons[weaponids[0]].missed = 1; - weapons[weaponids[0]].hitsomething = 0; - weapons[weaponids[0]].freetime = 0; - weapons[weaponids[0]].firstfree = 1; - weapons[weaponids[0]].physics = 1; num_weapons--; if (num_weapons) { weaponids[0] = weaponids[num_weapons]; diff --git a/Source/Weapons.cpp b/Source/Weapons.cpp index 5170ece..4b7936a 100644 --- a/Source/Weapons.cpp +++ b/Source/Weapons.cpp @@ -1078,6 +1078,20 @@ void Weapon::Draw() } } +void Weapon::drop(XYZ v, XYZ tv, bool sethitsomething) +{ + owner = -1; + velocity = v; + tipvelocity = tv; + missed = 1; + if (sethitsomething) { + hitsomething = 0; + } + freetime = 0; + firstfree = 1; + physics = 1; +} + int Weapons::Draw() { glAlphaFunc(GL_GREATER, 0.9); diff --git a/Source/Weapons.h b/Source/Weapons.h index 0c66b5d..6ed7e00 100644 --- a/Source/Weapons.h +++ b/Source/Weapons.h @@ -66,6 +66,8 @@ public: } void setType(int); + void drop(XYZ velocity, XYZ tipvelocity, bool sethitsomething = true); + int owner; XYZ position; XYZ tippoint; -- 2.39.5