📄 cracecar.cpp
字号:
}while(iX>0);
m_iLaps++;
if(m_iLaps == theTrack->GetNumberOfLaps())
{
m_iCarState = CARSTATE_RACECOMPLETED;
}
else
{
if(m_iControlType == CTRL_USER)
theTrack->m_sndSemaphore.Play();
}
}
else
{
iX = 0;
iY = 0;
m_iDistanceToNextCheckPoint = 999999999;
do
{
iX = iX / 40;
iY = iY / 40;
theTrack->GetCheckPointPosition(&iX, &iY, m_iCheckPoint+1);
if(m_iDistanceToNextCheckPoint > (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)) ))
{
m_iDistanceToNextCheckPoint = (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)));
}
if(iX != -1)
iX += 40;
}while(iX>0);
}
}
else
{
if(theTrack->GetCheckPoint((int)m_fPosX+10, (int)m_fPosY+18) == m_iCheckPoint+1)
{
m_iCheckPoint++;
iX = 0;
iY = 0;
m_iDistanceToNextCheckPoint = 999999999;
do
{
iX = iX / 40;
iY = iY / 40;
theTrack->GetCheckPointPosition(&iX, &iY, m_iCheckPoint+1);
if(m_iDistanceToNextCheckPoint > (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)) ))
{
m_iDistanceToNextCheckPoint = (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)));
}
if(iX != -1)
iX += 40;
}while(iX>0);
m_fBackupDirectionX = m_fDirectionX;
m_iBackupDistanceToNextCheckPoint = m_iDistanceToNextCheckPoint;
m_fBackupDirectionY = m_fDirectionY;
m_fBackupIncrementX = m_fIncrementX;
m_fBackupIncrementY = m_fIncrementY;
m_iBackupAngle = m_iAngle;
m_fBackupPosX = m_fPosX;
m_fBackupPosY = m_fPosY;
m_iBackupSprite = m_sprCar.m_iAbsolutePosition;
iBackupTurnCar = iTurnCar;
}
else
{
iX = 0;
iY = 0;
m_iDistanceToNextCheckPoint = 999999999;
do
{
iX = iX / 40;
iY = iY / 40;
theTrack->GetCheckPointPosition(&iX, &iY, m_iCheckPoint+1);
if(m_iDistanceToNextCheckPoint > (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)) ))
{
m_iDistanceToNextCheckPoint = (int) sqrt( ((m_fPosX - iX)*(m_fPosX - iX)) + ((m_fPosY - iY)*(m_fPosY - iY)));
}
if(iX != -1)
iX += 40;
}while(iX>0);
}
}
}
}
}
if(m_iControlType != CTRL_USER && m_iControlType != CTRL_NETWORK_LOCAL)
{
m_pSound.SetPosition(m_fPosX/200.0f, m_fPosY/200.0f, 0.0f);
m_pCrashSound.SetPosition(m_fPosX/200.0f, m_fPosY/200.0f, 0.0f);
//m_pSound.SetVelocity((float)m_iSpeed/27,(float)m_iSpeed/27, 0.0f);
}
DWORD dwFreq = 0;
if(GetSpeed() != 0)
{
m_pSound.Play(0, DSBPLAY_LOOPING);
dwFreq = 22050 + (GetSpeed() * 50);
m_pSound.m_pSoundBuffer->SetFrequency(dwFreq);
}
else
{
m_pSound.Stop();
}
if(m_iControlType == CTRL_USER || m_iControlType == CTRL_NETWORK_LOCAL)
{
cSoundInterface pSInterface;
pSInterface.SetListernerPosition( m_fPosX/200.0f, (m_fPosY/200.0f), 0.0f);
}
}
void cRaceCar::GetPos(int *iX, int *iY)
{
*iX = (int) m_fPosX;
*iY = (int) m_fPosY;
}
void cRaceCar::RotateBound(int iAngle)
{
#define PI 3.141592654f
m_hcRaceCar.Destroy();
POINT *pFirstPoint;
POINT *pPoints;
pPoints = (POINT*) malloc(CARBOUND_POINTS * sizeof(POINT));
memcpy(pPoints, m_pBound, CARBOUND_POINTS * sizeof(POINT));
pFirstPoint = pPoints;
// Move the vertices so that we made the center of the car the 0,0 axis
for(int i=0;i<CARBOUND_POINTS;i++)
{
pPoints->x = pPoints->x - 19;
pPoints->y = pPoints->y - 18;
pPoints++;
}
// Rotate the points
pPoints = pFirstPoint;
for(i=0;i<CARBOUND_POINTS;i++)
{
double x,y;
x = (cos(PI * (double)iAngle / 180.0) * (double)pPoints->x - sin(PI * (double)iAngle / 180.0) * (double) pPoints->y);
y = (sin(PI * (double)iAngle / 180.0) * (double)pPoints->x + cos(PI * (double)iAngle / 180.0) * (double) pPoints->y);
//x+=0.5;
//y+=0.5;
pPoints->x = (int)x;
pPoints->y = (int)y;
pPoints++;
}
pPoints = pFirstPoint;
// Move the vertices back to the correct position
for(i=0;i<CARBOUND_POINTS;i++)
{
pPoints->x = pPoints->x + 19;
pPoints->y = pPoints->y + 18;
pPoints++;
}
// Restore the original pointer
pPoints = pFirstPoint;
m_hcRaceCar.CreatePolygonBound(pPoints, CARBOUND_POINTS);
free(pPoints);
}
void cRaceCar::SetLastCheckPoint(int iLastCheckPoint)
{
m_iCheckPoint = iLastCheckPoint;
}
int cRaceCar::GetLastCheckPoint()
{
return m_iCheckPoint;
}
int cRaceCar::GetSpeed()
{
return m_iSpeed;
}
void cRaceCar::SetControlType(int iType)
{
m_iControlType = iType;
if(m_iControlType == CTRL_USER || m_iControlType == CTRL_NETWORK_LOCAL)
{
m_pSound.Create(MAKEINTRESOURCE(IDR_ENGINE), DSBCAPS_CTRLFREQUENCY, DS3DALG_DEFAULT);
m_pCrashSound.Create(MAKEINTRESOURCE(IDR_EXPLODE), DSBCAPS_CTRLFREQUENCY, DS3DALG_DEFAULT);
}
}
int cRaceCar::GetControlType()
{
return m_iControlType;
}
void cRaceCar::TurnCarLeft()
{
if(GetTickCount() - m_lLastTurn > TURN_TIME)
{
if(m_fDirectionY == 9)
{
m_fIncrementY = 1;
}
if(m_fDirectionY == -9)
{
m_fIncrementY = -1;
}
if(m_fDirectionX == 9)
{
m_fIncrementX = 1;
}
if(m_fDirectionX == -9)
{
m_fIncrementX = -1;
}
m_fDirectionX -= m_fIncrementX;
m_fDirectionY -= m_fIncrementY;
m_sprCar.Previous();
m_iAngle -= 10;
if(m_iAngle < 0)
m_iAngle = 360 + m_iAngle;
RotateBound(m_iAngle);
m_lLastTurn = GetTickCount();
}
}
void cRaceCar::TurnCarRight()
{
if(GetTickCount() - m_lLastTurn > TURN_TIME)
{
if(m_fDirectionY == 9)
{
m_fIncrementY = -1;
}
if(m_fDirectionY == -9)
{
m_fIncrementY = 1;
}
if(m_fDirectionX == 9)
{
m_fIncrementX = -1;
}
if(m_fDirectionX == -9)
{
m_fIncrementX = 1;
}
m_fDirectionX += m_fIncrementX;
m_fDirectionY += m_fIncrementY;
m_sprCar.Next();
m_iAngle += 10;
if(m_iAngle >= 360)
m_iAngle = (m_iAngle - 360);
RotateBound(m_iAngle);
m_lLastTurn = GetTickCount();
}
}
void cRaceCar::Accelerate()
{
// If its time to accelerate
if(GetTickCount() - m_lLastSpeedIncr > (UINT)RAISESPEED_TIME )
{
m_iSpeed=m_iSpeed+5;
// If the car is controlled by the computer and the level is above hard, make
// the cars accelerate faster
if(GetRaceXApp()->GetDifficultyLevel() > 40 && m_iControlType == CTRL_COMPUTER )
{
m_iSpeed += ((GetRaceXApp()->GetDifficultyLevel()-50)/10);
}
// If we haven磘 rechead the maximum speed, raise it up
if(m_iSpeed > m_iMaxSpeed - (m_iControlType == CTRL_COMPUTER ? 40 - GetRaceXApp()->GetDifficultyLevel(): 0) )
{
m_iSpeed = m_iMaxSpeed - (m_iControlType == CTRL_COMPUTER ? 40 - GetRaceXApp()->GetDifficultyLevel() : 0);
}
m_lLastSpeedIncr = GetTickCount();
}
}
void cRaceCar::HitCar()
{
// Car hitted something
// Apply heat behavior in car (decrease speed and maintain angle)
m_iSpeed=m_iSpeed-40;
m_lLastSpeedIncr = GetTickCount();
if(m_iSpeed < 0)
{
m_iSpeed = 0;
}
}
void cRaceCar::BreakCar()
{
// Break car
if(GetTickCount() - m_lLastSpeedIncr > (UINT)RAISESPEED_TIME )
{
m_iSpeed=m_iSpeed-20;
if(m_iSpeed < 0)
{
m_iSpeed = 0;
}
m_lLastSpeedIncr = GetTickCount();
}
}
void cRaceCar::Idle()
{
// User is not pressing anything
if(GetTickCount() - m_lLastSpeedIncr > (UINT)RAISESPEED_TIME )
{
m_iSpeed=m_iSpeed-5;
if(m_iSpeed < 0)
{
m_iSpeed = 0;
}
m_lLastSpeedIncr = GetTickCount();
}
}
void cRaceCar::SetCarState(int iState)
{
m_iCarState = iState;
// If we set the car to the crash state, we should
// load the crash tiles, depending on the car color
if(m_iCarState == CARSTATE_CRASHED_WALL)
{
switch(m_iColor)
{
case IDB_CAR_GREEN:
if(m_iAngle == 0)
m_sprCarExplode_0.Create(GetMainApp()->m_hInst, IDB_EXPLODE_0_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 0 && m_iAngle < 90)
m_sprCarExplode_45.Create(GetMainApp()->m_hInst, IDB_EXPLODE_45_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 90)
m_sprCarExplode_90.Create(GetMainApp()->m_hInst, IDB_EXPLODE_90_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 90 && m_iAngle < 180)
m_sprCarExplode_135.Create(GetMainApp()->m_hInst, IDB_EXPLODE_135_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 180)
m_sprCarExplode_180.Create(GetMainApp()->m_hInst, IDB_EXPLODE_180_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 180 && m_iAngle < 270)
m_sprCarExplode_225.Create(GetMainApp()->m_hInst, IDB_EXPLODE_225_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 270)
m_sprCarExplode_270.Create(GetMainApp()->m_hInst, IDB_EXPLODE_270_GREEN, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 270 && m_iAngle < 360)
m_sprCarExplode_315.Create(GetMainApp()->m_hInst, IDB_EXPLODE_315_GREEN, 320, 200, RGB(0,0,0), 40, 40);
break;
case IDB_CAR_RED:
if(m_iAngle == 0)
m_sprCarExplode_0.Create(GetMainApp()->m_hInst, IDB_EXPLODE_0_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 0 && m_iAngle < 90)
m_sprCarExplode_45.Create(GetMainApp()->m_hInst, IDB_EXPLODE_45_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 90)
m_sprCarExplode_90.Create(GetMainApp()->m_hInst, IDB_EXPLODE_90_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 90 && m_iAngle < 180)
m_sprCarExplode_135.Create(GetMainApp()->m_hInst, IDB_EXPLODE_135_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 180)
m_sprCarExplode_180.Create(GetMainApp()->m_hInst, IDB_EXPLODE_180_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 180 && m_iAngle < 270)
m_sprCarExplode_225.Create(GetMainApp()->m_hInst, IDB_EXPLODE_225_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 270)
m_sprCarExplode_270.Create(GetMainApp()->m_hInst, IDB_EXPLODE_270_RED, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 270 && m_iAngle < 360)
m_sprCarExplode_315.Create(GetMainApp()->m_hInst, IDB_EXPLODE_315_RED, 320, 200, RGB(0,0,0), 40, 40);
break;
case IDB_CAR_YEALLOW:
if(m_iAngle == 0)
m_sprCarExplode_0.Create(GetMainApp()->m_hInst, IDB_EXPLODE_0_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 0 && m_iAngle < 90)
m_sprCarExplode_45.Create(GetMainApp()->m_hInst, IDB_EXPLODE_45_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 90)
m_sprCarExplode_90.Create(GetMainApp()->m_hInst, IDB_EXPLODE_90_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 90 && m_iAngle < 180)
m_sprCarExplode_135.Create(GetMainApp()->m_hInst, IDB_EXPLODE_135_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 180)
m_sprCarExplode_180.Create(GetMainApp()->m_hInst, IDB_EXPLODE_180_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 180 && m_iAngle < 270)
m_sprCarExplode_225.Create(GetMainApp()->m_hInst, IDB_EXPLODE_225_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 270)
m_sprCarExplode_270.Create(GetMainApp()->m_hInst, IDB_EXPLODE_270_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 270 && m_iAngle < 360)
m_sprCarExplode_315.Create(GetMainApp()->m_hInst, IDB_EXPLODE_315_YEALLOW, 320, 200, RGB(0,0,0), 40, 40);
break;
case IDB_CAR_BLUE:
if(m_iAngle == 0)
m_sprCarExplode_0.Create(GetMainApp()->m_hInst, IDB_EXPLODE_0_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 0 && m_iAngle < 90)
m_sprCarExplode_45.Create(GetMainApp()->m_hInst, IDB_EXPLODE_45_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 90)
m_sprCarExplode_90.Create(GetMainApp()->m_hInst, IDB_EXPLODE_90_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 90 && m_iAngle < 180)
m_sprCarExplode_135.Create(GetMainApp()->m_hInst, IDB_EXPLODE_135_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 180)
m_sprCarExplode_180.Create(GetMainApp()->m_hInst, IDB_EXPLODE_180_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 180 && m_iAngle < 270)
m_sprCarExplode_225.Create(GetMainApp()->m_hInst, IDB_EXPLODE_225_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle == 270)
m_sprCarExplode_270.Create(GetMainApp()->m_hInst, IDB_EXPLODE_270_BLUE, 320, 200, RGB(0,0,0), 40, 40);
if(m_iAngle > 270 && m_iAngle < 360)
m_sprCarExplode_315.Create(GetMainApp()->m_hInst, IDB_EXPLODE_315_BLUE, 320, 200, RGB(0,0,0), 40, 40);
break;
}
}
}
int cRaceCar::GetCarState()
{
return m_iCarState;
}
void cRaceCar::Crashed()
{
// Car Crashed
if(GetTickCount() - m_lLastSpeedIncr > (UINT)RAISESPEED_TIME )
{
if(m_iSpeed > 180)
m_iSpeed=m_iSpeed-50;
else
m_iSpeed=m_iSpeed-25;
if(m_iSpeed < 0)
{
m_iSpeed = 0;
}
m_lLastSpeedIncr = GetTickCount();
}
}
int cRaceCar::GetPosition()
{
return m_iPosition;
}
void cRaceCar::SetPosition(int iPosition)
{
m_iPosition = iPosition;
}
int cRaceCar::GetDistanceToNextCheckPoint()
{
return m_iDistanceToNextCheckPoint;
}
long cRaceCar::GetLapElapseTime(int iLap)
{
// Return the elapse time of the lap
if(iLap == -1)
{
if(m_lCurrentTime == 0 && m_iControlType != CTRL_NETWORK_LOCAL)
return 0;
else
{
if(m_iControlType == CTRL_NETWORK_LOCAL)
return m_dwElapseTime;
else
return GetTickCount()-m_lCurrentTime;
}
}
else
return m_vcLapTimes[iLap];
}
long cRaceCar::GetLastLapTime()
{
if(m_vcLapTimes.size() == 0)
return 0;
else
return m_vcLapTimes[m_vcLapTimes.size()-1];
}
int cRaceCar::GetCarColor()
{
return m_iColor;
}
void cRaceCar::SetID(BYTE iValue)
{
m_bID = iValue;
}
BYTE cRaceCar::GetID()
{
return m_bID;
}
void cRaceCar::SetSpeed(int iValue)
{
m_iSpeed = iValue;
}
void cRaceCar::AddLapTime(DWORD dwTime)
{
m_vcLapTimes.push_back(dwTime);
}
void cRaceCar::Backup()
{
// Backup all the current state of the car
m_fBackupDirectionX = m_fDirectionX;
m_iBackupDistanceToNextCheckPoint = m_iDistanceToNextCheckPoint;
m_fBackupDirectionY = m_fDirectionY;
m_fBackupIncrementX = m_fIncrementX;
m_fBackupIncrementY = m_fIncrementY;
m_iBackupAngle = m_iAngle;
m_fBackupPosX = m_fPosX;
m_fBackupPosY = m_fPosY;
m_iBackupSprite = m_sprCar.m_iAbsolutePosition;
iBackupTurnCar = iTurnCar;
RotateBound(m_iAngle);
}
void cRaceCar::MoveForward(int iSpeed)
{
// Move the car forward, using an alternate speed
// Used when someone hits your car
m_fPosY = m_fPosY + ((float)m_fDirectionY / 9.0f) * ((float)iSpeed / _SPEED_FACTOR_);
m_fPosX = m_fPosX + ((float)m_fDirectionX / 9.0f) * ((float)iSpeed / _SPEED_FACTOR_);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -