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

📄 game_script.cpp

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "global.h"

///////////////////////////////////////////////////////////
// cGameScript class code
///////////////////////////////////////////////////////////
cGameScript::cGameScript()
{
  // Clear script data pointers
  m_App   = NULL;
  m_Route = NULL;

  // Clear flags and variables
  for(long i=0;i<256;i++) {
    m_Flags[i] = FALSE;
    m_Vars[i] = 0;
  }
}

cGameScript::~cGameScript()
{
  delete [] m_Route;  // Only need to free the internal route
  m_Window.Free();    // Free the text window object
}

BOOL cGameScript::SetData(cApp *App)
{ 
  // Remember the application class pointer
  if((m_App = App) == NULL)
    return FALSE;
  
  // Create a text window object
  m_Window.Create(&m_App->m_Graphics, &m_App->m_Font);

  return TRUE; 
}

BOOL cGameScript::Reset()
{
  // Clear flags and variables
  for(long i=0;i<256;i++) {
    m_Flags[i] = FALSE;
    m_Vars[i] = 0;
  }

  return TRUE;
}

BOOL cGameScript::Save(char *Filename)
{
  FILE *fp;

  if((fp=fopen(Filename, "wb"))==NULL)
    return FALSE;
  fwrite(&m_Flags, 1, sizeof(m_Flags), fp);
  fwrite(&m_Vars, 1, sizeof(m_Vars), fp);
  fclose(fp);

  return TRUE;
}

BOOL cGameScript::Load(char *Filename)
{
  FILE *fp;

  if((fp=fopen(Filename, "rb"))==NULL)
    return FALSE;
  fread(&m_Flags, 1, sizeof(m_Flags), fp);
  fread(&m_Vars, 1, sizeof(m_Vars), fp);
  fclose(fp);

  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_Else(Script);
    case  2: return Script_EndIf(Script);
    case  3: return Script_IfFlagThen(Script);
    case  4: return Script_IfVarThen(Script);
    case  5: return Script_SetFlag(Script);
    case  6: return Script_SetVar(Script);
    case  7: return Script_Label(Script);
    case  8: return Script_Goto(Script);
    case  9: return Script_Message(Script);

    case 10: return Script_Add(Script);
    case 11: return Script_Remove(Script);
    case 12: return Script_Move(Script);
    case 13: return Script_Direction(Script);
    case 14: return Script_Type(Script);
    case 15: return Script_AI(Script);
    case 16: return Script_Target(Script);
    case 17: return Script_NoTarget(Script);
    case 18: return Script_Bounds(Script);
    case 19: return Script_Distance(Script);
    case 20: return Script_Script(Script);
    case 21: return Script_CharMessage(Script);
    case 22: return Script_Enable(Script);
    case 23: return Script_CreateRoute(Script);
    case 24: return Script_AddPoint(Script);
    case 25: return Script_AssignRoute(Script);
    case 26: return Script_AlterHPMP(Script);
    case 27: return Script_Ailment(Script);
    case 28: return Script_AlterSpell(Script);
    case 29: return Script_Teleport(Script);
    case 30: return Script_ShortMessage(Script);
    case 31: return Script_Action(Script);
    case 32: return Script_IfExpLevel(Script);

    case 33: return Script_Barter(Script);

    case 34: return Script_IfItem(Script);
    case 35: return Script_AddItem(Script);
    case 36: return Script_RemoveItem(Script);

    case 37: return Script_AddBarrier(Script);
    case 38: return Script_EnableBarrier(Script);
    case 39: return Script_RemoveBarrier(Script);

    case 40: return Script_AddTrigger(Script);
    case 41: return Script_EnableTrigger(Script);
    case 42: return Script_RemoveTrigger(Script);

    case 43: return Script_Sound(Script);
    case 44: return Script_Music(Script);
    case 45: return Script_StopMusic(Script);

    case 46: return Script_WinGame(Script);

    case 47: return Script_CommentOrSeparator(Script);
    case 48: return Script_CommentOrSeparator(Script);

    case 49: return Script_Wait(Script);
    case 50: return Script_IfRandThen(Script);
    case 51: return Script_Render(Script);
  }

  return NULL; // Error executing
}

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

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

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

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

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

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

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

  // See if var matches second entry
  if(m_Vars[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_SetFlag(sScript *ScriptPtr)
{
  // Set boolean value
  m_Flags[ScriptPtr->Entries[0].lValue] =                     \
          ScriptPtr->Entries[1].bValue;

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_SetVar(sScript *ScriptPtr)
{
  // Set long value
  m_Vars[ScriptPtr->Entries[0].lValue] =                     \
          ScriptPtr->Entries[1].lValue;

  return ScriptPtr->Next;
}

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

sScript *cGameScript::Script_Goto(sScript *ScriptPtr)
{
  sScript *Script;

  // Search entire script for label
  Script = GetParentScript();
  while(Script != NULL) {

    // Return next entry if a match
    if(Script->Entries[0].lValue==ScriptPtr->Entries[0].lValue)
      return Script->Next;
    
    // Go to next entry
    Script = Script->Next;
  }

  return NULL;  // End of script, return completion
}

sScript *cGameScript::Script_Message(sScript *ScriptPtr)
{
  // Create the text and position the window
  m_Window.SetText(ScriptPtr->Entries[0].Text);
  m_Window.Move(4,4,624);

  // Lock the 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);

  // Render the scene while waiting for keypress/buttonpress
  while(1) {
    // Break when space pressed
    m_App->m_Keyboard.Acquire(TRUE);
    m_App->m_Keyboard.Read();
    if(m_App->m_Keyboard.GetKeyState(KEY_SPACE) == TRUE)
      break;

    // Break when left mouse button pressed
    m_App->m_Mouse.Acquire(TRUE);
    m_App->m_Mouse.Read();
    if(m_App->m_Mouse.GetButtonState(MOUSE_LBUTTON) == TRUE)
      break;

    // Render the scene w/window
    m_App->m_Graphics.ClearZBuffer();
    if(m_App->m_Graphics.BeginScene() == TRUE) {
      m_App->RenderFrame();
      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_Add(sScript *ScriptPtr)
{
  m_App->m_CharController.Add(                                \
                        ScriptPtr->Entries[0].lValue,         \
                        ScriptPtr->Entries[1].lValue,         \
                        ScriptPtr->Entries[2].Selection,      \
                        CHAR_STAND,                           \
                        ScriptPtr->Entries[3].fValue,         \
                        ScriptPtr->Entries[4].fValue,         \
                        ScriptPtr->Entries[5].fValue,         \
                        ScriptPtr->Entries[6].fValue);

  // Force an update of all character w/0 elapsed time
  m_App->m_CharController.Update(0);

  return ScriptPtr->Next;
}

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

  // Force an update of all character w/0 elapsed time
  m_App->m_CharController.Update(0);

  return ScriptPtr->Next;
}

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

  // Force an update of all character w/0 elapsed time
  m_App->m_CharController.Update(0);

  return ScriptPtr->Next;
}

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

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

  // Force an update of all character w/0 elapsed time
  m_App->m_CharController.Update(0);

  return ScriptPtr->Next;
}

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

  return ScriptPtr->Next;
}

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

  return ScriptPtr->Next;
}

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

  return ScriptPtr->Next;
}

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

  return ScriptPtr->Next;
}

