cRaceCar.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:29k
- // RACE X
- //
- // Written by Mauricio Teichmann Ritter
- //
- // Copyright (C) 2002, Brazil. All rights reserved.
- //
- //
- #include "stdafx.h"
- #include "RaceX.h"
- #include "cRaceCar.h"
- #include ".applibckeyboard.h"
- #include <math.h>
- #include <stdio.h>
- #include "cRaceTrack.h"
- #define CARBOUND_POINTS 4
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- cRaceCar::cRaceCar()
- {
- m_iMaxSpeed = 270;
- m_iSpeed = 0;
- m_fDirectionY = -9;
- m_fDirectionX = 0;
- m_fIncrementY = 1;
- m_fIncrementX = 1;
- m_fBackupIncrementX = 1;
- m_fBackupIncrementY = 1;
- m_iBackupDistanceToNextCheckPoint = 0;
- m_fBackupDirectionY = -9;
- m_fBackupDirectionX = 0;
- m_iBackupAngle = 0;
- m_iAngle = 0;
- m_iCheckPoint = -1;
- m_iLaps = 0;
- m_pBound = (POINT*) malloc(sizeof(POINT) * CARBOUND_POINTS);
- m_lLastTurn = 0;
- m_lLastSpeedIncr = 0;
- m_iCarState = CARSTATE_OK;
- POINT tmpPoints[] = { {12,4}, {26,4}, {26,29}, {12,29}};
- m_pBound =(POINT*) memcpy((void*) m_pBound,(void*) &tmpPoints, sizeof(POINT)* CARBOUND_POINTS);
-
- m_hcRaceCar.CreatePolygonBound(m_pBound, CARBOUND_POINTS);
- }
- cRaceCar::~cRaceCar()
- {
- free(m_pBound);
- m_hcRaceCar.Destroy();
- }
- void cRaceCar::SetPos(int iX, int iY)
- {
- m_fPosX = (float)iX;
- m_fPosY = (float)iY;
- }
- void cRaceCar::Create(int iColor)
- {
- m_sprCar.Create(GetMainApp()->m_hInst, iColor, 360, 160, RGB(0,0,0), 40, 40);
- m_hcRaceCar.CreateRectBound(40,40);
- iCrashTime = 0;
- m_iColor = iColor;
- m_pSound.Create(MAKEINTRESOURCE(IDR_ENGINE), DSBCAPS_CTRL3D | DSBCAPS_CTRLFREQUENCY, DS3DALG_HRTF_LIGHT);
- m_pSound.m_pSoundBuffer->SetCurrentPosition( 0 );
- m_pCrashSound.Create(MAKEINTRESOURCE(IDR_EXPLODE), DSBCAPS_CTRL3D, DS3DALG_HRTF_LIGHT);
- m_pCrashSound.m_pSoundBuffer->SetCurrentPosition( 0 );
- m_lCurrentTime = 0;
- m_bRemoteKeyboardStatus = 0;
- m_bLastSent_Speed = -1;
- m_bLastSent_Position = -1;
- m_bLastSent_CarState = -1;
- m_bLastSent_Laps = -1;
- m_bLastSent_Angle = -1;
- m_dwLastSent_LastLapTime = -1;
- m_nLastSent_PosX = -1;
- m_nLastSent_PosY = -1;
- }
- void cRaceCar::Destroy()
- {
- m_sprCar.Destroy();
- m_hcRaceCar.Destroy();
- m_sprCarExplode_0.Destroy();
- m_sprCarExplode_135.Destroy();
- m_sprCarExplode_180.Destroy();
- m_sprCarExplode_225.Destroy();
- m_sprCarExplode_270.Destroy();
- m_sprCarExplode_315.Destroy();
- m_sprCarExplode_45.Destroy();
- m_sprCarExplode_90.Destroy();
- m_pSound.Destroy();
- m_pCrashSound.Destroy();
- }
- void cRaceCar::Draw(int iViewX, int iViewY)
- {
- // Draw the car based on the current position in the map
- int iClipX = 0, iClipY = 0;
- int iClipWidth = 40, iClipHeight = 40;
- int iX = (int)m_fPosX - iViewX;
- int iY = (int) m_fPosY - iViewY;
- if((int)m_fPosX - iViewX > 640)
- return;
- if((int)m_fPosX - iViewX + 40 < 0)
- return;
- if((int) m_fPosY - iViewY > 460)
- return;
- if((int) m_fPosY - iViewY +40 < 0)
- return;
- if((int) m_fPosY - iViewY + 40 > 460)
- iClipHeight = 40-((int) m_fPosY - iViewY + 40 - 460);
- if((int) m_fPosX - iViewX + 40 > 640)
- iClipWidth = 40-((int) m_fPosX - iViewX + 40 - 640);
- if((int)m_fPosX - iViewX < 0)
- {
- iClipX = abs((int)m_fPosX - iViewX);
- iClipWidth -= iClipX;
- iX = 0;
- }
- if((int)m_fPosY - iViewY < 0)
- {
- iClipY = abs((int)m_fPosY - iViewY);
- iClipHeight -= iClipY;
- iY = 0;
- }
- if(m_iCarState == CARSTATE_CRASHED_WALL)
- {
- // If we crashed, play the explosion sprite
- if(m_iAngle == 0)
- m_sprCarExplode_0.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_0.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle > 0 && m_iAngle < 90)
- m_sprCarExplode_45.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_45.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle == 90)
- m_sprCarExplode_90.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_90.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle > 90 && m_iAngle < 180)
- m_sprCarExplode_135.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_135.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle == 180)
- m_sprCarExplode_180.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_180.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle > 180 && m_iAngle < 270)
- m_sprCarExplode_225.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_225.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle == 270)
- m_sprCarExplode_270.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_270.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- if(m_iAngle > 270 && m_iAngle < 360)
- m_sprCarExplode_315.Draw(GetMainApp()->m_pBackBuffer, iX, iY,m_sprCarExplode_315.IsEnd() ? FALSE : TRUE, iClipX, iClipY, iClipWidth, iClipHeight);
- }
- else
- {
- // otherwise, blit the normal car, in its current angle
- m_sprCar.Draw(GetMainApp()->m_pBackBuffer, iX, iY, FALSE, iClipX, iClipY, iClipWidth, iClipHeight);
- }
- // If you want to see the car bounding rect, uncomment this
-
- /*HDC hdc;
- GetMainApp()->m_pBackBuffer->GetDC(&hdc);
- OffsetRgn(m_hcRaceCar.hBoundingPoly, (int)m_fPosX - iViewX, (int) m_fPosY - iViewY);
- FillRgn(hdc, m_hcRaceCar.hBoundingPoly, (HBRUSH) GetStockObject(BLACK_BRUSH));
- OffsetRgn(m_hcRaceCar.hBoundingPoly, -1*((int)m_fPosX - iViewX), -1*((int) m_fPosY - iViewY));
- GetMainApp()->m_pBackBuffer->ReleaseDC(hdc);
- */
- }
- void cRaceCar::Process(void* pTrack)
- {
- #define SPEEDXTURN 70
- #define TURN_TIME 45
- #define RAISESPEED_TIME 85
- #define MAX_SPEED_FOR_CURVE 180
- #define MAX_SPEED_FOR_IDLE 200
- //升高速度量从而降低游戏速度
- //降低速度量从而使游戏更快速
- #define _SPEED_FACTOR_ 17
- int iX, iY;
- static long lLastMessage;
- //iTurnCar = TURN_RIGHT;
- cRaceTrack* theTrack = (cRaceTrack*)pTrack;
- cKeyboard pKeyboard;
- //如果赛车不在正常状态
- if(m_iCarState != CARSTATE_OK)
- {
- //赛车发生碰撞,需要调入爆炸的声音
- if(iCrashTime == 0)
- {
- iCrashTime = GetTickCount();
- if(m_iCarState != CARSTATE_RACECOMPLETED)
- m_pCrashSound.Play();
- }
- //爆炸
- Crashed();
- if(m_iCarState == CARSTATE_RACECOMPLETED)
- {
- }
- else
- {
- if(GetTickCount() - iCrashTime > 2500)
- {
- // Crash time elapse, return the car to the backuped position
- m_sprCarExplode_0.Destroy();
- m_sprCarExplode_135.Destroy();
- m_sprCarExplode_180.Destroy();
- m_sprCarExplode_225.Destroy();
- m_sprCarExplode_270.Destroy();
- m_sprCarExplode_315.Destroy();
- m_sprCarExplode_45.Destroy();
- m_sprCarExplode_90.Destroy();
- if( (GetRaceXApp()->m_bIsMultiplayer == TRUE && GetRaceXApp()->GetMultiplayer()->IsHosting()) ||
- GetRaceXApp()->m_bIsMultiplayer == FALSE)
- {
- m_fDirectionX = m_fBackupDirectionX;
- m_fDirectionY = m_fBackupDirectionY;
- m_fIncrementX = m_fBackupIncrementX;
- m_fIncrementY = m_fBackupIncrementY;
- m_iDistanceToNextCheckPoint = m_iBackupDistanceToNextCheckPoint;
- iTurnCar = iBackupTurnCar;
- m_iAngle = m_iBackupAngle;
- m_fPosX = m_fBackupPosX;
- m_fPosY = m_fBackupPosY;
- m_sprCar.m_iAbsolutePosition = m_iBackupSprite;
- if(theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_ROADQ2 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ0P3 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ0P2)
- {
- m_fPosY+=10;
- }
- if(theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_ROADQ3||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ2P3 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ2P2)
- {
- m_fPosY-=10;
- }
- if(theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_ROADQ0 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ2P1 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ2P2)
- {
- m_fPosX+=10;
- }
- if(theTrack->GetRoadType((int)m_fPosX+10,(int) m_fPosY+10) == ID_ROADTYPE_ROADQ1 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ3P1 ||
- theTrack->GetRoadType((int)m_fPosX+10, (int)m_fPosY+10) == ID_ROADTYPE_MEDIUMCURVEQ3P2)
- {
- m_fPosX-=10;
- }
- RotateBound(m_iAngle);
- 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);
- for(int i=0;i<theTrack->GetNumberOfCars();i++)
- {
- if(m_bID != theTrack->m_pRaceCars[i]->GetID())
- {
- int iSrcX, iSrcY;
- theTrack->m_pRaceCars[i]->GetPos(&iSrcX, &iSrcY);
- if(m_hcRaceCar.HaveHitted(&theTrack->m_pRaceCars[i]->m_hcRaceCar, m_fPosX, m_fPosY, iSrcX, iSrcY) == FALSE)
- {
- m_iCarState = CARSTATE_OK;
- iCrashTime = 0;
- }
- }
- }
- }
- }
- }
- }
- else
- {
- int i;
- iCrashTime = 0;
-
- //如果赛车由人控制,则读取键盘值,判断用户意图
- if(m_iControlType == CTRL_USER)
- {
- if(pKeyboard.CheckKey(DIK_RIGHT))
- {
- TurnCarRight();
- }
- if(pKeyboard.CheckKey(DIK_LEFT))
- {
- TurnCarLeft();
- }
- if(pKeyboard.CheckKey(DIK_DOWN))
- {
- BreakCar();
- }
-
- if(pKeyboard.CheckKey(DIK_UP))
- {
- Accelerate();
- }
- else
- {
- Idle();
- }
- }/*USER CONTROL*/
-
- //远端用户,发送键盘消息到主机
- if(m_iControlType == CTRL_NETWORK_REMOTE &&
- GetRaceXApp()->m_bIsMultiplayer == TRUE &&
- GetRaceXApp()->GetMultiplayer()->IsHosting() )
- {
- // 键盘状态是一个字节变量,设置某些位从而判断哪个键被按下
- if(m_bRemoteKeyboardStatus & 0x01)
- {
- TurnCarRight();
- }
- if(m_bRemoteKeyboardStatus & 0x02)
- {
- TurnCarLeft();
- }
- if(m_bRemoteKeyboardStatus & 0x04)
- {
- BreakCar();
- }
- if(m_bRemoteKeyboardStatus & 0x08)
- {
- Accelerate();
- }
- else
- {
- Idle();
- }
- }
- //电脑控制的赛车
- if(m_iControlType == CTRL_COMPUTER)
- {
- BOOL bCanBrake = FALSE;
- //如果当前地图中贴片的角度和当前赛车的行进角度不一样,则需调整赛车的行进角度
- if(m_iAngle != theTrack->GetAngle(m_fPosX+20, m_fPosY+20) &&
- theTrack->GetAngle(m_fPosX+20, m_fPosY+20) != 370)
- {
- int iValue1 = (360-m_iAngle)+theTrack->GetAngle(m_fPosX+20, m_fPosY+20);
- if(iValue1 > 360)
- {
- iValue1 = iValue1 % 360;
- }
- int iValue2 = (m_iAngle-theTrack->GetAngle(m_fPosX+20, m_fPosY+20));
- if(iValue2 < 0)
- {
- iValue2+=360;
- }
- if(iValue1 < iValue2)
- {
- TurnCarRight();
- bCanBrake = TRUE;
- }
- else
- {
- TurnCarLeft();
- bCanBrake = TRUE;
- }
- }
- //如果速度过快,则刹车
- if(m_iSpeed > 270)
- {
- if(theTrack->CarHittedRoad(this, (int) m_fPosX+20+(m_iSpeed * (m_fDirectionX/9.0f)), (int) m_fPosY+20+ (m_iSpeed * (m_fDirectionY/9.0f))) == TRUE)
- BreakCar();
- }
- if(bCanBrake == TRUE)
- {
- for(i=m_iSpeed-30;i>=0;i-=40)
- {
- if(theTrack->CarHittedRoad(this,(int)m_fPosX+20+(i*(m_fDirectionX/9.0f)),(int)m_fPosY+20+ (i * (m_fDirectionY/9.0f))) == TRUE)
- {
- BreakCar();
- }
- }
- }
- Accelerate();
- }/*COMPUTER CONTROL*/
- }
-
- //如果赛车被一个远端用户控制
- if(m_iControlType == CTRL_NETWORK_LOCAL &&
- GetRaceXApp()->m_bIsMultiplayer == TRUE &&
- !GetRaceXApp()->GetMultiplayer()->IsHosting() )
- {
- m_bRemoteKeyboardStatus = 0;
- //从远端读取键盘状态值
- if(pKeyboard.CheckKey(DIK_RIGHT))
- {
- m_bRemoteKeyboardStatus |= 0x01;
- }
- if(pKeyboard.CheckKey(DIK_LEFT))
- {
- m_bRemoteKeyboardStatus |= 0x02;
- }
- if(pKeyboard.CheckKey(DIK_DOWN))
- {
- m_bRemoteKeyboardStatus |= 0x04;
- }
-
- if(pKeyboard.CheckKey(DIK_UP))
- {
- m_bRemoteKeyboardStatus |= 0x08;
- }
-
- if(GetRaceXApp()->m_bSendKeyboard == TRUE || GetTickCount()-lLastMessage > 100)
- {
- GetRaceXApp()->GetMultiplayer()->SendTo(GetRaceXApp()->GetMultiplayer()->GetHost(), MSG_KEYBOARD_STATUS, &m_bRemoteKeyboardStatus, 1, 0/*DPNSEND_SYNC*/);
- GetRaceXApp()->m_bSendKeyboard = FALSE;
- lLastMessage = GetTickCount();
- }
- }
-
- if((GetRaceXApp()->m_bIsMultiplayer==TRUE&&GetRaceXApp()->GetMultiplayer()->IsHosting())||
- GetRaceXApp()->m_bIsMultiplayer == FALSE)
- {
- //处理完用户/电脑/网络输入后,重新定位赛车位置
- m_fPosY = m_fPosY + ((float)m_fDirectionY / 9.0f) * ((float)m_iSpeed / _SPEED_FACTOR_);
- m_fPosX = m_fPosX + ((float)m_fDirectionX / 9.0f) * ((float)m_iSpeed / _SPEED_FACTOR_);
- // Track control goes here
- // Check if we are passing throught a checkpoint or how far we are from thw
- // next check point
- if(m_iCarState == CARSTATE_OK)
- {
- if(m_iCheckPoint == -1)
- {
- if(theTrack->GetCheckPoint((int)m_fPosX+10,(int) m_fPosY+18) == 1)
- {
- m_iCheckPoint = 1;
- m_lCurrentTime = GetTickCount();
- 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)
- {
- if(theTrack->GetCheckPoint((int)m_fPosX+10, (int)m_fPosY+18) == 1 &&
- m_iCheckPoint == theTrack->GetMaxCheckPoint())
- {
- m_vcLapTimes.push_back(GetTickCount()-m_lCurrentTime);
- m_lCurrentTime = GetTickCount();
- m_iCheckPoint = 1;
- 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_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_);
- }