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

📄 game_script.cpp

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

  // Relock keyboard and mouse
  m_App->m_Keyboard.SetLock(KEY_SPACE, TRUE);
  m_App->m_Keyboard.SetKeyState(KEY_SPACE, FALSE);
  m_App->m_Mouse.SetLock(MOUSE_LBUTTON, TRUE);
  m_App->m_Mouse.SetKeyState(MOUSE_LBUTTON, FALSE);
    
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Enable(sScript *ScriptPtr)
{
  m_App->m_CharController.SetEnable(                          \
                      ScriptPtr->Entries[1].lValue,           \
                      ScriptPtr->Entries[0].bValue);

  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_App->m_CharController.SetRoute(                           \
                      ScriptPtr->Entries[0].lValue,           \
                      m_NumRoutePoints, (sRoutePoint*)m_Route);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AlterHPMP(sScript *ScriptPtr)
{
  sCharacter *CharPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[3].lValue)) != NULL) {
    if(!ScriptPtr->Entries[0].Selection) {
      if(!ScriptPtr->Entries[2].Selection)
        CharPtr->HealthPoints += ScriptPtr->Entries[1].lValue;
      else
        CharPtr->ManaPoints += ScriptPtr->Entries[1].lValue;
    } else {
      if(!ScriptPtr->Entries[2].Selection)
        CharPtr->HealthPoints -= ScriptPtr->Entries[1].lValue;
      else
        CharPtr->ManaPoints -= ScriptPtr->Entries[1].lValue;
    }

    // Bounds check values
    if(CharPtr->HealthPoints > CharPtr->Def.HealthPoints)
      CharPtr->HealthPoints = CharPtr->Def.HealthPoints;
    if(CharPtr->ManaPoints > CharPtr->Def.ManaPoints)
      CharPtr->ManaPoints = CharPtr->Def.ManaPoints;
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Ailment(sScript *ScriptPtr)
{
  sCharacter *CharPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[2].lValue)) != NULL) {
    if(!ScriptPtr->Entries[0].Selection) {
      // Cure ailments
      CharPtr->Ailments &= ~ScriptPtr->Entries[1].lValue;
    } else {
      // Cause ailments
      CharPtr->Ailments |= ScriptPtr->Entries[1].lValue;
    }
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AlterSpell(sScript *ScriptPtr)
{
  sCharacter *CharPtr;
  long SpellNum, BitFlag;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[0].lValue)) != NULL) {
    SpellNum = ScriptPtr->Entries[2].lValue;
    BitFlag = (1 << (SpellNum & 31));

    if(!ScriptPtr->Entries[1].Selection) {
      // Learn a spell
      CharPtr->Def.MagicSpells[SpellNum/32] |= BitFlag;
    } else {
      // Forget a spell
      CharPtr->Def.MagicSpells[SpellNum/32] &= ~BitFlag;
    }
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Teleport(sScript *ScriptPtr)
{
  sCharacter *CharPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[0].lValue)) != NULL) {
    m_App->SetupTeleport(ScriptPtr->Entries[1].lValue,        \
                           ScriptPtr->Entries[2].fValue,      \
                           ScriptPtr->Entries[3].fValue,      \
                           ScriptPtr->Entries[4].fValue);
    return NULL;  // Stop processing after teleport
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_ShortMessage(sScript *ScriptPtr)
{
  sCharacter *CharPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[0].lValue)) != NULL) {
    m_App->m_CharController.SetMessage(CharPtr,               \
                    ScriptPtr->Entries[1].Text,               \
                    ScriptPtr->Entries[2].lValue);
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Action(sScript *ScriptPtr)
{
  sCharacter *CharPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[0].lValue)) != NULL) {

    // Set the character's action
    m_App->m_CharController.SetAction(CharPtr,                \
                    ScriptPtr->Entries[1].lValue,             \
                    ScriptPtr->Entries[2].lValue);
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_IfExpLevel(sScript *ScriptPtr)
{
  BOOL Skipping;
  sCharacter *CharPtr;

  // Get character pointer
  CharPtr = m_App->m_CharController.GetCharacter(             \
                                ScriptPtr->Entries[0].lValue);

  // See if level matches values
  if(CharPtr != NULL && CharPtr->Def.Level >=                 \
                        ScriptPtr->Entries[1].lValue)
    Skipping = FALSE;
  else 
    Skipping = TRUE;

  // Let if/then processor handle the rest
  return Script_IfThen(ScriptPtr->Next, Skipping);
}

sScript *cGameScript::Script_Barter(sScript *ScriptPtr)
{
  m_App->SetupBarter(                                         \
              m_App->m_CharController.GetCharacter(0),        \
              ScriptPtr->Entries[0].Text);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_IfItem(sScript *ScriptPtr)
{
  BOOL Skipping;
  sCharacter *CharPtr;

  // Get character pointer
  CharPtr = m_App->m_CharController.GetCharacter(             \
                                ScriptPtr->Entries[2].lValue);

  // See if item in inventory and check count
  if(CharPtr != NULL && CharPtr->CharICS->GetItem(            \
                ScriptPtr->Entries[1].lValue)->Quantity >=    \
                ScriptPtr->Entries[0].lValue)
    Skipping = FALSE;
  else 
    Skipping = TRUE;


  // Let if/then processor handle the rest
  return Script_IfThen(ScriptPtr->Next, Skipping);
}

sScript *cGameScript::Script_AddItem(sScript *ScriptPtr)
{
  sCharacter *CharPtr;
  long ItemNum;
  sCharItem *ItemPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[2].lValue)) != NULL) {

    // Only handle add item if character has an ICS
    if(CharPtr->CharICS != NULL) {

      // Get item number in question
      ItemNum = ScriptPtr->Entries[1].lValue;

      // Find matching item and add to it's quantity
      ItemPtr = CharPtr->CharICS->GetParentItem();
      while(ItemPtr != NULL) {

        // If this is matching item, then just add quantity
        if(ItemPtr->ItemNum == ItemNum) {
          ItemPtr->Quantity += ScriptPtr->Entries[0].lValue;
          break;
        }
       
        // Go to next item
        ItemPtr = ItemPtr->Next;
      }

      // Add to inventory if nothing found
      if(ItemPtr == NULL)
        CharPtr->CharICS->Add(ItemNum,                        \
                              ScriptPtr->Entries[0].lValue);
    }
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_RemoveItem(sScript *ScriptPtr)
{
  sCharacter *CharPtr;
  sCharItem *ItemPtr;

  if((CharPtr = m_App->m_CharController.GetCharacter(         \
                    ScriptPtr->Entries[2].lValue)) != NULL) {
    if(CharPtr->CharICS != NULL) {

      // Find item that we're looking for first
      ItemPtr = CharPtr->CharICS->GetParentItem();
      while(ItemPtr != NULL) {

        // If this is item, remove quantity
        if(ItemPtr->ItemNum == ScriptPtr->Entries[1].lValue) {
          ItemPtr->Quantity -= ScriptPtr->Entries[0].lValue;
          if(ItemPtr->Quantity <= 0)
            CharPtr->CharICS->Remove(ItemPtr);
          break;
        }
       
        // Go to next item
        ItemPtr = ItemPtr->Next;
      }
    }
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AddBarrier(sScript *ScriptPtr)
{
  m_App->m_Barrier.AddTriangle(ScriptPtr->Entries[0].lValue,  \
                               TRUE,                          \
                               0.0f, 0.0f, 0.0f,              \
                               0.0f, 0.0f, 0.0f,              \
                               ScriptPtr->Entries[1].fValue,  \
                               ScriptPtr->Entries[2].fValue,  \
                               ScriptPtr->Entries[3].fValue,  \
                               ScriptPtr->Entries[4].fValue,  \
                               ScriptPtr->Entries[5].fValue,  \
                               ScriptPtr->Entries[6].fValue,  \
                               ScriptPtr->Entries[7].fValue,  \
                               ScriptPtr->Entries[8].fValue);
 
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_EnableBarrier(sScript *ScriptPtr)
{
  m_App->m_Barrier.Enable(ScriptPtr->Entries[1].lValue,       \
           (!ScriptPtr->Entries[0].Selection) ? TRUE : FALSE);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_RemoveBarrier(sScript *ScriptPtr)
{
  m_App->m_Barrier.Remove(ScriptPtr->Entries[0].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_AddTrigger(sScript *ScriptPtr)
{
  m_App->m_Trigger.AddTriangle(ScriptPtr->Entries[0].lValue,  \
                               TRUE,                          \
                               ScriptPtr->Entries[1].fValue,  \
                               ScriptPtr->Entries[2].fValue,  \
                               ScriptPtr->Entries[3].fValue,  \
                               ScriptPtr->Entries[4].fValue,  \
                               ScriptPtr->Entries[5].fValue,  \
                               ScriptPtr->Entries[6].fValue,  \
                               ScriptPtr->Entries[7].fValue,  \
                               ScriptPtr->Entries[8].fValue);
 
  return ScriptPtr->Next;
}

sScript *cGameScript::Script_EnableTrigger(sScript *ScriptPtr)
{
  m_App->m_Trigger.Enable(ScriptPtr->Entries[1].lValue,       \
           (!ScriptPtr->Entries[0].Selection) ? TRUE : FALSE);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_RemoveTrigger(sScript *ScriptPtr)
{
  m_App->m_Trigger.Remove(ScriptPtr->Entries[0].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Sound(sScript *ScriptPtr)
{
  m_App->PlaySound(ScriptPtr->Entries[0].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Music(sScript *ScriptPtr)
{
  m_App->PlayMusic(ScriptPtr->Entries[0].lValue);

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_StopMusic(sScript *ScriptPtr)
{
  m_App->StopMusic();

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_WinGame(sScript *ScriptPtr)
{
  m_App->WinGame();

  return ScriptPtr->Next;
}

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

sScript *cGameScript::Script_Wait(sScript *ScriptPtr)
{
  DWORD Timer = timeGetTime() + ScriptPtr->Entries[0].lValue;

  // Wait for # of milliseconds
  while(timeGetTime() < Timer);

  return ScriptPtr->Next;
}

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

  // See if random # is >= entry
  if((rand() % ScriptPtr->Entries[0].lValue) >=               \
                                 ScriptPtr->Entries[1].lValue)
    Skipping = FALSE;
  else 
    Skipping = TRUE;

  // Let if/then processor handle the rest
  return Script_IfThen(ScriptPtr->Next, Skipping);
}

sScript *cGameScript::Script_Render(sScript *ScriptPtr)
{
  if(m_App != NULL) {
    m_App->m_Graphics.ClearZBuffer();
    if(m_App->m_Graphics.BeginScene() == TRUE) {
      m_App->RenderFrame();
      m_App->m_Graphics.EndScene();
    }
    m_App->m_Graphics.Display();
  }

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_IfThen(sScript *ScriptPtr,       \
                                    BOOL Skip)
{
  BOOL Skipping = Skip;

  // 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.
  while(ScriptPtr != NULL) {

    // if else, flip skip mode
    if(ScriptPtr->Type == SCRIPT_ELSE)
      Skipping = (Skipping == TRUE) ? FALSE : TRUE;

    // break on end if
    if(ScriptPtr->Type == SCRIPT_ENDIF)
      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 {
      // Return to normal processing if goto encountered
      if(ScriptPtr->Type == SCRIPT_GOTO)
        return Process(ScriptPtr);
        
      if((ScriptPtr = Process(ScriptPtr)) == NULL)
        return NULL;
    }
  }

  return NULL; // End of script reached
}

⌨️ 快捷键说明

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