states_02.gm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. global ent_state_panic = function()
  2. {
  3. print(.name + " is panicking, firing off alrams and attracting attention to you");
  4. };
  5. global ent_state_awake = function()
  6. {
  7. print(.name + " is waking up");
  8. this:stateSet( stateGetLast() ); // revert to previous state
  9. };
  10. global ent_state_sleeping = function()
  11. {
  12. print(.name + " is sleeping");
  13. sig = block("quiet_noise", "loud_bang", "kill");
  14. if (sig == "quiet_noise")
  15. {
  16. this:stateSet( ent_state_awake );
  17. }
  18. else if (sig == "loud_bang")
  19. {
  20. this:stateSet( ent_state_panic );
  21. }
  22. else
  23. {
  24. print( .name + " killed" );
  25. }
  26. };
  27. /// Initialise the state on the entity
  28. global ent_state_init = function(func)
  29. {
  30. print( .name, " state initialised");
  31. this:stateSet(func);
  32. };
  33. global ent_1 = { name = "roboguard 1000" };
  34. global ent_2 = { name = "old ticker" };
  35. // Create two threads, one for each entity and initialise them in the sleeping state
  36. ent_1.threadid = ent_1:thread( ent_state_init, ent_state_sleeping );
  37. ent_2.threadid = ent_2:thread( ent_state_init, ent_state_sleeping );
  38. sleep(1);
  39. print( "You stand on a twig");
  40. signal("quiet_noise");
  41. sleep(1);
  42. print( "You fire a gun at " + ent_1.name + " causing a loud noise");
  43. signal("loud_bang", ent_1.threadid);
  44. // Tell the entity to die
  45. signal("kill", ent_2.threadid);