woaidaima2016 发表于 2017-6-13 11:10:00

金鲨银鲨【源码】

http://img.woaidaima.com//upload/image/20170525/1495673169017074156.jpghttp://img.woaidaima.com//upload/image/20170525/1495673170670091849.jpg#include "StdAfx.h"
#include ".\fishdraw.h"
#include "GameLogic.h"

CFishDraw::CFishDraw(void)
{
    m_nBackIndex = 0;
    m_rectFishPond.SetRect(0,0,0,0);
    m_bAddFish = FALSE;
    m_nCannonAlpha = 0;

    //    0
    //1      3
    //    2
    m_ptBullet.SetPoint(0,0);
    m_ptBullet.SetPoint(0,0);
    m_ptBullet.SetPoint(0,0);
    m_ptBullet.SetPoint(0,0);

    m_ptCannon.SetPoint(0,0);
    m_ptCannon.SetPoint(0,0);
    m_ptCannon.SetPoint(0,0);
    m_ptCannon.SetPoint(0,0);


    ZeroMemory(m_pBulletMove, sizeof(m_pBulletMove));
    ZeroMemory(m_pNetMove, sizeof(m_pNetMove));
    m_BulletCount.Initialization();
}

CFishDraw::~CFishDraw(void)
{
    for ( int i = 0; i < 4; ++i )
    {
      if( m_pBulletMove )
      {
            delete m_pBulletMove;
            m_pBulletMove = NULL;
      }

      if( m_pNetMove)
      {
            delete m_pNetMove;
            m_pNetMove = NULL;
      }
    }

    int nIndex = 0;
    while( nIndex < m_ArrayFishDraw.GetCount())
    {
      SafeDelete(m_ArrayFishDraw);
      m_ArrayFishDraw.RemoveAt(nIndex);
      continue;
    }
}

//设置鱼池
VOID CFishDraw::SetFishPond( CRect rectFishPond )
{
    m_rectFishPond = rectFishPond;

    m_ptBullet.SetPoint( rectFishPond.left + rectFishPond.Width()/2, rectFishPond.top + 200 );
    m_ptBullet.SetPoint( rectFishPond.left + 200, rectFishPond.top + rectFishPond.Height()/2 );
    m_ptBullet.SetPoint( rectFishPond.left + rectFishPond.Width()/2, rectFishPond.bottom - 200 );
    m_ptBullet.SetPoint( rectFishPond.right - 200 , rectFishPond.top + rectFishPond.Height()/2 );
   
    m_ptCannon.SetPoint( rectFishPond.left + rectFishPond.Width()/2, rectFishPond.top + 38 + 80);
    m_ptCannon.SetPoint( rectFishPond.left + 45 + 70, rectFishPond.top + rectFishPond.Height()/2 - 6 + 5 );
    m_ptCannon.SetPoint( rectFishPond.left + rectFishPond.Width()/2, rectFishPond.bottom - 50 - 60 );
    m_ptCannon.SetPoint( rectFishPond.right - 45 - 70, rectFishPond.top + rectFishPond.Height()/2 - 15 + 5);
}

