📄 hunter.cpp
字号:
// Hunter.cpp: implemention of class Hunter
#include <iostream>
#include <string>
#include <assert.h>
#include "Hunter.h"
using namespace std;
//------------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------------
Hunter::Hunter( SimpleWindow& Window ) : m_Window( Window ), m_Bmp ( Hunter_DirectNumber )
{
vector<string> HunterBmpBmps( Hunter_DirectNumber );
HunterBmpBmps[0] = "bmp\\HunterDirect-U.bmp";
HunterBmpBmps[1] = "bmp\\HunterDirect-D.bmp";
HunterBmpBmps[2] = "bmp\\HunterDirect-L.bmp";
HunterBmpBmps[3] = "bmp\\HunterDirect-R.bmp";
HunterBmpBmps[4] = "bmp\\HunterDirect-N.bmp";
for( HunterDirection d = Hunter_Up; d < Hunter_DirectNumber; d = (HunterDirection)(d+1) )
{
m_Bmp[d].SetWindow( GetWindow() );
m_Bmp[d].Load( HunterBmpBmps[d] );
assert( m_Bmp[d].GetStatus() == BitMapOkay );
}
SetDirection( Hunter_None );
SetPosition( Position(0.0, 0.0) );
SetHorizMovement( GetBmp(GetDirection()).GetWidth() / 5.0 );
SetVertMovement ( GetBmp(GetDirection()).GetHeight() / 5.0 );
}
//-----------------------------------------------------------------------------
SimpleWindow& Hunter::GetWindow() const
{
return m_Window;
}
//------------------------------------------------------------------------------
// get bitmaps
//------------------------------------------------------------------------------
const BitMap& Hunter::GetBmp( const HunterDirection& D ) const
{
return m_Bmp[D];
}
BitMap& Hunter::GetBmp( const HunterDirection& D )
{
return m_Bmp[D];
}
//-------------------------------------------------------------------------------
// get and set position
//-------------------------------------------------------------------------------
Position Hunter::GetPosition() const
{
return m_HunterPosition;
}
void Hunter::SetPosition( const Position& p )
{
for( HunterDirection d = Hunter_Up; d < Hunter_DirectNumber; d = (HunterDirection)(d+1) )
{
m_Bmp[d].SetPosition( p );
}
m_HunterPosition = p;
return;
}
//--------------------------------------------------------------------------
// get and set direction
//--------------------------------------------------------------------------
HunterDirection Hunter::GetDirection() const
{
return m_HunterDirection;
}
void Hunter::SetDirection( const HunterDirection &d)
{
m_HunterDirection = d;
return;
}
//-----------------------------------------------------------------------------
// get and vertical movement
//-----------------------------------------------------------------------------
float Hunter::GetVertMovement() const
{
return m_VertMovement;
}
void Hunter::SetVertMovement( float v )
{
m_VertMovement = v;
return;
}
//------------------------------------------------------------------------------
// get and horizontal movement
//------------------------------------------------------------------------------
float Hunter::GetHorizMovement() const
{
return m_HorizMovement;
}
void Hunter::SetHorizMovement( float h )
{
m_HorizMovement = h;
return;
}
//----------------------------------------------------------------------------------
// Draw and Erase
//------------------------------------------------------------------------------------
void Hunter::Draw()
{
GetBmp( GetDirection() ).Draw();
return;
}
void Hunter::Erase()
{
GetBmp( GetDirection() ).Erase();
return;
}
//-----------------------------------------------------------------------------------------
// judge whether the pet is at the Edge
//-----------------------------------------------------------------------------------------
bool Hunter::AtUpEdge() const
{
const float fHunterYPosn = GetPosition().GetYDistance();
return fHunterYPosn - GetVertMovement() <= 0.0;
}
bool Hunter::AtDownEdge() const
{
const float fHunterYPosn = GetPosition().GetYDistance();
const float fHunterYSize = GetBmp( GetDirection() ).GetHeight();
const float fWndYSize = GetWindow().GetHeight();
return fHunterYPosn + fHunterYSize + GetVertMovement() >= fWndYSize;
}
bool Hunter::AtLeftEdge() const
{
const float fHunterXPosn = GetPosition().GetXDistance();
return fHunterXPosn - GetHorizMovement() <= 0.0;
}
bool Hunter::AtRightEdge() const
{
const float fHunterXPosn = GetPosition().GetXDistance();
const float fHunterXSize = GetBmp( GetDirection() ).GetWidth();
const float fWndXSize = GetWindow().GetWidth();
return fHunterXPosn + fHunterXSize + GetHorizMovement() >= fWndXSize;
}
//--------------------------------------------------------------------------------------
// set a new position for pet to move to
//--------------------------------------------------------------------------------------
Position Hunter::NewPosition() const
{
const HunterDirection d = GetDirection();
const Position p = GetPosition() ;
switch ( d )
{
case Hunter_Up:
return p + Position( 0.0, -GetVertMovement() );
break;
case Hunter_Down:
return p + Position( 0.0, GetVertMovement() );
break;
case Hunter_Left:
return p + Position( -GetHorizMovement(), 0.0 );
break;
case Hunter_Right:
return p + Position( GetHorizMovement(), 0.0 );
break;
case Hunter_None:
return p; // Do nothing
break;
}
}
//----------------------------------------------------------------------
void Hunter::Create()
{
Draw();
}
//---------------------------------------------------------------------
// move the pet
//---------------------------------------------------------------------
void Hunter::Move()
{
Erase();
const HunterDirection d = GetDirection();
if ( ( d == Hunter_Up && AtUpEdge() )
|| ( d == Hunter_Down && AtDownEdge() )
|| ( d == Hunter_Left && AtLeftEdge() )
|| ( d == Hunter_Right && AtRightEdge() ) )
{
SetDirection( Hunter_None );
}
SetPosition( NewPosition() );
Draw();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -