⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winmain.cpp

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
          m_App->m_CharController.SetAction(Character, CHAR_SPELL);
          break;
        }
      }

      CharPtr = CharPtr->Next;
    }

  }

  return TRUE;
}

BOOL cChars::ValidateMove(sCharacter *Character,              \
                    float *XMove, float *YMove, float *ZMove)
{
  // Check against terrain mesh for collision
  if(m_App != NULL) {
    if(m_App->CheckIntersect(                                 \
         Character->XPos,                                     \
         Character->YPos+2.0f,                                \
         Character->ZPos,                                     \
         *XMove + Character->XPos,                            \
         *YMove + Character->YPos + 2.0f,                     \
         *ZMove + Character->ZPos) == TRUE)
      return FALSE;
  }

  return TRUE;
}

///////////////////////////////////////////////////////////
// Overloaded script class functions
///////////////////////////////////////////////////////////
cGameScript::cGameScript()
{
  m_Graphics = NULL;
  m_Font = NULL;
  m_Keyboard = NULL;
  m_App = NULL;
  m_CharController = NULL;
  m_Route = NULL;
}

cGameScript::~cGameScript()
{
  m_Window.Free();
  delete [] m_Route;
}

BOOL cGameScript::SetData(cApp *Application,                  \
                          cGraphics *Graphics, cFont *Font,   \
                          cInputDevice *Keyboard,             \
                          cChars *Controller)
{
  long i;

  if((m_App = Application) == NULL)
    return FALSE;

  if((m_Graphics = Graphics) == NULL)
    return FALSE;

  if((m_Font = Font) == NULL)
    return FALSE;

  if((m_Keyboard = Keyboard) == NULL)
    return FALSE;

  if((m_CharController = Controller) == NULL)
    return FALSE;

  // Clear flags
  for(i=0;i<256;i++)
    m_Flags[i] = FALSE;

  // Create the window object
  m_Window.Create(m_Graphics, m_Font);

  return TRUE;
}

BOOL cGameScript::Prepare()
{
  return TRUE;
}

BOOL cGameScript::Release()
{
  delete [] m_Route;
  m_Route = NULL;
  m_NumRoutePoints = 0;

  return TRUE;
}

sScript *cGameScript::Process(sScript *Script)
{
  switch(Script->Type) {
    case  0: return Script_End(Script);
    case  1: return Script_IfFlagThen(Script);
    case  2: return Script_Else(Script);
    case  3: return Script_EndIf(Script);
    case  4: return Script_SetFlag(Script);
    case  5: return Script_Message(Script);
    case  6: return Script_AddChar(Script);
    case  7: return Script_RemoveChar(Script);
    case  8: return Script_CharMessage(Script);
    case  9: return Script_CharType(Script);
    case 10: return Script_CharAI(Script);
    case 11: return Script_CharDistance(Script);
    case 12: return Script_CharBounds(Script);
    case 13: return Script_TargetChar(Script);
    case 14: return Script_NoTarget(Script);
    case 15: return Script_CreateRoute(Script);
    case 16: return Script_AddPoint(Script);
    case 17: return Script_AssignRoute(Script);
    case 18: return Script_MoveChar(Script);
    case 19: return Script_CharScript(Script);
 }

  return NULL; // Error executing
}

///////////////////////////////////////////////////////////
// Standard script processing functions
///////////////////////////////////////////////////////////
sScript *cGameScript::Script_End(sScript *ScriptPtr)
{
  return NULL;  // Force end of processing
}