sScript *cGameScript::Script_Bounds(sScript *ScriptPtr)
{
  m_App->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_Distance(sScript *ScriptPtr)
{
  m_App->m_CharController.SetDistance(                        \
                      ScriptPtr->Entries[0].lValue,           \
                      ScriptPtr->Entries[1].fValue);

  return ScriptPtr->Next;
}

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

  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_App->m_Graphics.GetDeviceCOM()->GetTransform(             \
                                   D3DTS_VIEW, &matView);
  m_App->m_Graphics.GetDeviceCOM()->GetTransform(             \
                                   D3DTS_PROJECTION, &matProj);

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

  // Get the character's height
  CharPtr = m_App->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);

  // Create the text and position the window
  m_Window.SetText(ScriptPtr->Entries[0].Text,                \
                                D3DCOLOR_RGBA(255,255,0,255));
  m_Window.Move(4,4,624,0,(long)vecTarget.x,(long)vecTarget.y);

  // Lock the 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);

  // Render the scene while waiting for keypress/buttonpress
  while(1) {
    // Break when space pressed
    m_App->m_Keyboard.Acquire(TRUE);
    m_App->m_Keyboard.Read();
    if(m_App->m_Keyboard.GetKeyState(KEY_SPACE) == TRUE)
      break;

    // Break when left mouse button pressed
    m_App->m_Mouse.Acquire(TRUE);
    m_App->m_Mouse.Read();
    if(m_App->m_Mouse.GetButtonState(MOUSE_LBUTTON) == TRUE)
      break;


    // Render the scene w/window
    m_App->m_Graphics.ClearZBuffer();
    if(m_App->m_Graphics.BeginScene() == TRUE) {
      m_App->RenderFrame();

⌨️ 快捷键说明

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