//动画
VOID CFishDraw::OnFishMovie()
{
    if ( m_bAddFish )
    {
      m_nCannonAlpha += 20;
      if ( m_nCannonAlpha > 255)
            m_nCannonAlpha = 255;
    }
    else
    {
      m_nCannonAlpha -= 20;
      if ( m_nCannonAlpha < 0)
            m_nCannonAlpha = 0;
    }

    //添加鱼
    if ( m_bAddFish && m_LapseCount.GetLapseCount( 500 ) > 0 && m_rectFishPond.Width() > 0 && m_rectFishPond.Height() > 0 )
    {
      int nRand =CGameLogic::GetInstance()->RandomArea(2, 3, 10 );
      switch ( nRand )
      {
      case 0:
            {
                INT      nNumber      = rand()%5 + 3;
                BYTE    cbFishIndex = CGameLogic::GetInstance()->RandomArea(2, 50, 20 );
                CPoint    ptFishMove;
                BYTE cbDirection = FishPath(ptFishMove);
                for ( INT y = 0; y < nNumber; ++y )
                {
                  CPoint ptOffset;
                  INT nTemp = ((rand()%2) == 1) ? -1 : 1;
                  if ( cbDirection == 0x01 || cbDirection == 0x03 )
                        ptOffset.SetPoint(((y + rand())%30),((y + rand())%30) * nTemp);
                  else
                        ptOffset.SetPoint(((y + rand())%30) * nTemp,((y + rand())%30));

                  for( INT z = 0; z < FISH_STEP; ++z )
                  {
                        ptFishMove.x += ptOffset.x;
                        ptFishMove.y += ptOffset.y;
                  }


                  BYTEcbMoveTime = { 10, 10, 10, 0, 0 };
                  tgaFishDraw* pFishDraw = new tgaFishDraw( 0, cbFishIndex, (m_ArrayFishDraw.GetCount()%10 == 5), 0, 0, GetTickCount(), 3, ptFishMove, cbMoveTime);
                  m_ArrayFishDraw.Add(pFishDraw);
                }
            }
            break;
      default:
            {
                CPoint ptMovePos;
                BYTEcbMoveTime = { 10, 10, 10, 0, 0 };
                FishPath(ptMovePos);
                int nFishIndex = CGameLogic::GetInstance()->RandomArea(FISH_MAX, 20, 18, 16, 15, 8, 3);
                tgaFishDraw* pFishDraw = new tgaFishDraw( 0, nFishIndex, (m_ArrayFishDraw.GetCount()%10 == 5), 0, 0, GetTickCount(), 3, ptMovePos, cbMoveTime);
                m_ArrayFishDraw.Add(pFishDraw);
            }
            break;
      }
    }
   
    //鱼移动
    int nIndex = 0;
    while( nIndex < m_ArrayFishDraw.GetCount())
    {
      if ( !m_ArrayFishDraw->Move() )
      {
            SafeDelete(m_ArrayFishDraw);
            m_ArrayFishDraw.RemoveAt(nIndex);
            continue;
      }

      if ( m_ArrayFishDraw->LapseCount.GetLapseCount(120)>0L )
            m_ArrayFishDraw->cbFrameIndex = (m_ArrayFishDraw->cbFrameIndex + 1)%4;

      //开炮
      CRect rectPond(m_rectFishPond);
      rectPond.left += 100;
      rectPond.top += 100;
      rectPond.right -= 100;
      rectPond.bottom -= 100;
      if ( rectPond.PtInRect(CPoint((int)m_ArrayFishDraw->ptPosition.x, (int)m_ArrayFishDraw->ptPosition.y)) && m_BulletCount.GetLapseCount(300) > 0 )
      {
            int nRand =CGameLogic::GetInstance()->RandomArea(2, 8, 10 );
            nRand = 0;
            if ( nRand == 0 )
            {
                int nCannon =CGameLogic::GetInstance()->RandomArea(4, 10, 10, 10, 10 );
                for ( int n = 0; n < 4; ++n )
                {
                  if ( m_pBulletMove == NULL && m_pNetMove == NULL )
                  {
                        m_pBulletMove = new CObjectMove( m_ptCannon, m_ArrayFishDraw->ptPosition, 1200, 80);
                        m_ptBullet.SetPoint( (int)m_ArrayFishDraw->ptPosition.x, (int)m_ArrayFishDraw->ptPosition.y );
                        break;               
                  }
                  nCannon = (nCannon + 1)%4;
                }
            }
      }
      nIndex++;
    }

    for ( int i = 0; i < 4; ++i )
    {
      if( m_pBulletMove && m_pBulletMove->Move() && m_pNetMove == NULL )
      {
            m_pNetMove = new CObjectMove( m_pBulletMove->ptPosition, m_pBulletMove->ptPosition, 2000, 60);

            delete m_pBulletMove;
            m_pBulletMove = NULL;
      }

      if( m_pNetMove)
      {
            m_pNetMove->Move();
            if( m_pNetMove->cbFrameIndex >= 12)
            {
                delete m_pNetMove;
                m_pNetMove = NULL;
            }
      }
    }
}

//创建
VOID CFishDraw::OnFishCreate( CD3DDevice * pD3DDevice )
{
    //载入背景
    HINSTANCE hResInstance=AfxGetInstanceHandle();
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_I"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_II"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_III"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_IV"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_V"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_VI"),TEXT("FISH"));
    m_ImgaeBack.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BACK_VII"),TEXT("FISH"));

    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_1"),TEXT("FISH"), 4, 1);
    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_2"),TEXT("FISH"), 4, 1);
    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_3"),TEXT("FISH"), 4, 1);
    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_4"),TEXT("FISH"), 4, 1);
    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_5"),TEXT("FISH"), 4, 1);
    m_ImgaeFish.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_6"),TEXT("FISH"), 4, 1);

    m_ImgaeGun.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_GUN"),TEXT("FISH"), 4, 1);
    m_ImgaeCannon.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_CANNON"),TEXT("FISH"), 2, 1);
   
    m_ImgaeBullet.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_BULLET"),TEXT("FISH"));
    m_ImgaeNet.LoadImage(pD3DDevice,hResInstance,TEXT("FISH_NET"),TEXT("FISH"), 4, 3);

    //X背景
    for ( int i = 0 ; i < CountArray(m_ImgaeWater); ++i )
    {
      TCHAR szResource = {0};
      _sntprintf(szResource, CountArray(szResource), TEXT("WATER_%d"),i + 1);
      m_ImgaeWater.LoadImage( pD3DDevice, hResInstance, szResource, TEXT("FISH") );
    }
}

