scoping_6.gm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Scoping example to show 'this' scoping in action
  3. */
  4. bigAlien = table(
  5. myName = "Big Alien"
  6. ,myPower = 100
  7. ,myHealth = 100
  8. ,isDead = false
  9. );
  10. smallAlien = table(
  11. myName = "Small Alien"
  12. ,myPower = 10
  13. ,myHealth = 10
  14. ,isDead = false
  15. );
  16. player = table(
  17. myName = "Player"
  18. ,myPower = 20
  19. ,myHealth = 20
  20. ,isDead = false
  21. );
  22. Attack = function( a_target )
  23. {
  24. print( a_target.myName, "was attacked by", .myName );
  25. // Attack
  26. a_target.myHealth -= .myPower;
  27. if (a_target.myHealth < 0 )
  28. {
  29. a_target.myHealth = 0;
  30. }
  31. if (a_target.myHealth == 0)
  32. {
  33. a_target.isDead = true;
  34. print( a_target.myName, "died" );
  35. }
  36. };
  37. ShowStats = function()
  38. {
  39. print( "Stats for **", .myName, "**" );
  40. print( "Health", .myHealth );
  41. print( "Power", .myPower );
  42. print( "Dead?", .isDead );
  43. print( "-----------------\n" );
  44. };
  45. // Show stats before attacks commence
  46. player:ShowStats();
  47. smallAlien:ShowStats();
  48. bigAlien:ShowStats();
  49. print( "Commencing attacks!\n" );
  50. smallAlien:Attack( player );
  51. player:Attack( smallAlien );
  52. bigAlien:Attack( player );
  53. print( "Attacks over!\n" );
  54. player:ShowStats();
  55. smallAlien:ShowStats();
  56. bigAlien:ShowStats();