Replace raw pointers in Corpse::Corpse with references

This commit is contained in:
yvt 2019-07-17 10:43:25 +09:00
parent 5db97f7f10
commit 3916a39713
No known key found for this signature in database
GPG Key ID: 48F2768FA8D07C92
4 changed files with 47 additions and 40 deletions

View File

@ -381,7 +381,7 @@ namespace spades {
if (name == "p" && down) { if (name == "p" && down) {
Corpse *corp; Corpse *corp;
Player &victim = world->GetLocalPlayer().value(); Player &victim = world->GetLocalPlayer().value();
corp = new Corpse(renderer, map, &victim); corp = new Corpse(*renderer, *map, victim);
corp->AddImpulse(victim.GetFront() * 32.f); corp->AddImpulse(victim.GetFront() * 32.f);
corpses.emplace_back(corp); corpses.emplace_back(corp);

View File

@ -828,7 +828,7 @@ namespace spades {
// create ragdoll corpse // create ragdoll corpse
if (cg_ragdoll && victim.GetTeamId() < 2) { if (cg_ragdoll && victim.GetTeamId() < 2) {
Corpse *corp; Corpse *corp;
corp = new Corpse(renderer, map, &victim); corp = new Corpse(*renderer, *map, victim);
if (&victim == world->GetLocalPlayer()) if (&victim == world->GetLocalPlayer())
lastMyCorpse = corp; lastMyCorpse = corp;
if (&killer != &victim && kt != KillTypeGrenade) { if (&killer != &victim && kt != KillTypeGrenade) {

View File

@ -33,23 +33,23 @@ DEFINE_SPADES_SETTING(r_corpseLineCollision, "1");
namespace spades { namespace spades {
namespace client { namespace client {
Corpse::Corpse(IRenderer *renderer, GameMap *map, Player *p) Corpse::Corpse(IRenderer &renderer, GameMap &map, Player &p)
: renderer(renderer), map(map) { : renderer{renderer}, map{map} {
SPADES_MARK_FUNCTION(); SPADES_MARK_FUNCTION();
playerId = p->GetId(); playerId = p.GetId();
IntVector3 col = p->GetWorld().GetTeam(p->GetTeamId()).color; IntVector3 col = p.GetWorld().GetTeam(p.GetTeamId()).color;
color = MakeVector3(col.x / 255.f, col.y / 255.f, col.z / 255.f); color = MakeVector3(col.x / 255.f, col.y / 255.f, col.z / 255.f);
bool crouch = p->GetInput().crouch; bool crouch = p.GetInput().crouch;
Vector3 front = p->GetFront(); Vector3 front = p.GetFront();
float yaw = atan2(front.y, front.x) + static_cast<float>(M_PI) * .5f; float yaw = atan2(front.y, front.x) + static_cast<float>(M_PI) * .5f;
// float pitch = -atan2(front.z, sqrt(front.x * front.x + front.y * front.y)); // float pitch = -atan2(front.z, sqrt(front.x * front.x + front.y * front.y));
// lower axis // lower axis
Matrix4 lower = Matrix4::Translate(p->GetOrigin()); Matrix4 lower = Matrix4::Translate(p.GetOrigin());
lower = lower * Matrix4::Rotate(MakeVector3(0, 0, 1), yaw); lower = lower * Matrix4::Rotate(MakeVector3(0, 0, 1), yaw);
Matrix4 torso; Matrix4 torso;
@ -290,16 +290,16 @@ namespace spades {
static float fractf(float v) { return v - floorf(v); } static float fractf(float v) { return v - floorf(v); }
static void CheckEscape(GameMap *map, IntVector3 hitBlock, IntVector3 a, IntVector3 b, static void CheckEscape(GameMap &map, IntVector3 hitBlock, IntVector3 a, IntVector3 b,
IntVector3 dir, float &bestDist, IntVector3 &bestDir) { IntVector3 dir, float &bestDist, IntVector3 &bestDir) {
hitBlock += dir; hitBlock += dir;
IntVector3 aa = a + dir; IntVector3 aa = a + dir;
IntVector3 bb = b + dir; IntVector3 bb = b + dir;
if (map->IsSolidWrapped(hitBlock.x, hitBlock.y, hitBlock.z)) if (map.IsSolidWrapped(hitBlock.x, hitBlock.y, hitBlock.z))
return; return;
if (map->IsSolidWrapped(aa.x, aa.y, aa.z)) if (map.IsSolidWrapped(aa.x, aa.y, aa.z))
return; return;
if (map->IsSolidWrapped(bb.x, bb.y, bb.z)) if (map.IsSolidWrapped(bb.x, bb.y, bb.z))
return; return;
float dist; float dist;
if (dir.x == 1) { if (dir.x == 1) {
@ -339,9 +339,9 @@ namespace spades {
IntVector3 hitBlock; IntVector3 hitBlock;
if (map->CastRay(n1.lastPos, n2.lastPos, 16.f, hitBlock)) { if (map.CastRay(n1.lastPos, n2.lastPos, 16.f, hitBlock)) {
GameMap::RayCastResult res1 = map->CastRay2(n1.lastPos, n2.lastPos - n1.lastPos, 8); GameMap::RayCastResult res1 = map.CastRay2(n1.lastPos, n2.lastPos - n1.lastPos, 8);
GameMap::RayCastResult res2 = map->CastRay2(n2.lastPos, n1.lastPos - n2.lastPos, 8); GameMap::RayCastResult res2 = map.CastRay2(n2.lastPos, n1.lastPos - n2.lastPos, 8);
if (!res1.hit) if (!res1.hit)
return; return;
@ -529,9 +529,9 @@ namespace spades {
// node.vel *= damp; // node.vel *= damp;
if (!map->ClipBox(oldPos.x, oldPos.y, oldPos.z)) { if (!map.ClipBox(oldPos.x, oldPos.y, oldPos.z)) {
if (map->ClipBox(node.pos.x, oldPos.y, oldPos.z)) { if (map.ClipBox(node.pos.x, oldPos.y, oldPos.z)) {
node.vel.x = -node.vel.x * .2f; node.vel.x = -node.vel.x * .2f;
if (fabsf(node.vel.x) < .3f) if (fabsf(node.vel.x) < .3f)
node.vel.x = 0.f; node.vel.x = 0.f;
@ -541,7 +541,7 @@ namespace spades {
node.vel.z *= .5f; node.vel.z *= .5f;
} }
if (map->ClipBox(node.pos.x, node.pos.y, oldPos.z)) { if (map.ClipBox(node.pos.x, node.pos.y, oldPos.z)) {
node.vel.y = -node.vel.y * .2f; node.vel.y = -node.vel.y * .2f;
if (fabsf(node.vel.y) < .3f) if (fabsf(node.vel.y) < .3f)
node.vel.y = 0.f; node.vel.y = 0.f;
@ -551,7 +551,7 @@ namespace spades {
node.vel.z *= .5f; node.vel.z *= .5f;
} }
if (map->ClipBox(node.pos.x, node.pos.y, node.pos.z)) { if (map.ClipBox(node.pos.x, node.pos.y, node.pos.z)) {
node.vel.z = -node.vel.z * .2f; node.vel.z = -node.vel.z * .2f;
if (fabsf(node.vel.z) < .3f) if (fabsf(node.vel.z) < .3f)
node.vel.z = 0.f; node.vel.z = 0.f;
@ -561,7 +561,7 @@ namespace spades {
node.vel.y *= .5f; node.vel.y *= .5f;
} }
if (map->ClipBox(node.pos.x, node.pos.y, node.pos.z)) { if (map.ClipBox(node.pos.x, node.pos.y, node.pos.z)) {
// TODO: getting out block // TODO: getting out block
// node.pos = oldPos; // node.pos = oldPos;
// node.vel *= .5f; // node.vel *= .5f;
@ -569,10 +569,10 @@ namespace spades {
} }
/* /*
if(map->ClipBox(node.pos.x, if(map.ClipBox(node.pos.x,
node.pos.y, node.pos.y,
node.pos.z)){ node.pos.z)){
if(!map->ClipBox(node.pos.x, if(!map.ClipBox(node.pos.x,
node.pos.y, node.pos.y,
oldPos.z)){ oldPos.z)){
node.vel.z = -node.vel.z * .2f; node.vel.z = -node.vel.z * .2f;
@ -580,7 +580,7 @@ namespace spades {
node.vel.z = 0.f; node.vel.z = 0.f;
node.pos.z = oldPos.z; node.pos.z = oldPos.z;
} }
if(!map->ClipBox(node.pos.x, if(!map.ClipBox(node.pos.x,
oldPos.y, oldPos.y,
node.pos.z)){ node.pos.z)){
node.vel.y = -node.vel.y * .2f; node.vel.y = -node.vel.y * .2f;
@ -588,7 +588,7 @@ namespace spades {
node.vel.y = 0.f; node.vel.y = 0.f;
node.pos.y = oldPos.y; node.pos.y = oldPos.y;
} }
if(!map->ClipBox(oldPos.x, if(!map.ClipBox(oldPos.x,
node.pos.y, node.pos.y,
node.pos.z)){ node.pos.z)){
node.vel.x = -node.vel.x * .2f; node.vel.x = -node.vel.x * .2f;
@ -638,14 +638,14 @@ namespace spades {
param.matrix = torso * scaler; param.matrix = torso * scaler;
model = renderer->RegisterModel("Models/Player/Torso.kv6"); model = renderer.RegisterModel("Models/Player/Torso.kv6");
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
} }
// draw Head // draw Head
{ {
Vector3 headBase = (torso * MakeVector3(0.0f, 0.f, 0.f)).GetXYZ(); Vector3 headBase = (torso * MakeVector3(0.0f, 0.f, 0.f)).GetXYZ();
model = renderer->RegisterModel("Models/Player/Head.kv6"); model = renderer.RegisterModel("Models/Player/Head.kv6");
Vector3 aX, aY, aZ; Vector3 aX, aY, aZ;
Vector3 center = (nodes[Torso1].pos + nodes[Torso2].pos) * .5f; Vector3 center = (nodes[Torso1].pos + nodes[Torso2].pos) * .5f;
@ -658,7 +658,7 @@ namespace spades {
aX = Vector3::Cross(aY, aZ).Normalize(); aX = Vector3::Cross(aY, aZ).Normalize();
param.matrix = Matrix4::FromAxis(-aX, aY, -aZ, headBase) * scaler; param.matrix = Matrix4::FromAxis(-aX, aY, -aZ, headBase) * scaler;
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
} }
// draw Arms // draw Arms
@ -666,7 +666,7 @@ namespace spades {
Vector3 arm1Base = (torso * MakeVector3(0.4f, 0.f, 0.2f)).GetXYZ(); Vector3 arm1Base = (torso * MakeVector3(0.4f, 0.f, 0.2f)).GetXYZ();
Vector3 arm2Base = (torso * MakeVector3(-0.4f, 0.f, 0.2f)).GetXYZ(); Vector3 arm2Base = (torso * MakeVector3(-0.4f, 0.f, 0.2f)).GetXYZ();
model = renderer->RegisterModel("Models/Player/Arm.kv6"); model = renderer.RegisterModel("Models/Player/Arm.kv6");
Vector3 aX, aY, aZ; Vector3 aX, aY, aZ;
@ -677,7 +677,7 @@ namespace spades {
aX = Vector3::Cross(aY, aZ).Normalize(); aX = Vector3::Cross(aY, aZ).Normalize();
param.matrix = Matrix4::FromAxis(aX, aY, aZ, arm1Base) * scaler; param.matrix = Matrix4::FromAxis(aX, aY, aZ, arm1Base) * scaler;
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
aZ = nodes[Arm2].pos - nodes[Torso2].pos; aZ = nodes[Arm2].pos - nodes[Torso2].pos;
aZ = aZ.Normalize(); aZ = aZ.Normalize();
@ -686,7 +686,7 @@ namespace spades {
aX = Vector3::Cross(aY, aZ).Normalize(); aX = Vector3::Cross(aY, aZ).Normalize();
param.matrix = Matrix4::FromAxis(aX, aY, aZ, arm2Base) * scaler; param.matrix = Matrix4::FromAxis(aX, aY, aZ, arm2Base) * scaler;
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
} }
// draw Leg // draw Leg
@ -694,7 +694,7 @@ namespace spades {
Vector3 leg1Base = (torso * MakeVector3(0.25f, 0.f, 0.9f)).GetXYZ(); Vector3 leg1Base = (torso * MakeVector3(0.25f, 0.f, 0.9f)).GetXYZ();
Vector3 leg2Base = (torso * MakeVector3(-0.25f, 0.f, 0.9f)).GetXYZ(); Vector3 leg2Base = (torso * MakeVector3(-0.25f, 0.f, 0.9f)).GetXYZ();
model = renderer->RegisterModel("Models/Player/Leg.kv6"); model = renderer.RegisterModel("Models/Player/Leg.kv6");
Vector3 aX, aY, aZ; Vector3 aX, aY, aZ;
@ -705,7 +705,7 @@ namespace spades {
aX = Vector3::Cross(aY, aZ).Normalize(); aX = Vector3::Cross(aY, aZ).Normalize();
param.matrix = Matrix4::FromAxis(aX, aY, aZ, leg1Base) * scaler; param.matrix = Matrix4::FromAxis(aX, aY, aZ, leg1Base) * scaler;
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
aZ = nodes[Leg2].pos - nodes[Torso4].pos; aZ = nodes[Leg2].pos - nodes[Torso4].pos;
aZ = aZ.Normalize(); aZ = aZ.Normalize();
@ -714,7 +714,7 @@ namespace spades {
aX = Vector3::Cross(aY, aZ).Normalize(); aX = Vector3::Cross(aY, aZ).Normalize();
param.matrix = Matrix4::FromAxis(aX, aY, aZ, leg2Base) * scaler; param.matrix = Matrix4::FromAxis(aX, aY, aZ, leg2Base) * scaler;
renderer->RenderModel(model, param); renderer.RenderModel(model, param);
} }
} }
@ -733,7 +733,7 @@ namespace spades {
for (int i = 0; i < NodeCount; i++) { for (int i = 0; i < NodeCount; i++) {
IntVector3 outBlk; IntVector3 outBlk;
if (map->CastRay(eye, nodes[i].pos, 256.f, outBlk)) if (map.CastRay(eye, nodes[i].pos, 256.f, outBlk))
return true; return true;
} }
return false; return false;

View File

@ -61,8 +61,8 @@ namespace spades {
Edge() { node1 = node2 = NodeCount; } Edge() { node1 = node2 = NodeCount; }
}; };
IRenderer *renderer; IRenderer &renderer;
GameMap *map; GameMap &map;
Vector3 color; Vector3 color;
int playerId; int playerId;
@ -85,8 +85,15 @@ namespace spades {
void LineCollision(NodeType a, NodeType b, float dt); void LineCollision(NodeType a, NodeType b, float dt);
public: public:
// TODO: Replace pointers with references /**
Corpse(IRenderer *renderer, GameMap *map, Player *p); * Construct a "corpse" client object.
*
* @param renderer The renderer. Must outlive `Corpse`.
* @param map The game map, used for physics. Must outlive `Corpse`.
* @param p The player to create a corpse from. Can be destroyed
* after `Corpse` is constructed.
*/
Corpse(IRenderer &renderer, GameMap &map, Player &p);
~Corpse(); ~Corpse();
void Update(float dt); void Update(float dt);