CPaddle.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:8k
- ///////////////////////////////////////////////////////////////////////////////
- /// File: CPaddle.cpp
- /// Purpose: Definition of CPaddle Class Members
- ///////////////////////////////////////////////////////////////////////////////
- /// Configuation Management
- ///
- /// Who When Description
- /// ===========================================================================
- /// R. Walter 28-Dec-2003 Initial Version/Release
- ///
- ///////////////////////////////////////////////////////////////////////////////
- /// Copyright 2003: Robert Walter All rights reserved
- ///////////////////////////////////////////////////////////////////////////////
- /// APPLICATION DEFINES ///////////////////////////////////////////////////////
- #define WIN32_LEAN_AND_MEAN /** no MFC **/
- /// HEADER FILE INCLUDES //////////////////////////////////////////////////////
- #include "CPaddle.h"
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: CPaddle --> default constructor
- /// Purpose: Initialize member variables
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: nothing
- ///////////////////////////////////////////////////////////////////////////////
- CPaddle::CPaddle()
- {
- return;
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: ~CPaddle --> class destructor
- /// Purpose: Uninitialize member variables
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: nothing
- ///////////////////////////////////////////////////////////////////////////////
- CPaddle::~CPaddle()
- {
- return;
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: GetXPos
- /// Purpose: Retrieve the left most coordinate (in pixels) of the current
- /// paddle location
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: X coordinate of paddle (integer)
- ///////////////////////////////////////////////////////////////////////////////
- int CPaddle::GetXPos()
- {
- return(m_x_pos);
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: GetXVel
- /// Purpose: Retrieve the speed the paddle is moving across the X axis
- /// (positive indicates moving right, negative moving left)
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: current X velocity of paddle (integer)
- ///////////////////////////////////////////////////////////////////////////////
- int CPaddle::GetXVel()
- {
- return(m_x_vel);
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: GetLength
- /// Purpose: Retrieve the current length (in pixels) of the paddle
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: length (in pixels) of the paddle (integer)
- ///////////////////////////////////////////////////////////////////////////////
- int CPaddle::GetLength()
- {
- return(m_length);
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: GetPaddleRects
- /// Purpose: Retrieve the count and RECT structures of the actual paddles
- /// in the playing area. NOTE: there may be more than one RECT
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: nothing
- /// Returns: count of paddle rectangles -> through a integer pointer
- /// array of paddle RECTs -> through a RECT pointer
- ///////////////////////////////////////////////////////////////////////////////
- void CPaddle::GetPaddleRects(int* v_rect_cnt, RECT* v_rects)
- {
- *v_rect_cnt = m_cnt;
- v_rects[0].top = m_bottom_limit - m_height;
- v_rects[0].left = m_x_pos;
- v_rects[0].right = (m_x_pos + m_length);
- v_rects[0].bottom = m_bottom_limit;
- if (m_cnt == 2)
- {
- v_rects[1].top = v_rects[0].top - m_hor_space - m_height;
- v_rects[1].left = m_x_pos;
- v_rects[1].right = (m_x_pos + m_length);
- v_rects[1].bottom = v_rects[0].top - m_hor_space;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: SetBaseProperties
- /// Purpose: Initializes the core set of properties that dictates how the
- /// paddle will look in the playing area
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: count of paddles (integer)
- /// amount of horizontal space (in pixels) between the paddles (integer)
- /// length of the paddles (in pixels) (integer)
- /// height of the paddles (in pixels) (integer)
- /// Returns: none
- ///////////////////////////////////////////////////////////////////////////////
- void CPaddle::SetBaseProperties(int const p_cnt, int const p_hor_space, int const p_length, int const p_height)
- {
- m_cnt = p_cnt;
- m_hor_space = p_hor_space;
- m_length = p_length;
- m_height = p_height;
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: SetPaddleLimits
- /// Purpose: Initializes the bounds the paddle can move between in the playing
- /// area (ie. how far left, how far right, etc.)
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: left boundary of playing area (integer)
- /// right boundary of playing area (integer)
- /// bottom boundary of playing area (integer)
- ///////////////////////////////////////////////////////////////////////////////
- void CPaddle::SetPaddleLimits(int const p_min_limit, int const p_max_limit, int const p_bottom_limit)
- {
- m_min_limit = p_min_limit;
- m_max_limit = p_max_limit;
- m_bottom_limit = p_bottom_limit;
- // calculate initial paddle position
- m_x_pos = ((p_max_limit - p_min_limit) - (m_length / 2)) / 2;
- m_x_vel = 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: SetXPos
- /// Purpose: Set the current X position of the paddle
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: X coordinate of paddle (integer)
- /// Returns: none
- ///////////////////////////////////////////////////////////////////////////////
- void CPaddle::SetXPos(int const p_x_pos)
- {
- m_x_pos = p_x_pos;
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// Method: SetXVel
- /// Purpose: Set the speed the paddle is moving across the X axis
- /// (positive indicates moving right, negative moving left)
- ///////////////////////////////////////////////////////////////////////////////
- /// Receives: new X velocity of paddle (integer)
- /// Returns: none
- ///////////////////////////////////////////////////////////////////////////////
- void CPaddle::SetXVel(int const p_x_vel)
- {
- m_x_vel = p_x_vel;
- }
- bool CPaddle::HitPaddle(CBall p_ball)
- {
- RECT l_rects[2];
- int l_rect_cnt;
- GetPaddleRects(&l_rect_cnt, l_rects);
- for (int i = 0; i < l_rect_cnt; i++)
- {
- int l_ball_x = 0;
- int l_ball_y = 0;
- int l_ball_bottom = 0;
- int l_ball_right = 0;
- l_ball_x = p_ball.GetXPos();
- l_ball_y = p_ball.GetYPos();
- l_ball_bottom = l_ball_y + BALL_HEIGHT;
- l_ball_right = l_ball_x + BALL_WIDTH;
- if ( (l_ball_x >= l_rects[i].left) && (l_ball_x <= l_rects[i].right) && (l_ball_bottom >= l_rects[i].top) )
- {
- return(true);
- }
- if ( (l_ball_right >= l_rects[i].left) && (l_ball_right <= l_rects[i].right) && (l_ball_bottom >= l_rects[i].top) )
- {
- return(true);
- }
- }
- return(false);
- }
- bool CPaddle::IsPaddleAtPosition(int p_x, int p_y, RECT* p_rect)
- {
- RECT l_rects[2];
- int l_rect_cnt;
- GetPaddleRects(&l_rect_cnt, l_rects);
- for (int i = 0; i < l_rect_cnt; i++)
- {
- if ( (p_x >= l_rects[i].left) && (p_x <= l_rects[i].right) && (p_y >= l_rects[i].top) && (p_y <= l_rects[i].bottom) )
- {
- *p_rect = l_rects[i];
- return(true);
- }
- }
- return(false);
- }
- void CPaddle::SetLength(int const p_length)
- {
- m_length = p_length;
- }
- /// END OF FILE ///////////////////////////////////////////////////////////////