//绘画
VOID CFishDraw::OnFishDraw( CD3DDevice * pD3DDevice, INT nWidth, INT nHeight )
{

    m_ImgaeBack.DrawImage(pD3DDevice, m_rectFishPond.left, m_rectFishPond.top);
   
    //绘画鱼
    for ( INT i = 0 ; i < m_ArrayFishDraw.GetCount(); ++i )
    {   
      if( !m_ArrayFishDraw->bWhetherToCreate)
            continue;

      INT nImageHeight = m_ImgaeFish->nFishType].GetHeight();
      INT nImageWidth = m_ImgaeFish->nFishType].GetWidth();

      FLOAT fAngle = (FLOAT)atan2((m_ArrayFishDraw->ptPosition.y - m_ArrayFishDraw->ptLast.y) , (m_ArrayFishDraw->ptLast.x - m_ArrayFishDraw->ptPosition.x));
      fAngle += D3DX_PI/2;

      CPoint ptSite((int)m_ArrayFishDraw->ptPosition.x, (int)m_ArrayFishDraw->ptPosition.y);
      ptSite.x += m_rectFishPond.left;
      ptSite.y += m_rectFishPond.top;
      BYTE cbFrameIndex = m_ArrayFishDraw->cbFrameIndex;
      m_ImgaeFish->nFishType].DrawImage(pD3DDevice,CPoint(nImageWidth/2,nImageHeight/2), fAngle, 'z',ptSite.x - nImageWidth/2, ptSite.y - nImageHeight/2, m_ArrayFishDraw->cbFrameIndex%4);
    }

    static INT nIndexC = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    INT nIndex = nIndexC[(GetTickCount()/100)%22];
    m_ImgaeWater.DrawImage(pD3DDevice, CPoint(0,0), 0.f, 'z', m_rectFishPond.left, m_rectFishPond.top, m_rectFishPond.Width(), m_rectFishPond.Height(), 0, 0, m_ImgaeWater.GetWidth(), m_ImgaeWater.GetHeight(), (BYTE)255);

    if ( m_nCannonAlpha <= 0 )
      return;

    if( m_nBackIndex == 0 )
      m_ImgaeBack.DrawImage(pD3DDevice, m_rectFishPond.left, m_rectFishPond.top, m_nCannonAlpha);

    //角度
    FLOAT fAngle;
    fAngle = (FLOAT)atan2((FLOAT)(m_ptBullet.y - m_ptCannon.y), (FLOAT)(m_ptCannon.x - m_ptBullet.x)) + D3DX_PI/2*3 + D3DX_PI;
    fAngle = -D3DX_PI/2 + (FLOAT)atan2((FLOAT)(m_ptBullet.y - m_ptCannon.y) , (FLOAT)(m_ptCannon.x - m_ptBullet.x)) + D3DX_PI;
    fAngle = (FLOAT)atan2((FLOAT)(m_ptBullet.y - m_ptCannon.y) , (FLOAT)(m_ptCannon.x - m_ptBullet.x)) + D3DX_PI/2;
    fAngle = D3DX_PI/2 +(FLOAT)atan2((FLOAT)(m_ptBullet.y - m_ptCannon.y) , (FLOAT)(m_ptCannon.x - m_ptBullet.x));

    //子弹
    for ( int i = 0; i < 4; ++i )
    {
      if( m_pBulletMove )
      {
            m_ImgaeBullet.DrawImage(pD3DDevice, CPoint(m_ImgaeBullet.GetWidth()/2, m_ImgaeBullet.GetHeight()/2),fAngle, 'z',(int)m_pBulletMove->ptPosition.x - m_ImgaeBullet.GetWidth()/2, (int)m_pBulletMove->ptPosition.y - m_ImgaeBullet.GetHeight()/2, (BYTE)m_nCannonAlpha);
      }
    }

    //绘画炮台   
    m_ImgaeGun.DrawImage(pD3DDevice, CPoint(m_ImgaeGun.GetWidth()/2, 32), fAngle, 'z',m_rectFishPond.left + m_rectFishPond.Width()/2 - m_ImgaeGun.GetWidth()/2, m_rectFishPond.top - 80 + 90, (BYTE)m_nCannonAlpha, 0);
    m_ImgaeGun.DrawImage(pD3DDevice, CPoint(m_ImgaeGun.GetWidth()/2, 32), fAngle, 'z',m_rectFishPond.left + 70, m_rectFishPond.top + m_rectFishPond.Height()/2 - m_ImgaeGun.GetHeight() + m_ImgaeGun.GetWidth()/2 - 13, (BYTE)m_nCannonAlpha, 2);
    m_ImgaeGun.DrawImage(pD3DDevice, CPoint(m_ImgaeGun.GetWidth()/2, 32), fAngle, 'z',m_rectFishPond.left + m_rectFishPond.Width()/2 - m_ImgaeGun.GetWidth()/2, m_rectFishPond.bottom - m_ImgaeGun.GetHeight() - 80, (BYTE)m_nCannonAlpha, 1);
    m_ImgaeGun.DrawImage(pD3DDevice, CPoint(m_ImgaeGun.GetWidth()/2, 32), fAngle, 'z',m_rectFishPond.right - 80 - 70, m_rectFishPond.top + m_rectFishPond.Height()/2 - m_ImgaeGun.GetHeight() + m_ImgaeGun.GetWidth()/2 - 13 , (BYTE)m_nCannonAlpha, 3);

    //渔网
    for ( int i = 0; i < 4; ++i )
    {
      if( m_pNetMove)
      {
            m_ImgaeNet.DrawImage(pD3DDevice, (int)m_pNetMove->ptPosition.x - m_ImgaeNet.GetWidth()/2, (int)m_pNetMove->ptPosition.y - m_ImgaeNet.GetHeight()/2, (BYTE)m_nCannonAlpha, m_pNetMove->cbFrameIndex);
      }
    }
   
}

