SQCSimulator2023/SQCSim2021/remoteplayer.cpp
2023-12-06 11:16:30 -05:00

180 lines
4.4 KiB
C++
Raw Blame History

#include "remoteplayer.h"
#include <iostream>
#include <cstring>
#include <thread>
#include <queue>
RemotePlayer::RemotePlayer(netprot::PlayerInfo pinfo) : m_pinfo(pinfo), m_aminacc(0.0f), m_animstate(Anim::STILL), m_team_id(0), current(), previous(), m_texture_front(), Player(Vector3f(0, 0, 0)){
}
RemotePlayer::RemotePlayer(netprot::PlayerInfo pinfo, const Vector3f& pos) : m_pinfo(pinfo), m_aminacc(0.0f), m_animstate(Anim::STILL), m_team_id(0), current(), previous(), m_texture_front(), Player(pos) {
}
RemotePlayer::~RemotePlayer()
{
}
void RemotePlayer::Init()
{
}
void RemotePlayer::Feed(const netprot::Output out) {
m_position = Vector3f(out.position);
//current.position = out.position;
//current.direction = out.direction;
//current.states = out.states;
//current.id = out.id;
//if (current.position != previous.position)
//{
// Vector3f positionDelta = current.position - previous.position;
// m_position = current.position + positionDelta;
// m_direction = current.direction;
//}
//if(current.direction != previous.direction)
//{
// m_direction = current.direction;
// current.direction = current.direction;
//}
//if (current.states.shooting) {
// m_animstate = Anim::SHOOTING;
//}
//else if (current.states.jumping) {
// m_animstate = Anim::JUMPING;
//}
//else if (current.states.dead) {
// m_animstate = Anim::DEAD;
//}
//else if(current.states.powerup){
// m_animstate = Anim::POWERUP;
//}
//else if (current.states.still) {
// m_animstate = Anim::STILL;
//}
//else if (current.states.running) {
// m_animstate = Anim::RUNNING;
//}
//previous.direction = current.direction;
//previous.position = current.position;
//previous.states = current.states;
//previous.id = current.id;
//m_direction = current.direction;
//m_position = current.position;
}
void RemotePlayer::Render(TextureAtlas& atlas, Shader& shader, Transformation tran, float elapsedTime, Player& camera)
{
float width = 1.f;
float height = 1.7f;
Vector3f DiffCam = GetPosition() - camera.GetPosition();
Vector3f UpCam = Vector3f(0.f, 1.f, 0.f);
Vector3f CrossA = DiffCam.Cross(UpCam);
Vector3f CrossB = DiffCam.Cross(CrossA);
CrossA.Normalize();
CrossB.Normalize();
Vector3f playerPosition = GetPosition() + Vector3f(0.f, -.75f, 0.f);
Vector3f v2 = (playerPosition + CrossA * 0.5 * width + CrossB * 0.5 * height);
Vector3f v1 = (playerPosition - CrossA * 0.5 * width + CrossB * 0.5 * height);
Vector3f v3 = (playerPosition + CrossA * 0.5 * width - CrossB * 0.5 * height);
Vector3f v4 = (playerPosition - CrossA * 0.5 * width - CrossB * 0.5 * height);
Vector3f angleRemote = GetDirection();
Vector3f angleCam = (v1 - v2).Cross(v3 - v2);
angleCam.y = 0;
angleRemote.y = 0;
angleCam.Normalize();
angleRemote.Normalize();
float angle = angleRemote.Dot(angleCam);
int index = 0;
Vector3f side = angleRemote.Cross(angleCam);
bool isLeft = side.y > 0;
if (angle >= 0.75 ) //Face //side positif
index = 0;
else if (angle >= 0.25 && isLeft) //Frontleft
index = 1;
else if (angle >= -0.25 && isLeft) //ProfileLeft
index = 3;
else if (angle >= -0.75 && isLeft) //BackLeft
index = 5;
else if (angle < -0.75) //Dos //side n<>gatif
index = 7;
else if (angle >= 0.25 && !isLeft) //FrontRight
index = 2;
else if (angle >= -0.25 && !isLeft) //ProfileRight
index = 4;
else if (angle >= -0.75 && !isLeft) //BackRight
index = 6;
std::cout << index << std::endl;
std::cout << "angle: " << angle << std::endl;
float u, v, w, h;
shader.Use();
atlas.Bind();
atlas.TextureIndexToCoord(index, u, v, w, h);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
glLoadMatrixf(tran.GetMatrix().GetInternalValues());
glBegin(GL_QUADS);
glTexCoord2f(u, v); glVertex3f(v1.x, v1.y, v1.z);
glTexCoord2f(u + w, v); glVertex3f(v2.x, v2.y, v2.z);
glTexCoord2f(u + w, v + h); glVertex3f(v3.x, v3.y, v3.z);
glTexCoord2f(u, v + h); glVertex3f(v4.x, v4.y, v4.z);
glEnd();
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT);
glDisable(GL_BLEND);
shader.Disable();
}
bool RemotePlayer::LoadTexture(Texture& texture, const std::string& filename, bool useMipmaps, bool stopOnError)
{
texture.Load(filename, useMipmaps);
if (!texture.IsValid()) {
std::cerr << "Unable to load texture (" << filename << ")" << std::endl;
if (stopOnError)
return false;
return false;
}
return true;
}