GameObject.as 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Create the sound channel
  65. SoundSource3D@ source = node.CreateComponent("SoundSource3D");
  66. Sound@ sound = cache.GetResource("Sound", soundName);
  67. source.SetDistanceAttenuation(2, 50, 1);
  68. source.Play(sound);
  69. source.autoRemove = true;
  70. }
  71. void HandleNodeCollision(StringHash eventType, VariantMap& eventData)
  72. {
  73. Node@ otherNode = eventData["OtherNode"].GetPtr();
  74. RigidBody@ otherBody = eventData["OtherBody"].GetPtr();
  75. // If the other collision shape belongs to static geometry, perform world collision
  76. if (otherBody.collisionLayer == 2)
  77. WorldCollision(eventData);
  78. // If the other node is scripted, perform object-to-object collision
  79. GameObject@ otherObject = cast<GameObject>(otherNode.scriptObject);
  80. if (otherObject !is null)
  81. ObjectCollision(otherObject, eventData);
  82. }
  83. void WorldCollision(VariantMap& eventData)
  84. {
  85. VectorBuffer contacts = eventData["Contacts"].GetBuffer();
  86. while (!contacts.eof)
  87. {
  88. Vector3 contactPosition = contacts.ReadVector3();
  89. Vector3 contactNormal = contacts.ReadVector3();
  90. float contactDistance = contacts.ReadFloat();
  91. float contactImpulse = contacts.ReadFloat();
  92. // If contact is below node center and mostly vertical, assume it's ground contact
  93. if (contactPosition.y < node.position.y)
  94. {
  95. float level = Abs(contactNormal.y);
  96. if (level > 0.75)
  97. onGround = true;
  98. else
  99. {
  100. // If contact is somewhere inbetween vertical/horizontal, is sliding a slope
  101. if (level > 0.1)
  102. isSliding = true;
  103. }
  104. }
  105. }
  106. // Ground contact has priority over sliding contact
  107. if (onGround == true)
  108. isSliding = false;
  109. }
  110. void ObjectCollision(GameObject@ otherObject, VariantMap& eventData)
  111. {
  112. }
  113. void ResetWorldCollision()
  114. {
  115. RigidBody@ body = node.GetComponent("RigidBody");
  116. if (body.active)
  117. {
  118. onGround = false;
  119. isSliding = false;
  120. }
  121. else
  122. {
  123. // If body is not active, assume it rests on the ground
  124. onGround = true;
  125. isSliding = false;
  126. }
  127. }
  128. }