home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / csobject.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  5KB  |  255 lines

  1. #include <math.h>
  2. #include <time.h>
  3. #include "system.h"
  4.  
  5. #ifndef DEF_H
  6. #include "def.h"
  7. #endif
  8.  
  9. #ifndef CSOBJECT_H
  10. #include "csobject.h"
  11. #endif
  12.  
  13. #ifndef THING_H
  14. #include "thing.h"
  15. #endif
  16.  
  17. #ifndef TOKEN_H
  18. #include "token.h"
  19. #endif
  20.  
  21. #ifndef WORLD_H
  22. #include "world.h"
  23. #endif
  24.  
  25. //---------------------------------------------------------------------------
  26.  
  27. CsObject::CsObject (char* name, int type)
  28. {
  29.   strcpy (CsObject::name, name);
  30.   CsObject::type = type;
  31. }
  32.  
  33. CsObject::~CsObject ()
  34. {
  35. }
  36.  
  37. void CsObject::new_activate_trigger (Script* script, CsObject* ob)
  38. {
  39.   activate_triggers.add_trigger (script, ob);
  40. }
  41.  
  42. void CsObject::do_activate_triggers (World* w)
  43. {
  44.   activate_triggers.perform (w, this);
  45. }
  46.  
  47. //---------------------------------------------------------------------------
  48.  
  49. Collection::Collection (char* name) : CsObject (name, CS_COLLECTION)
  50. {
  51.   objects = NULL;
  52.   num_objects = 0;
  53. }
  54.  
  55. Collection::~Collection ()
  56. {
  57.   if (objects) delete [] objects;
  58. }
  59.  
  60. CsObject* Collection::find_object (char* name)
  61. {
  62.   int i;
  63.   for (i = 0 ; i < num_objects ; i++)
  64.   {
  65.     if (!strcmp (objects[i]->get_name (), name)) return objects[i];
  66.   }
  67.   return NULL;
  68. }
  69.  
  70. void Collection::transform ()
  71. {
  72.   int i;
  73.   for (i = 0 ; i < num_objects ; i++)
  74.   {
  75.     if (objects[i]->get_type () == CS_THING)
  76.       ((Thing*)(objects[i]))->transform ();
  77.   }
  78. }
  79.  
  80. void Collection::set_move (Sector* home, float x, float y, float z)
  81. {
  82.   int i;
  83.   for (i = 0 ; i < num_objects ; i++)
  84.   {
  85.     if (objects[i]->get_type () == CS_THING)
  86.       ((Thing*)(objects[i]))->set_move (home, x, y, z);
  87.   }
  88. }
  89.  
  90. void Collection::set_transform (Matrix3& matrix)
  91. {
  92.   int i;
  93.   for (i = 0 ; i < num_objects ; i++)
  94.   {
  95.     if (objects[i]->get_type () == CS_THING)
  96.       ((Thing*)(objects[i]))->set_transform (matrix);
  97.   }
  98. }
  99.  
  100. void Collection::move (float dx, float dy, float dz)
  101. {
  102.   int i;
  103.   for (i = 0 ; i < num_objects ; i++)
  104.   {
  105.     if (objects[i]->get_type () == CS_THING)
  106.       ((Thing*)(objects[i]))->move (dx, dy, dz);
  107.   }
  108. }
  109.  
  110. void Collection::transform (Matrix3& matrix)
  111. {
  112.   int i;
  113.   for (i = 0 ; i < num_objects ; i++)
  114.   {
  115.     if (objects[i]->get_type () == CS_THING)
  116.       ((Thing*)(objects[i]))->transform (matrix);
  117.   }
  118. }
  119.  
  120. void Collection::save (FILE* fp, int indent)
  121. {
  122. #if 0
  123.   // @@@ Not implemented yet
  124. #endif
  125. }
  126.  
  127. void Collection::load (World* w, char** buf)
  128. {
  129.   char* t;
  130.   char* old_buf, * old_buf_restart;
  131.   int idx_objects;
  132.  
  133.   skip_token (buf, "COLLECTION");
  134.   t = get_token (buf);
  135.   strcpy (name, t);
  136.   skip_token (buf, "(", "Expected '%s' instead of '%s' after the name of a COLLECTION!\n");
  137.  
  138.   old_buf_restart = *buf;
  139.   num_objects = 0;
  140.  
  141.   while (TRUE)
  142.   {
  143.     old_buf = *buf;
  144.     t = get_token (buf);
  145.     if (*t == ')' || *t == 0) break;
  146.     if (!strcmp (t, "THING"))
  147.     {
  148.       get_token (buf);
  149.       num_objects++;
  150.     }
  151.     else if (!strcmp (t, "COLLECTION"))
  152.     {
  153.       get_token (buf);
  154.       num_objects++;
  155.     }
  156.     else if (!strcmp (t, "TRIGGER"))
  157.     {
  158.       get_token (buf);
  159.       skip_token (buf, ",", "Expected '%s' instead of '%s' in COLLECTION/TRIGGER!\n");
  160.       get_token (buf);
  161.       skip_token (buf, ":", "Expected '%s' instead of '%s' in COLLECTION/TRIGGER!\n");
  162.       get_token (buf);
  163.     }
  164.     else
  165.     {
  166.       printf ("What is '%s' doing in a COLLECTION statement?\n", t);
  167.       exit (0);
  168.     }
  169.   }
  170.  
  171.   *buf = old_buf_restart;
  172.   idx_objects = 0;
  173.   if (objects) delete [] objects;
  174.   objects = new CsObject* [num_objects];
  175.  
  176.   while (TRUE)
  177.   {
  178.     old_buf = *buf;
  179.     t = get_token (buf);
  180.     if (*t == ')' || *t == 0) break;
  181.     if (!strcmp (t, "THING"))
  182.     {
  183.       t = get_token (buf);
  184.       Thing* th = w->get_thing (t);
  185.       if (!th)
  186.       {
  187.     printf ("Thing '%s' not found!\n", t);
  188.     exit (0);
  189.       }
  190.       objects[idx_objects] = (CsObject*)th;
  191.       idx_objects++;
  192.     }
  193.     else if (!strcmp (t, "COLLECTION"))
  194.     {
  195.       t = get_token (buf);
  196.       Collection* th = w->get_collection (t);
  197.       if (!th)
  198.       {
  199.     printf ("Collection '%s' not found!\n", t);
  200.     exit (0);
  201.       }
  202.       objects[idx_objects] = (CsObject*)th;
  203.       idx_objects++;
  204.     }
  205.     else if (!strcmp (t, "TRIGGER"))
  206.     {
  207.       CsObject* cs = NULL;
  208.       Thing* th;
  209.       t = get_token (buf);
  210.       int i;
  211.       for (i = 0 ; i < num_objects ; i++)
  212.     if (!strcmp (objects[i]->get_name (), t))
  213.     {
  214.       cs = objects[i];
  215.       break;
  216.     }
  217.       if (!cs)
  218.       {
  219.     printf ("Object '%s' not found!\n", t);
  220.     exit (0);
  221.       }
  222.  
  223.       skip_token (buf, ",", "Expected '%s' instead of '%s' in COLLECTION/TRIGGER!\n");
  224.  
  225.       t = get_token (buf);
  226.       if (!strcmp (t, "activate"))
  227.       {
  228.     skip_token (buf, ":", "Expected '%s' instead of '%s' in TRIGGER!\n");
  229.     t = get_token (buf);
  230.     Script* s = w->get_script (t);
  231.     if (!s)
  232.     {
  233.       printf ("Don't know script '%s'!\n", t);
  234.       exit (0);
  235.     }
  236.     th = (Thing*)cs;
  237.     th->new_activate_trigger (s, this);
  238.       }
  239.       else
  240.       {
  241.     printf ("Trigger '%s' not supported or known for object '%s'!\n", t, name);
  242.     exit (0);
  243.       }
  244.     }
  245.     else
  246.     {
  247.       printf ("What is '%s' doing in a COLLECTION statement?\n", t);
  248.       exit (0);
  249.     }
  250.   }
  251. }
  252.  
  253.  
  254. //---------------------------------------------------------------------------
  255.