sScript *cGameScript::Script_IfFlagThen(sScript *ScriptPtr)
{
  BOOL Skipping;

  // See if a flag matches second entry
  if(m_Flags[ScriptPtr->Entries[0].lValue % 256] ==           \
             ScriptPtr->Entries[1].bValue)
    Skipping = FALSE;
  else 
    Skipping = TRUE;

  // At this point, Skipping states if the script actions
  // need to be skipped due to a conditional if..then statement.
  // Actions are further processed if skipped = FALSE, looking
  // for an else to flip the skip mode, or an endif to end
  // the conditional block.

  // Go to next action to process
  ScriptPtr = ScriptPtr->Next;

  while(ScriptPtr != NULL) {
    // if else, flip skip mode
    if(ScriptPtr->Type == 2)
      Skipping = (Skipping == TRUE) ? FALSE : TRUE;

    // break on end if
    if(ScriptPtr->Type == 3)
      return ScriptPtr->Next;

    // Process script function in conditional block
    // making sure to skip actions when condition not met.
    if(Skipping == TRUE)
      ScriptPtr = ScriptPtr->Next;
    else {
      if((ScriptPtr = Process(ScriptPtr)) == NULL)
        return NULL;
    }
  }

  return NULL; // End of script reached
}

sScript *cGameScript::Script_Else(sScript *ScriptPtr)
{
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_EndIf(sScript *ScriptPtr)
{
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_SetFlag(sScript *ScriptPtr)
{
  // Set boolean value
  m_Flags[ScriptPtr->Entries[0].lValue % 256] =               \
          ScriptPtr->Entries[1].bValue;

  return ScriptPtr->Next;
}

///////////////////////////////////////////////////////////
// Specialized script processing functions
///////////////////////////////////////////////////////////
sScript *cGameScript::Script_Message(sScript *ScriptPtr)
{
  // Set the text to display
  m_Window.SetText(ScriptPtr->Entries[0].Text);
  m_Window.Move(10, 10, 620, 0);

  // Display the window
  if(m_Graphics->BeginScene() == TRUE) {
    m_Window.Render();
    m_Graphics->EndScene();
    m_Graphics->Display();
  }

  // Wait for a key press
  m_Keyboard->Acquire(TRUE);
  m_Keyboard->SetLock(KEY_SPACE, TRUE);
  m_Keyboard->SetKeyState(KEY_SPACE, FALSE);
  while(1) {
    m_Keyboard->Read();
    if(m_Keyboard->GetKeyState(KEY_SPACE) == TRUE)
      break;
  }
  m_Keyboard->SetLock(KEY_SPACE, TRUE);
  m_Keyboard->SetKeyState(KEY_SPACE, FALSE);
    
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AddChar(sScript *ScriptPtr)
{
  m_CharController->Add(ScriptPtr->Entries[0].lValue,         \
                        ScriptPtr->Entries[1].lValue,         \
                        ScriptPtr->Entries[2].Selection,      \
                        ScriptPtr->Entries[3].Selection,      \
                        ScriptPtr->Entries[4].fValue,         \
                        ScriptPtr->Entries[5].fValue,         \
                        ScriptPtr->Entries[6].fValue,         \
                        ScriptPtr->Entries[7].fValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_RemoveChar(sScript *ScriptPtr)
{
  m_CharController->Remove(ScriptPtr->Entries[0].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharMessage(sScript *ScriptPtr)
{
  sCharacter *CharPtr;
  D3DXMATRIX matWorld, matView, matProj;
  D3DXVECTOR3 vecTarget;
  D3DVIEWPORT8 vpScreen;
  float MaxY;

  // Get the transformation matrices
  D3DXMatrixIdentity(&matWorld);
  m_Graphics->GetDeviceCOM()->GetTransform(D3DTS_VIEW,        \
                                           &matView);
  m_Graphics->GetDeviceCOM()->GetTransform(D3DTS_PROJECTION,  \
                                           &matProj);

  // Get the viewport
  m_Graphics->GetDeviceCOM()->GetViewport(&vpScreen);

  // Get the character's coordinates
  CharPtr = m_CharController->GetCharacter(                   \
                                ScriptPtr->Entries[1].lValue);
  CharPtr->Object.GetBounds(NULL,NULL,NULL,                   \
                            NULL,&MaxY,NULL,NULL);
  
  // Project the 3-D coordinates in 2-D coordinates
  D3DXVec3Project(&vecTarget,                                 \
                  &D3DXVECTOR3(CharPtr->XPos,                 \
                               CharPtr->YPos+MaxY,            \
                               CharPtr->ZPos),                \
                  &vpScreen, &matProj, &matView, &matWorld);

  // Set the text
  m_Window.SetText(ScriptPtr->Entries[0].Text, 
                   D3DCOLOR_RGBA(255,255,0,255));
  m_Window.Move(10, 10, 620, 0,                               \
                (long)vecTarget.x, (long)vecTarget.y);

  // Display the window while waiting for a keypress
  m_Keyboard->Acquire(TRUE);
  m_Keyboard->SetLock(KEY_SPACE, TRUE);
  m_Keyboard->SetKeyState(KEY_SPACE, FALSE);
  while(1) {
    m_Keyboard->Read();
    if(m_Keyboard->GetKeyState(KEY_SPACE) == TRUE)
      break;

    if(m_Graphics->BeginScene() == TRUE) {
      m_Window.Render();
      m_Graphics->EndScene();
      m_Graphics->Display();
    }
  }
  m_Keyboard->SetLock(KEY_SPACE, TRUE);
  m_Keyboard->SetKeyState(KEY_SPACE, FALSE);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharType(sScript *ScriptPtr)
{
  m_CharController->SetType(ScriptPtr->Entries[0].lValue,     \
                        ScriptPtr->Entries[1].Selection);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharAI(sScript *ScriptPtr)
{
  m_CharController->SetAI(ScriptPtr->Entries[0].lValue,       \
                      ScriptPtr->Entries[1].Selection);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharDistance(sScript *ScriptPtr)
{
  m_CharController->SetDistance(ScriptPtr->Entries[0].lValue, \
                            ScriptPtr->Entries[1].fValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharBounds(sScript *ScriptPtr)
{
  m_CharController->SetBounds(ScriptPtr->Entries[0].lValue,   \
                          ScriptPtr->Entries[1].fValue,       \
                          ScriptPtr->Entries[2].fValue,       \
                          ScriptPtr->Entries[3].fValue,       \
                          ScriptPtr->Entries[4].fValue,       \
                          ScriptPtr->Entries[5].fValue,       \
                          ScriptPtr->Entries[6].fValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_TargetChar(sScript *ScriptPtr)
{
  m_CharController->SetTargetCharacter(                       \
                            ScriptPtr->Entries[0].lValue,     \
                            ScriptPtr->Entries[1].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_NoTarget(sScript *ScriptPtr)
{
  m_CharController->SetTargetCharacter(                       \
                            ScriptPtr->Entries[0].lValue, -1);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CreateRoute(sScript *ScriptPtr)
{
  // Release old route
  delete [] m_Route;
  m_Route = NULL;
  m_NumRoutePoints = 0;

  m_NumRoutePoints = ScriptPtr->Entries[0].lValue;
  m_Route = new sRoutePoint[m_NumRoutePoints]();

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AddPoint(sScript *ScriptPtr)
{
  m_Route[ScriptPtr->Entries[0].lValue].XPos =                \
                     ScriptPtr->Entries[1].fValue;
  m_Route[ScriptPtr->Entries[0].lValue].YPos =                \
                     ScriptPtr->Entries[2].fValue;
  m_Route[ScriptPtr->Entries[0].lValue].ZPos =                \
                     ScriptPtr->Entries[3].fValue;

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AssignRoute(sScript *ScriptPtr)
{
  m_CharController->SetRoute(ScriptPtr->Entries[0].lValue,    \
                    m_NumRoutePoints, (sRoutePoint*)m_Route);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_MoveChar(sScript *ScriptPtr)
{
  m_CharController->Move(ScriptPtr->Entries[0].lValue,        \
                     ScriptPtr->Entries[1].fValue,            \
                     ScriptPtr->Entries[2].fValue,            \
                     ScriptPtr->Entries[3].fValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_CharScript(sScript *ScriptPtr)
{
  m_CharController->SetScript(ScriptPtr->Entries[0].lValue,   \
                     ScriptPtr->Entries[1].Text);

  return ScriptPtr->Next;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -