SnowBall.as 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "Scripts/NinjaSnowWar/GameObject.as"
  2. const float snowballMinHitSpeed = 1;
  3. const float snowballDampingForce = 20;
  4. const float snowballDuration = 5;
  5. const float snowballGroundHitDuration = 1;
  6. const float snowballObjectHitDuration = 0;
  7. const int snowballDamage = 1;
  8. class SnowBall : GameObject
  9. {
  10. int hitDamage;
  11. SnowBall()
  12. {
  13. duration = snowballDuration;
  14. hitDamage = snowballDamage;
  15. }
  16. void Start()
  17. {
  18. SubscribeToEvent(node, "NodeCollision", "HandleNodeCollision");
  19. }
  20. void FixedUpdate(float timeStep)
  21. {
  22. // Apply damping when rolling on the ground, or near disappearing
  23. RigidBody@ body = node.GetComponent("RigidBody");
  24. if ((onGround) || (duration < snowballGroundHitDuration))
  25. {
  26. Vector3 vel = body.linearVelocity;
  27. body.ApplyForce(Vector3(-snowballDampingForce * vel.x, 0, -snowballDampingForce * vel.z));
  28. }
  29. // Disappear when duration expired
  30. if (duration >= 0)
  31. {
  32. duration -= timeStep;
  33. if (duration <= 0)
  34. {
  35. SpawnParticleEffect(node.position, "Particle/SnowExplosion.xml", 1);
  36. node.Remove();
  37. }
  38. }
  39. }
  40. void WorldCollision(VariantMap& eventData)
  41. {
  42. GameObject::WorldCollision(eventData);
  43. // If hit the ground, disappear after a short while
  44. if (duration > snowballGroundHitDuration)
  45. duration = snowballGroundHitDuration;
  46. }
  47. void ObjectCollision(GameObject@ otherObject, VariantMap& eventData)
  48. {
  49. if (hitDamage > 0)
  50. {
  51. RigidBody@ body = node.GetComponent("RigidBody");
  52. if ((body.linearVelocity.length >= snowballMinHitSpeed))
  53. {
  54. if (side != otherObject.side)
  55. {
  56. otherObject.Damage(this, hitDamage);
  57. // Create a temporary node for the hit sound
  58. SpawnSound(node.position, "Sounds/PlayerFistHit.wav", 0.2);
  59. }
  60. hitDamage = 0;
  61. }
  62. }
  63. if (duration > snowballObjectHitDuration)
  64. duration = snowballObjectHitDuration;
  65. }
  66. }