//鱼路径
BYTE CFishDraw::FishPath( CPoint* pFishMove )
{
    if ( pFishMove == NULL || m_rectFishPond.Width() == 0 || m_rectFishPond.Height() == 0 )
      return 0;

    INT nEdge = rand()%4;
    INT NDW      = m_rectFishPond.Width();
    INT NDH      = 500;
    INT NOL      = 200;
    //开始位置
    INT nBeginPos = { { NDW + NOL                  , rand()% NDH    },
    { rand() % NDW                , NDH + NOL      },
    { -NOL                        , rand() % NDH    },
    { rand() % NDW                , -NOL            } };

    //中间位置 情况一
    INT nAmongPos = { { (rand() % (NDW/3*2))      , rand()% NDH                },
    { rand() % NDW                , (rand() % (NDH/3*2))      },
    { (rand()%(NDW/3*2)) + NDW/3, rand() % NDH                },
    { rand() % NDW                , (rand() % (NDH/3*2)) + NDH/3} };
    //中间位置 情况二
    INT nAmongPosTwo = { { (rand() % (NDW/2))      , rand()% NDH                },
    { rand() % NDW                , (rand() % (NDH/2))      },
    { (rand()%(NDW/2)) + NDW/2, rand() % NDH                },
    { rand() % NDW                , (rand() % (NDH/2)) + NDH/2} };

    //结束位置
    INT nEndPos = {    { -NOL                        , rand() % NDH    },
    { rand() % NDW                , -NOL            },
    { NDW + NOL                  , rand()% NDH    },
    { rand() % NDW                , NDH + NOL      } };

    int nType = rand()%3;

    // 三点弧形
    if ( nType == 0 )
    {
      pFishMove.SetPoint( nBeginPos, nBeginPos ) ;
      pFishMove.SetPoint( nAmongPosTwo, nAmongPosTwo ) ;
      pFishMove.SetPoint( (nAmongPosTwo - nBeginPos) * 2 + nBeginPos , (nAmongPosTwo - nBeginPos) * 2 + nBeginPos );
    }
    // 三点圆圈
    else if ( nType == 1)
    {
      pFishMove.SetPoint( nBeginPos, nBeginPos ) ;
      pFishMove.SetPoint( nAmongPos, nAmongPos ) ;
      pFishMove.SetPoint( nBeginPos, nBeginPos ) ;
    }
    // 随机三点圆圈
    else if ( nType == 2 )
    {
      pFishMove.SetPoint( nBeginPos, nBeginPos ) ;
      pFishMove.SetPoint( rand()%900 + 100, rand()%600 + 100 ) ;
      pFishMove.SetPoint( nBeginPos, nBeginPos ) ;
    }


    return nEdge;
}



页: [1]
查看完整版本: 金鲨银鲨【源码】