GameObject.as 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const int CTRL_UP = 1;
  2. const int CTRL_DOWN = 2;
  3. const int CTRL_LEFT = 4;
  4. const int CTRL_RIGHT = 8;
  5. const int CTRL_FIRE = 16;
  6. const int CTRL_JUMP = 32;
  7. const int CTRL_ALL = 63;
  8. const int SIDE_NEUTRAL = 0;
  9. const int SIDE_PLAYER = 1;
  10. const int SIDE_ENEMY = 2;
  11. class GameObject : ScriptObject
  12. {
  13. bool onGround;
  14. bool isSliding;
  15. float duration;
  16. int health;
  17. int maxHealth;
  18. int side;
  19. int lastDamageSide;
  20. uint lastDamageCreatorID;
  21. uint creatorID;
  22. GameObject()
  23. {
  24. onGround = false;
  25. isSliding = false;
  26. duration = -1; // Infinite
  27. health = 0;
  28. maxHealth = 0;
  29. side = SIDE_NEUTRAL;
  30. lastDamageSide = SIDE_NEUTRAL;
  31. lastDamageCreatorID = 0;
  32. creatorID = 0;
  33. // if (runClient)
  34. // Print("Warning! Logic object created on client!");
  35. }
  36. void FixedUpdate(float timeStep)
  37. {
  38. // Disappear when duration expired
  39. if (duration >= 0)
  40. {
  41. duration -= timeStep;
  42. if (duration <= 0)
  43. node.Remove();
  44. }
  45. }
  46. bool Damage(GameObject@ origin, int amount)
  47. {
  48. if ((origin.side == side) || (health == 0))
  49. return false;
  50. lastDamageSide = origin.side;
  51. lastDamageCreatorID = origin.creatorID;
  52. health -= amount;
  53. if (health < 0)
  54. health = 0;
  55. return true;
  56. }
  57. bool Heal(int amount)
  58. {
  59. // By default do not heal
  60. return false;
  61. }
  62. void PlaySound(const String&in soundName)
  63. {
  64. SoundSource3D@ source = node.CreateComponent("SoundSource3D");
  65. Sound@ sound = cache.GetResource("Sound", soundName);
  66. // Subscribe to sound finished for cleaning up the source
  67. SubscribeToEvent(node, "SoundFinished", "HandleSoundFinished");
  68. source.SetDistanceAttenuation(2, 50, 1);
  69. source.Play(sound);
  70. }
  71. void HandleSoundFinished(StringHash eventType, VariantMap& eventData)
  72. {
  73. SoundSource3D@ source = eventData["SoundSource"].GetPtr();
  74. source.Remove();
  75. }
  76. void HandleNodeCollision(StringHash eventType, VariantMap& eventData)
  77. {
  78. Node@ otherNode = eventData["OtherNode"].GetPtr();
  79. RigidBody@ otherBody = eventData["OtherBody"].GetPtr();
  80. // If the other collision shape belongs to static geometry, perform world collision
  81. if (otherBody.collisionLayer == 2)
  82. WorldCollision(eventData);
  83. // If the other node is scripted, perform object-to-object collision
  84. GameObject@ otherObject = cast<GameObject>(otherNode.scriptObject);
  85. if (otherObject !is null)
  86. ObjectCollision(otherObject, eventData);
  87. }
  88. void WorldCollision(VariantMap& eventData)
  89. {
  90. VectorBuffer contacts = eventData["Contacts"].GetBuffer();
  91. while (!contacts.eof)
  92. {
  93. Vector3 contactPosition = contacts.ReadVector3();
  94. Vector3 contactNormal = contacts.ReadVector3();
  95. float contactDistance = contacts.ReadFloat();
  96. float contactImpulse = contacts.ReadFloat();
  97. // If contact is below node center and pointing up, assume it's ground contact
  98. if (contactPosition.y < node.position.y)
  99. {
  100. float level = contactNormal.y;
  101. if (level > 0.75)
  102. onGround = true;
  103. else
  104. {
  105. // If contact is somewhere between vertical/horizontal, is sliding a slope
  106. if (level > 0.1)
  107. isSliding = true;
  108. }
  109. }
  110. }
  111. // Ground contact has priority over sliding contact
  112. if (onGround == true)
  113. isSliding = false;
  114. }
  115. void ObjectCollision(GameObject@ otherObject, VariantMap& eventData)
  116. {
  117. }
  118. void ResetWorldCollision()
  119. {
  120. RigidBody@ body = node.GetComponent("RigidBody");
  121. if (body.active)
  122. {
  123. onGround = false;
  124. isSliding = false;
  125. }
  126. else
  127. {
  128. // If body is not active, assume it rests on the ground
  129. onGround = true;
  130. isSliding = false;
  131. }
  132. }
  133. }